Search code examples
tensorflowkerasdeep-learningtheanopytorch

Get values of tensors in loss function


I would like to get the values of the y_pred and y_true tensors of this keras backend function. I need this to be able to perform some custom calculations and change the loss, these calculations are just possible with the real array values.

def mean_squared_error(y_true, y_pred):
    #some code here
    return K.mean(K.square(y_pred - y_true), axis=-1)

There is a way to do this in keras? Or in any other ML framework (tf, pytorch, theano)?


Solution

  • No, in general you can't compute the loss that way, because Keras is based on frameworks that do automatic differentiation (like Theano, TensorFlow) and they need to know which operations you are doing in between in order to compute the gradients of the loss.

    You need to implement your loss computations using keras.backend functions, else there is no way to compute gradients and optimization won't be possible.