Activation Functions
An activation function bends a neuron's weighted sum before passing it on. Without that bend, a stack of layers collapses into a single linear layer, so the nonlinearity is what makes depth worth anything; its slope is what learning will run on.
What the symbols mean
- Sigmoid
A smooth step from 0 to 1, equal to 1/2 at z = 0.
- Sigmoid's derivative
Its slope, computed from its own output: at most 1/4, at z = 0.
What the symbols mean
- Hyperbolic tangent
A smooth step from −1 to 1, centered at 0: a stretched, shifted sigmoid.
What the symbols mean
- Pre-activation
The neuron's weighted sum w·x + b.
What the symbols mean
- First layer's weights
The matrix of the first layer.
- Second layer's weights
The matrix of the second layer. The product W₂W₁ is one matrix, so two linear layers act like one.
Explanation
An activation function is simply the rule a neuron uses to turn its weighted sum into its output. It's called that because it decides how strongly the neuron is activated, or "fires", for a given input. There are only four you need to know, and each one is a one-line formula: the step (fire or don't), the sigmoid (a smooth step from to ), (a smooth step from to ) and ReLU (let positive numbers through, turn negatives into ). What matters about each is its shape and its slope, because the slope is what learning uses. It's a short list; bear with the derivatives, each one comes out in a few lines.
The step function. when and otherwise: the original perceptron. Its flaw is its slope, which is everywhere except at the jump, where it doesn't exist. Learning by gradients asks "if I nudge this weight a little, how much does the output change?", and the step answers "not at all" almost everywhere. No slope, no signal to learn from.
The sigmoid, and where its slope formula comes from. is the step with its corner sanded off. Two handy facts: , and , so it's symmetric about the point . For the slope, write and use the chain rule with the derivative of an exponential:
Now split that fraction into two factors:
The first factor is . The second is , because . So
Think about why that's so convenient. Once the forward pass has computed , its slope costs one subtraction and one multiplication. And notice the size: is a product of two numbers that add up to , which is largest when both are . So , with the maximum at . Far from the curve goes flat (we say the neuron is saturated) and the slope is nearly . Chained through many layers, those small slopes multiply, which is the story of vanishing gradients later in the course.
tanh. Same S shape, but centered: it runs from to and . It's really a rescaled sigmoid. Check: , and multiplying top and bottom by gives . Its slope peaks at when , four times the sigmoid's peak, and its outputs average out near , which keeps the next layer's sums balanced.
ReLU. , short for rectified linear unit: "rectified" because it cuts off the negative half, like an electrical rectifier. Its slope is for and for (at exactly software just picks one, usually ). It's cheap, and its slope doesn't shrink for positive inputs, which is why most deep networks use it. The catch: a neuron whose is negative for every input has slope everywhere and stops learning, a "dead ReLU".
Why a nonlinearity is needed at all. Suppose you skip the activation. Two layers compute and then . Substitute the first into the second:
That's a single layer, with weight matrix and bias . Ten linear layers collapse the same way, because a product of matrices is one matrix (matrix operations). So a deep network without activations can only draw what one neuron draws: a straight boundary. Put a nonlinear between the layers and the collapse stops, because can't be multiplied out. What that buys you is the subject of the last lesson of this unit.
A common mistake. It's tempting to think ReLU is "basically linear", since each half of it is a straight line, so it can't help. It isn't linear: but . The kink at is the whole point, and sums of kinks placed at different spots can bend into any shape. Another slip is forgetting the inner derivative: when , the slope with respect to the weight is , not just .
See it
Worked examples
Find the slope of the sigmoid at using , and check it against the unsimplified derivative.
- 1
Start with the output itself: .
Two layers with no activation compute and then . Show they collapse into one neuron, then see what a ReLU on changes.
- 1
Substitute into the second layer: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
What is the largest value the sigmoid's slope ever takes?
For the sigmoid , find the slope to 4 decimal places.
Find the slope of at , to 4 decimal places.
Two linear layers (no activation) have and . Find the weight matrix of the single layer they collapse into, entered row by row.
A predictive-maintenance model ends in a sigmoid neuron, and a crew is sent out when its output reaches . What weighted sum triggers a dispatch? Give 3 decimal places.
Which of these can produce a graph with a bend in it?
A sigmoid neuron computes with and . At , how fast does change as changes, ?
The hardest one here. Get it, and you've got this lesson.
A tiny two-layer model computes . Find to 4 decimal places.
Where it's used
The choice of activation shapes how real models behave. Image and speech networks use ReLU or a smoothed cousin of it in nearly every hidden layer, because its slope stays for active neurons and training stays fast even with dozens of layers. Sigmoids live on where an output must be a probability (is this email spam?) and inside the gates of recurrent networks that decide how much memory to keep, while tanh appears where a signed output is natural, like a steering command between full left and full right.