From 5f87c7a56b476c122da7594a9f029439c838fb4d Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 27 Feb 2017 02:43:51 +0000 Subject: [PATCH] . --- optim_nn_core.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/optim_nn_core.py b/optim_nn_core.py index e2be96d..897a2a9 100644 --- a/optim_nn_core.py +++ b/optim_nn_core.py @@ -63,6 +63,18 @@ class CategoricalCrossentropy(Loss): df = (p - y) / (p * (1 - p)) return df / len(y) +class Accuracy(Loss): + # returns percentage of categories correctly predicted. + # utilizes max(), so it cannot be used for gradient descent. + # use CategoricalCrossentropy for that instead. + + def F(self, p, y): + correct = np.argmax(p, axis=-1) == np.argmax(y, axis=-1) + return np.mean(correct) + + def dF(self, p, y): + raise NotImplementedError("cannot take the gradient of Accuracy") + class ResidualLoss(Loss): def F(self, p, y): # mean return np.mean(self.f(p - y))