Simple Algorithm
Deep Learning · Emergence · 26 min

Activation Functions in Depth

ReLU, GELU, SiLU and gated units: saturation, dead neurons and what smoothness buys.

Every non-polynomial activation makes a network a universal approximator, so expressivity cannot be why practice moved from sigmoids to ReLU, then to GELU and SwiGLU. The deciding properties are quieter: how an activation carries means, variances and gradients through depth, how smooth it is, how it gates, and what it costs.

What you’ll learn

  • Explain why universality (Leshno et al., 1993) does not decide the choice of activation, and list the properties that do.
  • Compute the Gaussian moments E[ϕ(z)2]\E[\phi(z)^2] and E[ϕ′(z)2]\E[\phi'(z)^2] that set forward and backward signal propagation at initialization, and derive why ReLU with σw2=2\sigma_w^2=2 preserves both at every scale.
  • Derive SELU's constants from its fixed-point conditions, check the fixed point's stability, and state the assumptions of the self-normalizing theorem.
  • Interpret GELU and SiLU as smooth self-gated ReLUs, and derive the hidden size 83d\tfrac83d that gives a SwiGLU feed-forward layer the parameters of a standard one.

Universality is not the criterion

Polynomials fail because sums of polynomials of degree kk in w⊤x+b\mathbf w^\top\mathbf x+b stay polynomials of degree kk; every activation in use is non-polynomial, so all are universal (the proofs are in What Networks Can Represent). The choice is made on other grounds, and the useful vocabulary is this:

PropertyWhy it mattersWho has it
Non-saturating on one sideϕ′\phi' stays away from 00 for large inputs, so gradients survive depthReLU family, ELU, GELU, SiLU (right side); not sigmoid, tanh
Zero mean under z∼N(0,1)z\sim\mathcal N(0,1)no bias shift handed to the next layer; avoids the sign-locked updates of positive inputstanh, SELU (exactly); ELU comes closer than ReLU (0.160.16 against 0.400.40)
Smooth (C1C^1 or better)well-defined Hessians; landscapes without kinks; better behaved second-order methodstanh, sigmoid, softplus, GELU, SiLU; not ReLU (kink at 00)
Monotoneeach unit's output orders its inputs; simple to analyzeall but GELU and SiLU, which dip below 00
Exact zerossparse activations that hardware can skipReLU only
Cheapa comparison is cheaper than exp⁡\exp or erf⁡\operatorname{erf}; matters at inference scaleReLU, leaky ReLU
No activation has every property: ReLU is cheap, sparse and non-saturating but neither smooth nor zero-mean; tanh is smooth and zero-mean but saturates.

Two more differences are sharp. Lipschitz constants (sup⁡∣ϕ′∣\sup\abs{\phi'}) bound how much a layer can amplify perturbations: 11 for ReLU, leaky ReLU, ELU, softplus and tanh, 14\tfrac14 for the sigmoid, about 1.1291.129 for GELU (at z=2z=\sqrt2) and 1.1001.100 for SiLU. And networks can die as a whole: a deep, narrow ReLU network with weights and biases drawn symmetrically around zero collapses to a constant function with probability tending to one as the depth grows (Lu, Shin, Su & Karniadakis, 2020), a failure mode of width and depth, not of a single unit.

Gaussian moments decide signal propagation

At initialization a wide layer with weights Wij∼N(0,σw2/n)W_{ij}\sim\mathcal N(0,\sigma_w^2/n) and no biases turns each pre-activation into a sum of many independent terms, approximately Gaussian: zi(ℓ)≈N(0,qℓ)z^{(\ell)}_i\approx\mathcal N(0,q_\ell). Two expectations of ϕ\phi under that Gaussian then control everything.

DerivationForward variance and backward gradient factor
  1. qℓ=σw2  Ez∼N(0, qℓ−1)[ϕ(z)2]q_\ell=\sigma_w^2\;\E_{z\sim\mathcal N(0,\,q_{\ell-1})}\big[\phi(z)^2\big]

    Forward. Var⁡(zi(ℓ))=∑jVar⁡(Wij) E[hj2]=σw2 E[ϕ(z)2]\Var(z^{(\ell)}_i)=\sum_j\Var(W_{ij})\,\E[h_j^2]=\sigma_w^2\,\E[\phi(z)^2] with z∼N(0,qℓ−1)z\sim\mathcal N(0,q_{\ell-1}):

  2. χ=σw2  Ez∼N(0, q)[ϕ′(z)2]\chi=\sigma_w^2\;\E_{z\sim\mathcal N(0,\,q)}\big[\phi'(z)^2\big]

    Backward. The gradient obeys δ(ℓ−1)=(W(ℓ)⊤δ(ℓ))⊙ϕ′(z(ℓ−1))\boldsymbol\delta^{(\ell-1)}=\big(W^{(\ell)\top}\boldsymbol\delta^{(\ell)}\big)\odot\phi'(\mathbf z^{(\ell-1)}). Treating the weights in the backward pass as independent of the forward pass (the standard mean-field approximation), the mean squared gradient is multiplied per layer by

  3. qℓ=σw22 qℓ−1,χ=σw22⟹σw2=2q_\ell=\tfrac{\sigma_w^2}{2}\,q_{\ell-1},\qquad \chi=\tfrac{\sigma_w^2}{2}\qquad\Longrightarrow\qquad \sigma_w^2=2

    ReLU is scale-free. For z∼N(0,q)z\sim\mathcal N(0,q), symmetry gives E[ReLU⁡(z)2]=q/2\E[\operatorname{ReLU}(z)^2]=q/2 and E[ReLU⁡′(z)2]=P(z>0)=12\E[\operatorname{ReLU}'(z)^2]=P(z>0)=\tfrac12 for every qq, so both factors equal σw2/2\sigma_w^2/2. The choice σw2=2\sigma_w^2=2, He initialization (He et al., 2015), preserves forward variance and gradient size at every scale and depth. This is positive homogeneity at work: ϕ(cz)=c ϕ(z)\phi(cz)=c\,\phi(z) for c>0c>0.

  4. Everything else depends on scale. Near 00, tanh⁡z≈z\tanh z\approx z and GELU⁡(z)≈z/2\operatorname{GELU}(z)\approx z/2, while large inputs saturate tanh. So the right gain depends on qq: for tanh at σw2=1\sigma_w^2=1 the variance drifts slowly toward the fixed point q∗=0q^*=0; for GELU at σw2=2\sigma_w^2=2 the variance of a small signal halves at every layer. These maps, and the correlation maps behind the "edge of chaos", are developed in Initialization & Vanishing Gradients.

ϕ\phiE[ϕ(z)]\E[\phi(z)]E[ϕ(z)2]\E[\phi(z)^2]E[ϕ′(z)2]\E[\phi'(z)^2]Lipschitz
ReLU0.3990.3990.5000.5000.5000.50011
leaky ReLU (a=0.01a=0.01)0.3950.3950.5000.5000.5000.50011
ELU (α=1\alpha=1)0.1610.1610.6450.6450.6680.66811
SELU00111.0721.072λα≈1.758\lambda\alpha\approx1.758
GELU0.2820.2820.4250.4250.4560.4561.1291.129
SiLU0.2070.2070.3560.3560.3790.3791.1001.100
softplus0.8060.8060.9210.9210.2930.29311
tanh000.3940.3940.4640.46411
sigmoid0.5000.5000.2930.2930.0450.04514\tfrac14
Moments for z∼N(0,1)z\sim\mathcal N(0,1), computed by numerical integration. ReLU's 12\tfrac12 and 12\tfrac12 are exact; E[ReLU⁡(z)]=1/2π\E[\operatorname{ReLU}(z)]=1/\sqrt{2\pi} is the mean shift each ReLU layer hands to the next. The sigmoid's 0.0450.045 means a backward factor of 0.045 σw20.045\,\sigma_w^2 per layer.

Self-normalizing networks

Can an activation make the moments converge to a target instead of merely preserving them? Klambauer, Unterthiner, Mayr and Hochreiter (2017) designed one, the scaled exponential linear unit:

SELU⁡(z)=λ{z,z>0α (ez−1),z≤0with (λ,α) chosen so that z∼N(0,1)⇒E[SELU⁡(z)]=0, E[SELU⁡(z)2]=1\operatorname{SELU}(z)=\lambda\begin{cases}z,&z>0\\ \alpha\,(e^z-1),&z\le0\end{cases}\qquad \text{with }(\lambda,\alpha)\text{ chosen so that } z\sim\mathcal N(0,1)\Rightarrow \E[\operatorname{SELU}(z)]=0,\ \E[\operatorname{SELU}(z)^2]=1
Two conditions, two unknowns. Then if a unit's weights satisfy ∑iwi=0\sum_iw_i=0 and ∑iwi2=1\sum_iw_i^2=1 (true in expectation for Wij∼N(0,1/n)W_{ij}\sim\mathcal N(0,1/n)), inputs with mean 00 and variance 11 produce outputs with mean 00 and variance 11: (0,1)(0,1) is a fixed point of the map from one layer's moments to the next.
DerivationSolving for α and λ
  1. 12π+α(e Φ(−1)−12)=0⟹α=1/2π12−e Φ(−1)\frac{1}{\sqrt{2\pi}}+\alpha\Big(\sqrt e\,\Phi(-1)-\tfrac12\Big)=0\quad\Longrightarrow\quad \alpha=\frac{1/\sqrt{2\pi}}{\tfrac12-\sqrt e\,\Phi(-1)}

    Use the Gaussian integrals E[z 1z>0]=1/2π\E[z\,\mathbf 1_{z>0}]=1/\sqrt{2\pi} and E[etz1z<0]=et2/2 Φ(−t)\E[e^{tz}\mathbf 1_{z<0}]=e^{t^2/2}\,\Phi(-t). The mean condition does not involve λ\lambda:

  2. λ2[12+α2(e2Φ(−2)−2e Φ(−1)+12)]=1⟹λ≈1.0507,α≈1.6733\lambda^2\Big[\tfrac12+\alpha^2\Big(e^2\Phi(-2)-2\sqrt e\,\Phi(-1)+\tfrac12\Big)\Big]=1\quad\Longrightarrow\quad \lambda\approx1.0507,\quad \alpha\approx1.6733

    The second moment fixes λ\lambda, with E[(ez−1)21z<0]=e2Φ(−2)−2e Φ(−1)+12\E[(e^z-1)^2\mathbf 1_{z<0}]=e^2\Phi(-2)-2\sqrt e\,\Phi(-1)+\tfrac12:

  3. ν~(ν)=Var⁡[SELU⁡(z)], z∼N(0,ν)ν~(1)=1,ν~′(1)≈0.78<1\tilde\nu(\nu)=\Var\big[\operatorname{SELU}(z)\big],\ z\sim\mathcal N(0,\nu)\qquad \tilde\nu(1)=1,\quad \tilde\nu'(1)\approx0.78<1

    Stability. With normalized weights the next layer's pre-activations are N(0,ν)\mathcal N(0,\nu), so the moments evolve as ν↦ν~(ν)\nu\mapsto\tilde\nu(\nu). Numerically, ν~(0.8)=0.839\tilde\nu(0.8)=0.839, ν~(1.5)=1.372\tilde\nu(1.5)=1.372 and dν~/dν≈0.78d\tilde\nu/d\nu\approx0.78 at ν=1\nu=1: deviations shrink by about a fifth per layer. Klambauer et al. report a Jacobian spectral norm of about 0.7880.788 at (0,1)(0,1).

Predict before you look

Use 1/2π≈0.39891/\sqrt{2\pi}\approx0.3989, e≈1.6487\sqrt e\approx1.6487 and Φ(−1)≈0.1587\Phi(-1)\approx0.1587 to evaluate SELU's α=1/2π12−e Φ(−1)\alpha=\dfrac{1/\sqrt{2\pi}}{\tfrac12-\sqrt e\,\Phi(-1)}.

Show the answer

1.67

e Φ(−1)≈0.2616\sqrt e\,\Phi(-1)\approx0.2616, so α≈0.3989/0.2384≈1.673\alpha\approx0.3989/0.2384\approx1.673. The constant is not tuned by experiment: it is forced by asking for zero mean output from a standard normal input.

Read the assumptions before relying on it: independent inputs and wide layers (the Gaussian approximation), weights that stay close to normalized during training, and plain fully connected layers; ordinary dropout breaks the moments, so the paper introduces "alpha dropout".

Self-normalization also concerns activations, not gradients. Klambauer et al. argue that bounding the variance rules out exploding and vanishing gradients, but by the table above the backward factor at the fixed point is E[SELU⁡′(z)2]≈1.072\E[\operatorname{SELU}'(z)^2]\approx1.072: in the mean-field picture the mean squared gradient still doubles about every ten layers going backward (the norm grows about 1.4×1.4\times), and Philipp, Song and Carbonell (2017) measured exploding gradients in deep SELU networks. SELU did well on fully connected benchmarks but did not displace normalization layers, which enforce the moments explicitly at any depth and width (BatchNorm, LayerNorm & RMSNorm).

Smooth gates: GELU, SiLU and GLUs

GELU as an expected gate. Hendrycks and Gimpel (2016) derive GELU from a stochastic gate: multiply the input by m∼Bernoulli(Φ(z))m\sim\text{Bernoulli}(\Phi(z)), so that larger inputs are more likely to pass. The expected output is E[mz]=z Φ(z)\E[mz]=z\,\Phi(z). ReLU is the deterministic gate 1z>0\mathbf 1_{z>0}; GELU replaces it with a smooth, input-dependent probability. Implementations often use 0.5z(1+tanh⁡(2/π(z+0.044715z3)))0.5z\big(1+\tanh(\sqrt{2/\pi}(z+0.044715z^3))\big), which differs from zΦ(z)z\Phi(z) by less than 5×10−45\times10^{-4}.

SiLU and Swish. The SiLU z σ(z)z\,\sigma(z) gates the input with its own sigmoid (Elfwing, Uchibe & Doya, 2018). Ramachandran, Zoph and Le (2017) found Swish⁡β(z)=z σ(βz)\operatorname{Swish}_\beta(z)=z\,\sigma(\beta z) by automated search: β=1\beta=1 is SiLU, β→∞\beta\to\infty gives ReLU, β=0\beta=0 gives z/2z/2, and β≈1.702\beta\approx1.702 approximates GELU to within 0.0210.021. Both are smooth and dip below zero, with a stationary point where the slope is exactly zero.

Gated linear units. Dauphin, Fan, Auli and Grangier (2017) gated one linear map with another: GLU⁡(x)=(xW+b)⊙σ(xV+c)\operatorname{GLU}(\mathbf x)=(\mathbf xW+\mathbf b)\odot\sigma(\mathbf xV+\mathbf c). Each unit multiplies two learned features, a multiplicative interaction that an additive unit cannot form in one layer. Shazeer (2020) swapped the sigmoid for other activations inside transformer feed-forward layers (GLU variants such as GEGLU and SwiGLU), without biases:

DerivationMatching parameters: the 8/3 rule
  1. FFN⁡(x)=ϕ(xW1) W2  (2 d dff),FFN⁡SwiGLU(x)=(SiLU⁡(xW)⊙xV)W2  (3 d dff′)\operatorname{FFN}(\mathbf x)=\phi(\mathbf xW_1)\,W_2\ \ (2\,d\,d_{ff}),\qquad \operatorname{FFN}_{\text{SwiGLU}}(\mathbf x)=\big(\operatorname{SiLU}(\mathbf xW)\odot\mathbf xV\big)W_2\ \ (3\,d\,d'_{ff})

    A standard feed-forward layer has two d×dffd\times d_{ff} matrices; a gated one has three:

  2. 3 d dff′=2 d⋅4d⟹dff′=83 d3\,d\,d'_{ff}=2\,d\cdot4d\quad\Longrightarrow\quad d'_{ff}=\tfrac83\,d

    Equal parameter counts, and therefore equal FLOPs per token, require dff′=23dffd'_{ff}=\tfrac23d_{ff}. With the usual dff=4dd_{ff}=4d:

  3. Shazeer's baseline had d=768d=768 and dff=3072d_{ff}=3072, so the gated variants used dff′=2048d'_{ff}=2048. In that comparison, at equal parameters and compute, GEGLU and SwiGLU reached the lowest pre-training perplexities of all the variants tried, ahead of ReLU, GELU and Swish layers.

Predict before you look

A transformer has width d=1536d=1536 and a standard feed-forward layer with dff=4dd_{ff}=4d. What hidden size gives a SwiGLU layer the same number of parameters?

Show the answer

4096

dff′=83⋅1536=4096d'_{ff}=\tfrac83\cdot1536=4096, against dff=6144d_{ff}=6144 for the ungated layer. In practice the result is rounded to a hardware-friendly multiple.

What is known, and what is open

The evidence for gated layers is empirical but unusually robust. Narang et al. (2021) re-implemented dozens of proposed transformer modifications in one codebase and found that most did not help, while SwiGLU and GeGLU improved pre-training, fine-tuning and supervised training without slowing the model down. By the mid-2020s SwiGLU feed-forward layers were standard in large language models such as PaLM and LLaMA (Anatomy of a Modern LLM). Shazeer's own explanation is often quoted: "we attribute their success, as all else, to divine benevolence."

Key takeaways

  • Any non-polynomial activation is universal (Leshno et al., 1993); activations differ in saturation, mean, smoothness, monotonicity, sparsity, Lipschitz constant and cost.
  • At initialization, σw2E[ϕ(z)2]\sigma_w^2\E[\phi(z)^2] and σw2E[ϕ′(z)2]\sigma_w^2\E[\phi'(z)^2] set the forward variance and the backward gradient factor per layer; ReLU's homogeneity makes both σw2/2\sigma_w^2/2 at every scale, hence σw2=2\sigma_w^2=2.
  • SELU's λ≈1.0507\lambda\approx1.0507 and α≈1.6733\alpha\approx1.6733 solve E[SELU⁡]=0\E[\operatorname{SELU}]=0 and E[SELU⁡2]=1\E[\operatorname{SELU}^2]=1; the fixed point (0,1)(0,1) attracts (slope about 0.780.78) under normalized weights, independent inputs and wide layers, but gradients still grow slowly.
  • GELU (zΦ(z)z\Phi(z)) and SiLU (zσ(z)z\sigma(z)) are smooth self-gated ReLUs; GLU variants multiply two projections, and dff′=83dd'_{ff}=\tfrac83d keeps a SwiGLU layer at the cost of a 4d4d one. Their advantage is well replicated but not explained.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why does σw2=2\sigma_w^2=2 preserve both the forward variance and the backward gradient size of a deep ReLU network at any input scale, while no single gain does this for tanh?

  1. ReLU's derivative is exactly 11 everywhere
  2. ReLU is positively homogeneous, so E[ReLU⁡(z)2]=q/2\E[\operatorname{ReLU}(z)^2]=q/2 and E[ReLU⁡′(z)2]=12\E[\operatorname{ReLU}'(z)^2]=\tfrac12 for every variance qq
  3. ReLU outputs have mean zero
  4. ReLU networks are linear, so variances simply add
Show the answer

B. ReLU is positively homogeneous, so E[ReLU⁡(z)2]=q/2\E[\operatorname{ReLU}(z)^2]=q/2 and E[ReLU⁡′(z)2]=12\E[\operatorname{ReLU}'(z)^2]=\tfrac12 for every variance qq

Homogeneity, ReLU⁡(cz)=cReLU⁡(z)\operatorname{ReLU}(cz)=c\operatorname{ReLU}(z) for c>0c>0, makes both moment ratios independent of qq, so σw2=2\sigma_w^2=2 cancels them exactly. For tanh both ratios depend on qq (near-linear for small inputs, saturating for large), so any fixed gain is right for only one scale.

Question 2

What do SELU's constants λ≈1.0507\lambda\approx1.0507 and α≈1.6733\alpha\approx1.6733 achieve?

  1. They make the gradient factor E[SELU⁡′(z)2]\E[\operatorname{SELU}'(z)^2] exactly 11
  2. They make SELU's derivative continuous at 00
  3. They bound SELU's output to [−1,1][-1,1]
  4. A standard normal input gives an output with mean 00 and variance 11, so (0,1)(0,1) is a fixed point of the layer-to-layer moment map for normalized weights
Show the answer

D. A standard normal input gives an output with mean 00 and variance 11, so (0,1)(0,1) is a fixed point of the layer-to-layer moment map for normalized weights

The two constants solve the two equations E[SELU⁡(z)]=0\E[\operatorname{SELU}(z)]=0 and E[SELU⁡(z)2]=1\E[\operatorname{SELU}(z)^2]=1 for z∼N(0,1)z\sim\mathcal N(0,1). Stability of the fixed point (slope about 0.780.78) is then checked separately.

Question 3

With one hidden layer of arbitrary width, which activation does not give a universal approximator on compact sets?

  1. ϕ(z)=sin⁡z\phi(z)=\sin z
  2. ϕ(z)=z3+z\phi(z)=z^3+z
  3. GELU
  4. leaky ReLU
Show the answer

B. ϕ(z)=z3+z\phi(z)=z^3+z

By Leshno et al. (1993) universality holds exactly for non-polynomial activations. With ϕ(z)=z3+z\phi(z)=z^3+z, every network of one hidden layer is a polynomial of degree at most 33 in x\mathbf x, whatever its width.

Question 4

Why do SwiGLU feed-forward layers typically use a hidden size of about 83d\tfrac83d instead of 4d4d?

  1. Gating halves the effective width, so less is needed
  2. Larger hidden sizes make SiLU saturate
  3. A gated layer has three weight matrices instead of two, and 23⋅4d\tfrac23\cdot4d keeps parameters and FLOPs equal to the ungated 4d4d layer
  4. It was found by a hyperparameter search to maximize accuracy
Show the answer

C. A gated layer has three weight matrices instead of two, and 23⋅4d\tfrac23\cdot4d keeps parameters and FLOPs equal to the ungated 4d4d layer

3 d dff′=2 d⋅4d3\,d\,d'_{ff}=2\,d\cdot4d gives dff′=83dd'_{ff}=\tfrac83d. The rule makes comparisons fair at equal cost; it is a bookkeeping choice, not a tuned optimum.

Further reading

  • M. Leshno, V. Ya. Lin, A. Pinkus & S. Schocken, "Multilayer feedforward networks with a nonpolynomial activation function can approximate any function," Neural Networks 6 (1993).
  • G. Klambauer, T. Unterthiner, A. Mayr & S. Hochreiter, "Self-normalizing neural networks," NeurIPS (2017).
  • D. Hendrycks & K. Gimpel, "Gaussian error linear units (GELUs)," arXiv:1606.08415 (2016).
  • P. Ramachandran, B. Zoph & Q. V. Le, "Searching for activation functions," arXiv:1710.05941 (2017).
  • N. Shazeer, "GLU variants improve transformer," arXiv:2002.05202 (2020).
  • S. Narang et al., "Do transformer modifications transfer across implementations and applications?," EMNLP (2021).