Skip to content

Loss function

Also called: loss functions, cost function

Published:

What does a loss function do?

Loss measures the distance between a model's prediction and the actual value, ignoring direction. In Google's words, the goal of training a model is to minimise the loss [1]. IBM stresses that a loss function is more than an evaluation metric. Optimisation algorithms such as gradient descent use its gradient to decide how to adjust the parameters; in deep learning that gradient is computed by backpropagation, so the function has to be differentiable [2]. "Cost function" and "error function" are usually used as synonyms [2].

How is it calculated in practice?

In Google's fuel-efficiency example, a model predicts 23.1 miles per gallon for a 2,370-pound car that actually gets 24. The L2 loss, which squares the difference, is (24 - 23.1)² = 0.81 [1]. When many examples are processed together, their losses are typically averaged [1].

Which loss function should you pick?

For regression the two usual choices are mean squared error (MSE) and mean absolute error (MAE). Squaring magnifies large errors, so MSE pulls the model towards outliers, while MAE is less affected by them [1]. Use MSE when large errors should be punished hard, and MAE when outliers shouldn't dominate the model [1]. For classification the standard choice is cross-entropy; its binary form penalises confident wrong predictions heavily [2].

Frequently asked questions

Is loss the same as accuracy?

No. Loss measures how far predictions are from the true values and is what training minimises; a lower loss means more accurate predictions.

Why must a loss function be differentiable?

Gradient descent updates parameters using the function's gradient, so that gradient has to be computable.

Sources

  1. Linear regression: Loss (Machine Learning Crash Course) — Google for Developers
  2. What is Loss Function? — IBM