Blame

12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
1
# 2. Activation functions
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.
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
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
5
## 2.1 Why nonlinearity is required
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.
6
7
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:
8
9
$$\boxed{ g^{[l]} = \text{identity} \;\Rightarrow\; \hat{y} = W' x + b' }$$
10
11
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.
12
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
13
*Remark:* the bias is kept explicit here as $b^{[l]}$, unlike the Machine Learning course where the intercept was folded into $w^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]}$.
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.
14
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
15
## 2.2 Sigmoid and tanh
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.
16
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
17
![Common activation functions plotted against z](/en/Deep%20Learning/02%20Activation%20functions/a/activation-functions.png)
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.
18
19
*Common activation functions: the bounded sigmoid and tanh saturate in their tails, while ReLU and its variants stay linear for positive inputs.*
20
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
21
### 2.2.1 Sigmoid
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.
22
23
The sigmoid squashes any real pre-activation into the open interval $(0, 1)$:
24
25
$$\boxed{ \sigma(z) = \frac{1}{1 + e^{-z}} \in (0, 1) }$$
26
27
Its derivative has the convenient closed form below, which reuses the forward value $\sigma(z)$ already computed:
28
29
$$\boxed{ \sigma'(z) = \sigma(z)\left(1 - \sigma(z)\right) }$$
30
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
31
### 2.2.2 Tanh
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.
32
33
The hyperbolic tangent is a rescaled sigmoid centred at zero, with output in $(-1, 1)$. Its derivative is likewise expressible from the forward value:
34
35
$$\boxed{ \tanh'(z) = 1 - \tanh(z)^2 }$$
36
37
*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$.
38
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
39
### 2.2.3 Saturation
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.
40
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
41
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, made precise in the next section.
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.
42
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
43
![Derivatives of sigmoid, tanh, and ReLU against z](/en/Deep%20Learning/02%20Activation%20functions/a/activation-derivatives.png)
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.
44
45
*Activation derivatives: sigmoid and tanh gradients vanish in the tails, whereas the ReLU gradient is 1 wherever the unit is active.*
46
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
47
## 2.3 The vanishing gradient
48
49
Saturation is not a cosmetic flaw, it decides whether a deep stack can train at all. Backpropagation multiplies the error by the local slope $g'(z^{[l]})$ at every layer it crosses, so the gradient reaching layer 1 contains one such factor per layer. With sigmoid activations those factors are small by construction: section 2.2.1 gave $\sigma'(z) = \sigma(z)(1 - \sigma(z))$, and that product never exceeds
50
51
$$\boxed{ \sigma'(z) = \sigma(z)\left(1 - \sigma(z)\right) \le \tfrac{1}{4} }$$
52
53
The result is the vanishing gradient: the layers near the output learn, the layers near the input receive almost nothing and barely move. Deep sigmoid networks stall, and the fix is not a better optimizer, it is a better activation, the ReLU of the next section.
54
55
<details class="proof">
56
<summary>Proof: the gradient shrinks geometrically with depth</summary>
57
58
**Step 1: the sigmoid's slope never exceeds $1/4$.** Write $s = \sigma(z) \in (0, 1)$. The product $s(1 - s)$ is a downward parabola in $s$, largest at $s = \tfrac{1}{2}$ where it equals $\tfrac{1}{4}$. So the bound holds, with equality only at $z = 0$, and saturation makes it far worse: a unit sitting at $\sigma(2) \approx 0.88$ already has slope $0.88 \cdot 0.12 \approx 0.10$.
59
60
**Step 2: backpropagation multiplies those slopes.** Take the simplest deep network, a chain of $L$ layers with one unit each, so every quantity is a scalar. Each layer crossed contributes the factor $\partial z^{[l]} / \partial z^{[l-1]} = w^{[l]}\, \sigma'(z^{[l-1]})$:
61
62
$$\frac{\partial L}{\partial z^{[1]}} = \frac{\partial L}{\partial z^{[L]}} \prod_{l=2}^{L} w^{[l]}\, \sigma'(z^{[l-1]})$$
63
64
With weights of typical size $|w^{[l]}| \le 1$, every factor is at most $\tfrac{1}{4}$ in absolute value, so
65
66
$$\boxed{ \left|\frac{\partial L}{\partial z^{[1]}}\right| \le \left(\tfrac{1}{4}\right)^{L-1} \left|\frac{\partial L}{\partial z^{[L]}}\right| }$$
67
68
Ten layers already shrink the gradient by about $10^{-6}$. $\blacksquare$
69
70
</details>
71
72
Weights much larger than $1$ only trade the problem for its mirror image, the exploding gradient. [Training deep networks](/en/Deep%20Learning/04%20Training%20deep%20networks) picks the weight scale so this product stays near $1$.
73
74
## 2.4 The ReLU family
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.
75
76
The rectified linear unit keeps the positive part of its input and zeroes the rest:
77
78
$$\boxed{ \text{ReLU}(z) = \max(0, z) }$$
79
80
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.
81
82
| name | formula | derivative | dies / saturates? |
83
| --- | --- | --- | --- |
84
| ReLU | $\max(0, z)$ | $1$ if $z>0$ else $0$ | can die (zero gradient for $z<0$) |
85
| Leaky ReLU | $\max(\alpha z, z)$, $\alpha \approx 0.01$ | $1$ if $z>0$ else $\alpha$ | rarely dies (small negative slope) |
86
| PReLU | $\max(\alpha z, z)$, $\alpha$ learned | $1$ if $z>0$ else $\alpha$ | rarely dies ($\alpha$ trained per channel) |
87
| ELU | $z$ if $z>0$ else $\alpha(e^z - 1)$ | $1$ if $z>0$ else $\alpha e^z$ | saturates gently for $z\to-\infty$ |
88
| GELU | $z\,\Phi(z)$, $\Phi$ the normal CDF | smooth, near $1$ for large $z$ | smooth, no hard death |
89
90
*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.
91
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
92
## 2.5 Softmax for multiclass outputs
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.
93
94
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:
95
96
$$\boxed{ \text{softmax}(z)_k = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}} }$$
97
98
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.
99
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
100
*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 was settled in section 6.3 of [Multilayer neural networks](/en/Machine%20Learning/06%20Multilayer%20neural%20networks).
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.
101
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
102
## 2.6 Choosing an activation
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.
103
104
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.
105
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
106
![Decision flow for choosing an activation per layer](/en/Deep%20Learning/02%20Activation%20functions/a/activation-choice.svg)
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.
107
108
*Choosing an activation: ReLU or GELU for hidden layers, and an output activation matched to the task.*
109
110
| layer / task | recommended activation | reason |
111
| --- | --- | --- |
112
| hidden (default) | ReLU or GELU | no positive-side saturation, cheap, trains fast |
113
| hidden (dead units) | leaky ReLU or ELU | keeps a nonzero gradient for $z < 0$ |
114
| output, regression | identity (none) | prediction is an unbounded real value |
115
| output, binary | sigmoid | maps score to a probability in $(0, 1)$ |
116
| output, multiclass | softmax | maps scores to a distribution over classes |
117
118
*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.
119
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
120
*With the per-layer nonlinearities fixed, the next lesson turns the gradients into updates: the optimizers that make deep training practical.*
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.
121
122
---
12d21f lugonthier 2026-07-24 11:49:19
Remove unused SVG files and update Markdown content for clarity and accuracy in MLOps and Machine Learning modules. Adjust references to optimization techniques and activation functions, and enhance explanations in the mathematical refresher section.
123
Next: [Optimization](/en/Deep%20Learning/03%20Optimization) · [Course overview](/en/Deep%20Learning)