Md. Asif Uddin

Chapter 3 · I.3

Problems

A chapter that uses mathematics has to teach that mathematics by making the reader compute. A chapter that only displays equations has failed, however correct the equations are.

M3Load-bearing

7/5 problems7/4 variants10/10 exercisesquota met, and enforced

The contract

  • 5 worked problems, minimum.
  • 4 distinct variants, and no variant more than half of them.
  • 10 exercises, every one with a published solution.
  • At least one numeric problem — present.
  • At least one symbolic problem — present.
  • At least one limit or counterexample problem — present.
  • At least one complexity or shape problem — present.
  • At least one ▲▲▲ problem — present.

Numerical instantiationSymbolic derivationLimiting caseConstructed failureProof or impossibilityDifferentiationCost accounting

Problem I.3.B01

Five activations at five points

numeric▲△△

All values rounded to 4 d.p.

STATEMENT

Evaluate sigmoid, tanh, ReLU, LeakyReLU with slope 0.010.01, and GELU at z{2, 0.5, 0, 0.5, 2}z \in \{-2,\ -0.5,\ 0,\ 0.5,\ 2\}. Then read four structural facts off the table that no single value shows.

GIVEN

σ(z)=11+ez,tanh(z)=ezezez+ez,ReLU(z)=max(0,z)\sigma(z) = \frac{1}{1+e^{-z}}, \qquad \tanh(z) = \frac{e^{z}-e^{-z}}{e^{z}+e^{-z}}, \qquad \mathrm{ReLU}(z) = \max(0, z)LeakyReLU(z)={zz>00.01zz0,GELU(z)=zΦ(z)\mathrm{LeakyReLU}(z) = \begin{cases} z & z > 0\\ 0.01z & z \le 0\end{cases}, \qquad \mathrm{GELU}(z) = z\,\Phi(z)

with Φ\Phi the standard normal CDF.

FIND

A 5×55\times 5 table of values, and four properties visible only across it.

STRATEGY

Compute the two exponential families first, since tanh follows from sigmoid; the two rectifiers need no arithmetic at all; GELU needs Φ\Phi.

SOLUTION

Step 1 — sigmoid. σ(2)=1/(1+e2)=1/8.3891=0.1192\sigma(-2) = 1/(1+e^{2}) = 1/8.3891 = 0.1192. By the symmetry σ(z)=1σ(z)\sigma(-z) = 1 - \sigma(z), σ(2)=0.8808\sigma(2) = 0.8808 without further work. σ(0)=1/2\sigma(0) = 1/2 exactly. σ(0.5)=1/(1+e0.5)=1/2.6487=0.3775\sigma(-0.5) = 1/(1+e^{0.5}) = 1/2.6487 = 0.3775, so σ(0.5)=0.6225\sigma(0.5) = 0.6225.

Step 2 — tanh from sigmoid. Using tanh(z)=2σ(2z)1\tanh(z) = 2\sigma(2z) - 1: tanh(0.5)=2σ(1)1=2(0.7311)1=0.4621\tanh(0.5) = 2\sigma(1) - 1 = 2(0.7311) - 1 = 0.4621, and tanh(2)=2σ(4)1=2(0.9820)1=0.9640\tanh(2) = 2\sigma(4) - 1 = 2(0.9820) - 1 = 0.9640. Odd symmetry gives the negatives.

Step 3 — the rectifiers. No arithmetic. ReLU zeroes the negatives and copies the positives; LeakyReLU scales the negatives by 0.010.01.

Step 4 — GELU. Φ(2)=0.0228\Phi(-2) = 0.0228, so GELU(2)=(2)(0.0228)=0.0455\mathrm{GELU}(-2) = (-2)(0.0228) = -0.0455. Φ(0)=0.5\Phi(0) = 0.5 so GELU(0)=0\mathrm{GELU}(0) = 0. Φ(2)=0.9772\Phi(2) = 0.9772, giving 1.95451.9545.

The table.

zzσ\sigmatanh\tanhReLULeakyReLUGELU
2.0-2.00.11920.11920.9640-0.96400.00000.00000.0200-0.02000.0455-0.0455
0.5-0.50.37750.37750.4621-0.46210.00000.00000.0050-0.00500.1543-0.1543
0.00.00.50000.50000.00000.00000.00000.00000.00000.00000.00000.0000
0.50.50.62250.62250.46210.46210.50000.50000.50000.50000.34570.3457
2.02.00.88080.88080.96400.96402.00002.00002.00002.00001.95451.9545

Four structural facts.

Only sigmoid fails to pass zero to zero. σ(0)=0.5\sigma(0) = 0.5. Every other column has 000 \mapsto 0. A layer of sigmoids therefore emits a nonzero mean even from centred input, and that offset compounds with depth — the historical reason tanh replaced sigmoid in hidden layers.

Sigmoid and tanh are bounded; the rectifiers are not. At z=2z = 2 the bounded pair are already at 0.880.88 and 0.960.96, close to their ceilings, while ReLU returns 22 and would return 200200 at z=200z = 200. Boundedness is what causes saturation and also what prevents blow-up.

GELU is not monotonic. From z=2z = -2 to z=0.5z = -0.5 the output falls from 0.0455-0.0455 to 0.1543-0.1543. No other column does this. GELU is a soft gate, not a soft switch, and this dip is where its behaviour genuinely differs from a smoothed ReLU.

GELU is close to ReLU where it matters and different where it does not. At z=2z = 2 the two differ by 0.04550.0455; at z=0.5z = -0.5 they differ by 0.15430.1543. The whole difference lives in a band around the origin, which is exactly where gradients are decided.

Answer

The 5×55\times5 table above. The four properties: sigmoid alone has φ(0)0\varphi(0) \neq 0; the bounded pair saturate while the rectifiers do not; GELU is non-monotonic on (,0.75)(-\infty, -0.75) roughly; and GELU and ReLU agree away from the origin and differ only near it.

Check — numeric · i-3-b01-activation-values.py
def sigmoid(z): return 1.0 / (1.0 + exp(-z))
def gelu(z):    return 0.5 * z * (1.0 + erf(z / sqrt(2.0)))

Prints the table above, to 4 d.p.

Executed in CI. The digits above are the digits it printed.

Check — sanity

The symmetries hold. σ(2)+σ(2)=0.1192+0.8808=1.0000\sigma(-2) + \sigma(2) = 0.1192 + 0.8808 = 1.0000 exactly, as σ(z)=1σ(z)\sigma(-z) = 1 - \sigma(z) requires. And tanh(2)=tanh(2)\tanh(-2) = -\tanh(2) to every printed digit.

The identity tanh(z)=2σ(2z)1\tanh(z) = 2\sigma(2z) - 1 checks out. At z=0.5z = 0.5: 2σ(1)1=2(0.7311)1=0.46222\sigma(1) - 1 = 2(0.7311) - 1 = 0.4622, against tanh(0.5)=0.4621\tanh(0.5) = 0.4621. The last digit differs by rounding of the intermediate, not by error.

GELU is between 00 and zz for z>0z > 0. 0<0.3457<0.50 < 0.3457 < 0.5 and 0<1.9545<20 < 1.9545 < 2. Since Φ(0,1)\Phi \in (0,1) and GELU=zΦ(z)\mathrm{GELU} = z\Phi(z), that must hold — and it is the fastest way to catch a sign or scale slip.

Where this breaks

Every value here assumes the activation is applied to a scalar independently. That is what “elementwise” means, and it is why one table of five numbers characterises the function completely. Softmax is not elementwise — its output at one coordinate depends on all of them — so no such table exists for it, and it belongs to Chapter I.4 with the losses rather than here with the activations.

Variation

Add SiLU (also called Swish), zσ(z)z\cdot\sigma(z), as a sixth column. Compare it with GELU at all five points and say where the two differ most.

Problem I.3.B02

Three identities that make the arithmetic disappear

symbolic▲▲△

Symbolic.

STATEMENT

Derive σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)\,(1 - \sigma(z)) and tanh(z)=1tanh2(z)\tanh'(z) = 1 - \tanh^2(z) from the definitions, then show tanh(z)=2σ(2z)1\tanh(z) = 2\sigma(2z) - 1. Say in each case what the identity buys.

GIVEN

σ(z)=(1+ez)1\sigma(z) = (1 + e^{-z})^{-1} and tanh(z)=(ezez)/(ez+ez)\tanh(z) = (e^{z} - e^{-z})/(e^{z} + e^{-z}).

FIND

The two derivative identities and the relation between the functions.

STRATEGY

Differentiate by the chain rule, then rewrite the result in terms of the function itself rather than of zz — that rewriting is the entire content of each identity.

SOLUTION

Step 1 — differentiate sigmoid. Write σ=u1\sigma = u^{-1} with u=1+ezu = 1 + e^{-z}, so u=ezu' = -e^{-z}. By the chain rule (The chain rule 0.MC.03):

σ(z)=u2u=ez(1+ez)2\sigma'(z) = -u^{-2}\,u' = \frac{e^{-z}}{(1+e^{-z})^{2}}

Step 2 — rewrite in terms of σ\sigma. Split the fraction deliberately:

ez(1+ez)2=11+ezez1+ez=σ(z)ez1+ez\frac{e^{-z}}{(1+e^{-z})^{2}} = \frac{1}{1+e^{-z}} \cdot \frac{e^{-z}}{1+e^{-z}} = \sigma(z) \cdot \frac{e^{-z}}{1+e^{-z}}

