Keras crossentropy

Keras backend functions such K.categorical_crossentropy expect tensors.

It's not obvious from your question what type label is. However, we know that model.predict always returns NumPy ndarrays, so we know label_pred is not a tensor. It is easy to convert, e.g. (assuming label is already a tensor),

custom_entropy(label, K.constant(label_pred))

Since the output of this function is a tensor, to actually evaluate it, you'd call

K.eval(custom_entropy(label, K.constant(label_pred)))

Alternatively, you can just use model as an op, and calling it on a tensor results in another tensor, i.e.

label_pred = model(K.constant(mfsc_train[:,:,5]))
cc = custom_entropy(label, label_pred)
ce = K.categorical_crossentropy(label, label_pred)

Now label_pred, cc and ce will all be tensors.


As given in the documentation, arguments are tensors:

y_true: True labels. TensorFlow/Theano tensor.
y_pred: Predictions. TensorFlow/Theano tensor of the same shape as y_true.

Converting numpy arrays to tensors should solve it.

Tags:

Python

Keras