Skip to content Skip to sidebar Skip to footer

Keras Custom Loss Function Not Printing Value Of Tensor

I am writing just a simple loss function in which I have to convert the tensor to numpy array(it's essential). I am just trying to print value of the tensor but I am getting this e

Solution 1:

The print statement is redundant. print_tensor will already print the values.

From the documentation of print_tensor:

"Note that print_tensor returns a new tensor identical to xwhich should be used in the following code. Otherwise the print operation is not taken into account during evaluation."

In the code above, since y_pred was assigned to x and x was no longer used, the print failed.

Use the version below.

def Lc(y_true, y_pred):
    y_pred=K.print_tensor(y_pred)
    return K.mean(y_pred)

def cat_loss(y_true, y_pred):
    y_pred = K.print_tensor(y_pred)
    return K.categorical_crossentropy(y_true, y_pred)

After I put this cat_loss function in my training loop, I can see the output like this:

[[0.000191014129 0.230871275 0.43813318]...]

190/255 [=====================>........] - ETA: 0s - loss: 0.3442 - acc: 0.9015

[[3.16367514e-05 1.70419597e-07 0.000147014405]...]

Post a Comment for "Keras Custom Loss Function Not Printing Value Of Tensor"