add Huber loss

This commit is contained in:
Connor Olding 2017-09-25 16:37:52 +00:00
parent eb16377ba6
commit 9bb26b1ec5
2 changed files with 17 additions and 1 deletions

4
onn.py
View File

@ -71,7 +71,7 @@ class Confidence(Loss):
categories = p.shape[-1]
confidence = (np.max(p, axis=-1) - 1/categories) / (1 - 1/categories)
# 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.
return np.mean(confidence)
@ -1102,6 +1102,8 @@ def lookup_loss(maybe_name):
return Absolute()
elif maybe_name == 'msee':
return SomethingElse()
elif maybe_name == 'huber':
return Huber(delta=0.1)
raise Exception('unknown objective', maybe_name)
def ritual_from_config(config, learner):

View File

@ -249,6 +249,20 @@ class Absolute(ResidualLoss):
def df(self, 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
class Regularizer: