Quant Memo
Core

Activation Functions

An activation function is the small bend a network inserts after every linear step, and without it — no matter how many layers you stack — the whole network collapses into one giant straight line, unable to learn anything a linear model couldn't already do.

Prerequisites: The Multilayer Perceptron

Stack ten layers of pure linear transformations — each just a weighted sum of the layer before — and multiply them out, and the result is still just one linear transformation. Depth adds nothing if every layer only does addition and scaling. Activation functions are the ingredient that breaks this: a small nonlinear bend inserted after each layer's linear step, without which a thousand-layer network would be mathematically no more expressive than a single layer.

The analogy: a thermostat versus a dimmer switch with a kink

A dimmer switch that scales brightness perfectly linearly with the dial position can only ever represent "more dial, proportionally more light" — chain ten such dimmers together and you still just get one overall scaling factor. Now imagine a switch with a kink: below a certain dial position, nothing happens at all; above it, brightness ramps up. That single kink is enough that combining several such switches can approximate almost any on/off, staged, or curved lighting pattern you like — sharp transitions, plateaus, complex combinations no pure scaling could ever produce. The kink is the nonlinearity, and it's the entire reason chaining many of these switches together buys you anything at all.

The maths: three common shapes

The sigmoid squashes any real number into the range (0,1)(0,1):

σ(z)=11+ez\sigma(z) = \frac{1}{1+e^{-z}}

In words: very negative inputs go to nearly 0, very positive inputs go to nearly 1, and there's a smooth S-shaped transition in between — useful for representing a probability, but with a problem: for large z|z| the curve goes almost flat, meaning its slope (needed for backpropagation) is nearly zero there.

Tanh is a rescaled sigmoid ranging over (1,1)(-1,1): tanh(z)=2σ(2z)1\tanh(z) = 2\sigma(2z)-1. Same S-shape, but centred at zero, which tends to make gradients flow more evenly through a network than plain sigmoid.

ReLU (rectified linear unit) is the simplest and most widely used today:

ReLU(z)=max(0,z)\text{ReLU}(z) = \max(0, z)

In words: negative inputs are crushed to exactly zero, positive inputs pass through completely unchanged. It has no flat region on the positive side, so its gradient there is always exactly 1 — a huge practical advantage for training deep networks, at the cost of a "dead" flat zero region on the negative side.

Worked example 1: sigmoid saturates, ReLU doesn't

Take input z=5z = 5. Sigmoid: σ(5)=1/(1+e5)=1/(1+0.0067)0.993\sigma(5) = 1/(1+e^{-5}) = 1/(1+0.0067) \approx 0.993. Its derivative is σ(z)(1σ(z))0.993×0.0070.0067\sigma(z)(1-\sigma(z)) \approx 0.993 \times 0.007 \approx 0.0067 — tiny. Now z=10z=10: σ(10)0.99995\sigma(10)\approx0.99995, derivative 0.00005\approx0.00005, even tinier. As inputs grow, sigmoid's gradient shrinks toward zero — this is saturation, and it's why gradients vanish in deep sigmoid networks. ReLU at z=5z=5 and z=10z=10: output is 55 and 1010 respectively, gradient is exactly 11 at both — no shrinking at all, however large the input.

Worked example 2: a dead ReLU

A ReLU neuron has weight w=2w=-2, bias b=0.1b=0.1. For input x=1x=1: z=2(1)+0.1=1.9z = -2(1)+0.1=-1.9, so ReLU(1.9)=0\text{ReLU}(-1.9)=0, and the gradient flowing backward through this neuron is exactly 00 (ReLU's slope is 00 for any negative input). No matter what the loss says should happen, this neuron passes zero gradient back and its weights don't update on this example. If most training inputs push zz negative for this neuron, it can get permanently stuck outputting zero forever — a "dead ReLU." Variants like Leaky ReLU, max(0.01z,z)\max(0.01z, z), fix this by giving negative inputs a small nonzero slope: at z=1.9z=-1.9, Leaky ReLU outputs 0.019-0.019 and its gradient is 0.010.01, not 00 — small, but alive.

Function explorer
-221.1
x = 1.00f(x) = 0.731

Drag the parameters in the explorer above to see the sigmoid's characteristic S-shape flatten out at the extremes — that flattening is exactly the saturation from worked example 1, and it's the visual signature of vanishing gradients.

Gradient descent
parameter →
position -0.623loss -0.141gradient -0.930converging

The explorer above shows how a flat region of a loss surface makes descent crawl to a near-halt — a saturated sigmoid or a dead ReLU produces exactly this kind of flat region locally within the network, even if the overall loss surface elsewhere is fine.

Activation functions supply the nonlinearity that makes depth meaningful at all — without one, any number of stacked layers is mathematically equivalent to a single linear layer, and none of it could learn a curved decision boundary or a nonlinear pattern.

What this means in practice

ReLU and its variants (Leaky ReLU, GELU, ELU) dominate hidden layers in modern deep networks because they avoid saturation on the positive side and are cheap to compute. Sigmoid survives mainly at output layers producing probabilities, and tanh sometimes in recurrent architectures. The choice of activation interacts directly with weight initialization and learning rate — a network can train fine or fail to train at all depending on whether these three choices are consistent with each other.

The classic confusion is treating activation function choice as a minor stylistic detail. It isn't: a deep sigmoid network without careful initialisation reliably fails to train because gradients saturate to near-zero at every layer and the product across many layers underflows to nothing, while the same architecture with ReLU often trains without issue. Also, "ReLU never has vanishing gradients" is itself a myth — dead ReLUs are a real, common failure mode, just a different one than saturation.

Related concepts

Practice in interviews

Further reading

  • Goodfellow, Bengio & Courville, Deep Learning, ch. 6.3
  • Nair & Hinton, Rectified Linear Units Improve Restricted Boltzmann Machines (2010)
ShareTwitterLinkedIn