Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 122/ 160
07. Deep Learning & NLP

Activation Functions (ReLU, Sigmoid, Softmax, GELU)

Choose activations for hidden units and output heads with vanishing-gradient awareness.

BeginnerReLU · Sigmoid/Softmax3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

After a neuron adds up its inputs, the activation decides how loudly it speaks: ReLU stays silent for negatives and repeats positives, sigmoid squeezes everything between 0 and 1, and softmax turns a list of scores into percentages that sum to one.

Think of it like…

A volume knob with a dead zone. ReLU is a gate that passes positive signals through untouched and blocks negative ones; sigmoid is a dimmer that squeezes everything towards fully off or fully on; softmax is a vote count that turns raw scores into shares that add up to 100%.

Where you’ve seen it

A support-ticket classifier has three classes (billing, bug, other) and produces logits (2.0, 1.0, 0.1). Softmax exponentiates them to 7.389, 2.718 and 1.105, which sum to 11.212, giving probabilities 0.659, 0.242 and 0.099. In the hidden layers the team uses ReLU because it is cheap and its gradient is 1 for active units; a deeper experiment with sigmoid hidden layers trains badly because the best-case derivative 0.25 compounded over ten layers is 0.25¹⁰ ≈ 9.5×10⁻⁷, so the first layer receives almost no gradient. The binary version of the task (is this ticket urgent?) uses a single logit and a sigmoid: z = 2 gives 0.881 and z = −2 gives 0.119. A regression head predicting resolution hours uses no activation at all. When they later move to a transformer encoder, the feed-forward blocks use GELU, where GELU(1) = 1 × 0.841 = 0.841 and GELU(−1) = −1 × 0.159 = −0.159: negative inputs are damped smoothly rather than cut to zero. A unit that dies (its inputs are negative for every example) shows up as a hidden activation that is exactly zero across a whole batch, which they monitor.

Words you’ll hear

Saturation
A region where the activation is flat, so its gradient is near zero.
Vanishing gradient
Gradients that shrink layer by layer until early layers stop learning.
Dying ReLU
A unit whose input is always negative, so it outputs and learns nothing.
Logits
The raw, unnormalised scores fed into softmax or sigmoid.

Visual diagram

Loading interactive diagram

Theory & deep dive

Sigmoid, tanh and the vanishing gradient

Sigmoid has the derivative σ′(z) = σ(z)(1 − σ(z)). At z = 0 it is 0.25 (its maximum); at z = 2, σ = 0.881 and σ′ = 0.881 × 0.119 = 0.105; at z = 5, σ = 0.993 and σ′ = 0.0066. Backpropagation multiplies one such factor per layer, so ten sigmoid layers scale the gradient by at most 0.25¹⁰ ≈ 9.5×10⁻⁷, and in practice far less once units saturate. Tanh is zero-centred and has a maximum slope of 1 at zero, which is better, but it also saturates at large |z|. Both are still correct choices at the output (sigmoid for a probability) and inside gates, such as LSTM gates.

Zero-centring matters too: sigmoid outputs are always positive, so every weight in the next layer receives gradients of the same sign for a given example, which makes optimisation zig-zag. Normalisation layers and careful initialisation reduce these problems, but the simplest fix for hidden layers was changing the activation itself.

Gradient shrinkage

Best-case sigmoid derivative 0.25 per layer: after 3 layers 0.0156, after 5 layers 9.8×10⁻⁴, after 10 layers 9.5×10⁻⁷. A ReLU path with active units multiplies by 1 at every layer, so the activation does not shrink the gradient.

Key takeaway: Sigmoid and tanh saturate; use them at outputs and in gates, not as the default for deep hidden stacks.

ReLU, GELU and softmax

ReLU is max(0, z): cheap, sparse (roughly half the units are off for zero-centred inputs) and non-saturating for positive z. Its flaw is that a unit with negative pre-activations for all examples has zero gradient and never recovers; Leaky ReLU (slope 0.01 for z < 0) or a lower learning rate avoids this. GELU multiplies z by the standard normal CDF Φ(z), giving GELU(1) = 0.841, GELU(0) = 0, GELU(2) = 1.954 and a small negative dip such as GELU(−1) = −0.159; transformers use it because the smooth curve works well with large-scale training.