For the second factor, add and subtract 11 in the numerator:

ez1+ez=(1+ez)11+ez=1σ(z)\frac{e^{-z}}{1+e^{-z}} = \frac{(1+e^{-z}) - 1}{1+e^{-z}} = 1 - \sigma(z)

\boxed{\ \sigma'(z) = \sigma(z)\big(1 - \sigma(z)\big)\ } \tag{I.3.1}

What it buys. The derivative costs nothing extra at run time. The forward pass already computed σ(z)\sigma(z); the backward pass needs only one multiply and one subtract, and never touches eze^{-z} again. That is why frameworks cache activations rather than pre-activations for these units.

Step 3 — differentiate tanh. Quotient rule on tanh=(ezez)/(ez+ez)\tanh = (e^{z}-e^{-z})/(e^{z}+e^{-z}). Writing NN and DD for numerator and denominator, N=DN' = D and D=ND' = N, so

tanh(z)=NDNDD2=D2N2D2=1(ND)2\tanh'(z) = \frac{N'D - ND'}{D^{2}} = \frac{D^{2} - N^{2}}{D^{2}} = 1 - \left(\frac{N}{D}\right)^{2}

 tanh(z)=1tanh2(z) \boxed{\ \tanh'(z) = 1 - \tanh^{2}(z)\ }

What it buys. The same as before, and one more thing: at z=0z = 0, tanh=10=1\tanh' = 1 - 0 = 1. Compared with σ(0)=1/4\sigma'(0) = 1/4, tanh passes four times as much gradient at its best point. That factor of four, compounded over depth, is the quantitative form of “tanh trains better than sigmoid”.

Step 4 — the relation between them. Start from the right-hand side:

2σ(2z)1=21+e2z1=2(1+e2z)1+e2z=1e2z1+e2z2\sigma(2z) - 1 = \frac{2}{1+e^{-2z}} - 1 = \frac{2 - (1 + e^{-2z})}{1+e^{-2z}} = \frac{1 - e^{-2z}}{1+e^{-2z}}

Multiply numerator and denominator by eze^{z}:

=ezezez+ez=tanh(z)= \frac{e^{z} - e^{-z}}{e^{z} + e^{-z}} = \tanh(z)

\boxed{\ \tanh(z) = 2\sigma(2z) - 1\ } \tag{I.3.2}

What it buys. Tanh is not a second idea. It is sigmoid, stretched vertically by 22, shifted down by 11, and compressed horizontally by 22. Every property of one transfers to the other with those three transformations applied — including, by the chain rule, the derivative relation tanh(z)=4σ(2z)\tanh'(z) = 4\sigma'(2z), which is where the factor of four came from.

Answer

σ(z)=σ(z)(1σ(z)),tanh(z)=1tanh2(z),tanh(z)=2σ(2z)1\sigma'(z) = \sigma(z)(1-\sigma(z)), \qquad \tanh'(z) = 1 - \tanh^{2}(z), \qquad \tanh(z) = 2\sigma(2z) - 1

All three are exact identities on all of R\R, not approximations.

Check — sanity

Numerically at z=0.5z = 0.5. σ(0.5)=0.6225\sigma(0.5) = 0.6225, so (I.3.1) predicts σ(0.5)=0.6225×0.3775=0.2350\sigma'(0.5) = 0.6225 \times 0.3775 = 0.2350. Differencing numerically: (σ(0.501)σ(0.499))/0.002=0.2350(\sigma(0.501) - \sigma(0.499))/0.002 = 0.2350. Agreement to four digits.

The maxima are where they should be. σ(1σ)\sigma(1-\sigma) is a downward parabola in σ\sigma, maximised at σ=1/2\sigma = 1/2, i.e. z=0z = 0, with value 1/41/4. And 1tanh21 - \tanh^2 is maximised where tanh=0\tanh = 0, again z=0z = 0, with value 11. Both match the shapes plotted from I.3.B01.

The factor of four is consistent. Differentiating (I.3.2) gives tanh(z)=4σ(2z)\tanh'(z) = 4\sigma'(2z). At z=0z = 0: 1=4×0.251 = 4 \times 0.25. ✓

Where this breaks

Identity (I.3.1) expresses the derivative in terms of the output. That is convenient and it is also a trap in mixed precision: if σ(z)\sigma(z) has been stored in fp16 and rounded to exactly 1.01.0, then σ(1σ)\sigma(1-\sigma) evaluates to exactly 00 and the unit reports no gradient — even though the true derivative is small but nonzero. Recomputing from zz would not help much, but the failure is silent either way, and it is the reason logits rather than probabilities are carried through a loss (Log-sum-exp 0.NU.02).

Variation

Derive the derivative of SiLU, zσ(z)z\sigma(z), and express it using σ\sigma and σ\sigma' only. Then show it can exceed 11, unlike every derivative above.

Problem I.3.B03

The depth at which a sigmoid stack stops passing gradient

limit▲▲▲

Derivatives to 6 s.f.; products in scientific notation.

STATEMENT

Find the maximum of σ\sigma'. Compute the product of ten sigmoid derivatives at z=4|z| = 4. Then find the depth at which the product underflows fp16, both at z=4|z| = 4 and in the best case.

GIVEN

σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z)) from I.3.B02. The smallest positive fp16 value is the subnormal 224=5.96×1082^{-24} = 5.96\times10^{-8} (Floating point 0.NU.01). A gradient reaching layer 11 of an LL-layer stack is multiplied by one σ\sigma' per layer.

FIND

maxzσ(z)\max_z \sigma'(z); the value of σ(4)10\sigma'(4)^{10}; and the smallest LL with σL<224\sigma'^{\,L} < 2^{-24} at z=4|z| = 4 and at z=0z = 0.

STRATEGY

Maximise the derivative by treating it as a quadratic in σ\sigma rather than in zz — the substitution turns calculus into inspection. Then take logarithms to turn the repeated product into a linear count.

SOLUTION

Step 1 — the maximum. Let s=σ(z)(0,1)s = \sigma(z) \in (0,1). Then σ=s(1s)=ss2\sigma' = s(1-s) = s - s^2, a downward parabola in ss with vertex at s=1/2s = 1/2:

maxσ=12(112)=14=0.2500\max \sigma' = \tfrac12\left(1 - \tfrac12\right) = \tfrac14 = 0.2500

and s=1/2s = 1/2 means z=0z = 0. So the best a sigmoid ever does for a gradient is divide it by four, and it does that only at one point.

Step 2 — the derivative at z=4|z| = 4. σ(4)=1/(1+e4)=0.982014\sigma(4) = 1/(1+e^{-4}) = 0.982014, so

σ(4)=(0.982014)(0.017986)=0.0176627\sigma'(4) = (0.982014)(0.017986) = 0.0176627

Fourteen times smaller than the best case, from a pre-activation that is not extreme — z=4z = 4 is entirely ordinary in an unnormalised network.

Step 3 — ten layers.

σ(4)10=(0.0176627)10=2.955×1018\sigma'(4)^{10} = (0.0176627)^{10} = 2.955\times10^{-18}

For comparison the best case gives 0.2510=9.537×1070.25^{10} = 9.537\times10^{-7}. Even at every layer’s single most favourable point, ten layers cost six orders of magnitude.

Step 4 — the fp16 depth. Solve gL<224g^{L} < 2^{-24} by taking logarithms:

L>24ln2lng=16.6355lngL > \frac{-24\ln 2}{\ln g} = \frac{-16.6355}{\ln g}

At z=4|z| = 4, ln(0.0176627)=4.0362\ln(0.0176627) = -4.0362, so L>4.12L > 4.12, giving L=5\boxed{L = 5}.

At z=0z = 0, ln(0.25)=1.3863\ln(0.25) = -1.3863, so L>11.999L > 11.999, giving L=12\boxed{L = 12}.

What the two numbers say. A sigmoid network in fp16 has a hard ceiling of about twelve layers even under conditions that never occur — every unit sitting exactly at its most favourable point on every example. In realistic conditions, with pre-activations spread over a range including z=4|z| = 4, the ceiling is five. Not “training is slow past five layers”: the gradient is exactly zero, because the product is not representable and rounds to it.

This is why deep networks were not trained before roughly 2010, and it is why three separate later chapters exist. ReLU (this chapter) removes the shrinking factor entirely for active units. Normalisation (I.8) keeps zz near the favourable region. Residual connections (II.6) provide a path with derivative exactly 11 that no activation stands on.

Answer

maxzσ(z)=14,σ(4)10=2.955×1018\max_z \sigma'(z) = \tfrac14, \qquad \sigma'(4)^{10} = 2.955\times10^{-18}

fp16 underflow at depth 5\mathbf{5} for z=4|z| = 4, and at depth 12\mathbf{12} in the impossible best case z=0z = 0 throughout.

Check — numeric · i-3-b03-saturation-depth.py
def dsigmoid(z): s = sigmoid(z); return s * (1.0 - s)
prod, L = 1.0, 0
while prod > 2.0 ** -24: prod *= g; L += 1

Prints max sigma' 0.2500, sigma'(4)^10 2.955102e-18, and both depths.

Executed in CI. The digits above are the digits it printed.

Check — sanity

