add Huber loss
This commit is contained in:
parent
eb16377ba6
commit
9bb26b1ec5
2 changed files with 17 additions and 1 deletions
4
onn.py
4
onn.py
|
@ -71,7 +71,7 @@ class Confidence(Loss):
|
||||||
categories = p.shape[-1]
|
categories = p.shape[-1]
|
||||||
confidence = (np.max(p, axis=-1) - 1/categories) / (1 - 1/categories)
|
confidence = (np.max(p, axis=-1) - 1/categories) / (1 - 1/categories)
|
||||||
# the exponent in softmax puts a maximum on confidence,
|
# the exponent in softmax puts a maximum on confidence,
|
||||||
# but we don't compensate for that. if necessary,
|
# but we don't compensate for that. if necessary,
|
||||||
# it'd be better to use an activation that doesn't have this limit.
|
# it'd be better to use an activation that doesn't have this limit.
|
||||||
return np.mean(confidence)
|
return np.mean(confidence)
|
||||||
|
|
||||||
|
@ -1102,6 +1102,8 @@ def lookup_loss(maybe_name):
|
||||||
return Absolute()
|
return Absolute()
|
||||||
elif maybe_name == 'msee':
|
elif maybe_name == 'msee':
|
||||||
return SomethingElse()
|
return SomethingElse()
|
||||||
|
elif maybe_name == 'huber':
|
||||||
|
return Huber(delta=0.1)
|
||||||
raise Exception('unknown objective', maybe_name)
|
raise Exception('unknown objective', maybe_name)
|
||||||
|
|
||||||
def ritual_from_config(config, learner):
|
def ritual_from_config(config, learner):
|
||||||
|
|
14
onn_core.py
14
onn_core.py
|
@ -249,6 +249,20 @@ class Absolute(ResidualLoss):
|
||||||
def df(self, r):
|
def df(self, r):
|
||||||
return np.sign(r)
|
return np.sign(r)
|
||||||
|
|
||||||
|
class Huber(ResidualLoss):
|
||||||
|
def __init__(self, delta=1.0):
|
||||||
|
self.delta = _f(delta)
|
||||||
|
|
||||||
|
def f(self, r):
|
||||||
|
return np.where(r <= self.delta,
|
||||||
|
np.square(r) / 2,
|
||||||
|
self.delta * (np.abs(r) - self.delta / 2))
|
||||||
|
|
||||||
|
def df(self, r):
|
||||||
|
return np.where(r <= self.delta,
|
||||||
|
r,
|
||||||
|
self.delta * np.sign(r))
|
||||||
|
|
||||||
# Regularizers {{{1
|
# Regularizers {{{1
|
||||||
|
|
||||||
class Regularizer:
|
class Regularizer:
|
||||||
|
|
Loading…
Add table
Reference in a new issue