Closed vishesh49 closed 8 years ago
Is there something I am missing
You are missing the basics of writing Theano code. It's a symbolic tensor manipulation language, it doesn't work like Numpy. Here's a start: http://deeplearning.net/software/theano/tutorial/index.html#tutorial
Yes, I am new to writing Theano code. I also saw that all the objective functions in https://github.com/fchollet/keras/blob/master/keras/objectives.py call Theano tensor functions so tried doing this, I copy pasted the categorical crossentropy code from objectives.py and pasted it into my code no changes made
def categorical_crossentropy(y_true, y_pred):
'''Expects a binary class matrix instead of a vector of scalar classes
'''
y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
# scale preds so that the class probas of each sample sum to 1
y_pred /= y_pred.sum(axis=-1, keepdims=True)
cce = T.nnet.categorical_crossentropy(y_pred, y_true)
return cce
I still get the same error: TypeError: cost must be a scalar.
This should work if you have properly defined T and epsilon and your inputs are in the correct format. See the example below:
import theano.tensor as T
import theano
from keras.utils.np_utils import to_categorical
import numpy as np
if theano.config.floatX == 'float64':
epsilon = 1.0e-9
else:
epsilon = 1.0e-7
def categorical_crossentropy(y_true, y_pred):
'''Expects a binary class matrix instead of a vector of scalar classes
'''
y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
# scale preds so that the class probas of each sample sum to 1
y_pred /= y_pred.sum(axis=-1, keepdims=True)
cce = T.nnet.categorical_crossentropy(y_pred, y_true)
return cce
y_true = T.matrix()
y_pred = T.matrix()
loss_fun = theano.function([y_true, y_pred], categorical_crossentropy(y_true,y_pred))
ytrue = np.asarray(to_categorical(np.random.randint(5,size=10)), dtype=theano.config.floatX)
ypred = np.asarray(np.random.rand(10,5), dtype=theano.config.floatX)
ypred = ypred/np.expand_dims(np.sum(ypred,-1),1) # class probabilities should sum up to 1
loss = loss_fun(ytrue, ypred)
The output is a cost for every sample (here: 10), which keras will sum up automatically.
Note that keras will take care of compiling your loss function (theano.function(...)). You simply have to provide a function that can handle symbolic Theano expressions. Make sure that the outputs of the previous layer conform to what the cost function expects.
How can you create your own objective function, I tried to create a very basic objective function but it gives an error and I there is no way to know the size of the parameters passed to the function at run time.
I get this error saying
Now if I change the code at the second line , it shows this error
Error:
Is there something I am missing about how to create new objective functions because this is all I was able to understand from the limited documentation and examples about creating custom objective functions