The maximum is attained where the function is symmetric. σ\sigma' inherits σ(z)=1σ(z)\sigma(-z) = 1-\sigma(z), so σ(z)=σ(z)\sigma'(-z) = \sigma'(z): the derivative is even, and an even function’s extremum on R\R sits at 00 unless it is bimodal. Consistent.

The logarithmic estimate agrees with the loop. The closed form gave L>4.12L > 4.12 and the loop counted 55; L>11.999L > 11.999 and the loop counted 1212. Two methods, same answer.

The magnitudes bracket correctly. 2.955×1018<9.537×1072.955\times10^{-18} < 9.537\times10^{-7}, since 0.0177<0.250.0177 < 0.25 and both are raised to the same power. A product of smaller numbers must be smaller.

Where this breaks

The analysis assumes every layer’s derivative is the same, which it is not: real pre-activations vary per unit and per example, so the true product is a mix of factors. That makes the situation worse, not better — the product is dominated by its smallest factors, and a single saturated layer anywhere in the stack zeroes the whole path regardless of how favourable the others are.

The analysis also assumes fp16 without loss scaling. Multiplying the loss by 2152^{15} before the backward pass shifts every gradient up by that factor and buys about four more layers, which is exactly what mixed-precision training does (II.8.B04). It buys layers; it does not remove the mechanism.

Variation

Repeat for tanh, whose maximum derivative is 11 rather than 1/41/4. Find the fp16 depth at z=4|z| = 4 and state how many extra layers tanh buys over sigmoid — then say whether that is enough to matter.

Problem I.3.B04

A unit that can never come back

counterexample▲▲△

Exact.

STATEMENT

Construct explicit weights and a bias for which a ReLU unit’s gradient is exactly zero for every input in the dataset, forever. Then show LeakyReLU repairs it, and quantify how long the repair takes.

GIVEN

A ReLU unit a=max(0,w,x+b)a = \max(0, \langle\vec{w},\vec{x}\rangle + b) with inputs drawn from [0,1]2[0,1]^2. Downstream the unit feeds a loss L\loss with L/a\partial \loss/\partial a of order 11.

FIND

A (w,b)(\vec{w}, b) making the unit permanently dead; a proof that no update revives it; and the number of steps LeakyReLU needs to recover, at a stated learning rate.

STRATEGY

Make the pre-activation negative on the whole input domain, then trace the gradient backwards and observe that it is zero at the first multiplication.

SOLUTION

Step 1 — the construction. Take

w=(1,1),b=10\vec{w} = (1, 1), \qquad b = -10

For any x[0,1]2\vec{x} \in [0,1]^2 the pre-activation is

z=x1+x210[10,8]z = x_1 + x_2 - 10 \in [-10, -8]

always negative, with a margin of at least 88 from the kink.

Step 2 — the output is always zero. a=max(0,z)=0a = \max(0, z) = 0 for every input in the domain. The unit contributes nothing to any prediction.

Step 3 — the gradient is always zero. By the chain rule, the gradient reaching the weights is

