88

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

Zaratruta
  • 2,097
  • 2
  • 20
  • 26
  • 2
    Have you read [this](https://stackoverflow.com/questions/47302085/what-is-metrics-in-keras-libray-in-python/47306502#47306502) answer? – Marcin Możejko Jan 16 '18 at 13:25
  • These answers (disclaimer: mine) might also be useful: 1) [How does Keras evaluate the accuracy?](https://stackoverflow.com/questions/47508874/how-does-keras-evaluate-the-accuracy/47515095#47515095) 2) [Loss & accuracy - Are these reasonable learning curves?](https://stackoverflow.com/questions/47817424/loss-accuracy-are-these-reasonable-learning-curves/47819022#47819022) – desertnaut Jan 16 '18 at 17:48
  • It seems to me that usually you'd want to use identical values for both of them in regression tasks (e.g. `MAE`), whereas in classification tasks you'd use some crossentropy loss function (which, unlike typical "accuracy" which changes in jumps, changes smoothly), and for the metric you'd use typical accuracy so the results are easily explicable. – Alaa M. May 11 '21 at 10:49

3 Answers3

122

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.

sietschie
  • 7,425
  • 3
  • 33
  • 54
  • 47
    It might be worthwile to add that those are often not the same (and someone might ask: why?) as we need some kind of proxy-function (loss which somewhat effects in a low error metric) as some metrics are not smooth and can't be optimized by the algorithms in play. – sascha Jan 16 '18 at 12:44
  • 3
    @sascha viewing loss as an optimization-friendly proxy of the evaluation metric is a great way to view the relationship between the two. Very well said! – Shan Dou Aug 12 '21 at 04:24
  • Why don't we use the cross-entropy for the metric too? – skan Jul 11 '22 at 01:28
  • @skan If you classify cat pictures, seeing your algorithm with an accuracy of 90% will tell you 9 cats out of 10 are correctly detected. Mortals need those metrics to understand the world. I personally prefer reading the binary floating-point directly in the register as it gets computed by the cross-entropy subroutine. – Florian Fasmeyer Jul 25 '23 at 00:21
32

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    Well, MSE is applicable to regression problems while AUC only to classification ones, so the example is not good (although the rationale is) - edited to rectify (replaced MSE with cross-entropy). – desertnaut Mar 25 '21 at 13:29
8

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.

Sida Zhou
  • 3,529
  • 2
  • 33
  • 48