From 6a0cdbea3aa735f78608c2456c5cb6240fdfffce Mon Sep 17 00:00:00 2001 From: AntreasAntoniou Date: Fri, 10 Nov 2017 19:13:54 +0000 Subject: [PATCH] Stabilized softmax use instead of unstable version --- mlp/errors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlp/errors.py b/mlp/errors.py index 5ef95f7..a61b757 100644 --- a/mlp/errors.py +++ b/mlp/errors.py @@ -154,7 +154,7 @@ class CrossEntropySoftmaxError(object): Returns: Scalar error function value. """ - probs = np.exp(outputs) + probs = np.exp(outputs - outputs.max(-1)[:, None]) probs /= probs.sum(-1)[:, None] return -np.mean(np.sum(targets * np.log(probs), axis=1)) @@ -168,7 +168,7 @@ class CrossEntropySoftmaxError(object): Returns: Gradient of error function with respect to outputs. """ - probs = np.exp(outputs) + probs = np.exp(outputs - outputs.max(-1)[:, None]) probs /= probs.sum(-1)[:, None] return (probs - targets) / outputs.shape[0]