Md. Asif Uddin
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.

Draws on