An activation function decides what a neuron outputs from its weighted input. Without activations, a deep network would collapse into a single linear model; with them, it can learn curves, images and language.
What You Will Learn
- Why non-linearity is needed
- Sigmoid, tanh, ReLU, Leaky ReLU, GELU and softmax
- Vanishing gradients
- Which activation to use where
Prerequisites
Compare Them in NumPy
import numpy as np
np.set_printoptions(precision=3, suppress=True)
z = np.array([-3., -1., 0., 1., 3.])
sigmoid = lambda z: 1 / (1 + np.exp(-z))
gelu = lambda z: 0.5 * z * (1 + np.tanh(np.sqrt(2 / np.pi) * (z + 0.044715 * z ** 3)))
print("sigmoid", sigmoid(z))
print("tanh ", np.tanh(z))
print("relu ", np.maximum(0, z))
print("leaky ", np.where(z > 0, z, 0.01 * z))
print("gelu ", gelu(z))
sigmoid [0.047 0.269 0.5 0.731 0.953] tanh [-0.995 -0.762 0. 0.762 0.995] relu [0. 0. 0. 1. 3.] leaky [-0.03 -0.01 0. 1. 3. ] gelu [-0.004 -0.159 0. 0.841 2.996]
Explanation: Sigmoid squashes to (0, 1), tanh to (−1, 1) centred on zero. ReLU passes positives and zeroes negatives. Leaky ReLU keeps a small slope for negatives. GELU is a smooth ReLU used in transformers like BERT and GPT.
Softmax for Multi-Class Output
def softmax(v):
e = np.exp(v - v.max()) # subtract max for numerical stability
return e / e.sum()
p = softmax(np.array([2.0, 1.0, 0.1]))
print(p, p.sum())
[0.659 0.242 0.099] 1.0
Explanation: Softmax turns raw scores (logits) into probabilities that sum to 1 – used in the last layer of classifiers and to pick the next token in LLMs.
Vanishing Gradients
s = sigmoid(z)
print(s * (1 - s)) # derivative of sigmoid
[0.045 0.197 0.25 0.197 0.045]
Explanation: The sigmoid’s gradient is at most 0.25 and almost 0 for large |z|. In backpropagation gradients are multiplied layer by layer, so 10 sigmoid layers can shrink them by 0.25¹⁰ ≈ 0.000001 – early layers stop learning. ReLU’s gradient is 1 for positive inputs, which is why it made deep networks practical. A ReLU neuron that always gets negative input “dies” (gradient 0); Leaky ReLU fixes that.
Which One to Use
| Function | Range | Typical use |
|---|---|---|
| ReLU | [0, ∞) | Default for hidden layers in CNNs and MLPs |
| Leaky ReLU / ELU | (−∞, ∞) | When many ReLUs die |
| GELU / SiLU (Swish) | ≈ (−0.17, ∞) | Transformers, modern CNNs |
| tanh | (−1, 1) | RNN/LSTM hidden states |
| Sigmoid | (0, 1) | Binary output layer; LSTM gates |
| Softmax | (0, 1), sums to 1 | Multi-class output layer |
| Linear (none) | (−∞, ∞) | Regression output layer |
# Keras
from tensorflow import keras
model = keras.Sequential([keras.layers.Dense(64, activation="relu"),
keras.layers.Dense(10, activation="softmax")])
# PyTorch
import torch.nn as nn
net = nn.Sequential(nn.Linear(20, 64), nn.ReLU(), nn.Linear(64, 10)) # softmax inside CrossEntropyLoss
Series
Deep learning series: Intro to DL · ANN · Activation Functions · Loss Functions · Backpropagation · Optimizers · Dropout & BatchNorm · CNN · RNN · LSTM & GRU · Transfer Learning · Autoencoders · GANs · PyTorch vs TensorFlow
Interview Questions
Q: Why do neural networks need non-linear activations?
Stacked linear layers are still linear; non-linearity lets the network model complex functions.
Q: Why is ReLU popular?
It is cheap, does not saturate for positive inputs and reduces vanishing gradients.
Q: Sigmoid vs softmax at the output?
Sigmoid for binary or multi-label outputs; softmax for one-of-many classes.
Practice Exercises
- Implement the derivative of tanh and compare it with sigmoid’s.
- Plot all activations between −5 and 5 with Matplotlib.
Summary
Use ReLU (or GELU) in hidden layers, sigmoid for binary output, softmax for multi-class and no activation for regression. Saturating functions cause vanishing gradients.
Next: Loss Functions
Machine Learning Tutorials, Courses and Certifications