Search

Search papers and lessons

Activation Function

Level 2 · 10 min read

An activation function is the bend a neuron applies to its weighted sum; without it, any stack of layers collapses into a single linear map.

On this page

TL;DR

An activation function is a fixed, usually simple, bend applied to a neuron's weighted sum before the result is passed on. It is what lets a network draw curved boundaries and fit curved functions. Without it, every layer is just a matrix multiply, and a hundred layers do no more than one.

The common choices are the sigmoid, the hyperbolic tangent, and the rectified linear unit (ReLU). Each one bends the line in a different place and by a different amount.

Where you'll see it: after almost every hidden layer of a feedforward or convolutional network, in the gate of an LSTM, and in the squashing step that turns a network's last layer into probabilities.

Intuition

A straight line cannot fold

Picture a neuron as a small decision maker. It takes some inputs, multiplies each by a weight, adds them up with a bias, and gets one number. That number is a straight-line (linear) function of the inputs. Stack two of those neurons and the output is still a straight-line function of the original inputs, because a line fed into a line is a line.

The problem is that most real patterns are not straight. Whether a loan is risky, whether a pixel belongs to an edge, whether a sound is a vowel: none of these change in a steady proportion to their inputs. They have thresholds, saturation and folds. An activation function inserts the bend. After it, the next layer sees a shape that a single linear map could not have produced, and stacking layers starts to add real power.

Think of it as a dimmer switch versus a plain wire. A wire passes the signal unchanged. A dimmer responds differently at low and high settings. The activation function is the dimmer that each neuron places after its sum.

What a good bend needs

Three properties matter in practice:

  1. Nonlinear, so that depth adds expressive power.
  2. Easy to compute, because it runs billions of times per training step.
  3. Has a useful derivative, because training (see Backpropagation) pushes error signals backwards through it using its Derivative, and a derivative that is always near zero stops learning.

The rest of this lesson is about the tension between these, especially the third.

The math

The neuron

A neuron with inputs x∈Rn\mathbf{x} \in \mathbb{R}^n, weights w∈Rn\mathbf{w} \in \mathbb{R}^n and bias b∈Rb \in \mathbb{R} computes first a pre-activation and then an output:

z=w⊤x+b,a=σ(z).z = \mathbf{w}^\top \mathbf{x} + b, \qquad a = \sigma(z).