Lw=Laazzw=LaReLU(z)=0 for z<0x=0\frac{\partial \loss}{\partial \vec{w}} = \frac{\partial \loss}{\partial a} \cdot \frac{\partial a}{\partial z} \cdot \frac{\partial z}{\partial \vec{w}} = \frac{\partial \loss}{\partial a} \cdot \underbrace{\mathrm{ReLU}'(z)}_{=\,0\ \text{for } z<0} \cdot \vec{x} = \vec{0}

and identically L/b=0\partial\loss/\partial b = 0. Whatever the downstream error, it is multiplied by zero at this unit.

Step 4 — the death is permanent. Any gradient-based update has the form wwηL/w\vec{w} \leftarrow \vec{w} - \eta\,\partial\loss/\partial\vec{w}. Since the gradient is 0\vec{0}, the update is ww\vec{w} \leftarrow \vec{w}. The parameters cannot move, so zz stays in [10,8][-10,-8], so the gradient stays zero. The state is a fixed point of training. No learning rate, no schedule, no optimiser and no amount of data changes it. Momentum does not help either: momentum accumulates past gradients, and every past gradient was also zero.

The only escapes are external — weight decay pulling bb toward zero, or a different unit’s weights changing the input distribution — and neither is a property of this unit’s own learning.

Step 5 — LeakyReLU repairs it. With φ(z)=z\varphi(z) = z for z>0z>0 and 0.01z0.01z otherwise, the derivative on the negative side is 0.010.01 rather than 00. Now

Lb=La0.01\frac{\partial \loss}{\partial b} = \frac{\partial \loss}{\partial a}\cdot 0.01

Small, but not zero — and a small nonzero gradient applied repeatedly moves.

Step 6 — how long. Suppose L/a=1\partial\loss/\partial a = -1 on average (the loss wants this unit’s output larger) and η=0.01\eta = 0.01. Each step moves the bias by

Δb=ηLb=(0.01)(1)(0.01)=104\Delta b = -\eta\,\frac{\partial\loss}{\partial b} = -(0.01)(-1)(0.01) = 10^{-4}

To raise bb from 10-10 to 1-1, where the unit begins firing on part of the domain, needs

9104=90,000 steps\frac{9}{10^{-4}} = 90{,}000 \text{ steps}

The honest reading. LeakyReLU converts impossible into slow. Ninety thousand steps is a real cost, and it is why the slope is often set to 0.10.1 rather than 0.010.01 — which cuts the recovery to 9,0009{,}000 steps — and why careful initialisation, which prevents the death in the first place, matters more than the repair.

Answer

w=(1,1)\vec{w} = (1,1), b=10b = -10 over [0,1]2[0,1]^2 gives z8z \le -8 always, hence a=0a = 0 and =0\nabla = \vec{0} always. The state is a fixed point of gradient descent, so the unit is permanently dead.

LeakyReLU(0.01)(0.01) recovers it in about 90,000\mathbf{90{,}000} steps at η=0.01\eta = 0.01 with unit downstream gradient; at slope 0.10.1, about 9,0009{,}000.

Check — sanity

The margin is comfortable. The largest possible pre-activation is 1+110=81 + 1 - 10 = -8, not 0.001-0.001. So this is not a knife-edge construction that a single unusual input could disturb — the whole domain is 88 units from the kink.

The zero is structural, not numerical. ReLU(z)=0\mathrm{ReLU}'(z) = 0 exactly for z<0z < 0, not approximately. There is no underflow here and no precision to recover: the arithmetic is exact and the answer is exactly zero.

The LeakyReLU arithmetic is dimensionally right. Δb\Delta b is η×\eta \times (downstream gradient) ×\times (slope) =0.01×1×0.01= 0.01 \times 1 \times 0.01, three dimensionless factors, giving 10410^{-4} per step in the units of bb. And 9/104=9×1049 / 10^{-4} = 9\times10^{4}. ✓

Where this breaks

The construction needs the input domain to be bounded. On unbounded inputs — an unnormalised feature that occasionally reaches 10310^{3} — the unit is not dead, merely almost always silent, and it will receive a gradient on the rare examples that wake it. That is a different and much less severe failure, and it is one reason input normalisation (III.1) changes the character of this problem rather than only its magnitude.

Variation

Show that a GELU unit in the same state is never exactly dead, by computing GELU(10)\mathrm{GELU}'(-10). Then say whether the gradient it does receive is large enough to matter, and compare with LeakyReLU’s 0.010.01.

Problem I.3.B05

Depth without curvature is one layer

proof▲▲▲

Symbolic.

STATEMENT

Prove that a network of any depth whose activations are all the identity — or any linear map — computes a single affine function. Then state exactly what the theorem does and does not rule out.

GIVEN

An LL-layer network with h(0)=x\vec{h}^{(0)} = \vec{x} and h()=h(1)W()+b()\vec{h}^{(\ell)} = \vec{h}^{(\ell-1)}\mat{W}^{(\ell)} + \vec{b}^{(\ell)}, row-major throughout, with no nonlinearity between layers.

FIND

A single (W,b)(\mat{W}, \vec{b}) with h(L)=xW+b\vec{h}^{(L)} = \vec{x}\mat{W} + \vec{b} for all x\vec{x}, together with explicit formulas for both.

STRATEGY

Induct on depth. The base case is one layer; the inductive step substitutes the hypothesis into the next layer and collects terms. The only algebra needed is that matrix multiplication distributes over addition.

SOLUTION

Base case, L=1L = 1. h(1)=xW(1)+b(1)\vec{h}^{(1)} = \vec{x}\mat{W}^{(1)} + \vec{b}^{(1)}, which is already of the required form with W=W(1)\mat{W} = \mat{W}^{(1)} and b=b(1)\vec{b} = \vec{b}^{(1)}.

Inductive hypothesis. Suppose after \ell layers

h()=xA+c\vec{h}^{(\ell)} = \vec{x}\,\mat{A}_{\ell} + \vec{c}_{\ell}

for some A\mat{A}_\ell and c\vec{c}_\ell not depending on x\vec{x}.

Inductive step. Apply layer +1\ell+1 and expand, using distributivity:

h(+1)=h()W(+1)+b(+1)=(xA+c)W(+1)+b(+1)=x(AW(+1))A+1+(cW(+1)+b(+1))c+1\begin{aligned} \vec{h}^{(\ell+1)} &= \vec{h}^{(\ell)}\mat{W}^{(\ell+1)} + \vec{b}^{(\ell+1)} \\ &= \big(\vec{x}\mat{A}_{\ell} + \vec{c}_{\ell}\big)\mat{W}^{(\ell+1)} + \vec{b}^{(\ell+1)} \\ &= \vec{x}\underbrace{\big(\mat{A}_{\ell}\mat{W}^{(\ell+1)}\big)}_{\textstyle \mat{A}_{\ell+1}} + \underbrace{\big(\vec{c}_{\ell}\mat{W}^{(\ell+1)} + \vec{b}^{(\ell+1)}\big)}_{\textstyle \vec{c}_{\ell+1}} \end{aligned}

which is again of the required form. By induction it holds for all LL, with

W=W(1)W(2)W(L),b==1Lb() ⁣ ⁣j=+1L ⁣ ⁣W(j)\mat{W} = \mat{W}^{(1)}\mat{W}^{(2)}\cdots\mat{W}^{(L)}, \qquad \vec{b} = \sum_{\ell=1}^{L}\vec{b}^{(\ell)}\!\!\prod_{j=\ell+1}^{L}\!\!\mat{W}^{(j)}

\blacksquare (I.3.3)

The same result for any linear activation. If φ(z)=αz\varphi(z) = \alpha z then applying it elementwise is multiplication by αI\alpha\mat{I}, which is itself a linear map and can be absorbed into the neighbouring W\mat{W}. The proof is unchanged with W()\mat{W}^{(\ell)} replaced by αW()\alpha\mat{W}^{(\ell)}.

Even an affine activation collapses. φ(z)=αz+β\varphi(z) = \alpha z + \beta adds a constant row, which merges into the bias. So no affine activation escapes.

What is ruled out. The hypothesis class of the deep linear network equals that of a single layer of the same input and output widths. Nothing reachable by the deep one is unreachable by the shallow one. Depth has bought zero expressiveness.

What is not ruled out — and this is the part usually stated wrongly.

The parameter counts differ. The deep network has dd+1\sum d_\ell d_{\ell+1} parameters against the shallow one’s d0dLd_0 d_L. It is over-parameterised for what it computes, and the map from parameters to functions is many-to-one.

The rank can be lower. If some intermediate width dd_\ell is smaller than both d0d_0 and dLd_L, the product W\mat{W} has rank at most dd_\ell (Rank, eigenvalues and the singular value decomposition 0.LA.04). So a deep linear network with a bottleneck is more restricted than one layer, not equal to it. This is the linear autoencoder of Chapter I.12, and its restriction is the whole point.

The optimisation differs entirely. The loss surface of the deep linear network is non-convex in its parameters even though the function class is linear. It has saddle points the single layer does not, gradient descent on it has a different implicit bias, and it converges at a different rate. Studying deep linear networks is a live research programme precisely because the trajectory is interesting while the destination is not.

Answer

h(L)=xW+b,W==1LW()\vec{h}^{(L)} = \vec{x}\,\mat{W} + \vec{b}, \qquad \mat{W} = \prod_{\ell=1}^{L}\mat{W}^{(\ell)}

The function class is that of one affine layer, for every depth LL and any linear or affine activation. Expressiveness is unchanged; parameterisation, rank and optimisation are not.

Check — sanity

Shapes conform. W(1)\mat{W}^{(1)} is d0×d1d_0\times d_1, …, W(L)\mat{W}^{(L)} is dL1×dLd_{L-1}\times d_L, so the product is d0×dLd_0\times d_L — exactly the shape a single layer from input to output would have.

A two-layer instance checks by hand. With W(1)=[1201]\mat{W}^{(1)} = \begin{bmatrix}1&2\\0&1\end{bmatrix}, b(1)=(1,0)\vec{b}^{(1)} = (1,0), W(2)=[1011]\mat{W}^{(2)} = \begin{bmatrix}1&0\\1&1\end{bmatrix}, b(2)=(0,1)\vec{b}^{(2)} = (0,1) and x=(1,1)\vec{x} = (1,1): layer by layer, h(1)=(2,3)\vec{h}^{(1)} = (2,3) then h(2)=(5,4)\vec{h}^{(2)} = (5,4). By the formula, W=[3211]\mat{W} = \begin{bmatrix}3&2\\1&1\end{bmatrix} and b=(1,0)W(2)+(0,1)=(1,1)\vec{b} = (1,0)\mat{W}^{(2)} + (0,1) = (1,1), so xW+b=(4,3)+(1,1)=(5,4)\vec{x}\mat{W} + \vec{b} = (4,3) + (1,1) = (5,4). ✓

It is consistent with I.1.B01. There, removing the ReLU changed y^\hat{y} from 22 to 11. If the collapse theorem said the nonlinearity did nothing, that check would have had to give the same number. It did not, which is the same fact from the other side.

Where this breaks

The proof needs the activation to be applied to the whole vector uniformly and to be linear. Two near-misses:

A single nonlinear unit among linear ones does not collapse. The proof’s inductive step requires every layer to be affine; one exception breaks the chain.

Weight sharing does not save it. Setting W(1)==W(L)\mat{W}^{(1)} = \cdots = \mat{W}^{(L)} gives W=(W(1))L\mat{W} = (\mat{W}^{(1)})^{L}, still a single matrix. A recurrent network without a nonlinearity is a single linear map too, which is worth knowing before Chapter I.9.

Variation

Take W(1)R100×2\mat{W}^{(1)} \in \R^{100\times 2} and W(2)R2×100\mat{W}^{(2)} \in \R^{2\times 100}. State the rank of the product and describe the set of functions this two-layer linear network can compute, compared with one 100×100100\times100 layer.

Problem I.3.B06

The GELU derivative, and the kink that is not there

gradient▲▲△

4 d.p.

STATEMENT

Derive GELU\mathrm{GELU}', evaluate it at seven points, and compare its behaviour at the origin with ReLU’s. Then state the optimisation consequence of the difference and find where the derivative is most negative.

GIVEN

GELU(z)=zΦ(z)\mathrm{GELU}(z) = z\,\Phi(z) with Φ\Phi the standard normal CDF and Φ=ϕ\Phi' = \phi, the standard normal density ϕ(z)=ez2/2/2π\phi(z) = e^{-z^2/2}/\sqrt{2\pi}.

FIND

A closed form for GELU\mathrm{GELU}'; its values at z{3,1,0.5,0,0.5,1,3}z \in \{-3,-1,-0.5,0,0.5,1,3\}; the value at z=0z=0 against ReLU’s; and minzGELU(z)\min_z \mathrm{GELU}'(z).

STRATEGY

Product rule, then evaluate. The interesting content is entirely in what the second term does for negative zz.

SOLUTION

Step 1 — differentiate. By the product rule on zΦ(z)z \cdot \Phi(z):

\mathrm{GELU}'(z) = \Phi(z) + z\,\phi(z) \tag{I.3.4}

Two terms with different characters. Φ(z)(0,1)\Phi(z) \in (0,1) is a soft gate rising from 00 to 11. The correction zϕ(z)z\phi(z) is positive for z>0z>0, negative for z<0z<0, and decays to zero in both tails because the Gaussian density does.

Step 2 — evaluate.

zzGELUGELU\mathrm{GELU}'ReLU\mathrm{ReLU}'
3.0-3.00.0040-0.00400.0119-0.011900
1.0-1.00.1587-0.15870.0833-0.083300
0.5-0.50.1543-0.1543+0.1325+0.132500
0.00.00.00000.0000+0.5000+0.5000undefined
0.50.50.34570.3457+0.8675+0.867511
1.01.00.84130.8413+1.0833+1.083311
3.03.02.99602.9960+1.0119+1.011911

Step 3 — the origin. GELU(0)=Φ(0)+0=0.5\mathrm{GELU}'(0) = \Phi(0) + 0 = 0.5, and it is a genuine two-sided derivative: GELU is CC^\infty everywhere, being a product of smooth functions.

ReLU has no derivative at 00 at all. Its left slope is 00, its right slope is 11, and every framework silently picks a subgradient from [0,1][0,1] — usually 00, sometimes 0.50.5, sometimes 11. GELU’s 0.50.5 is not a convention: it is the value.

Step 4 — the minimum. Setting GELU=0\mathrm{GELU}'' = 0 gives 2ϕ(z)+zϕ(z)=ϕ(z)(2z2)=02\phi(z) + z\phi'(z) = \phi(z)(2 - z^2) = 0, so z=2=1.4142z = -\sqrt2 = -1.4142 (taking the negative root). There

GELU(2)=Φ(1.4142)+(1.4142)ϕ(1.4142)=0.07860.2075=0.1289\mathrm{GELU}'(-\sqrt2) = \Phi(-1.4142) + (-1.4142)\phi(-1.4142) = 0.0786 - 0.2075 = -0.1289

The derivative goes negative. No activation studied so far does this. Between roughly z=z = -\infty and z0.75z \approx -0.75, increasing the pre-activation decreases the output.

Step 5 — the optimisation consequences. Three, in order of how often they are stated correctly.

No dead region. GELU(10)=Φ(10)+(10)ϕ(10)\mathrm{GELU}'(-10) = \Phi(-10) + (-10)\phi(-10), which is about 7.6×1023-7.6\times10^{-23} — vanishingly small but nonzero. So the dead-unit failure of I.3.B04 cannot occur exactly, though at 102310^{-23} the distinction is academic and fp16 will round it to zero regardless.

No kink, so second-order methods are well defined. Anything relying on curvature — natural gradient, K-FAC, or simply a smooth loss landscape — is better behaved when the activation is twice differentiable. ReLU’s Hessian is a sum of Dirac deltas at the kinks.

The negative lobe is a real functional difference. It lets a unit express “this input is moderately against my feature” with a small negative output rather than with silence. Whether that helps is empirical, and the honest answer is that GELU’s advantage over ReLU in transformers is consistent but small, and no convincing theoretical account of it exists.

Answer

GELU(z)=Φ(z)+zϕ(z)\mathrm{GELU}'(z) = \Phi(z) + z\,\phi(z)

GELU(0)=0.5\mathrm{GELU}'(0) = 0.5 exactly, where ReLU(0)\mathrm{ReLU}'(0) does not exist. The minimum is GELU(2)=0.1289\mathrm{GELU}'(-\sqrt2) = -0.1289, so the derivative is negative on roughly (,0.75)(-\infty, -0.75).

Check — numeric · i-3-b06-gelu-derivative.py
def dgelu(z): return Phi(z) + z * phi(z)
lo = min(dgelu(z / 1000.0) for z in range(-3000, 1))

Prints the table, most negative dGELU -0.1289, and dGELU(0) 0.5000.

Executed in CI. The digits above are the digits it printed.

Check — sanity

The tails match ReLU. At z=3z = 3, GELU=1.01191\mathrm{GELU}' = 1.0119 \approx 1; at z=3z=-3, 0.01190-0.0119 \approx 0. GELU is asymptotically ReLU in both directions, as its construction intends.

The derivative integrates back to the function. Between z=0z=0 and z=0.5z=0.5 the mean of GELU\mathrm{GELU}' is roughly (0.5+0.8675)/2=0.684(0.5+0.8675)/2 = 0.684, so the predicted rise is 0.684×0.5=0.3420.684 \times 0.5 = 0.342 against the actual 0.34570=0.34570.3457 - 0 = 0.3457. Agreement to two digits from a two-point trapezoid, which is as much as that method deserves.

The minimum is where the second derivative says. 2z2=02 - z^2 = 0 at z=±2z = \pm\sqrt2, and the numerical scan over 3,0013{,}001 points found 0.1289-0.1289, consistent with the closed-form evaluation at 2-\sqrt2.

Where this breaks

Everything above is for the exact GELU. The tanh approximation 0.5z(1+tanh(2/π(z+0.044715z3)))0.5z(1 + \tanh(\sqrt{2/\pi}(z + 0.044715z^3))), which most implementations actually use, has a slightly different derivative — the minimum is near 0.1288-0.1288 rather than 0.1289-0.1289, and the two functions differ by up to about 10310^{-3} around z2|z| \approx 2. Small, but the two are not the same function, and a paper reporting “GELU” may mean either.

Variation

Derive the derivative of SiLU, zσ(z)z\sigma(z), and find its minimum. Compare with GELU’s 0.1289-0.1289 and say which activation has the deeper negative lobe.

Problem I.3.B07

What activations cost, in memory and in arithmetic

complexity▲▲△

Memory to 1 d.p.; FLOP counts approximate and stated as such.

STATEMENT

For a transformer-scale layer, compute the memory one activation tensor occupies and the arithmetic each activation function costs. Then compare both with the matrix multiply they sit next to, and draw the conclusion.

GIVEN

Batch B=8B = 8, sequence T=2048T = 2048, width d=4096d = 4096, depth L=32L = 32. Elementwise costs, approximately: ReLU 11 FLOP (a compare), LeakyReLU 22, sigmoid 44 (exp, add, divide), tanh 66, GELU with erf about 1212, GELU with the tanh approximation about 88.

FIND

The element count and memory of one activation tensor in bf16 and fp32; the total across LL layers; the GFLOPs each activation costs per layer; and the ratio to one d×dd\times d matrix multiply.

STRATEGY

Count elements once and reuse. Memory scales with the count; arithmetic scales with the count times a small constant; the matmul scales with the count times dd — and that last factor is the whole answer.

SOLUTION

Step 1 — the element count.

B×T×d=8×2048×4096=67,108,8646.71×107B \times T \times d = 8 \times 2048 \times 4096 = 67{,}108{,}864 \approx 6.71\times10^{7}

Step 2 — memory. At 22 bytes per element in bf16:

6.71×107×2=1.342×108 bytes=134.2 MB6.71\times10^{7} \times 2 = 1.342\times10^{8}\ \text{bytes} = 134.2\ \text{MB}

and in fp32, 268.4268.4 MB. Across 3232 layers, storing one activation tensor per layer for the backward pass:

32×134.2 MB=4.29 GB (bf16),8.59 GB (fp32)32 \times 134.2\ \text{MB} = 4.29\ \text{GB (bf16)}, \qquad 8.59\ \text{GB (fp32)}

Step 3 — arithmetic. Each function is cc FLOPs per element:

ActivationFLOP/elementGFLOP per layer
ReLU110.070.07
LeakyReLU220.130.13
sigmoid440.270.27
tanh660.400.40
GELU (erf)12120.810.81
GELU (tanh approx)880.540.54

Step 4 — the comparison that settles it. One d×dd\times d matrix multiply on the same tensor costs 2×(BTd)×d2 \times (BTd) \times d FLOPs:

2×6.71×107×4096=5.50×1011=549.8 GFLOP2 \times 6.71\times10^{7} \times 4096 = 5.50\times10^{11} = 549.8\ \text{GFLOP}

against the costliest activation’s 0.810.81 GFLOP — a ratio of about 680\mathbf{680}.

The conclusion, in two halves.

Arithmetically, activations are free. Even GELU with erf is under 0.2%0.2\% of the matmul it follows. Choosing ReLU over GELU to save compute is optimising the wrong term by a factor of several hundred. Any argument for ReLU on grounds of speed is, at this scale, wrong.

In memory they are not free at all. 4.294.29 GB of stored activations is a substantial fraction of an 8080 GB accelerator, and it is why activation checkpointing exists (II.8.B03): recompute the activation in the backward pass rather than store it, trading that 0.810.81 GFLOP — which is free — against 134134 MB per layer, which is not.

That asymmetry is the general shape of the thing. Elementwise operations are bounded by memory bandwidth, matrix operations by arithmetic. An activation reads BTdBTd values and writes BTdBTd values to do cBTdc \cdot BTd FLOPs, giving an arithmetic intensity of about c/4c/4 FLOPs per byte in bf16 — for ReLU that is 0.250.25, against roughly 200200 for the matmul. Chapter VIII.6 turns this into a roofline argument; here it is enough to notice that the two operations live in different worlds.

Answer

One activation tensor: 6.71×1076.71\times10^{7} elements, 134.2134.2 MB in bf16, 268.4268.4 MB in fp32. Across 3232 layers, 4.29\mathbf{4.29} GB in bf16.

Arithmetic ranges from 0.070.07 GFLOP (ReLU) to 0.810.81 GFLOP (GELU-erf) per layer, against 549.8\mathbf{549.8} GFLOP for the accompanying matmul — a ratio of about 680680.

Check — numeric · i-3-b07-activation-cost.py
acts = B * T * d
print(f"{name}: {acts * bits // 8 / 1e6:.1f} MB each")
mm = 2 * acts * d

Prints every figure above, including the 682×682\times ratio.

Executed in CI. The digits above are the digits it printed.

Check — sanity

Memory scales with elements, not with the function. ReLU and GELU store identical tensors: 134.2134.2 MB either way. Only the arithmetic column varies. If memory had varied by activation, an element would have been double-counted.

bf16 is exactly half of fp32. 134.2×2=268.4134.2 \times 2 = 268.4. Two bytes against four.

The matmul ratio is d/2d/2 times the activation constant. Matmul is 2BTdd2 \cdot BTd \cdot d and the activation is cBTdc \cdot BTd, so the ratio is 2d/c=8192/12=6832d/c = 8192/12 = 683 for GELU-erf. The snippet printed 682682, differing by integer division. Consistent.

Where this breaks

The count assumes one stored tensor per layer, which understates reality: a transformer block stores several intermediates, and the FFN’s inner width is typically 4d4d, so its activation tensor is four times the size computed here. The 4.294.29 GB is therefore a floor, not an estimate. II.8.B01 does the full accounting; this problem establishes only that the activation’s arithmetic is negligible and its memory is not.

Variation

Recompute for the FFN’s inner activation at width 4d=163844d = 16384. State the new per-layer memory and say what fraction of an 8080 GB accelerator 3232 such layers would occupy.

Exercises

Every one has a published solution. A hidden solution is a solution; a missing one is an abandonment.

I.3.X01Sigmoid and tanh, and the identity between themnumeric▲△△

Compute σ(z)\sigma(z), tanh(z)\tanh(z), σ(z)\sigma'(z) and tanh(z)\tanh'(z) at z{1,0,1.5}z \in \{-1, 0, 1.5\}. Verify tanh(z)=2σ(2z)1\tanh(z) = 2\sigma(2z)-1 at each point, and state the ratio tanh/σ\tanh'/\sigma' at z=0z=0.

Hint

Compute σ\sigma first and get everything else from it.

Solution
zzσ\sigmatanh\tanh2σ(2z)12\sigma(2z)-1σ\sigma'tanh\tanh'
1.0-1.00.26890.26890.7616-0.76160.7616-0.76160.19660.19660.42000.4200
0.00.00.50000.50000.00000.00000.00000.00000.25000.25001.00001.0000
1.51.50.81760.81760.90510.90510.90510.90510.14910.14910.18070.1807

Working at z=1z = -1. σ(1)=1/(1+e)=1/3.7183=0.2689\sigma(-1) = 1/(1+e) = 1/3.7183 = 0.2689. Then σ=(0.2689)(0.7311)=0.1966\sigma' = (0.2689)(0.7311) = 0.1966. For tanh use the identity: 2σ(2)1=2(0.1192)1=0.76162\sigma(-2) - 1 = 2(0.1192) - 1 = -0.7616, and tanh=1(0.7616)2=10.5800=0.4200\tanh' = 1 - (-0.7616)^2 = 1 - 0.5800 = 0.4200.

The identity holds to every printed digit at all three points, which is what an exact identity should do.

The ratio at z=0z = 0. tanh(0)/σ(0)=1.0000/0.2500=4\tanh'(0)/\sigma'(0) = 1.0000/0.2500 = \mathbf{4}.

Why that four matters. Over LL layers the gradient is multiplied by one derivative per layer, so at their best points a tanh stack passes 4L4^{L} times more gradient than a sigmoid stack. At L=10L = 10 that is a factor of about a million. It is the entire quantitative content of the historical preference for tanh over sigmoid in hidden layers, and I.3.B02 showed it follows from the identity by the chain rule: tanh(z)=4σ(2z)\tanh'(z) = 4\sigma'(2z).

But notice the third row. At z=1.5z = 1.5 the two derivatives are 0.14910.1491 and 0.18070.1807 — a ratio of 1.211.21, not 44. The advantage is largest at the origin and shrinks as either function saturates. Tanh delays the problem; it does not solve it, which is why Chapter I.8 exists.

I.3.X02Softplus and its derivativesymbolic▲△△

Softplus is ς(z)=log(1+ez)\varsigma(z) = \log(1 + e^{z}). Show that ς(z)=σ(z)\varsigma'(z) = \sigma(z), that ς(z)>ReLU(z)\varsigma(z) > \mathrm{ReLU}(z) everywhere, and that ς(z)ReLU(z)0\varsigma(z) - \mathrm{ReLU}(z) \to 0 as z|z| \to \infty. Then say why softplus is nevertheless rarely used.

Hint

For the last part, evaluate ς\varsigma naively at z=100z = 100 in fp32.

Solution

The derivative. By the chain rule,

ς(z)=11+ezez=ez1+ez=11+ez=σ(z)\varsigma'(z) = \frac{1}{1+e^{z}} \cdot e^{z} = \frac{e^{z}}{1+e^{z}} = \frac{1}{1+e^{-z}} = \sigma(z)

dividing through by eze^{z} at the last step. Softplus is the antiderivative of the sigmoid — a fact worth carrying, because it means the smooth gate and the smooth rectifier are the same object seen at two orders of differentiation.

It dominates ReLU. For z0z \le 0, ς(z)=log(1+ez)>log1=0=ReLU(z)\varsigma(z) = \log(1+e^{z}) > \log 1 = 0 = \mathrm{ReLU}(z). For z>0z > 0, write ς(z)=z+log(1+ez)>z=ReLU(z)\varsigma(z) = z + \log(1 + e^{-z}) > z = \mathrm{ReLU}(z), since the log term is positive. So ς>ReLU\varsigma > \mathrm{ReLU} everywhere.

The gap vanishes. From the two forms above, the gap is log(1+ez)\log(1+e^{z}) for z<0z<0 and log(1+ez)\log(1+e^{-z}) for z>0z>0 — in both cases log(1+ez)\log(1+e^{-|z|}), which tends to log1=0\log 1 = 0. At z=5|z| = 5 the gap is already 0.00670.0067.

Why it is rarely used. Three reasons, in decreasing order of importance.

It overflows if written naively. At z=100z = 100, e100=2.7×1043e^{100} = 2.7\times10^{43} — fine in fp32, but at z=800z = 800 it is \infty and log()=\log(\infty) = \infty. The correct implementation is max(z,0)+log(1+ez)\max(z,0) + \log(1 + e^{-|z|}) (Log-sum-exp 0.NU.02), which never overflows. Every framework does this; the point is that the naive form is a real trap.

It costs more. Roughly 66 FLOPs per element against ReLU’s 11 — though I.3.B07 showed that difference is under 0.2%0.2\% of the layer, so this reason is weaker than it is usually stated.

It has no exact zeros. ς(z)>0\varsigma(z) > 0 always, so no unit is ever silent and no activation is ever sparse. Sparsity was the original argument for ReLU, and whether it matters is still debated — but softplus definitively does not have it.

What it is used for. Where a strictly positive output is required and smoothness matters: a predicted variance, a rate parameter, a scale in a distribution. Chapter I.13 uses it exactly there.

I.3.X06What an elementwise function does to a shapeshape▲△△

State the output shape of an elementwise activation applied to a tensor of shape (B,T,d)(B, T, d), and the shape of its Jacobian if written out in full. Then name three operations that look like activations but do not preserve shape or elementwiseness, and say where each belongs.

Hint

The full Jacobian of a map from nn numbers to nn numbers is n×nn \times n, even when almost all of it is zero.

Solution

The output shape. (B,T,d)(B, T, d) — identical. That is the defining property: an elementwise function applies a scalar map to each entry independently, so nothing about the arrangement changes.

The Jacobian’s shape. Flattening the tensor to n=BTdn = BTd numbers, the full Jacobian is n×nn \times n. At B=8B=8, T=2048T=2048, d=4096d=4096 that is (6.71×107)24.5×1015(6.71\times10^7)^2 \approx 4.5\times10^{15} entries — around 99 petabytes in bf16.

Nobody stores it, and the reason is the point. The Jacobian is diagonal: ai/zj=0\partial a_i/\partial z_j = 0 whenever iji \neq j, because aia_i depends on ziz_i alone. So it is represented by its nn diagonal entries, and the backward pass is one elementwise multiply rather than a matrix product. Elementwiseness is not a stylistic choice — it is what makes the backward pass affordable (I.3.X08 shows the contrast).

Three impostors.

Softmax. Shape (B,T,d)(B,T,d)(B,T,d) \to (B,T,d), so it passes the shape test. But entry ii depends on every entry in its row, so its Jacobian is dense within each row — d×dd \times d blocks, not a diagonal. It is a normalisation, and it belongs with the losses in Chapter I.4 and with attention in II.3.

LayerNorm. Same shape in and out, and again not elementwise: subtracting the mean and dividing by the standard deviation couples every entry in the normalised group. Its backward pass has three terms rather than one, which is exactly problem I.8.B02. It belongs in Chapter I.8.

Max-pooling. Not even shape-preserving: (B,C,H,W)(B,C,H/2,W/2)(B, C, H, W) \to (B, C, H/2, W/2) for a 2×22\times2 window. It is a downsampling, its Jacobian routes each output gradient to exactly one input, and it belongs in Chapter I.11.

The test to remember. Ask whether changing one input entry can change any other output entry. If yes, it is not elementwise, its Jacobian is not diagonal, and its backward pass is not one multiply — three consequences that always arrive together.

I.3.X03A ReLU network is piecewise linearproof▲▲△

Prove that a network with ReLU activations computes a piecewise-linear function of its input, and that the pieces are convex polytopes. Then bound the number of pieces for a network with LL layers of nn units each, and say what that bound does and does not tell you.

Hint

Fix which units are active. What does the network compute then?

Solution

Step 1 — fix an activation pattern. For a given input, each ReLU unit is either active (z>0z>0, passing zz) or inactive (z0z \le 0, passing 00). Record this as a binary pattern p{0,1}nLp \in \{0,1\}^{nL}, one bit per unit.

Step 2 — the network is affine on each pattern’s region. Hold pp fixed. Then every ReLU is replaced by either the identity or the zero map, both linear. The network becomes a composition of affine maps, which by I.3.B05 is a single affine map xxWp+bp\vec{x} \mapsto \vec{x}\mat{W}_p + \vec{b}_p. So on the set of inputs producing pattern pp, the network is exactly affine. \blacksquare

Step 3 — the regions are convex polytopes. Each unit’s condition is w,x+b>0\langle\vec{w},\vec{x}\rangle + b > 0 or 0\le 0 — a half-space in x\vec{x}, provided its inputs are affine in x\vec{x}, which they are once the earlier layers’ patterns are fixed. A region is the intersection of nLnL half-spaces, and an intersection of half-spaces is a convex polytope. \blacksquare

Step 4 — counting. Naively there are 2nL2^{nL} patterns, but almost all are unrealisable: with nn units in the first layer over a dd-dimensional input, the number of regions nn hyperplanes cut Rd\R^{d} into is k=0d(nk)\sum_{k=0}^{d}\binom{n}{k}, not 2n2^{n}. Composing layers gives the standard bound

#regions  =1Lk=0d(nk)  O ⁣(ndL)\#\text{regions} \ \le\ \prod_{\ell=1}^{L}\sum_{k=0}^{d}\binom{n_\ell}{k} \ \approx\ O\!\left(n^{dL}\right)

which is exponential in depth and polynomial in width. That asymmetry is the usual formal argument for depth: to match a depth-LL network’s region count, a shallow one needs width exponential in LL.

What the bound does not tell you. Three things, and they matter.

It counts pieces, not usefulness. A function can have 102010^{20} linear pieces and be a poor model. Region count is a capacity measure, and I.1.T2 already warned that capacity is not accuracy.

It is an upper bound, and trained networks are far below it. Empirically the number of regions a trained network actually uses is orders of magnitude smaller than the bound, and grows roughly linearly rather than exponentially with depth.

It says nothing about optimisation. The regions exist in the hypothesis class whether or not gradient descent can arrange them usefully — the same gap I.3.T2 flags for universal approximation.

A consequence worth keeping. Since the network is affine on each region, it is differentiable on the interior of each region and non-differentiable only on the boundaries, which form a measure-zero set. That is why training works at all despite ReLU having no derivative at its kink: the probability of landing exactly on a boundary is zero, and the subgradient convention of I.2.X09 covers the rest.

I.3.X04How many units die at initialisationcounterexample▲▲△

At initialisation with symmetric weights and zero bias, roughly half of all ReLU pre-activations are negative. Explain why that is not the dying-unit problem, then construct an initialisation under which it becomes one, and give the fraction of units that die.

Hint

A unit is dead only if it is negative for every input, not for half of them.

Solution

Why half-negative is fine. With w\vec{w} drawn from a symmetric distribution and b=0b = 0, the pre-activation z=w,xz = \langle\vec{w},\vec{x}\rangle is symmetric about zero for any fixed x\vec{x}, so P(z<0)=1/2P(z < 0) = 1/2 per example. But the unit is dead only if z<0z<0 for every example. For NN roughly independent examples that probability is about 2N2^{-N} — at N=100N = 100, 103010^{-30}. So at symmetric initialisation essentially no unit is dead; each is merely silent on about half its inputs, which is the sparsity ReLU was chosen for.

The construction that does kill units. Initialise the bias to a large negative constant, b=cb = -c, keeping weights at He scale Var(w)=2/nin\mathrm{Var}(w) = 2/n_{\text{in}}. Then

z=w,xcz = \langle\vec{w},\vec{x}\rangle - c

and if cc exceeds the largest value w,x\langle\vec{w},\vec{x}\rangle attains over the dataset, the unit is dead from step zero.

The fraction. With inputs normalised so w,xN(0,1)\langle\vec{w},\vec{x}\rangle \sim \mathcal{N}(0, 1) approximately, a unit is dead if cc exceeds the maximum of NN standard normal draws. For N=104N = 10^4 that maximum is about 3.93.9. So:

b=cb = -cFraction of units dead
000%\approx 0\%
2-22.3%\approx 2.3\%
3-30.1%\approx 0.1\% … but of examples, not units
4-450%\approx 50\% of units
6-6100%\approx 100\% of units

The middle rows need care, and the care is the point: a bias of 2-2 makes each unit silent on 97.7%97.7\% of examples but dead on none, because some example still exceeds it. A bias of 4-4 exceeds the dataset maximum for about half the units, and those are genuinely dead. The transition is sharp, and it depends on the dataset size through the maximum of NN draws — which grows only like 2logN\sqrt{2\log N}.

The real-world version. Nobody initialises the bias to 4-4. What happens instead is that a large learning rate drives a bias there during training: one step with an unusually large gradient pushes bb far negative, the unit stops firing, its gradient becomes zero, and it is stuck. This is the standard account of why ReLU networks trained at high learning rates can lose a substantial fraction of their units in the first few hundred steps.

What prevents it. Initialising biases to a small positive constant such as 0.010.01 — once common practice — guarantees no unit starts dead. Careful learning rate warmup (I.7) prevents the large early steps. And normalisation (I.8) keeps pre-activations centred, so a bias would have to fight the normaliser to run away.

I.3.X05How many layers tanh buyslimit▲▲△

Repeat the fp16 depth calculation of I.3.B03 for tanh, whose maximum derivative is 11. Find the depth at z=4|z| = 4 and at z=0z = 0, compare with sigmoid, and say whether the improvement is enough.

Hint

tanh(4)=1tanh2(4)\tanh'(4) = 1 - \tanh^2(4), and tanh(4)\tanh(4) is very close to 11.

Solution

At z=0z = 0. tanh(0)=1\tanh'(0) = 1. A product of ones never underflows: there is no depth limit at all in the best case. Sigmoid’s best case was 1212 layers.

At z=4|z| = 4. tanh(4)=0.999329\tanh(4) = 0.999329, so

tanh(4)=1(0.999329)2=10.998659=0.001341\tanh'(4) = 1 - (0.999329)^2 = 1 - 0.998659 = 0.001341

That is thirteen times smaller than σ(4)=0.017663\sigma'(4) = 0.017663. Solving gL<224g^{L} < 2^{-24}:

L>16.6355ln0.001341=16.63556.6146=2.51L=3L > \frac{-16.6355}{\ln 0.001341} = \frac{-16.6355}{-6.6146} = 2.51 \quad\Rightarrow\quad L = 3

against sigmoid’s 55.

The comparison, which is the opposite of what the headline says.

| | z=0z = 0 | z=4|z| = 4 | |---|---|---| | sigmoid | 12 layers | 5 layers | | tanh | unlimited | 3 layers |

Tanh is better at the origin and worse in saturation. Its derivative starts four times higher and falls off faster, because tanh\tanh approaches its asymptote more quickly than σ\sigma does. The often-repeated “tanh is better than sigmoid” is true only where the pre-activations are small.

Is it enough? No, and the table says why: three layers at z=4|z|=4. Whether tanh helps depends entirely on keeping z|z| small — which is not a property of the activation but of the initialisation and the normalisation. That is the real lesson, and it reframes both chapters that follow:

Chapter I.8’s normalisation keeps zz near zero, moving every unit toward the favourable column. With normalisation, tanh’s unlimited best case becomes reachable; without it, tanh is worse than sigmoid.

ReLU sidesteps the question. Its derivative is exactly 11 for every active unit regardless of magnitude, so there is no favourable region to stay in. That is a stronger property than a higher maximum, and it is why ReLU rather than tanh was the change that made depth practical.

A caution. All of this assumes a uniform derivative per layer, which I.3.B03’s Where this breaks already flagged as optimistic. The real product is dominated by its smallest factor, so a single saturated layer costs more than the average suggests — and tanh, saturating harder, produces smaller minima.

I.3.X07Why the theorem says non-polynomialcounterexample▲▲△

The second assumption of this chapter is that the activation is non-polynomial — the exact hypothesis of I.3.T2. Show that a polynomial activation fails universal approximation, by identifying precisely what such a network can compute. Then state the smallest change that restores universality.

Hint

What is a polynomial of a polynomial?

Solution

The claim. Let φ\varphi be a polynomial of degree kk. Then a network of depth LL with activation φ\varphi computes a polynomial of degree at most kLk^{L} in its inputs — and only polynomials of that bounded degree.

Proof. Induct on depth. A linear layer applied to a polynomial of degree mm gives a polynomial of degree mm, since it is a linear combination. Applying φ\varphi, of degree kk, to a polynomial of degree mm gives degree kmkm. Starting from degree 11 at the input, after LL layers the degree is at most kLk^{L}. \blacksquare

Why that defeats universality. The set of polynomials of degree kL\le k^{L} is a finite-dimensional vector space — for dd inputs its dimension is (kL+dd)\binom{k^L + d}{d}, a finite number. But the continuous functions on a compact set form an infinite-dimensional space. A finite-dimensional subspace is closed and nowhere dense in it, so there are continuous functions at a bounded positive distance from everything the network can compute, no matter how many units it has.

Concretely. With φ(z)=z2\varphi(z) = z^2 and L=2L = 2, the network computes only polynomials of degree 4\le 4. Ask it to approximate sin(10x)\sin(10x) on [0,2π][0, 2\pi] to within 0.10.1: impossible, because the best degree-4 polynomial approximation to sin(10x)\sin(10x) on that interval has error close to 11. Adding a million hidden units does not help — width increases the number of degree-4 polynomials available, not the degree.

Contrast with a non-polynomial. ReLU is piecewise linear, and by I.3.X03 a ReLU network is piecewise linear with a number of pieces growing exponentially in depth. Piecewise-linear functions are dense in the continuous functions on a compact set, so the obstruction disappears. Sigmoid, tanh and GELU are all non-polynomial too — indeed all three are transcendental.

The smallest change that restores universality. Add a single non-polynomial element. Even one layer of ReLU somewhere in an otherwise-polynomial network breaks the degree argument, because the induction requires every layer to be polynomial. This is the same structural point as I.3.B05’s Where this breaks: these collapse theorems are chains, and one non-conforming link is enough.

Why the hypothesis is stated the way it is. Hornik’s 1991 result is often quoted as needing a “sigmoidal” activation, which is what Cybenko proved in 1989. The sharper statement — non-polynomial is necessary and sufficient — is Leshno, Lin, Pinkus and Schocken (1993). It is worth knowing the sharp version, because it explains why ReLU works despite being neither bounded nor sigmoidal.

I.3.X08The diagonal Jacobian, and what breaks without itgradient▲▲△

The first assumption of this chapter is elementwiseness. Compute the Jacobian of sigmoid applied to z=(0.5,1,2)\vec{z} = (0.5, -1, 2), then the Jacobian of softmax on the same vector. Count the non-zero off-diagonal entries in each, and state the cost consequence.

Hint

For softmax, use ai/zj=ai(δijaj)\partial a_i/\partial z_j = a_i(\delta_{ij} - a_j) from the Apparatus.

Solution

Sigmoid. σ(0.5)=0.6225\sigma(0.5) = 0.6225, σ(1)=0.2689\sigma(-1) = 0.2689, σ(2)=0.8808\sigma(2) = 0.8808, so the diagonal entries are σ(1σ)\sigma(1-\sigma):

Jσ=[0.23500000.19660000.1050]\mat{J}_\sigma = \begin{bmatrix} 0.2350 & 0 & 0\\ 0 & 0.1966 & 0\\ 0 & 0 & 0.1050 \end{bmatrix}

Off-diagonal non-zeros: 0, and not approximately — exactly, because aia_i does not contain zjz_j at all.

Softmax. With a=softmax(z)=(0.2312,0.0385,0.7303)\vec{a} = \mathrm{softmax}(\vec{z}) = (0.2312, 0.0385, 0.7303) and ai/zj=ai(δijaj)\partial a_i/\partial z_j = a_i(\delta_{ij} - a_j) (The softmax Jacobian 0.MC.06):

Jsoft=[+0.14460.00690.13770.0069+0.03760.03070.13770.0307+0.1684]\mat{J}_{\text{soft}} = \begin{bmatrix} +0.1446 & -0.0069 & -0.1377\\ -0.0069 & +0.0376 & -0.0307\\ -0.1377 & -0.0307 & +0.1684 \end{bmatrix}

Off-diagonal non-zeros: 6 — every one of them.

The row sums are zero. 0.14460.00690.1377=0.00000.1446 - 0.0069 - 0.1377 = 0.0000, and likewise for the other rows. That is the shift-invariance of softmax: adding a constant to every logit changes nothing, so the derivative in that direction must vanish.

The cost consequence. For a length-nn vector:

storagebackward pass
elementwisennnn multiplies
softmaxn2n^2n2n^2 multiply–adds

At n=4096n = 4096 that is 40964096 against 16.816.8 million — a factor of nn. This is why activations are elementwise. It is not an aesthetic preference: a non-diagonal activation at every layer would make the backward pass cost as much as the forward matrix multiplies, doubling training cost for no representational gain.

And why softmax is worth its cost where it is used. It appears once per model at the output, or once per attention head — never at every hidden layer. In attention (II.3) the n2n^2 Jacobian is unavoidable and is exactly the T×TT \times T score matrix already being formed, so the cost is shared rather than added. Placement is the whole difference.

What breaks if the assumption is dropped. Everything in I.3.B03’s depth analysis assumed a scalar derivative per layer. With a dense Jacobian the product over layers is a product of matrices, whose growth is governed by singular values rather than by a single number (Rank, eigenvalues and the singular value decomposition 0.LA.04), and vanishing becomes a statement about spectral radius — precisely the analysis Chapter I.9 has to do for recurrent networks, where the Jacobian genuinely is dense.

I.3.X09SiLU, and a derivative that exceeds onesymbolic▲▲△

SiLU (also called Swish) is φ(z)=zσ(z)\varphi(z) = z\,\sigma(z). Derive its derivative in terms of σ\sigma alone, show it can exceed 11, find its minimum, and compare with GELU’s 0.1289-0.1289.

Hint

Product rule, then use σ=σ(1σ)\sigma' = \sigma(1-\sigma) to eliminate the derivative.

Solution

The derivative. By the product rule and then I.3.1:

φ(z)=σ(z)+zσ(z)=σ(z)+zσ(z)(1σ(z))\varphi'(z) = \sigma(z) + z\,\sigma'(z) = \sigma(z) + z\,\sigma(z)\big(1-\sigma(z)\big)

which can be tidied to

φ(z)=σ(z)(1+z(1σ(z)))\varphi'(z) = \sigma(z)\Big(1 + z\big(1 - \sigma(z)\big)\Big)

Written this way it costs one sigmoid evaluation and three arithmetic operations, and needs nothing but the value already computed in the forward pass — the same economy I.3.B02 noted for sigmoid itself.

It exceeds 1. At z=1z = 1: σ(1)=0.7311\sigma(1) = 0.7311, so

φ(1)=0.7311(1+1(0.2689))=0.7311×1.2689=0.9276\varphi'(1) = 0.7311\big(1 + 1(0.2689)\big) = 0.7311 \times 1.2689 = 0.9276

not yet. Try z=2z = 2: σ(2)=0.8808\sigma(2) = 0.8808,

φ(2)=0.8808(1+2(0.1192))=0.8808×1.2384=1.0908\varphi'(2) = 0.8808\big(1 + 2(0.1192)\big) = 0.8808 \times 1.2384 = 1.0908

Greater than 1. Scanning, the maximum is about 1.09981.0998 near z=2.4z = 2.4.

Why that is notable. Every derivative met so far is at most 11: sigmoid 1/4\le 1/4, tanh 1\le 1, ReLU 1\le 1. SiLU can amplify a gradient. Over LL layers a factor above 11 compounds upward rather than downward — the mirror image of I.3.B03’s problem, and one reason SiLU networks can be sensitive to learning rate in a way ReLU networks are not.

The minimum. Setting φ=0\varphi'' = 0 numerically gives z1.2785z \approx -1.2785, where

φ(1.2785)=(1.2785)σ(1.2785)=(1.2785)(0.2178)=0.2785\varphi(-1.2785) = (-1.2785)\sigma(-1.2785) = (-1.2785)(0.2178) = -0.2785

and the derivative there is about 0.0998-0.0998.

Comparison with GELU.

most negative outputmost negative derivativeat zz
GELU0.1700-0.17000.1289-0.12892=1.4142-\sqrt2 = -1.4142
SiLU0.2785-0.27850.0998-0.09981.2785\approx -1.2785

SiLU has the deeper output dip; GELU has the deeper derivative dip. They are different functions with the same qualitative shape — smooth, non-monotonic, ReLU-like in the tails — and the practical difference between them is small enough that the choice is usually made by what a codebase already uses.

The honest summary. Both were found by search rather than derived from a principle. SiLU came out of an automated activation search; GELU from a stochastic-regularisation argument that the final formula does not really depend on. The literature’s post-hoc explanations for why either works should be read with that history in mind.

I.3.X10Why there is no best activationproof▲▲▲

The chapter’s open exercise. Given that universal approximation holds for every non-polynomial activation (I.3.T2), argue precisely what is left for the choice of activation to decide — and what it cannot decide. Support the argument with at least two quantities computed in this chapter.

Hint

If every non-polynomial activation reaches the same set of functions, the difference cannot be about which functions are reachable.

Solution

The starting point. I.3.T2 says any non-polynomial activation gives universal approximation. ReLU, tanh, GELU and SiLU are all non-polynomial. So the hypothesis classes are the same in the sense that matters to the theorem: each can approximate any continuous function on a compact set.

Therefore the choice of activation cannot be a choice about what is representable. Whatever it decides, it decides elsewhere.

What it does decide — four things, each with a number from this chapter.

How much gradient survives depth. I.3.B03: a sigmoid stack underflows fp16 at depth 55 for z=4|z| = 4; I.3.X05: a tanh stack at depth 33; ReLU never, for active units. This is not a representational difference — all three classes are universal — it is a difference in whether gradient descent can find the representation. The activation decides trainability, not expressibility.

How many units are permanently lost. I.3.B04: a ReLU unit driven to z8z \le -8 is dead forever, and no optimiser recovers it. LeakyReLU converts that to 90,00090{,}000 steps of recovery. GELU makes it 102310^{-23} of a gradient, which is zero in practice. Different activations lose different amounts of the network to this failure.

What the function looks like between the training points. All four are universal, but ReLU interpolates piecewise-linearly (I.3.X03) while GELU interpolates smoothly. On the training set they can agree exactly; off it they differ, and which behaviour is preferable is a property of the problem, not of the activation.

Almost nothing about cost. I.3.B07: the most expensive activation is 0.2%0.2\% of the matmul beside it. Any argument for one activation over another on compute grounds is, at transformer scale, quantitatively wrong.

What it cannot decide.

Whether the target is representable. Settled by I.3.T2 for all of them.

Whether the model generalises. I.1.T1 and I.1.T2 locate that in the hypothesis class’s size and the data, not in the shape of φ\varphi.

Whether training converges to a good optimum. That is the optimiser, the initialisation and the loss surface, and Chapter I.7’s business.

The strongest claim available.

The activation is a choice about the conditioning of the optimisation problem, not about the set of functions being optimised over. It changes how easily gradient descent moves through the parameter space and how much of the network stays usable, and it changes almost nothing else.

A test of that claim. If it is right, then any intervention that improves conditioning by another route should shrink the differences between activations — and it does. With batch normalisation keeping pre-activations near zero (I.8), the gap between sigmoid and ReLU narrows sharply, because normalisation puts every unit in the favourable region that I.3.X05 identified. With residual connections (II.6) providing a derivative-11 path, the gap narrows again. The activation matters most exactly where nothing else is managing the gradient, which is what a conditioning story predicts and an expressiveness story does not.

The honest remainder. None of this explains why GELU consistently beats ReLU by a small margin in transformers. The conditioning argument predicts they should be nearly equivalent once normalisation and residuals are present, and they are nearly equivalent — but not exactly, and the residual difference has no accepted account. That is a real open question, and the correct response to it is to report the gap rather than to invent a mechanism for it.