Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find loss values using keras?

I want to use the various loss function defined in keras for calculating the loss value manually. For example:

from keras.losses import binary_crossentropy
error=binary_crossentropy([1,2,3,4],[6,7,8,9])

gives me error

AttributeError: 'list' object has no attribute 'dtype'.

Similar way I want to use other keras loss function. I have my y_pred and y_true lists/arrays.

like image 225
Bhaskar Dhariyal Avatar asked Oct 21 '17 09:10

Bhaskar Dhariyal


People also ask

How do you define a loss in Keras?

Creating custom loss functions in Keras A custom loss function can be created by defining a function that takes the true values and predicted values as required parameters. The function should return an array of losses. The function can then be passed at the compile stage.

What is loss and metrics in Keras?

The loss function is used to optimize your model. This is the function that will get minimized by the optimizer. A metric is used to judge the performance of your model. This is only for you to look at and has nothing to do with the optimization process.

What is binary cross entropy loss in Keras?

BinaryCrossentropy class Computes the cross-entropy loss between true labels and predicted labels. Use this cross-entropy loss for binary (0 or 1) classification applications. The loss function requires the following inputs: y_true (true label): This is either 0 or 1.


1 Answers

You can use K.variable() to wrap the inputs and use K.eval() to get the value.

from keras.losses import binary_crossentropy
from keras import backend as K
y_true = K.variable(np.array([[1], [0], [1], [1]]))
y_pred = K.variable(np.array([[0.5], [0.6], [0.7], [0.8]]))
error = K.eval(binary_crossentropy(y_true, y_pred))

print(error)
[ 0.69314718  0.91629082  0.35667494  0.22314353]
like image 124
Yu-Yang Avatar answered Oct 15 '22 13:10

Yu-Yang