Base class for loss functions.
| 18 | |
| 19 | |
| 20 | class Loss: |
| 21 | """Base class for loss functions.""" |
| 22 | |
| 23 | def __init__(self, regularization=1.0): |
| 24 | self.regularization = regularization |
| 25 | |
| 26 | def grad(self, actual, predicted): |
| 27 | """First order gradient.""" |
| 28 | raise NotImplementedError() |
| 29 | |
| 30 | def hess(self, actual, predicted): |
| 31 | """Second order gradient.""" |
| 32 | raise NotImplementedError() |
| 33 | |
| 34 | def approximate(self, actual, predicted): |
| 35 | """Approximate leaf value.""" |
| 36 | return self.grad(actual, predicted).sum() / ( |
| 37 | self.hess(actual, predicted).sum() + self.regularization |
| 38 | ) |
| 39 | |
| 40 | def transform(self, pred): |
| 41 | """Transform predictions values.""" |
| 42 | return pred |
| 43 | |
| 44 | def gain(self, actual, predicted): |
| 45 | """Calculate gain for split search.""" |
| 46 | nominator = self.grad(actual, predicted).sum() ** 2 |
| 47 | denominator = self.hess(actual, predicted).sum() + self.regularization |
| 48 | return 0.5 * (nominator / denominator) |
| 49 | |
| 50 | |
| 51 | class LeastSquaresLoss(Loss): |
nothing calls this directly
no outgoing calls
no test coverage detected