Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

activation
A rule applied to each number in a layer that lets the network bend, rather than only scale and add.
autograd
Software that records operations during a forward pass and later computes their gradients automatically.
backpropagation
Working backwards through a network to find how each weight affects the loss, by multiplying local slopes.
batch
A group of examples processed together in one step, averaged into a single update.
epoch
One full pass over the training examples.
forward pass
Running inputs through a network to produce outputs, before any error is measured.
gradient
The direction and steepness in which a value rises fastest; training moves weights the opposite way.
learning rate
How far each update moves the weights; too large overshoots, too small crawls.
loss
One number saying how wrong the current predictions are, which training tries to reduce.
optimiser
The procedure that uses gradients to decide how each weight changes.
overfitting
When a network memorises accidental details of its training examples and does worse on new ones.
regularisation
Any penalty or limit that discourages a network from chasing noise in its training examples. The aim is better performance on new data.
tensor
A nested list of numbers with a shape that says what each axis means.

Deep learning from the ground up

Loss functions

A loss turns a prediction into one number the optimiser can lower. Mean squared error and cross-entropy are the two you will use, and both have a careful form.

Part 3 of 10

Intermediate15 min readBack to Courses

The loss defines the question

Training is optimisation, and optimisation needs a scalar, which is a single number. The loss is that scalar. It compares a prediction with a target and returns a number that is small when the prediction is good. Change the loss and you change what “good” means, even if the architecture and the data stay the same.

Mean squared error averages the squared difference between prediction and target. Squaring punishes large errors more than small ones. That is right when the noise is roughly Gaussian, and wrong when one outlier should not dominate. Mean absolute error averages the absolute difference and is more robust. Its cost is a gradient that is constant in size and undefined at zero.

The gradient of squared error is proportional to the error. A large mistake therefore produces a large correction, and the fit converges quickly when the model is far away. The same property lets an outlier pull the whole fit, because its error dominates the sum. If the outliers are noise, Huber loss switches to an absolute penalty beyond a threshold. That keeps the best of both.

Cross-entropy for classification

A classifier outputs one score per class. Softmax turns those scores into a distribution, and cross-entropy measures the negative log of the probability assigned to the correct class. A confident correct answer costs almost nothing. A confident wrong one costs a lot. That asymmetry is the behaviour a classifier should have.

The softmax and the log combine into a simpler expression. It is the log-sum-exp of the logits, minus the logit of the correct class. Log-sum-exp is a stable way to add exponentials without overflow. That form never divides by a probability and never takes the log of zero. Every framework exposes it as a fused loss that takes logits. The model should therefore not apply softmax itself.

Soft targets are the other reason the log form matters. A soft target is a distribution rather than a single index. Cross-entropy is then minus the sum of target times log probability, and label smoothing fills the target with a small amount for every class. The loss still differentiates cleanly, and the model stops pushing one logit to infinity.

Cross-entropy from logits

import torch
import torch.nn.functional as F

logits = torch.tensor([[2.0, 1.0, -1.0], [0.5, 0.1, 0.4]])
targets = torch.tensor([0, 2])

loss = F.cross_entropy(logits, targets)
print(loss.item())          # mean over the batch

per_example = F.cross_entropy(logits, targets, reduction="none")
print(per_example)          # one value per row

Mean squared error and its gradient

Run the loss and one gradient step, then raise the learning rate to 2.0 and check whether the loss still falls. The gradient is the direction, the rate is the distance.

Output
Click "Run" to execute your code

Match the loss to the output

Regression wants squared error or Huber loss. Binary classification wants binary cross-entropy with logits. That is a sigmoid plus a log loss in one stable function. Multiclass wants cross-entropy over the class axis. Multilabel wants one binary loss per label, because the labels are independent and do not sum to one.

The output layer and the loss have to agree. A sigmoid followed by cross-entropy applies the sigmoid twice. A softmax followed by cross-entropy flattens the gradients. Both run, both learn slowly, and neither raises an error. Read the loss documentation once, and note what it expects.

Class weights are the standard fix for imbalance. Multiplying the loss of a rare class by a factor makes a missed rare example more expensive, which moves the boundary. Focal loss goes further. It down-weights examples the model already classifies confidently, so training concentrates on the hard cases. Both change the objective. Both should be evaluated per class rather than by the headline average.

Never take the log of a softmax output. A probability can be exactly zero, the log is negative infinity, and one bad row turns the whole run into NaN. Subtract the maximum or call the fused loss.

The number is a diagnostic, not a score

A loss value is only comparable across runs with the same reduction, the same preprocessing and the same class balance. A mean loss of 0.3 on one dataset says nothing about a mean loss of 0.3 on another. Report task metrics next to it, such as accuracy, precision, or error in the units of the problem.

Watch the shape of the curve rather than its absolute value. A loss that falls smoothly is healthy. One that spikes may have a learning rate that is too high. One that is flat from the first step usually has a broken gradient or a misaligned label. The first batch is the cheapest place to notice all three.

A NaN is a different signal. It usually comes from a log of zero, a division by a zero variance, or a learning rate that has already diverged. Print the loss and the gradient norm for the first few steps. If the norm grows by an order of magnitude per step, the rate is too high. No amount of loss shaping will fix it.

A stable cross-entropy

Run both versions on the same logits, then add 1000 to every logit. The stable loss is unchanged; the naive one overflows. That difference is why the fused loss exists.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does mean squared error emphasise compared with mean absolute error?