Machine Learning Β· Chapter 33 of 40
Loss Functions
LOSS measures how wrong the model is. Regression: MSE, MAE. Binary classification: BINARY CROSS-ENTROPY. Multi-class: CATEGORICAL CROSS-ENTROPY.
Training aims to minimise the loss.
Example 1 (python)
# Regression
loss = 'mse'
# Binary
loss = 'binary_crossentropy'
# Multi-class
loss = 'sparse_categorical_crossentropy'
Pick to match the task.
Example 2 (python)
# Custom loss example
import tensorflow as tf
def mse(y_true, y_pred):
return tf.reduce_mean(tf.square(y_true - y_pred))
You can write custom losses.
Key points
- Loss = how wrong the model is.
- Regression: MSE, MAE.
- Classification: cross-entropy.
- Training minimizes the loss.
π‘ Note: Match the loss to the last-layer activation: sigmoid + binary cross-entropy, softmax + categorical cross-entropy.
Understanding Loss Functions
A loss function converts a prediction and its target into a numerical penalty. Training usually minimizes the average penalty across training examples, sometimes plus a regularization term that discourages complex models. The loss defines what counts as improvement; the optimizer chooses parameter updates intended to achieve it. Lower training loss alone does not establish better performance on unseen data.
For regression, define the residual as prediction minus target: r = Ε· β y. Mean squared error (MSE) averages rΒ²; mean absolute error (MAE) averages |r|. Squaring makes large errors disproportionately expensive: residuals of 1 and 4 contribute 1 and 16 to MSE, but 1 and 4 to MAE. MSE uses squared target units, whereas MAE retains the original units. Neither is universally preferable.
Their optimization mechanics differ. The derivative of squared error with respect to a prediction is 2r, so larger residuals produce stronger gradient signals. Absolute error has derivative +1 or β1 away from zero and is not differentiable at zero; optimization can use a subgradient there. With sufficient model flexibility, expected squared error favors the conditional mean, while expected absolute error favors a conditional median. MAE is less sensitive to extreme target values, not immune to all outliers.
For binary classification, let y be 0 or 1 and p be the predicted probability of class 1. Binary cross-entropy is β[y log(p) + (1 β y) log(1 β p)], using natural logarithms. It penalizes confident incorrect predictions heavily. Unlike accuracy, it distinguishes probabilities of 0.51 and 0.99 even when both produce the same predicted class. With sigmoid probability p derived from a logit z, its derivative with respect to z is p β y.
For mutually exclusive multiclass outcomes, cross-entropy is βlog of the probability assigned to the true class. Softmax produces probabilities summing to one. Categorical and sparse categorical cross-entropy express the same objective with different target encodings: one-hot vectors versus integer class indices. Multiple labels that can coexist instead commonly require separate sigmoid outputs and binary losses. Libraries may accept logits directly for numerical stability; output representation and loss configuration must agree.
Practical walkthrough
Start by specifying the prediction contract: what does each target represent, and what exactly does the model output? A continuous quantity suggests a regression objective. A single binary event suggests one probability. One outcome among several classes suggests a probability distribution. Check target encoding, output shape, and whether the loss expects probabilities or raw logits before training.
Next, decide which errors should receive emphasis. If occasional large regression errors should dominate updates, MSE may match that preference. If their influence should grow only linearly, consider MAE. This is an objective choice, not proof that unusual observations are mistakes. Inspect questionable records rather than deleting them automatically. For classification, decide whether equal example weights match the intended use; weighting changes the optimization objective.
Reproduce the example below using Python's standard library. First inspect the three residuals for each regression candidate. Compute absolute and squared penalties separately, then average them. Notice that the candidates reverse their ranking depending on the loss. Next inspect the two binary classifiers: thresholding at 0.5 gives identical predictions, but cross-entropy distinguishes their confidence.
In an actual experiment, split data before fitting preprocessing. Fit scalers, imputers, and feature selection only on training data; apply those fitted transformations unchanged to validation and test data. Use validation results to choose configurations, and reserve the test set for final evaluation. Compare losses using the same target scale, weighting, and reduction convention. Track a relevant evaluation metric alongside training loss. The example evaluates fixed predictions only: it demonstrates arithmetic, not generalization or successful model training.
Worked example
from math import log
# Fixed predictions: no fitting or external data required.
y = [0.0, 0.0, 0.0]
candidates = {
"A": [0.0, 0.0, 3.0],
"B": [1.2, 1.2, 1.2],
}
for name, predictions in candidates.items():
residuals = [p - t for t, p in zip(y, predictions)]
mae = sum(abs(r) for r in residuals) / len(y)
mse = sum(r * r for r in residuals) / len(y)
print(f"{name}: MAE={mae:.3f}, MSE={mse:.3f}")
labels = [1, 0]
classifiers = {
"C": [0.6, 0.4],
"D": [0.9, 0.1],
}
for name, probabilities in classifiers.items():
# These fixed probabilities are strictly between zero and one.
bce = -sum(
t * log(p) + (1 - t) * log(1 - p)
for t, p in zip(labels, probabilities)
) / len(labels)
accuracy = sum(
int(p >= 0.5) == t
for t, p in zip(labels, probabilities)
) / len(labels)
print(f"{name}: BCE={bce:.3f}, accuracy={accuracy:.3f}")
Expected output
A: MAE=1.000, MSE=3.000
B: MAE=1.200, MSE=1.440
C: BCE=0.511, accuracy=1.000
D: BCE=0.105, accuracy=1.000
Candidate A makes two exact predictions and one error of three units. Its absolute penalties total three, while its squared penalties total nine. Candidate B spreads smaller errors across all observations. MAE prefers A, but MSE prefers B because squaring emphasizes A's large miss. Both classifiers correctly identify both labels. D assigns probability 0.9 to each observed outcome, compared with C's 0.6, so its average negative log probability is lower. This does not establish that D is generally better calibrated: two constructed observations cannot support that conclusion. All displayed values are rounded to three decimal places for reproducible comparison.
Common mistakes and how to avoid them
Applying sigmoid before a loss configured to receive logits.
Choose one consistent path: raw logits with a logits-aware loss, or probabilities with a probability-aware loss. Prefer stable library implementations over manually taking logarithms of extreme probabilities.
Treating sparse categorical cross-entropy as a different classification task.
Check the target representation. Integer class indices use the sparse form; one-hot targets use the categorical form. With equivalent labels and predictions, the underlying cross-entropy is the same.
Comparing a summed loss with an averaged loss.
Inspect reduction settings and sample weights. Summed loss grows with example count; averaged loss normalizes it. Keep conventions consistent when comparing runs or changing batch sizes.
Selecting the model with the lowest training loss.
Use held-out validation performance for selection. Falling training loss alongside worsening validation loss can indicate overfitting. Do not repeatedly consult the test set to guide changes.
Review questions
How does doubling a residual affect squared and absolute error?
Squared error becomes four times larger; absolute error doubles. This explains why MSE places comparatively greater emphasis on large misses.
Why can cross-entropy improve while accuracy remains unchanged?
Probabilities can move toward the observed labels without crossing the classification threshold. Cross-entropy detects that change; accuracy records only whether the selected class is correct.
What gradient signal does binary cross-entropy provide for a sigmoid logit?
The per-example derivative is p β y. For a positive label, increasing the logit reduces loss; for a negative label, decreasing it reduces loss.
Why fit preprocessing only on training data?
Preprocessing estimates information from data, even without labels. Including validation or test observations leaks information into training and compromises their role as independent performance checks.