Softmax maps logits to probabilities: p_i = exp(z_i) / Σ exp(z_j). For (2.0, 1.0, 0.1) the exponentials are 7.389, 2.718 and 1.105 (sum 11.212), so the probabilities are 0.659, 0.242 and 0.099. Softmax is invariant to adding a constant to all logits, so implementations subtract the maximum before exponentiating to avoid overflow. It makes classes compete: raise one probability and the others must fall. For multi-label problems (a ticket can be both billing and bug) use independent sigmoids instead. In PyTorch, pass raw logits to CrossEntropyLoss or BCEWithLogitsLoss, which apply the normalisation internally in a numerically stable way.

Softmax by hand

Logits (2.0, 1.0, 0.1) → exp (7.389, 2.718, 1.105) → sum 11.212 → probabilities (0.659, 0.242, 0.099), which add to 1. Adding 10 to every logit leaves the result unchanged.

Key takeaway: ReLU or GELU inside, softmax for one-of-K, sigmoid for yes/no and multi-label, no activation for regression.

How it works, step by step

Step 1 of 4
Data scientist

1. Pick the hidden activation

Default to ReLU (or GELU in transformer blocks); switch to Leaky ReLU if many units die.

ReLU: gradient 1 for z > 0
Key numbers at this step
Monitor
fraction of zero activations

When to use it & trade-offs

What it is

An activation function is the nonlinearity applied to a neuron's pre-activation z. Sigmoid σ(z) = 1/(1 + e⁻ᶻ) squashes values into (0, 1) and suits binary probabilities; tanh is its zero-centred cousin on (−1, 1); ReLU max(0, z) is cheap, does not saturate for positive z and is the default for hidden layers; Leaky ReLU keeps a small slope (such as 0.01) for negative z so units cannot die; GELU z·Φ(z) is a smooth, probabilistic gate used in transformers; and softmax turns a vector of logits into a probability distribution over classes. Hidden layers want activations with healthy gradients, while output layers want activations that match the target: sigmoid for binary or multi-label, softmax for single-label multiclass, identity for regression.

Why it matters: The activation controls two things: which functions the network can represent and how well gradients flow backwards. Sigmoid's derivative never exceeds 0.25, so gradients shrink geometrically with depth and early layers barely learn (the vanishing-gradient problem). ReLU's derivative is exactly 1 for positive inputs, which is why deep networks became trainable, but neurons whose inputs stay negative output zero forever (dying ReLU). Choosing the wrong output activation is also a classic bug: softmax on a multi-label problem forces the classes to compete, and sigmoid on regression bounds the predictions.

When to reach for it

  • ReLU or GELU in hidden layers
  • Softmax with cross-entropy for one-of-K classification
  • Sigmoid for multi-label tagging

When to avoid or be careful

  • Deep stacks of sigmoid hidden layers
  • Softmax on multi-label targets
  • Applying softmax manually and then a cross-entropy loss that applies it again

Common activations

ReLU
Best for: Default hidden layers in MLPs and CNNs

Strengths: Cheap; no saturation for positive inputs.

Watch out: Units can die when inputs stay negative.

GELU
Best for: Transformer feed-forward layers

Strengths: Smooth; works well at large scale.

Watch out: Slightly costlier than ReLU.

Sigmoid or tanh
Best for: Output probabilities and recurrent gates

Strengths: Bounded outputs.

Watch out: Saturates; vanishing gradients in deep stacks.

Softmax
Best for: Single-label multiclass outputs

Strengths: Proper probability distribution.

Watch out: Forces classes to compete; wrong for multi-label.

Interview signals & numbers to know

When this topic comes up

  • “Why did ReLU replace sigmoid?”
  • Vanishing gradients and the 0.25 bound
  • Softmax versus sigmoid for multi-label
  • Dying ReLU and Leaky ReLU
  • Why GELU in transformers

Benchmarks & rules of thumb

0.25 -
maximum sigmoid derivative, reached at z = 0
0.25¹⁰ ≈ 9.5e-7 -
best-case gradient factor through ten sigmoid layers
0.659 · 0.242 · 0.099 -
softmax of logits 2.0, 1.0, 0.1

Common mistakes & follow-ups

Common interview mistakes

  • Using softmax on a multi-label problem.
  • Double-applying softmax before a loss that already includes it.
  • Ignoring dead units until the loss stops moving.

Likely interviewer follow-ups

  • Why does sigmoid cause vanishing gradients?
  • When would you choose Leaky ReLU?
  • What activation goes on a regression output?
Interview tip: Quote the 0.25 bound and the ten-layer factor; the arithmetic shows why the field moved to ReLU.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Activation Functions (ReLU, Sigmoid, Softmax, GELU) with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Perceptrons & Multi-Layer Perceptrons (MLP)
Next topic
Loss Functions (Cross-Entropy, MSE, Huber)