MCPcopy Create free account
hub / github.com/rushter/MLAlgorithms / Loss

Class Loss

mla/ensemble/gbm.py:20–48  ·  view source on GitHub ↗

Base class for loss functions.

Source from the content-addressed store, hash-verified

18
19
20class 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
51class LeastSquaresLoss(Loss):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected