TangentialLoss.py 1.32 KB
Newer Older
1
from . import LossFunction
2
3
4
5

import numpy


6
7
class TangentialLoss(LossFunction):
    """Tangent loss function, as described in http://www.svcl.ucsd.edu/projects/LossDesign/TangentBoost.html."""
8

9
10
    def loss(self, targets, scores):
        """The function computes the logit loss values using prediction scores and targets.
11

12
        Keyword parameters:
13

14
          targets (float <#samples, #outputs>): The target values that should be reached.
15

16
          scores (float <#samples, #outputs>): The scores provided by the classifier.
17

18
19
20
21
        Returns
          (float <#samples, #outputs>): The loss values for the samples, always >= 0
        """
        return (2. * numpy.arctan(targets * scores) - 1.) ** 2
22

23
24
    def loss_gradient(self, targets, scores):
        """The function computes the gradient of the tangential loss function using prediction scores and targets.
25

26
        Keyword parameters:
27

28
          targets (float <#samples, #outputs>): The target values that should be reached.
29

30
          scores (float <#samples, #outputs>): The scores provided by the classifier.
31

32
33
34
35
36
37
38
        Returns
          loss (float <#samples, #outputs>): The gradient of the loss based on the given scores and targets.
        """
        m = targets * scores
        numer = 4. * (2. * numpy.arctan(m) - 1.)
        denom = 1. + m ** 2
        return numer / denom