ExponentialLoss.py 1.32 KB
Newer Older
1
import numpy
2
from . import LossFunction
3
4


5
6
class ExponentialLoss(LossFunction):
    """ The class implements the exponential loss function for the boosting framework."""
7

8
9
10
    def loss(self, targets, scores):
        """The function computes the exponential loss values using prediction scores and targets.
        It can be used in classification tasks, e.g., in combination with the StumpTrainer.
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 numpy.exp(-(targets * scores))
22

23
24
    def loss_gradient(self, targets, scores):
        """The function computes the gradient of the exponential 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
        Returns
          loss (float <#samples, #outputs>): The gradient of the loss based on the given scores and targets.
        """
        loss = numpy.exp(-(targets * scores))
        return -targets * loss