Source code for layered.cost

import numpy as np


[docs]class Cost:
[docs] def __call__(self, prediction, target): raise NotImplementedError
[docs] def delta(self, prediction, target): raise NotImplementedError
[docs]class SquaredError(Cost): """ Fast and simple cost function. """
[docs] def __call__(self, prediction, target): return (prediction - target) ** 2 / 2
[docs] def delta(self, prediction, target): return prediction - target
[docs]class CrossEntropy(Cost): """ Logistic cost function used for classification tasks. Learns faster in the beginning than SquaredError because large errors are penalized exponentially. This makes sense in classification since only the best class will be the predicted one. """ def __init__(self, epsilon=1e-11): self.epsilon = epsilon
[docs] def __call__(self, prediction, target): clipped = np.clip(prediction, self.epsilon, 1 - self.epsilon) cost = target * np.log(clipped) + (1 - target) * np.log(1 - clipped) return -cost
[docs] def delta(self, prediction, target): denominator = np.maximum(prediction - prediction ** 2, self.epsilon) delta = (prediction - target) / denominator assert delta.shape == target.shape == prediction.shape return delta