pythonneural-networkkeraslossmetric

Keras - Loss and Metric calculated differently?


I have a model in Keras which I'm optimizing the mean squared error. However, if I use the same code as in losses.py from Keras in the metric, I get a different result. Why is this?

As a metric:

def MSE_metric(y_true, y_pred):
    return K.mean(K.square(y_pred, y_true))

For the model:

model.compile(optimizer=SGD(lr=0.01, momntum=0.9), loss='MSE', metrics=[MSE_metric])

This results in a loss of 6.07 but an MSE_metric of 0.47


Solution

  • Remember - that if you use any kind of regularization - it affects your loss. Your actual loss is equal to:

    loss = mse + regularization
    

    and this is where your discrepancy comes from.