Search code examples
keraslossloss-functionmse

Plot MSE over epochs when the loss function is a customized function


I use a customzied loss function and would like to plot the MSE within epochs (I use Keras Library). This is the code I use to fit my neural network and save the history.

model.compile(loss =new_loss2, metrics=['mse'], optimizer=opt)
hist = model3.fit(X_train, y_train, batch_size=32, shuffle=False, epochs=epochs, validation_split=0.15, callbacks = callbackz)

When I try to plot the MSE by using the plot function

plt.plot(hist.history['mse'])

I get this error

Traceback (most recent call last):
  File "D:\Keras1 (1).py", line 150, in <module>
    plt.plot(hist.history['mse'])
KeyError: 'mse'

How can I plot that in a correct way?


Solution

  • Somehow, the metrics are referenced by their expanded names in the history dictionary. hist.history['mean_squared_error'] should work.

    Note: if the name discrepancy bothers you, it is possible to use the expanded name too when compiling the model, i.e. model.compile(loss=new_loss2, metrics=['mean_squared_error'], ...).