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
# 3. Activation functions
2
3
Each layer computes a pre-activation $z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}$ and then an activation $a^{[l]} = g^{[l]}(z^{[l]})$. The choice of the nonlinearity $g^{[l]}$ is what makes depth worthwhile. This lesson explains why a nonlinear $g$ is required, surveys the sigmoid, tanh, and ReLU families, introduces the softmax used at the output, and gives practical guidance on which activation to pick.
4
5
**Objectives**
6
- Show that a stack of purely linear layers collapses to a single linear map.
7
- Define the sigmoid and tanh, derive their derivatives, and explain saturation.
8
- Survey the ReLU family (ReLU, leaky ReLU, PReLU, ELU, GELU) and the dead-unit problem.
9
- Define the softmax and place it at the output rather than in hidden layers.
10
- Give a short rule of thumb for choosing an activation per layer.
11
12
## 3.1 Why nonlinearity is required
13
14
Suppose every activation were the identity, $g^{[l]}(z) = z$. Then each layer is just $a^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}$, and composing two of them gives $W^{[2]}(W^{[1]} x + b^{[1]}) + b^{[2]} = (W^{[2]} W^{[1]}) x + (W^{[2]} b^{[1]} + b^{[2]})$. That is again of the form $W x + b$. By induction the whole $L$-layer network reduces to a single affine map:
15
16
$$\boxed{ g^{[l]} = \text{identity} \;\Rightarrow\; \hat{y} = W' x + b' }$$
17
18
with $W' = W^{[L]} \cdots W^{[1]}$ and $b'$ the accumulated bias. No matter how many linear layers are stacked, the model can only fit a linear function, so the extra depth buys nothing. A nonlinear $g$ between layers is exactly what breaks this collapse and lets the network represent curved decision boundaries and nonlinear regressions.
19
20
*Remark:* the bias is kept explicit here as $b^{[l]}$, unlike the Machine Learning course where the intercept was folded into $\theta^T x$ via the augmented input $x_0 = 1$. In this Deep Learning course each layer has its own weight matrix $W^{[l]}$ and its own bias vector $b^{[l]}$.
21
22
## 3.2 Sigmoid and tanh
23
24
![Common activation functions plotted against z](/en/Deep%20Learning/03%20Activation%20functions/a/activation-functions.png)
25
26
*Common activation functions: the bounded sigmoid and tanh saturate in their tails, while ReLU and its variants stay linear for positive inputs.*
27
28
### 3.2.1 Sigmoid
29
30
The sigmoid squashes any real pre-activation into the open interval $(0, 1)$:
31
32
$$\boxed{ \sigma(z) = \frac{1}{1 + e^{-z}} \in (0, 1) }$$
33
34
Its derivative has the convenient closed form below, which reuses the forward value $\sigma(z)$ already computed:
35
36
$$\boxed{ \sigma'(z) = \sigma(z)\left(1 - \sigma(z)\right) }$$
37
38
### 3.2.2 Tanh
39
40
The hyperbolic tangent is a rescaled sigmoid centred at zero, with output in $(-1, 1)$. Its derivative is likewise expressible from the forward value:
41
42
$$\boxed{ \tanh'(z) = 1 - \tanh(z)^2 }$$
43
44
*Remark:* $\tanh$ is zero-centred while $\sigma$ is not, so $\tanh$ often trains a little better as a hidden activation. The two are related by $\tanh(z) = 2\sigma(2z) - 1$.
45
46
### 3.2.3 Saturation
47
48
Both curves flatten in their tails. For large $|z|$ the output is close to a constant ($0$ or $1$ for $\sigma$, $\pm 1$ for $\tanh$), so the derivative is close to zero: $\sigma'(z) \to 0$ and $\tanh'(z) \to 0$. A unit sitting in that flat region is said to saturate, and it passes almost no gradient backward. When many such factors multiply through a deep stack the gradient shrinks toward zero, the vanishing-gradient problem revisited in [Initialization and vanishing gradients](/en/Deep%20Learning/07%20Initialization%20and%20vanishing%20gradients).
49
50
![Derivatives of sigmoid, tanh, and ReLU against z](/en/Deep%20Learning/03%20Activation%20functions/a/activation-derivatives.png)
51
52
*Activation derivatives: sigmoid and tanh gradients vanish in the tails, whereas the ReLU gradient is 1 wherever the unit is active.*
53
54
## 3.3 The ReLU family
55
56
The rectified linear unit keeps the positive part of its input and zeroes the rest:
57
58
$$\boxed{ \text{ReLU}(z) = \max(0, z) }$$
59
60
Its derivative is $1$ for $z > 0$ and $0$ for $z < 0$ (undefined at $z = 0$, taken to be $0$ or $1$ by convention). ReLU does not saturate on the positive side, so it keeps a healthy gradient flowing there, which is a large part of why it became the default hidden activation. The cost is the dead-unit problem: if a unit's pre-activation is always negative across the data, its gradient is always zero and it stops learning entirely. The variants below trade a little simplicity to soften that failure or to smooth the kink at the origin.
61
62
| name | formula | derivative | dies / saturates? |
63
| --- | --- | --- | --- |
64
| ReLU | $\max(0, z)$ | $1$ if $z>0$ else $0$ | can die (zero gradient for $z<0$) |
65
| Leaky ReLU | $\max(\alpha z, z)$, $\alpha \approx 0.01$ | $1$ if $z>0$ else $\alpha$ | rarely dies (small negative slope) |
66
| PReLU | $\max(\alpha z, z)$, $\alpha$ learned | $1$ if $z>0$ else $\alpha$ | rarely dies ($\alpha$ trained per channel) |
67
| ELU | $z$ if $z>0$ else $\alpha(e^z - 1)$ | $1$ if $z>0$ else $\alpha e^z$ | saturates gently for $z\to-\infty$ |
68
| GELU | $z\,\Phi(z)$, $\Phi$ the normal CDF | smooth, near $1$ for large $z$ | smooth, no hard death |
69
70
*Remark:* leaky ReLU and PReLU add a small slope $\alpha$ on the negative side so a unit is never fully switched off. GELU weights the input by the probability $\Phi(z)$ that a standard normal is below $z$, giving a smooth curve that behaves like ReLU for large $|z|$. It is the standard choice inside Transformers.
71
72
## 3.4 Softmax for multiclass outputs
73
74
For a classification with $K$ classes the final layer outputs a vector $z \in \mathbb{R}^K$ of scores, and the softmax turns it into a probability distribution over the classes:
75
76
$$\boxed{ \text{softmax}(z)_k = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}} }$$
77
78
Each component lies in $(0, 1)$ and the components sum to $1$, so $\text{softmax}(z)_k$ reads as the predicted probability of class $k$. The largest score becomes the most likely class.
79
80
*Remark:* softmax belongs at the output layer, not in a hidden layer. It couples every unit through the shared denominator (a normalization across the whole vector), which is exactly what a probability output needs but is not a useful per-unit hidden nonlinearity. For a single output ($K = 1$ vs its complement) softmax reduces to the sigmoid. The pairing of softmax with its loss is the subject of [Loss functions and output layers](/en/Deep%20Learning/04%20Loss%20functions%20and%20output%20layers).
81
82
## 3.5 Choosing an activation
83
84
A useful default: use ReLU or GELU in the hidden layers, and choose the output activation from the task. The diagram and table below summarize the decision.
85
86
![Decision flow for choosing an activation per layer](/en/Deep%20Learning/03%20Activation%20functions/a/activation-choice.svg)
87
88
*Choosing an activation: ReLU or GELU for hidden layers, and an output activation matched to the task.*
89
90
| layer / task | recommended activation | reason |
91
| --- | --- | --- |
92
| hidden (default) | ReLU or GELU | no positive-side saturation, cheap, trains fast |
93
| hidden (dead units) | leaky ReLU or ELU | keeps a nonzero gradient for $z < 0$ |
94
| output, regression | identity (none) | prediction is an unbounded real value |
95
| output, binary | sigmoid | maps score to a probability in $(0, 1)$ |
96
| output, multiclass | softmax | maps scores to a distribution over classes |
97
98
*Remark:* sigmoid and tanh are now rarely used as hidden activations in deep feed-forward networks precisely because of the saturation in Section 3.2.3. They survive at the output (sigmoid) and inside gated recurrent units, where their bounded range is the point.
99
100
*With the per-layer nonlinearities fixed, the next lesson pairs the output activation with a matching loss so the network has something to minimize.*
101
102
---
103
Next: [Loss functions and output layers](/en/Deep%20Learning/04%20Loss%20functions%20and%20output%20layers) · [Course overview](/en/Deep%20Learning)