Reputation: 24121
I am using Keras with TensorFlow backend, and I want to record the individual losses that are calculated during back propagation, for every training sample. This could be done by printing out each loss to the terminal, when it is calculated using the loss function.
But from what I have seen, there is no way to do this using the Keras API. So, my solution is to override one of the Keras loss functions, e.g.:
def mean_squared_error(y_true, y_pred):
loss = K.mean(K.square(y_pred - y_true), axis=-1)
print('Loss = ' + str(loss))
return loss
This compiles ok; however, nothing is printed to my terminal.
Any suggestions on why nothing is being printed, or what a better solution to this might be?
Upvotes: 1
Views: 117
Reputation: 805
You can use keras.backend.print_tensor
, which is just an identity transform that has the side-effect of printing the value of the tensor, and optionally a message. For your example, you can try:
import keras.backend as K
def mean_squared_error(y_true, y_pred):
loss = K.mean(K.square(y_pred - y_true), axis=-1)
return K.print_tensor(loss, message='Loss: ')
See the documentation for print_tensor and this answer for another example.
Upvotes: 3