From Neurons to Networks
Activation functions
The nonlinearity that makes depth matter
The tiny non-linear squish inside every neuron - the one thing that lets a deep network bend, instead of quietly collapsing into a single straight line.
A neuron does two things: it takes a weighted sum of its inputs - a straight, linear operation - and then it passes that number through a small activation function that bends it. That bend is not a detail. Strip it out and something startling happens: a 100-layer network, for all its millions of weights, collapses into a single straight line. The activation is the entire reason depth buys you power.
The picture to hold in your head
Why a stack of lines is still one line
This isn't hand-waving; it's a one-line proof. Take two linear layers with no activation between them. The first maps input to ; the second maps to . Substitute the first into the second:
Two linear layers fold into one
The whole two-layer stack is just - a single linear layer with new weights and bias . The same trick collapses ten layers, or ten thousand. Depth without a non-linearity is an illusion: you paid for a deep network and received one matrix multiply.
So we insert a non-linear function after each layer: . Because is not linear, you cannot fold the two multiplies into one - every layer now genuinely adds expressive power. The question becomes: which ? Four choices dominate, and the demo below lets you feel each one's personality - its shape and, just as importantly, the shape of its gradient.
Four squishes and their gradients
Meet the cast. Sigmoid gently squashes any number into . Tanh does the same into but is centred on zero. ReLU - the modern workhorse - simply zeroes out negatives and leaves positives untouched. Leaky ReLU lets a whisper of the negative side through so a neuron never fully dies. Here are the exact functions and their derivatives (the derivative is what gradient descent multiplies through - watch it closely):
The four, with derivatives
In the plotter, the left panel is the function; the right panel is its gradient . Swap the activation with the dropdown, then drag the slider to move the input . The amber dashed tangent on the left is the slope at that point - and its steepness is exactly the height of the amber dot on the right. Slide a sigmoid out to and watch the tangent go flat and the gradient collapse toward zero. That flattening is the famous vanishing gradient, and it's the reason ReLU took over.
Dead zone: for x ≤ 0 the gradient is exactly 0 - this unit is switched off and stops learning until its input turns positive.
Why ReLU is the default - and what "vanishing gradient" means
The output layer speaks probability: softmax
Hidden layers squish per-neuron, but the final layer of a classifier needs to answer "how likely is each class?" - a whole vector of numbers that are all positive and sum to . That's the job of softmax, which exponentiates the raw scores (called logits) and normalizes them:
Softmax turns scores into a distribution
Exponentiating keeps everything positive and exaggerates the leader; dividing by the sum forces the outputs to add to . The largest logit becomes the most probable class - and when a language model predicts the next token, this is the exact step that turns a vector of scores over the vocabulary into the probabilities it samples from.
import numpy as np
def sigmoid(x): return 1 / (1 + np.exp(-x))
def tanh(x): return np.tanh(x)
def relu(x): return np.maximum(0, x)
def leaky(x, a=0.01): return np.where(x > 0, x, a * x)
def softmax(z): # output layer: scores -> probabilities
e = np.exp(z - z.max()) # subtract max for numerical stability
return e / e.sum() # positive, and sums to 1Check yourself
Why does stacking linear layers with no activation collapse into a single layer?
For very large positive x, the sigmoid's gradient becomes:
Recall: why is a non-linear activation essential, and why is ReLU the common default?
Try to state it, then check.
Primary source
Linear vs ReLU). For the deepest intuition on how these bends compose into learning, 3Blue1Brown's Neural Networks series is the gold standard.Ask your teacher