The Big Question
In the previous lesson, a neural network was built by stacking layers. Each layer computed a weighted sum and then applied an activation function:
The natural question is not which activation function to memorize first. The deeper question is why activation functions are needed at all.
If we remove the activation functions, does stacking many layers still make the network more powerful?
Surprisingly, no. Without activation functions, a deep neural network is equivalent to a single linear model. Activation functions are what let deep networks actually become non-linear.
The Collapse Problem
Suppose a two-layer network has no activation function. The first layer computes:
The second layer computes:
Substitute the first equation into the second:
Expand:
Now define:
Then the whole network becomes:
That is one linear layer. More parameters were used, but nothing fundamentally more expressive was created.
Core Intuition
Imagine a factory where every worker can only multiply the incoming number by 2. Worker 1 doubles the number, Worker 2 doubles it again, and Worker 3 doubles it again. The whole factory multiplies by 8.
You could replace all three workers with one worker who multiplies by 8. The factory looks deeper, but its behavior is still simple.
Now give one worker a rule: if the number is negative, replace it with zero. Suddenly the whole factory cannot be simplified into one multiplication. The rule introduced a bend.
Activation functions are those bends. The linear step mixes information. The activation step changes the shape of the representation.
Why Non-Linearity Matters
Think back to XOR. A straight line cannot separate the classes in the original input space. But if a hidden layer can bend, gate, or reshape the representation, points that were inseparable can become separable.
Linear transformations can rotate, stretch, shrink, and shift space. Non-linear activations allow a network to warp space. That is why hidden layers can build representations that linear models cannot.
What An Activation Function Does
Every neuron performs two conceptual steps:
| Step | Equation | Role |
|---|---|---|
| Linear transformation | z = Wx + b | mixes the incoming features |
| Activation | a = f(z) | introduces non-linearity |
This pattern repeats through deep learning: linear transformation, non-linear activation, linear transformation, non-linear activation.
Interactive Demo
| Activation | f(z) | slope | Typical use |
|---|---|---|---|
| Sigmoid | 0.731 | 0.197 | Binary output layers |
| Tanh | 0.762 | 0.420 | Older recurrent networks |
| ReLU | 1.000 | 1.000 | Common hidden layers |
| Leaky ReLU | 1.000 | 1.000 | Avoiding dead ReLUs |
| GELU | 0.841 | 1.083 | Transformers |
Sigmoid and tanh flatten
Move z far left or far right. The output barely changes, and the slope gets close to zero. Small slope means a small gradient signal.
ReLU keeps positive gradients alive
For positive z, ReLU has slope 1. Large positive values do not saturate, which helps deep networks learn faster.
Negative ReLU units can die
For negative z, ReLU outputs zero and has zero slope. Leaky ReLU keeps a small slope so the neuron can still receive updates.
GELU is a smooth gate
GELU softly decides how much signal to pass through. That smooth behavior is one reason it appears in modern transformer models.
Move the input across negative and positive values. Watch which activations saturate, which keep gradients alive, and which suppress negative signals.
Mathematics
Sigmoid
The sigmoid function maps any real number into the interval :
Large negative values become close to 0. Large positive values become close to 1. This makes sigmoid useful when the output should be interpreted as a binary probability.
The problem is saturation. When is very positive or very negative, the curve becomes almost flat. Its derivative is:
Near the flat regions, this derivative is close to zero, so gradient updates become tiny. In deep networks, this contributes to the vanishing gradient problem.
Tanh
The hyperbolic tangent maps inputs into :
Unlike sigmoid, tanh is centered around zero. Negative inputs produce negative outputs, positive inputs produce positive outputs, and values near zero remain near zero.
This zero-centered behavior often helps optimization compared with sigmoid, but tanh still saturates for very large positive or negative inputs.
ReLU
The Rectified Linear Unit is extremely simple:
If , ReLU outputs 0. If , ReLU outputs .
ReLU became popular because it has three practical advantages:
- It does not saturate for positive values, so positive gradients stay strong.
- It is computationally cheap because it only requires a comparison.
- It creates sparse activations because many neurons output exactly zero.
Dead ReLU
ReLU also has a failure mode. If a neuron always receives negative pre-activations, it always outputs zero. Its gradient is also zero, so the neuron may stop learning.
This is called a dead ReLU. The neuron is still present in the network, but it no longer contributes useful signal.
Leaky ReLU
Leaky ReLU keeps a small slope for negative values:
Negative inputs are still suppressed, but not completely. Because the negative side has a small derivative, the neuron can still receive gradient updates.
GELU
Transformers often use Gaussian Error Linear Units, usually called GELU:
Here is the cumulative distribution function of the standard normal distribution. You do not need to memorize that definition to understand the intuition.
GELU softly decides how much of the input to keep. Large positive values pass through almost unchanged. Large negative values are mostly suppressed. Values near zero are partially kept rather than being cut off sharply.
Worked Example
Apply several activation functions to the same vector:
| Activation | Output | What changed? |
|---|---|---|
| Sigmoid | [0.047, 0.731, 0.993] | squashes everything between 0 and 1 |
| Tanh | [-0.995, 0.762, 0.9999] | keeps sign but saturates near -1 and 1 |
| ReLU | [0, 1, 5] | removes the negative value and keeps positives unchanged |
| Leaky ReLU | [-0.03, 1, 5] | keeps a small negative signal |
Comparison
| Activation | Output range | Main use | Key idea |
|---|---|---|---|
| Step | 0 or 1 | Historical perceptrons | Hard decision, not useful for gradient-based training |
| Sigmoid | (0, 1) | Binary output layers | Saturates for large positive or negative inputs |
| Tanh | (-1, 1) | Older recurrent networks | Zero-centered but still saturates |
| ReLU | [0, infinity) | Many hidden layers | Fast, sparse, but can produce dead neurons |
| Leaky ReLU | (-infinity, infinity) | Hidden layers when dead ReLUs are a concern | Keeps a small negative-side gradient |
| GELU | smoothly gated | Transformers | Softly keeps or suppresses the input |
Implementation
Activation functions are ordinary functions applied element by element to arrays. The implementation is short, but the modeling consequences are enormous.
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def tanh(z):
return np.tanh(z)
def relu(z):
return np.maximum(0, z)
def leaky_relu(z, alpha=0.01):
return np.where(z > 0, z, alpha * z)
def gelu(z):
return 0.5 * z * (
1 + np.tanh(np.sqrt(2 / np.pi) * (z + 0.044715 * z**3))
)
z = np.array([-3.0, 1.0, 5.0])
print("sigmoid:", np.round(sigmoid(z), 3))
print("tanh:", np.round(tanh(z), 3))
print("relu:", relu(z))
print("leaky_relu:", leaky_relu(z))
print("gelu:", np.round(gelu(z), 3))Where The Activation Goes
In a neural network, the activation comes immediately after each hidden layer linear transformation:
def forward(x, W1, b1, W2, b2):
z1 = W1 @ x + b1
a1 = relu(z1)
z2 = W2 @ a1 + b2
y_hat = sigmoid(z2)
return y_hatHidden layers often use ReLU, Leaky ReLU, GELU, or another non-linear activation. The output layer depends on the task: sigmoid for binary classification, softmax for multiclass classification, and often no activation for regression.
Interview Discussion
Why do neural networks need activation functions?
Activation functions introduce non-linearity. Without them, stacked linear layers collapse into one equivalent linear layer.
Prove that stacked linear layers are still linear.
If h = W1x + b1 and y = W2h + b2, then y = W2W1x + W2b1 + b2. Define W = W2W1 and b = W2b1 + b2, so y = Wx + b.
Why can sigmoid cause vanishing gradients?
For large positive or negative inputs, sigmoid becomes almost flat. Its derivative becomes close to zero, so gradient updates shrink as they move backward through layers.
Why is ReLU common in hidden layers?
ReLU is cheap to compute, creates sparse activations, and does not saturate for positive values, so gradients stay strong on the positive side.
What is the dead ReLU problem?
A ReLU neuron can become stuck outputting zero for all inputs. Its gradient is then zero, so it may stop updating.
Why do transformers often use GELU?
GELU smoothly gates inputs instead of sharply cutting them off. This smooth behavior often works well in transformer feed-forward networks.
Active Recall
1. What happens if you remove every activation function from a neural network?
2. Why is non-linearity necessary for solving problems like XOR?
3. Which activation is commonly used for binary classification output layers?
4. Why does sigmoid saturating cause optimization trouble?
5. Why is ReLU computationally cheap?
6. What causes a dead ReLU?
7. Why might a model use GELU instead of ReLU?
Common Mistakes
- Thinking activation functions only squash numbers. Their main job is introducing non-linearity.
- Using sigmoid in every hidden layer without considering saturation and vanishing gradients.
- Assuming ReLU is always best. Different architectures often benefit from different activations.
- Forgetting that output-layer activations depend on the prediction task.
- Thinking a deeper network is automatically more expressive even if every layer is linear.
Connection To Backpropagation
Neural networks become powerful because they alternate linear mixing with non-linear activation. The forward pass can now produce rich predictions. But the weights are still just numbers. The next question is how each weight knows how to change after the network makes a mistake. That is backpropagation.