The nonlinearity that makes depth worth having β and the choice that decides whether training works.
π Where this lives: the switch from sigmoid to ReLU around 2011 is one of the small changes that made deep learning practical. Nothing about the architecture changed; a one-line replacement of the activation function let gradients survive through many layers, and networks that had been untrainable started to train. It is the clearest example in the syllabus of a detail that looks cosmetic and is not. Search "ReLU vanishing gradient deep learning breakthrough".
Why a nonlinearity is essential
Ο(x) = 1/(1 + e^(βx)), Ο'(x) = Ο(1 β Ο) With x = 0, k = 1.
Move the input to Β±6 and watch the gradient fall to almost nothing while the output barely moves. A neuron in that region has stopped learning β the error signal reaching it is multiplied by a number close to zero.
FROM THE PREVIOUS TOPIC: without a nonlinear activation, stacked
layers collapse.
y = Wβ½Β²βΎ(Wβ½ΒΉβΎx + bβ½ΒΉβΎ) + bβ½Β²βΎ
= (Wβ½Β²βΎWβ½ΒΉβΎ)x + (Wβ½Β²βΎbβ½ΒΉβΎ + bβ½Β²βΎ)
= Wβ²x + bβ²
A HUNDRED LINEAR LAYERS ARE ONE LINEAR LAYER. So the activation
function is not a refinement β it is the reason depth exists.
WHAT AN ACTIVATION FUNCTION MUST PROVIDE:
1. NONLINEARITY, so that composition adds expressive power
2. DIFFERENTIABILITY (or near enough), so gradient-based
learning can work β this is why the McCulloch-Pitts step
function had to be abandoned
3. A USEFUL DERIVATIVE β not merely nonzero, but not
vanishingly small either, as the last section shows
4. CHEAPNESS, since it is evaluated once per unit per
example per pass
AND ONE PROPERTY THAT IS DESIRABLE RATHER THAN REQUIRED:
ZERO-CENTRED OUTPUT. If a layer's outputs are all positive,
the gradients for the next layer's weights all share a sign,
so the weight vector can only move in certain diagonal
directions and convergence zigzags. This is the standard
argument for tanh over sigmoid.
The functions
STEP (THRESHOLD, HEAVISIDE)
f(x) = 1 if x β₯ 0, else 0
derivative: 0 everywhere it exists, undefined at 0
β the original, used by McCulloch-Pitts and the perceptron
β NOT USABLE WITH GRADIENT DESCENT β a zero derivative carries
no information about which way to move the weights. THIS IS
WHY THE PERCEPTRON NEEDED ITS OWN LEARNING RULE rather than
gradient descent.
LINEAR (IDENTITY)
f(x) = x, fβ²(x) = 1
β used in the OUTPUT layer for REGRESSION, where the output
must be an unbounded real number
β useless in hidden layers, by the collapse argument
SIGMOID (LOGISTIC)
f(x) = 1 / (1 + e^βx) range (0, 1)
fβ²(x) = f(x)(1 β f(x))
THE DERIVATIVE IS EXPRESSED IN TERMS OF THE OUTPUT, which is
why implementations cache the forward-pass activations β the
backward pass needs them and would otherwise recompute.
WORKED VALUES:
x = β2 β 0.1192 x = 0 β 0.5000 x = 2 β 0.8808
DERIVATIVE VALUES:
x = 0 β 0.5(0.5) = 0.2500 β the MAXIMUM
x = 1 β 0.7311(0.2689) = 0.1966
x = 2 β 0.8808(0.1192) = 0.1050
x = 4 β 0.9820(0.0180) = 0.0177
β smooth, bounded, interpretable as a probability
β still the standard for a BINARY output unit
β VANISHING GRADIENT β the derivative never exceeds 0.25, and
approaches zero when the unit saturates
β NOT ZERO-CENTRED
β the exponential is comparatively expensive
TANH (HYPERBOLIC TANGENT)
f(x) = (eΛ£ β e^βx)/(eΛ£ + e^βx) range (β1, 1)
fβ²(x) = 1 β f(x)Β²
and note tanh(x) = 2Β·sigmoid(2x) β 1, so it is a rescaled
sigmoid
WORKED: x = β2 β β0.9640, x = 0 β 0, x = 2 β +0.9640
MAXIMUM DERIVATIVE: 1 β 0Β² = 1.0 at x = 0 β FOUR TIMES the
sigmoid's
β ZERO-CENTRED, which helps convergence
β a larger maximum gradient than sigmoid
β still SATURATES at both ends, so the vanishing gradient
problem is reduced and not solved
ReLU (RECTIFIED LINEAR UNIT)
f(x) = max(0, x)
fβ²(x) = 1 if x > 0, else 0 (undefined at 0; taken as 0)
WORKED: x = β2 β 0, x = 0 β 0, x = 0.5 β 0.5, x = 2 β 2
β NO SATURATION for positive input β the gradient is exactly 1,
so it neither shrinks nor grows as it propagates. THIS IS THE
PROPERTY THAT MADE DEEP NETWORKS TRAINABLE.
β extremely cheap: a comparison, no exponential
β produces SPARSE activation, since negative inputs give
exactly zero
β THE DYING ReLU PROBLEM: a unit whose input is always
negative has gradient zero forever, so it never recovers and
is permanently dead. A large learning rate can kill a
substantial fraction of a layer.
β not zero-centred, and unbounded above
LEAKY ReLU
f(x) = x if x > 0, else Ξ±x with Ξ± small, typically 0.01
WORKED at x = β2, Ξ± = 0.01: f = β0.02, and the derivative is
0.01 rather than 0
β FIXES THE DYING ReLU PROBLEM β a dead unit retains a small
gradient and can recover
(PARAMETRIC ReLU makes Ξ± a learned parameter.)
ELU (EXPONENTIAL LINEAR UNIT)
f(x) = x if x > 0, else Ξ±(eΛ£ β 1)
WORKED at x = β2, Ξ± = 1: 1(e^β2 β 1) = β0.8647
β smooth, closer to zero-centred than ReLU
β the exponential costs more
SOFTMAX β for a MULTI-CLASS OUTPUT LAYER
f(zα΅’) = e^{zα΅’} / Ξ£β±Ό e^{zβ±Ό}
It converts a vector of scores into a PROBABILITY
DISTRIBUTION: every output is in (0,1) and they sum to 1.
WORKED with z = (2.0, 1.0, 0.1):
e^2.0 = 7.3891
e^1.0 = 2.7183
e^0.1 = 1.1052
sum = 11.2125
softmax = (7.3891/11.2125, 2.7183/11.2125, 1.1052/11.2125)
= (0.6590, 0.2424, 0.0986)
and 0.6590 + 0.2424 + 0.0986 = 1.0000 β
NOTE THAT IT IS NOT APPLIED ELEMENTWISE β each output depends
on ALL the inputs, which is what makes the outputs compete
and sum to one. Every other function in this list is
elementwise.
The vanishing gradient problem, and how to choose
THE VANISHING GRADIENT PROBLEM β the central practical reason the
choice of activation matters, and the arithmetic makes it plain.
Backpropagation (a later topic) computes a gradient for an early
layer by MULTIPLYING the derivatives along the path back from
the output. With sigmoid units, each factor is at most 0.25.
after 1 layer: 0.25 = 2.5 Γ 10β»ΒΉ
after 5 layers: 0.25β΅ = 9.77 Γ 10β»β΄
after 10 layers: 0.25ΒΉβ° = 9.54 Γ 10β»β·
after 20 layers: 0.25Β²β° = 9.10 Γ 10β»ΒΉΒ³
THE GRADIENT REACHING AN EARLY LAYER IN A 20-LAYER SIGMOID
NETWORK IS ABOUT A TRILLIONTH OF THE GRADIENT AT THE OUTPUT.
In floating point that is indistinguishable from zero, so the
early layers DO NOT LEARN AT ALL β they keep their random
initial weights while the last few layers do all the work.
AND THAT IS AN OPTIMISTIC CALCULATION, because 0.25 is the
MAXIMUM derivative, attained only at x = 0. A saturated unit
contributes far less.
THIS IS WHY DEEP NETWORKS WERE UNTRAINABLE BEFORE ReLU. The
architecture was known β the multilayer perceptron dates from the
1980s β and the gradients simply did not arrive. With ReLU each
factor is exactly 1 for active units, so
1Β²β° = 1
and the gradient reaches the first layer undiminished.
THE OPPOSITE FAILURE, EXPLODING GRADIENTS, occurs when the
factors exceed 1 and the product grows without bound. The
standard remedy is GRADIENT CLIPPING β cap the gradient's
magnitude β and careful initialisation.
OTHER RESPONSES TO THE PROBLEM, worth naming:
BATCH NORMALISATION normalise each layer's inputs, keeping
units away from their saturated regions
RESIDUAL CONNECTIONS add a shortcut path that skips layers,
so the gradient has a route back that
does not pass through every
nonlinearity. This is what permitted
networks of hundreds of layers.
BETTER INITIALISATION Xavier and He scaling, as the previous
topic noted
HOW TO CHOOSE β the practical guidance:
WHERE USE
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
hidden layers, default ReLU (or Leaky ReLU if units are
dying)
hidden layers, shallow net tanh is acceptable
output, binary SIGMOID β gives a probability
classification
output, multi-class SOFTMAX β gives a distribution
classification
output, regression LINEAR β unbounded real value
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
THE ONE RULE THAT MATTERS MOST: THE OUTPUT ACTIVATION IS
DETERMINED BY THE TASK, NOT BY PREFERENCE. A regression network
with a sigmoid output cannot predict a value above 1; a
multi-class network without softmax produces scores that do not
compare. THE HIDDEN ACTIVATIONS ARE A TUNING CHOICE; THE OUTPUT
ACTIVATION IS PART OF THE PROBLEM SPECIFICATION.
A SUMMARY TABLE OF THE DERIVATIVES, since backpropagation needs
them and exams ask for them:
f(x) fβ²(x)
βββββββββββββββββββββββββββββββββββββββββββββ
step 0 (undefined at 0)
linear x 1
sigmoid Ο(x) Ο(x)(1 β Ο(x)) max 0.25
tanh tanh(x) 1 β tanhΒ²(x) max 1.0
ReLU max(0,x) 1 if x > 0 else 0
leaky x or Ξ±x 1 if x > 0 else Ξ±
βββββββββββββββββββββββββββββββββββββββββββββ
THE SIGMOID AND TANH DERIVATIVES BEING EXPRESSIBLE IN THE
FUNCTION'S OWN OUTPUT is a genuine computational convenience β
the backward pass reuses the forward pass's stored activations
and needs no fresh exponentials.
The 0.25Β²β° β 10β»ΒΉΒ³ figure is the whole story of why deep learning waited. The multilayer architecture existed in the 1980s; what did not exist was an activation whose derivative did not shrink the gradient at every layer. ReLU's derivative of exactly 1 is the entire fix, and it is a one-line change.
π Go further: the residual connection is the other half of the answer, and it is worth understanding as arithmetic rather than architecture. A block computing y = x + F(x) has derivative 1 + Fβ²(x) β so even if F's gradient vanishes, the 1 guarantees a path back. That single term is why ResNets trained at 152 layers when plain networks stalled around 20, and it generalises: whenever a signal must survive many transformations, an additive identity path preserves it. Search "residual connections identity shortcut gradient flow ResNet".
π‘ Exam angle: state why a nonlinear activation is essential β stacked linear layers collapse to one. Give each function with its formula, range and derivative: step (derivative 0, so unusable with gradient descent), linear, sigmoid Ο(x)(1βΟ(x)) with maximum 0.25, tanh 1βtanhΒ²(x) with maximum 1.0, ReLU, leaky ReLU. Be ready to compute values and derivatives at given inputs and to work a softmax showing the outputs sum to 1. Explain the vanishing gradient problem with the 0.25βΏ arithmetic and why ReLU solves it, plus the dying ReLU problem and leaky ReLU's fix. Know the rule that the output activation is set by the task: sigmoid for binary, softmax for multi-class, linear for regression.
Syllabus points
Step, sigmoid, tanh, ReLU
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.