TangentialLoss.py 1.32 KB
 Tiago de Freitas Pereira committed Oct 15, 2016 1 ``````from . import LossFunction `````` Manuel Günther committed Feb 13, 2014 2 3 4 5 `````` import numpy `````` Tiago de Freitas Pereira committed Oct 15, 2016 6 7 ``````class TangentialLoss(LossFunction): """Tangent loss function, as described in http://www.svcl.ucsd.edu/projects/LossDesign/TangentBoost.html.""" `````` Manuel Günther committed Feb 13, 2014 8 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 9 10 `````` def loss(self, targets, scores): """The function computes the logit loss values using prediction scores and targets. `````` Manuel Günther committed Feb 13, 2014 11 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 12 `````` Keyword parameters: `````` Manuel Günther committed Feb 13, 2014 13 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 14 `````` targets (float <#samples, #outputs>): The target values that should be reached. `````` Manuel Günther committed Feb 13, 2014 15 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 16 `````` scores (float <#samples, #outputs>): The scores provided by the classifier. `````` Manuel Günther committed Feb 13, 2014 17 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 18 19 20 21 `````` Returns (float <#samples, #outputs>): The loss values for the samples, always >= 0 """ return (2. * numpy.arctan(targets * scores) - 1.) ** 2 `````` Manuel Günther committed Feb 13, 2014 22 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 23 24 `````` def loss_gradient(self, targets, scores): """The function computes the gradient of the tangential loss function using prediction scores and targets. `````` Manuel Günther committed Feb 13, 2014 25 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 26 `````` Keyword parameters: `````` Manuel Günther committed Feb 13, 2014 27 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 28 `````` targets (float <#samples, #outputs>): The target values that should be reached. `````` Manuel Günther committed Feb 13, 2014 29 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 30 `````` scores (float <#samples, #outputs>): The scores provided by the classifier. `````` Manuel Günther committed Feb 13, 2014 31 `````` `````` Tiago de Freitas Pereira committed Oct 15, 2016 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``````