What is the difference between loss function and metric in Keras?

Machine LearningNeural NetworkDeep LearningKeras

Machine Learning Problem Overview


It is not clear for me the difference between loss function and metrics in Keras. The documentation was not helpful for me.

Machine Learning Solutions


Solution 1 - Machine Learning

The loss function is used to optimize your model. This is the function that will get minimized by the optimizer.

A metric is used to judge the performance of your model. This is only for you to look at and has nothing to do with the optimization process.

Solution 2 - Machine Learning

The loss function is that parameter one passes to Keras model.compile which is actually optimized while training the model . This loss function is generally minimized by the model.

Unlike the loss function , the metric is another list of parameters passed to Keras model.compile which is actually used for judging the performance of the model.

For example : In classification problems, we want to minimize the cross-entropy loss, while also want to assess the model performance with the AUC. In this case, cross-entropy is the loss function and AUC is the metric. Metric is the model performance parameter that one can see while the model is judging itself on the validation set after each epoch of training. It is important to note that the metric is important for few Keras callbacks like EarlyStopping when one wants to stop training the model in case the metric isn't improving for a certaining no. of epochs.

Solution 3 - Machine Learning

I have a contrived example in mind: Let's think about linear regression on a 2D-plane. In this case, loss function would be the mean squared error, the fitted line would minimize this error.

However, for some reason we are very very interested in the area under the curve from 0 to 1 of our fitted line, and thus this can be one of the metrics. And we monitor this metric while the model minimizes the mean squared error loss function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZaratrutaView Question on Stackoverflow
Solution 1 - Machine LearningsietschieView Answer on Stackoverflow
Solution 2 - Machine LearningPranay MukherjeeView Answer on Stackoverflow
Solution 3 - Machine LearningSida ZhouView Answer on Stackoverflow