Goal
Understand activation functions as geometric gates and gradient gates.
An affine layer computes:
An activation then transforms each coordinate:
The activation has two jobs:
- It prevents a stack of layers from collapsing into one affine map.
- Its local derivative controls how much sensitivity passes backward.
The forward value determines what information continues. The derivative determines what learning signal continues.
The Problem Pressure
Lesson 40 showed that two affine layers collapse:
Adding more affine layers changes the factorization, but not the family of functions. The final map still has one flat decision boundary per output.
Insert a nonlinear activation:
Now the intermediate transformation cannot generally be absorbed into a single matrix and bias. Different inputs can activate different intermediate units, so the effective affine map can change from one region of input space to another.
Notation / Symbol Table
| Symbol | Plain-English name | Meaning |
|---|---|---|
| pre-activation vector | Affine scores before the activation. | |
| scalar activation | One nonlinear rule applied to a scalar score. | |
| activation vector | Coordinatewise output with . | |
| local derivative vector | Coordinate is . | |
| incoming sensitivity | Gradient arriving from later computation. | |
| outgoing sensitivity | Gradient passed back through the activation. | |
| Hadamard product | Elementwise multiplication of matching coordinates. | |
| negative slope | Small positive slope used by leaky ReLU for . |
Elementwise Means Coordinate By Coordinate
For:
the notation means:
This is not matrix multiplication. Each output coordinate depends only on the matching input coordinate.
The Jacobian is therefore diagonal:
That diagonal structure is why the backward pass becomes an elementwise product.
The Activation Backward Pass
Let a scalar loss depend on:
The chain rule gives:
Because the Jacobian is diagonal, its transpose is unchanged. Coordinate by coordinate:
In vector notation:
The shapes remain:
No transpose appears in the final elementwise form because the diagonal Jacobian does not mix coordinates.
Sigmoid
The sigmoid function is:
Its range is:
It turns an unbounded score into a value that can be interpreted as a binary probability when paired with an appropriate probabilistic model and loss.
Derivative
Start from:
Differentiate:
Now use:
and:
Therefore:
The derivative can be computed from the stored forward output rather than recomputing the exponential.
Saturation
For large positive :
For large negative :
These flat outer regions are called saturation. The unit still has an output, but its local derivative passes almost no gradient backward.
The largest sigmoid derivative occurs at :
Even in its most responsive region, sigmoid shrinks the incoming sensitivity by at least a factor of four.
Tanh
The hyperbolic tangent is:
Its range is:
Unlike sigmoid, tanh is centered around zero:
Its derivative is:
The derivative reaches at the origin, but approaches zero for large positive or negative inputs. Tanh therefore also saturates.
ReLU
The rectified linear unit is:
Equivalently:
Away from zero, its derivative is:
At , the mathematical derivative is undefined. Implementations choose a convention, commonly zero.
ReLU does not saturate on its positive side. A positive pre-activation passes both its value and its incoming gradient unchanged.
Its negative side is a hard gate:
forward value: blocked
backward signal: blocked
Dead ReLU Units
Suppose a ReLU unit has:
for every training example. Then:
for every example, so the gradient reaching and through that unit is zero.
The unit still receives inputs and still computes its pre-activation. It is called dead because it never activates and receives no parameter update that would move it back into the positive region.
Large learning rates or poorly chosen initial biases can push units into this state.
Leaky ReLU
Leaky ReLU keeps a small negative-side slope:
where is a small positive constant.
Its derivative is:
The negative region is suppressed rather than completely blocked. This reduces the chance that a unit becomes permanently unable to learn.
Worked Example: Forward And Backward
Let:
and apply ReLU:
Suppose the incoming sensitivity is:
The local derivative vector is:
Therefore:
The first coordinate is blocked. The other two pass backward unchanged.
Why ReLU Networks Are Piecewise Affine
Consider one hidden ReLU layer:
For a fixed input region, every hidden pre-activation has a fixed sign. Represent the active coordinates with a diagonal matrix:
where:
Inside that region:
A following affine layer becomes:
So the network is affine while the active-set matrix remains fixed. Crossing a ReLU boundary changes , selecting a different affine map.
This is the geometric source of ReLU expressiveness:
many affine regions
+ input-dependent gates
= one piecewise-affine function
Comparing Common Activations
The same pre-activation can produce very different forward values and backward signals. Move one shared input through all four functions before compressing the differences into the table.
| Activation | Output range | Derivative behavior | Main tradeoff |
|---|---|---|---|
| Sigmoid | Small in both tails | Probability-shaped output, but saturates | |
| Tanh | Small in both tails | Zero-centered, but still saturates | |
| ReLU | Zero for negative inputs, one for positive inputs | Efficient and nonsaturating when active, but can die | |
| Leaky ReLU | Small positive slope for negative inputs | Preserves some negative-side gradient |
There is no universally best activation. The choice changes the function’s geometry and the gradient paths available during training.
Gradient Flow Through Depth
In a chain of scalar operations:
the chain rule multiplies local derivatives:
If many activation derivatives are much smaller than one, their product can become tiny. This is one source of the vanishing-gradient problem.
Sigmoid and saturated tanh units repeatedly multiply by small numbers. ReLU units avoid that shrinkage on their active side because their derivative is one, although inactive ReLUs block the path completely.
A Useful Programming View
An activation layer has no trainable parameters. It needs only the forward value required to reconstruct its local derivative:
type Vector = readonly number[];
type ActivationCache = Readonly<{
input: Vector;
}>;
type ActivationResult = Readonly<{
output: Vector;
cache: ActivationCache;
}>;
type Activation = Readonly<{
forward: (input: Vector) => ActivationResult;
backward: (
cache: ActivationCache,
incomingGradient: Vector,
) => Vector;
}>;
For ReLU, the backward rule is coordinatewise:
const reluDerivative = (z: number) => (z > 0 ? 1 : 0);
const inputGradient = incomingGradient.map(
(gradient, index) => gradient * reluDerivative(cache.input[index]),
);
For sigmoid, store either the input or the output . If the output is stored:
const sigmoidDerivativeFromOutput = (a: number) => a * (1 - a);
The implementation should reject an incoming gradient whose length differs from the cached activation length.
Numerical Stability For Sigmoid
The direct expression:
1 / (1 + Math.exp(-z))
can overflow internally when is a large negative number.
A stable branch is:
const sigmoid = (z: number) => {
if (z >= 0) {
return 1 / (1 + Math.exp(-z));
}
const expZ = Math.exp(z);
return expZ / (1 + expZ);
};
Both branches compute the same mathematical function while avoiding an unnecessarily huge exponential.
Common Mistakes
Calling Elementwise Activation Matrix Multiplication
applies one scalar function per coordinate. The diagonal Jacobian describes its derivative, but the forward pass does not construct or multiply by that matrix.
Confusing A Small Output With A Small Derivative
For ReLU, a small positive output still has derivative one. For sigmoid, an output near zero has a derivative near zero. Output magnitude and derivative magnitude are separate facts.
Saying A Dead ReLU Stops Receiving Inputs
The unit still receives and computes . Its local derivative is zero, so gradient-based training cannot change its incoming weights through that example.
Treating The Derivative At A Kink As A Training Mystery
ReLU is nondifferentiable at exactly zero. Software selects a conventional subgradient. The value at one point is usually less important than the behavior over whole positive and negative regions.
Assuming Nonlinearity Automatically Fixes Gradient Flow
Nonlinearity adds expressiveness, but its derivative can still shrink or block gradients.
Exercises
-
In plain language, distinguish a pre-activation , an activation , and an activation function . Which of these is a value, and which is a rule?
-
For each of sigmoid, tanh, ReLU, and leaky ReLU, state its output range and describe where its derivative becomes small or zero.
-
Let:
Using the convention , compute and with shapes.
-
Derive from the exponential definition. Then identify where the derivative is largest.
-
Derive the elementwise activation backward pass from the diagonal Jacobian and explain why it becomes a Hadamard product.
-
A chain contains six sigmoid activations, each evaluated where its derivative is . Ignoring the affine factors, by what factor is a sensitivity scaled across those activations? Explain the training implication.
-
Diagnose the claim: “A ReLU output of passes almost no gradient.” Separate output magnitude from local derivative.
-
Explain why leaky ReLU reduces dead-unit risk without making its negative and positive regions equally influential.
-
For a one-hidden-layer ReLU network, show that fixing the active-unit pattern reduces the network to one affine map. What event changes that affine map?
-
Implement sigmoid, tanh, ReLU, and leaky ReLU activation layers in TypeScript with:
- forward caches,
- dimension checks in the backward pass,
- stable sigmoid evaluation,
- and central finite-difference tests for every input coordinate.
Readiness Check
You are ready for the next lesson when you can:
- explain why nonlinearities prevent affine-layer collapse,
- compare activation functions using both forward geometry and derivative behavior,
- identify saturation and dead-unit behavior,
- derive from the Jacobian,
- and explain why a fixed ReLU activation pattern defines one affine region.
Summary
- Activations make stacked layers more expressive by changing the effective map according to the input.
- Elementwise activations have diagonal Jacobians.
- Their backward pass multiplies the incoming sensitivity by local derivatives coordinatewise.
- Sigmoid and tanh saturate in their tails.
- ReLU passes gradients on its positive side and blocks them on its negative side.
- ReLU networks are piecewise affine because each activation pattern selects an affine map.
Next Lesson
Lesson 42 builds feedforward networks by composing linear and activation layers. It will track hidden representations through the forward pass and show how depth organizes many local affine regions into one model.