Loss Functions in Machine Learning Explained

Three deliveries took 10, 12 and 26 minutes. If you had to predict one time for all three, would you choose 12 or 16 minutes? Either can be the best answer. It depends on how you score the errors.

What is a loss function?

A loss function assigns a cost to a prediction given the observed outcome. Training usually aims to reduce that cost across many examples. For a delivery prediction, the cost could be the number of minutes off or the square of that difference. Each rule gives the model a different target to learn.

The loss says what to minimize. The optimizer says how to change the model’s parameters to reduce it. Follow one gradient descent update

Same deliveries, different best predictions

Imagine a baseline model with no information about individual orders. It predicts the same time for every delivery. To find the best constant for the three training examples, we can minimize either mean absolute error (MAE) or mean squared error (MSE).

  • MAE averages how far each prediction is from the actual time. Its units are minutes.
  • MSE squares each difference before averaging. Its units are minutes squared. Squaring makes large misses count more heavily relative to small ones.
Training loss on the three fictional deliveries
PredictionMAE (min)MSE (min²)
12 min5.3366.67
16 min6.6750.67

Predicting 12 gives absolute errors of 2, 0 and 14 minutes. Their average is 16 ÷ 3, or 5.33 minutes. Predicting 16 gives errors of 6, 4 and 10. The average error rises to 6.67 minutes, but after squaring each difference the average drops from 66.67 to 50.67 minutes squared.

For a constant prediction, MAE is minimized by a median of the observed values, which is 12 here. MSE is minimized by their mean, which is 16. The 26-minute delivery pulls the mean upward. This is a worked example using three fictional training cases. Test with unseen deliveries before drawing conclusions about future accuracy. scikit-learn’s mean and median example

Log loss scores predicted probabilities

Log loss scores the probability assigned to the outcome that actually happened. If a delivery arrives within 20 minutes, estimates of 60% and 90% both predict “yes” at a 50% threshold. Their log losses are about 0.51 and 0.11, respectively. A lower probability for what actually happened produces a larger loss. scikit-learn’s log loss definition

Training loss and evaluation metrics

A formula can do both jobs. MAE can guide a model during training and report its average error during evaluation. The role depends on where it is used. For a delivery classifier, you might train with log loss and also track how often the system misses late deliveries at the chosen threshold.

Start with the outcome you need to predict and the mistakes that matter. MAE and MSE treat errors of the same size equally in either direction. If telling customers to expect an earlier arrival is more costly than predicting a later one, that asymmetry should be reflected in the evaluation and may justify a different training loss. Compare your choices on validation data, settle on a model, then check it on a separate test set. Model Evaluation Metrics · Training, Validation, and Test Data

Sources

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top