Here zz is the weighted sum, aa is the output, and σ\sigma is the activation function. A layer applies the same σ\sigma elementwise to a vector of sums (W\mathbf{W} is a matrix whose rows are the weight vectors of the layer's neurons): a=σ(Wx+b)\mathbf{a} = \sigma(\mathbf{W}\mathbf{x} + \mathbf{b}). The symbol σ\sigma is reused here for the activation; it is not the logistic function unless we say so.

Why linearity collapses

Take two layers without any activation between them:

h=W1x+b1,y=W2h+b2.\mathbf{h} = \mathbf{W}_1 \mathbf{x} + \mathbf{b}_1, \qquad \mathbf{y} = \mathbf{W}_2 \mathbf{h} + \mathbf{b}_2.

Substitute the first into the second:

y=W2(W1x+b1)+b2=(W2W1) x+(W2b1+b2).\begin{aligned} \mathbf{y} &= \mathbf{W}_2(\mathbf{W}_1 \mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2 \\ &= (\mathbf{W}_2 \mathbf{W}_1)\,\mathbf{x} + (\mathbf{W}_2 \mathbf{b}_1 + \mathbf{b}_2). \end{aligned}

The result is one matrix and one bias. This holds for any number of layers, so depth buys nothing. Inserting a nonlinear σ\sigma between the layers breaks the substitution, and the composition σ(W2σ(W1x))\sigma(\mathbf{W}_2 \sigma(\mathbf{W}_1 \mathbf{x})) cannot in general be written as a single affine map.

The three standard choices

Sigmoid squashes any real number into the open interval (0,1)(0, 1):

σ(z)=11+e−z,σ′(z)=σ(z)(1−σ(z)).\sigma(z) = \frac{1}{1 + e^{-z}}, \qquad \sigma'(z) = \sigma(z)\bigl(1 - \sigma(z)\bigr).

Its largest derivative is σ′(0)=0.25\sigma'(0) = 0.25, and the derivative shrinks towards zero as ∣z∣|z| grows.

Hyperbolic tangent is a rescaled sigmoid that maps into (−1,1)(-1, 1):

tanh⁡(z)=ez−e−zez+e−z,ddztanh⁡(z)=1−tanh⁡2(z).\tanh(z) = \frac{e^{z} - e^{-z}}{e^{z} + e^{-z}}, \qquad \frac{d}{dz}\tanh(z) = 1 - \tanh^2(z).

It is zero-centred, which helps optimisation a little, and its largest derivative is 11 at z=0z = 0.

Rectified linear unit (ReLU) keeps positive values and clips negative ones to zero:

ReLU(z)=max⁡(0,z),ReLU′(z)={1z>00z<0\mathrm{ReLU}(z) = \max(0, z), \qquad \mathrm{ReLU}'(z) = \begin{cases} 1 & z \gt 0 \\ 0 & z \lt 0 \end{cases}

At exactly z=0z = 0 the derivative is undefined. Implementations pick 0 or 1, and this does not matter in practice.

−3−2−10123−10123zoutput
  • sigmoid
  • tanh
  • ReLU
Three activations. The sigmoid and tanh saturate at their ends; ReLU does not saturate for positive z but is flat at zero for negative z.

Derivatives and the vanishing gradient

Backpropagation multiplies local derivatives along a path. For a chain of LL sigmoid layers, the gradient reaching the first layer contains a product of LL factors of the form σ′(zℓ)\sigma'(z_\ell). Since σ′(z)≤0.25\sigma'(z) \le 0.25 everywhere, that product is at most 0.25L0.25^L. Tanh is at most 11 but is 11 only at zero, so saturated tanh units also send almost no gradient back. ReLU's local derivative is exactly 11 on its active side, so the product does not shrink from that factor alone. This is the core reason ReLU replaced sigmoid in deep hidden layers.

Worked examples

Example
One neuron, three activations

Inputs x=(1.0, −2.0)\mathbf{x} = (1.0,\ -2.0), weights w=(0.5, 0.25)\mathbf{w} = (0.5,\ 0.25), bias b=0.1b = 0.1.

  1. Weighted sum: z=0.5⋅1.0+0.25⋅(−2.0)+0.1=0.5−0.5+0.1=0.1z = 0.5 \cdot 1.0 + 0.25 \cdot (-2.0) + 0.1 = 0.5 - 0.5 + 0.1 = 0.1.
  2. ReLU: max⁡(0,0.1)=0.1\max(0, 0.1) = 0.1.
  3. Sigmoid: 1/(1+e−0.1)=1/(1+0.904837)≈0.5251 / (1 + e^{-0.1}) = 1 / (1 + 0.904837) \approx 0.525.
  4. Tanh: tanh⁡(0.1)≈0.0997\tanh(0.1) \approx 0.0997.
  5. Same sum, three outputs. Because zz is small, sigmoid and tanh are both close to their centre point and nearly linear here. ReLU is exactly linear for z>0z \gt 0 but has a kink at z=0z = 0, right next to this input, and that kink is what lets it carve sharp regions.
Example
Why deep sigmoid stacks stall

Ten sigmoid layers, and suppose each neuron happens to sit at its best point, z=0z = 0, where σ′(0)=0.25\sigma'(0) = 0.25.

  1. Gradient factor per layer: 0.250.25.
  2. After ten layers the product is 0.2510=1/1 048 576≈9.5×10−70.25^{10} = 1 / 1\,048\,576 \approx 9.5 \times 10^{-7}.
  3. So the first layer receives a learning signal about a millionth the size of the last one.
  4. (This ignores the weights, which also scale the signal at each layer; the point is the activation's contribution.) Now suppose a neuron drifts to z=6z = 6. Then σ(6)=1/(1+e−6)≈0.99753\sigma(6) = 1 / (1 + e^{-6}) \approx 0.99753 and σ′(6)≈0.00247\sigma'(6) \approx 0.00247. One saturated neuron cuts the signal by a factor of about 100 on its own.
  5. The same ten layers of ReLUs with positive inputs multiply to 110=11^{10} = 1. This is the arithmetic behind the switch to ReLU.
Example
A realistic setting: a hidden layer of a small classifier

A hidden layer has 3 neurons with pre-activations z=(−1.5, 0.2, 2.0)\mathbf{z} = (-1.5,\ 0.2,\ 2.0), and we use ReLU.

  1. Outputs: ReLU(z)=(0, 0.2, 2.0)\mathrm{ReLU}(\mathbf{z}) = (0,\ 0.2,\ 2.0).
  2. The first neuron is switched off for this input, so it contributes nothing to the next layer and receives no gradient from this example.
  3. The other two pass their values through unchanged.
  4. Interpretation: each neuron acts as a gate that is either open (a linear function of its input) or closed (zero). A network of many such gates is piecewise linear, and it is the pattern of open and closed gates that makes the overall function curved.

Tradeoffs and failure modes

No activation is best everywhere. The choice depends on where the unit sits and what the training signal needs.

SituationProblemBetter choice
Deep hidden layersSigmoid and tanh saturate, so gradients vanishReLU or a variant
Neurons stuck at zero"Dying ReLU": a unit with negative pre-activation for every input gets no gradient and never recoversLeaky ReLU, max⁡(0.01z,z)\max(0.01z, z), or GELU
Output of a binary classifierNeeds a probability in (0,1)(0, 1)Sigmoid on the final layer
Output over several classesNeeds probabilities that sum to 1Softmax (see Softmax)
Recurrent gates (LSTM, GRU)Need a value between 0 and 1 to act as a gateSigmoid for the gate, tanh for the content
Transformers and modern large modelsSmooth curvature helps optimisationGELU or SiLU

Practical warnings:

  • Sigmoid in a hidden layer is rarely the right default. It is fine for gates and outputs, but it makes deep training slow.
  • Initialisation matters. Weight scales must keep pre-activations in the range where the chosen function has a useful slope. This is why initialisation schemes are tied to the activation.
  • Watch for dead units. Count how many ReLU outputs are zero across a batch. If a large fraction are always zero, the learning rate or initialisation is usually to blame.

History

The first mathematical neuron, proposed by Warren McCulloch and Walter Pitts in 1943, used a hard threshold: it fired (output 1) or did not (output 0). That is a step activation, which is nonlinear but has zero derivative almost everywhere, so it cannot be trained by gradient descent. Frank Rosenblatt's perceptron in the late 1950s kept the step and learned only one layer.

Smooth activations came with training by backpropagation. The sigmoid was the usual choice through the 1980s and 1990s, partly because its smooth derivative made the method work and partly because it was already used as a model of firing rates. David Rumelhart, Geoffrey Hinton and Ronald Williams popularised backpropagation with sigmoid units in 1986.

ReLU was used in earlier work, but it became standard around 2010 to 2012. Vinod Nair and Geoffrey Hinton showed its advantages in 2010, and the deep convolutional network that won the ImageNet competition in 2012 used it heavily. Its speed and its non-vanishing gradient on the active side made very deep networks trainable in practice. Variants such as leaky ReLU, and smoother ones such as GELU, followed.

Check yourself

QuestionWhy does a network with no activation functions, however many layers it has, behave like a single layer?Show answer

Composing affine maps gives an affine map. Two layers W2(W1x+b1)+b2\mathbf{W}_2(\mathbf{W}_1\mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2 simplify to (W2W1)x+(W2b1+b2)(\mathbf{W}_2\mathbf{W}_1)\mathbf{x} + (\mathbf{W}_2\mathbf{b}_1 + \mathbf{b}_2), which is one matrix and one bias. Repeating this for any number of layers gives the same form, so the extra depth adds no expressive power.

QuestionA neuron has z=−0.8z = -0.8. What are its ReLU output and its ReLU derivative, and what does a zero derivative mean for training on this example?Show answer

The output is max⁡(0,−0.8)=0\max(0, -0.8) = 0. The derivative is 0 because zz is negative. No error signal flows back through this neuron for this input, so its incoming weights do not change from this example.

QuestionCompute σ(0)\sigma(0) and σ′(0)\sigma'(0) for the sigmoid. Why is σ′(0)\sigma'(0) the largest value the derivative ever takes?Show answer

σ(0)=1/(1+1)=0.5\sigma(0) = 1/(1+1) = 0.5, and σ′(0)=0.5×0.5=0.25\sigma'(0) = 0.5 \times 0.5 = 0.25. The derivative σ(z)(1−σ(z))\sigma(z)(1-\sigma(z)) is largest when σ(z)\sigma(z) is closest to 0.50.5, and that happens at z=0z = 0. Moving away from zero pushes σ\sigma towards 0 or 1, which makes the product smaller.

QuestionYou train a 20-layer network with sigmoid hidden units, and the early layers barely change. You inspect the activations and most sit near 0 or 1. What is happening, and what two changes would you try?Show answer

The units are saturated, so their local derivatives are close to zero, and the product of these derivatives across 20 layers is tiny. This is the vanishing gradient problem. Two reasonable changes are to switch hidden units to ReLU (or a variant such as leaky ReLU or GELU), and to use an initialisation scheme that keeps pre-activations in the non-saturated range.

Sources