From 9bb26b1ec5c9d791330bef73ec0919097b3cd1f0 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 25 Sep 2017 16:37:52 +0000 Subject: [PATCH] add Huber loss --- onn.py | 4 +++- onn_core.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/onn.py b/onn.py index 26e8c0c..37aa304 100755 --- a/onn.py +++ b/onn.py @@ -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): diff --git a/onn_core.py b/onn_core.py index 27aa6a0..7de0752 100644 --- a/onn_core.py +++ b/onn_core.py @@ -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: