Blame

36084c lugonthier 2026-07-02 14:39:19
Add new content and images for Linear Models, Regularization, SVMs, and Decision Trees - Added images for linear regression, logistic regression, and perceptron. - Introduced a new section on Regularization and High-Dimensional Inference with detailed explanations and images. - Added content on Support Vector Machines, including definitions, loss functions, and kernel methods. - Created a new section on Decision Trees and Ensemble Methods, covering CART, bagging, random forests, and boosting. - Included relevant images to illustrate concepts in Decision Trees and Ensemble Methods.
1
# 4. Loss functions and output layers
2
3
Before a network can learn it needs a target to descend toward. The output layer turns the last activation $a^{[L]}$ into a prediction, and the loss measures how far that prediction is from the true label. This module fixes both choices per task, because backpropagation in the next module differentiates a concrete loss. The output activation and the loss are not picked independently: matching them to the task shape is what makes the training signal clean.
4
5
**Objectives**
6
- Go from a per-example loss $L$ to the cost $J$ averaged over the batch.
7
- Choose a linear output with mean squared error for regression.
8
- Choose a sigmoid output with binary cross-entropy for two-class problems.
9
- Choose a softmax output with categorical cross-entropy for multiclass problems.
10
- Derive the clean logit gradient of the softmax and cross-entropy pair.
11
- Map any task to its output activation and loss with a single lookup table.
12
13
## 4.1 From per-example loss to cost
14
15
The network predicts $\hat{y} = a^{[L]}$ from input $a^{[0]} = x$. For a single example the loss $L(\hat{y}, y)$ scores that prediction against the target $y$. Training minimizes the cost $J$, defined as the average of $L$ over the $m$ examples in the batch or dataset:
16
17
$$\boxed{ J = \frac{1}{m}\sum_{i=1}^{m} L\!\left(\hat{y}^{(i)}, y^{(i)}\right) }$$
18
19
*Remark:* the loss $L$ scores one prediction, the cost $J$ is what the optimizer actually reduces. Averaging (rather than summing) keeps the gradient scale independent of the batch size, so the learning rate does not have to be retuned when $m$ changes.
20
21
The three tasks below reuse the losses introduced in the Machine Learning course. The cross-entropy row of the loss table at [General concepts](/en/Machine%20Learning/02%20General%20concepts), labelled "Neural networks", is exactly the objective a classification network minimizes. The novelty here is pairing each loss with the output activation $g^{[L]}$ that produces $\hat{y}$.
22
23
## 4.2 Regression: linear output and mean squared error
24
25
For a continuous target $y \in \mathbb{R}^{n_L}$ the output layer uses no activation, so it is linear (the identity) and the prediction can take any real value:
26
27
$$\boxed{ \hat{y} = a^{[L]} = z^{[L]} = W^{[L]} a^{[L-1]} + b^{[L]} }$$
28
29
The per-example loss is the squared Euclidean distance between prediction and target, scaled by one half:
30
31
$$\boxed{ L(\hat{y}, y) = \tfrac{1}{2}\,\lVert \hat{y} - y \rVert^2 }$$
32
33
*Remark:* the factor $\tfrac{1}{2}$ cancels the $2$ that appears on differentiating the square, leaving the tidy residual gradient $\partial L / \partial \hat{y} = \hat{y} - y$. This is the same mean-squared-error objective used for linear regression, now sitting on top of a deep network instead of a single linear score.
34
35
## 4.3 Binary classification: sigmoid output and binary cross-entropy
36
37
For a two-class label $y \in \{0, 1\}$ the output layer has a single unit whose activation is the sigmoid, squashing the logit $z^{[L]}$ into a probability:
38
39
$$\hat{y} = a^{[L]} = \sigma\!\left(z^{[L]}\right) = \frac{1}{1 + e^{-z^{[L]}}} \in (0, 1)$$
40
41
Here $\hat{y}$ is read as $p(y = 1 \mid x)$. The matching loss is the binary cross-entropy, the negative log-likelihood of the Bernoulli label:
42
43
$$\boxed{ L(\hat{y}, y) = -\Big[\, y \log \hat{y} + (1 - y)\log(1 - \hat{y}) \,\Big] }$$
44
45
*Remark:* only one of the two terms is active for any given label. When $y = 1$ the loss is $-\log \hat{y}$, penalizing a small predicted probability, and when $y = 0$ it is $-\log(1 - \hat{y})$. Cross-entropy is preferred over squared error here because it keeps the gradient large when the prediction is confidently wrong, so learning does not stall.
46
47
![Binary cross-entropy loss versus predicted probability, with the mean squared error parabola](/en/Deep%20Learning/04%20Loss%20functions%20and%20output%20layers/a/loss-curves.png)
48
49
*Cross-entropy loss grows without bound as the predicted probability moves away from the true label.*
50
51
## 4.4 Multiclass classification: softmax output and categorical cross-entropy
52
53
For a $K$-class label the output layer has $K$ units and the softmax activation turns the logit vector $z^{[L]} \in \mathbb{R}^{K}$ into a probability distribution over the classes:
54
55
$$\boxed{ \hat{y}_k = \frac{e^{z^{[L]}_k}}{\sum_{j=1}^{K} e^{z^{[L]}_j}} }$$
56
57
The outputs are positive and sum to one, so $\hat{y}$ is a proper distribution and $\hat{y}_k = p(y = k \mid x)$. The target $y$ is one-hot: $y_k = 1$ for the true class and $0$ otherwise. The matching loss is the categorical cross-entropy:
58
59
$$\boxed{ L(\hat{y}, y) = -\sum_{k=1}^{K} y_k \log \hat{y}_k }$$
60
61
*Remark:* because $y$ is one-hot the sum collapses to a single term, $-\log \hat{y}_{k^\star}$, where $k^\star$ is the true class. The loss therefore rewards putting probability mass on the correct class and ignores how the remaining mass is split. Binary cross-entropy is the special case $K = 2$.
62
63
## 4.5 The softmax and cross-entropy gradient
64
65
The softmax output and the categorical cross-entropy loss are used together because their composition has a remarkably clean derivative at the logits $z^{[L]}$. Differentiating $L$ with respect to a single logit $z^{[L]}_k$ gives:
66
67
$$\boxed{ \frac{\partial L}{\partial z^{[L]}_k} = \hat{y}_k - y_k }$$
68
69
The gradient at the output layer is just the prediction minus the target, a plain residual with no awkward sigmoid or softmax factor left over. The same identity holds for the sigmoid and binary cross-entropy pair, which is its $K = 2$ instance. This is precisely why each activation is coupled to its matching loss rather than mixed with, say, squared error.
70
71
*Remark:* the elementwise form $\partial L / \partial z^{[L]} = \hat{y} - y$ is what seeds backpropagation. The next module starts the backward pass from this vector and then repeatedly applies the chain rule and the Hadamard product $\odot$ to push it back through the hidden layers.
72
73
## 4.6 Task to output to loss
74
75
The three cases collapse into one lookup. Fix the task, and the output activation and loss follow.
76
77
| Task | Output activation $g^{[L]}$ | Per-example loss $L$ | Logit gradient $\partial L / \partial z^{[L]}$ |
78
| --- | --- | --- | --- |
79
| Regression | linear (identity) | mean squared error | $\hat{y} - y$ |
80
| Binary classification | sigmoid | binary cross-entropy | $\hat{y} - y$ |
81
| Multiclass classification | softmax | categorical cross-entropy | $\hat{y} - y$ |
82
83
*Remark:* the last column is identical across all three rows. Matching the output activation to its natural loss makes the network start its backward pass from the same simple residual regardless of the task.
84
85
![Three task branches mapping output activation to loss, converging to the shared logit gradient](/en/Deep%20Learning/04%20Loss%20functions%20and%20output%20layers/a/output-loss-map.svg)
86
87
*The output activation and loss are chosen together per task, and the matched pairs share the clean logit gradient yhat minus y.*
88
89
*With a concrete loss chosen and its output-layer gradient in hand, the next module runs the chain rule backward through every layer: backpropagation.*
90
91
---
92
Next: [Backpropagation](/en/Deep%20Learning/05%20Backpropagation) · [Course overview](/en/Deep%20Learning)