3.1.5Neural Network Fundamentals

ReLU and variants (Leaky ReLU, ELU, GELU)

1,803 words8 min readdifficulty · medium

Activation functions inject non-linearity into a network. Without them, stacked linear layers collapse into a single linear map. This note derives the ReLU family from first principles and shows why each variant exists.

Why do we need non-linearity at all?

So an activation must be non-linear, ideally cheap, and must not destroy gradients during backprop.


ReLU — the Rectified Linear Unit

WHY it works so well. Its derivative is: ddxReLU(x)={1x>00x<0\frac{d}{dx}\text{ReLU}(x) = \begin{cases} 1 & x>0 \\ 0 & x<0 \end{cases}

The gradient in the active region is exactly 11. Compare with sigmoid, whose derivative σ(1σ)\sigma(1-\sigma) peaks at 0.250.25 and shrinks toward 00 for large x|x|. Multiplying many such small numbers through a deep network causes the vanishing gradient problem. ReLU's flat gradient of 11 keeps signals alive through many layers.

HOW it helps computation. max(0,x)\max(0,x) is a single comparison — no exp\exp, no division. Cheap forward and backward passes.

The Dying ReLU problem


Leaky ReLU — give the negatives a small slope

WHY. By letting a tiny signal αx\alpha x leak through for x<0x<0, the derivative is α\alpha (not 00) on the negative side: ddxLReLU(x)={1x>0αx<0\frac{d}{dx}\text{LReLU}(x)=\begin{cases}1 & x>0\\ \alpha & x<0\end{cases} A dead neuron can now recover because a non-zero gradient still flows. (PReLU makes α\alpha a learned parameter.)


ELU — Exponential Linear Unit

WHY the exponential. For x0x\le 0, α(ex1)\alpha(e^x-1) smoothly saturates to α-\alpha as xx\to-\infty. Two benefits:

  • Smooth: differentiable-ish and its output can go negative, pushing the mean activation toward 00 (self-normalizing effect → faster training).
  • Bounded negatives: unlike Leaky ReLU it doesn't blow up for very negative inputs; it saturates, giving robustness to noise.

Derivative. For x0x\le0, ddxα(ex1)=αex\frac{d}{dx}\alpha(e^x-1)=\alpha e^x, which continuously matches slope 11 at x=0x=0 when α=1\alpha=1 (unlike ReLU's abrupt corner).


GELU — Gaussian Error Linear Unit

WHY multiply by Φ(x)\Phi(x)? Think of a stochastic gate: keep the input xx with probability Φ(x)\Phi(x) (how likely a standard Gaussian is below xx), drop it otherwise. The expected output is xΦ(x)x\cdot\Phi(x). So GELU is a smooth, probabilistic version of ReLU's hard 0/10/1 gate.

  • Small xx: Φ(x)0.5\Phi(x)\approx 0.5, so output x/2\approx x/2 — gentle.
  • Large positive xx: Φ1\Phi\to 1, output x\to x (like ReLU).
  • Large negative xx: Φ0\Phi\to 0, output 0\to 0 (like ReLU) but smoothly, even dipping slightly negative near 00.

Practical approximation (used in BERT/GPT): GELU(x)0.5x(1+tanh[2π(x+0.044715x3)])\text{GELU}(x)\approx 0.5x\Big(1+\tanh\big[\sqrt{\tfrac{2}{\pi}}(x+0.044715x^3)\big]\Big) because Φ\Phi has no elementary form.


Worked examples



Flashcards

Why can't a network use only linear layers?
They collapse into one linear map W2W1x=WxW_2W_1x=Wx; no added expressive power, so non-linear activations are required.
Define ReLU and its derivative.
ReLU(x)=max(0,x)\text{ReLU}(x)=\max(0,x); derivative is 11 for x>0x>0, 00 for x<0x<0.
Why does ReLU avoid vanishing gradients better than sigmoid?
Its active-region gradient is exactly 11, whereas sigmoid's derivative maxes at 0.250.25 and shrinks, so products decay through depth.
What is the Dying ReLU problem?
Neurons stuck with negative pre-activation output 00 and have 00 gradient, so they never update — permanently dead.
How does Leaky ReLU fix dying neurons?
It uses slope α0.01\alpha\approx0.01 for x<0x<0, giving non-zero gradient α\alpha so dead neurons can recover.
Write ELU and why it uses an exponential.
ELU(x)=x\text{ELU}(x)=x for x>0x>0, α(ex1)\alpha(e^x-1) for x0x\le0; it saturates smoothly to α-\alpha, allows negative mean-centering, and is bounded for very negative inputs.
Define GELU and give its probabilistic interpretation.
GELU(x)=xΦ(x)\text{GELU}(x)=x\,\Phi(x); keep input with probability Φ(x)\Phi(x) (chance a standard Gaussian is below xx), a smooth stochastic gate.
GELU value behaviour for large ±x\pm x?
Large positive x\to x (like ReLU), large negative 0\to 0, but smooth near origin with a slight negative dip.
Why keep Leaky ReLU's α\alpha small?
If α1\alpha\to1 the function becomes linear (y=xy=x), destroying non-linearity.
Compute ELU at x=2x=-2, α=1\alpha=1.
e210.865e^{-2}-1\approx-0.865.

Recall Feynman: explain to a 12-year-old

Imagine a water gate. ReLU is a one-way gate: if the water pushes forward (positive) it flows freely; if it pushes backward (negative) the gate slams shut and nothing moves — the pipe there is "dead" forever. Leaky ReLU leaves a tiny crack so a trickle always gets through, keeping the pipe alive. ELU is a smooth curved gate that lets a little water flow backward but never floods. GELU is a smart gate that opens more the harder the water pushes, deciding gently instead of all-or-nothing. All of them let the network learn bendy shapes instead of only straight lines.

Connections

Concept Map

motivates

simplest choice

derivative 1 in active region

single comparison

piecewise linear kink

zero gradient for negatives

fixed by leak alpha x

learned alpha

fixed by smooth exp

negative outputs

self-normalizing

smooth Gaussian gating

Stacked linear layers collapse

Need non-linearity

ReLU max 0 x

Avoids vanishing gradient

Cheap compute

Universal approximation

Dying ReLU problem

Leaky ReLU

PReLU

ELU

Mean activation near 0

Faster training

GELU

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, activation function ka kaam hai network me non-linearity daalna. Agar sirf linear layers ho, to W2W1x=WxW_2 W_1 x = Wx ban jaata hai — matlab chahe 100 layers lagao, ek hi matrix jaisa. Isliye beech me ek "mod" (kink) chahiye. ReLU yahi karta hai: positive input ko waise ka waisa pass karo, negative ko zero. Iska gradient positive side pe exactly 11 hota hai, isliye deep network me signal marta nahi (sigmoid me gradient 0.250.25 tak hi jaata hai, isliye vanish ho jaata hai).

Problem ye hai ki agar kisi neuron ka input hamesha negative rehta hai, to ReLU output 00 aur gradient bhi 00 — neuron mar jaata hai (dying ReLU), kabhi update nahi hota. Isko fix karne ke liye Leaky ReLU aaya: negative side pe thoda sa slope α0.01\alpha \approx 0.01 rakh do, taaki gradient zero na ho aur neuron zinda rahe. Par α\alpha chhota hi rakhna, warna function pura linear ban jayega.

ELU ek step aage hai — negative side pe α(ex1)\alpha(e^x - 1) use karta hai jo smoothly α-\alpha tak saturate hota hai. Isse output negative ja sakta hai, mean 00 ke paas aata hai (training fast hoti hai) aur bahut negative input pe bhi blow-up nahi hota. GELU sabse smart hai: xΦ(x)x \cdot \Phi(x), yaani input ko probability Φ(x)\Phi(x) ke saath "gate" karta hai — jitna zyada positive push, utna zyada gate khulta hai. BERT aur GPT jaise transformers me GELU hi default hai.

Yaad rakhne ka trick: ReLU → Leaky → ELU → GELU, jitna aage jaoge utna smooth aur sophisticated. Exam aur real projects dono me ye samajhna zaroori hai ki kaunsa kab use karna hai.

Go deeper — visual, from zero

Test yourself — Neural Network Fundamentals

Connections