ReLU and variants (Leaky ReLU, ELU, GELU)
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:
The gradient in the active region is exactly . Compare with sigmoid, whose derivative peaks at and shrinks toward for large . Multiplying many such small numbers through a deep network causes the vanishing gradient problem. ReLU's flat gradient of keeps signals alive through many layers.
HOW it helps computation. is a single comparison — no , 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 leak through for , the derivative is (not ) on the negative side: A dead neuron can now recover because a non-zero gradient still flows. (PReLU makes a learned parameter.)
ELU — Exponential Linear Unit
WHY the exponential. For , smoothly saturates to as . Two benefits:
- Smooth: differentiable-ish and its output can go negative, pushing the mean activation toward (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 , , which continuously matches slope at when (unlike ReLU's abrupt corner).
GELU — Gaussian Error Linear Unit
WHY multiply by ? Think of a stochastic gate: keep the input with probability (how likely a standard Gaussian is below ), drop it otherwise. The expected output is . So GELU is a smooth, probabilistic version of ReLU's hard gate.
- Small : , so output — gentle.
- Large positive : , output (like ReLU).
- Large negative : , output (like ReLU) but smoothly, even dipping slightly negative near .
Practical approximation (used in BERT/GPT): because has no elementary form.
Worked examples
Flashcards
Why can't a network use only linear layers?
Define ReLU and its derivative.
Why does ReLU avoid vanishing gradients better than sigmoid?
What is the Dying ReLU problem?
How does Leaky ReLU fix dying neurons?
Write ELU and why it uses an exponential.
Define GELU and give its probabilistic interpretation.
GELU value behaviour for large ?
Why keep Leaky ReLU's small?
Compute ELU at , .
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
- Vanishing and Exploding Gradients — the disease ReLU cures and dying-ReLU reintroduces.
- Sigmoid and Tanh Activations — the saturating functions ReLU replaced.
- Backpropagation — activation derivatives drive gradient flow.
- Weight Initialization (He vs Xavier) — He init is designed specifically for ReLU.
- Transformers and Attention — GELU is the default activation in BERT/GPT.
- Universal Approximation Theorem — why piecewise-linear kinks suffice.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, activation function ka kaam hai network me non-linearity daalna. Agar sirf linear layers ho, to 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 hota hai, isliye deep network me signal marta nahi (sigmoid me gradient 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 aur gradient bhi — neuron mar jaata hai (dying ReLU), kabhi update nahi hota. Isko fix karne ke liye Leaky ReLU aaya: negative side pe thoda sa slope rakh do, taaki gradient zero na ho aur neuron zinda rahe. Par chhota hi rakhna, warna function pura linear ban jayega.
ELU ek step aage hai — negative side pe use karta hai jo smoothly tak saturate hota hai. Isse output negative ja sakta hai, mean ke paas aata hai (training fast hoti hai) aur bahut negative input pe bhi blow-up nahi hota. GELU sabse smart hai: , yaani input ko probability 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.