import theano
import theano.tensor as T
import numpy as np

def mean_squared_error(y_true, y_pred):
    return T.sqr(y_pred - y_true).mean()

def mean_absolute_error(y_true, y_pred):
    return T.abs_(y_pred - y_true).mean()

def squared_hinge(y_true, y_pred):
    return T.sqr(T.maximum(1. - y_true * y_pred, 0.)).mean()

def hinge(y_true, y_pred):
    return T.maximum(1. - y_true * y_pred, 0.).mean()

def categorical_crossentropy(y_true, y_pred):
    '''Expects a binary class matrix instead of a vector of scalar classes
    '''
    return T.nnet.categorical_crossentropy(y_pred, y_true).mean()

def binary_crossentropy(y_true, y_pred):
    return T.nnet.binary_crossentropy(y_pred, y_true).mean()

# aliases
mse = MSE = mean_squared_error
mae = MAE = mean_absolute_error

from utils.generic_utils import get_from_module
def get(identifier):
    return get_from_module(identifier, globals(), 'objective')

def to_categorical(y):
    '''Convert class vector (integers from 0 to nb_classes)
    to binary class matrix, for use with categorical_crossentropy
    '''
    nb_classes = np.max(y)+1
    Y = np.zeros((len(y), nb_classes))
    for i in range(len(y)):
        Y[i, y[i]] = 1.
    return Y
@@ -53,16 +53,35 @@
  as np%0A%0A
+epsilon = 1.0e-15%0A%0A
 def mean
@@ -542,24 +542,76 @@
 ses%0A    '''%0A
+    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)%0A
     return T
@@ -698,32 +698,84 @@
 _true, y_pred):%0A
+    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)%0A
     return T.nne
@@ -1304,16 +1304,16 @@
 %5D%5D = 1.%0A
-
     retu
@@ -1316,8 +1316,9 @@
 return Y
+%0A

Fix categorical_crossentropy/binary_crossentropy with NaN value