Search code examples
pythontensorflowmachine-learningkerasloss-function

How do I get back the error values of a keras loss function (tensor)


I would like to plot all the different loss functions available in Keras. Therefore I have created a dataframe and invoke the loss function. But how can I get back the values from the tensor?

import numpy as np
import pandas as pd
from keras import losses

points = 100
df = pd.DataFrame({"error": np.linspace(-3,3,points)})
df["mean_squared_error"] = losses.mean_squared_error(np.zeros(points), df["error"])
df.plot(x="error")

Solution

  • The loss functions in Keras return a Tensor object. You need to evaluate that Tensor object using the eval() function from the backend to get its actual value. Further, if you take a look at the definition of loss functions in Keras, say mean_squared_error(), you would realize that there is K.mean() operation which takes the average over the last axis which is the output axis (don't confuse this with batch or sample axis). Therefore, you may need to pass the true and predicted values in a shape of (n_samples, n_outputs), hence the reshapes in the following code:

    import numpy as np
    import pandas as pd
    from keras import losses
    from keras import backend as K
    
    points = 100
    df = pd.DataFrame({"error": np.linspace(-3,3,points)})
    mse_loss = losses.mean_squared_error(np.zeros((points,1)), df["error"].values.reshape(-1,1))
    df["mean_squared_error"] = K.eval(mse_loss)
    df.plot(x="error")
    

    Here is the output plot:

    enter image description here