LogitLoss.py 1.28 KB
Newer Older
1
from . import LossFunction
2
3
4
5

import numpy


6
7
class LogitLoss(LossFunction):
    """ The class to implement the logit loss function for the boosting framework."""
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
22
        Returns
          (float <#samples, #outputs>): The loss values for the samples, which is always >= 0
        """
        e = numpy.exp(-(targets * scores))
        return numpy.log(1. + e)
23

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

27
        Keyword parameters:
28

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

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

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