All posts

Activation functions

/AI/4 min read

Without a bend between layers, a deep network is one linear layer in disguise. Which bend matters too: a sigmoid's slope is at most 0.25, so ten sigmoid layers can shrink a gradient by a factor of a million — 9.54 × 10⁻⁷. ReLU's slope is 1, which is most of why deep networks became trainable.

Every layer of a neural network computes a weighted sum of its inputs and then passes the result through an activation function. The weighted sum is the part that learns; the activation is the part that makes learning worth anything.

Why there has to be one

A weighted sum is a linear operation. Two linear operations in a row are still one linear operation: multiplying by one matrix and then another is the same as multiplying by their product. So a hundred layers with no activation between them collapse into a single layer. However deep the network looks, it can only draw straight lines and flat planes — it cannot even learn XOR.

An activation function bends the output of each layer. With a bend between every pair of layers, stacking finally means something: each layer can build curved, folded, combined shapes out of the previous layer's output.

The common ones

inputsigmoidtanhReLUGELU
−20.119−0.9640−0.046
00.5000.00000.000
20.8810.96421.954

Sigmoid, 1 / (1 + e⁻ˣ), squashes any input into (0, 1). It reads naturally as a probability and is still used for that at the output of binary classifiers.

Tanh squashes into (−1, 1) and is centred on zero, which made it preferable to sigmoid in hidden layers for a long time.

ReLU, max(0, x), passes positive inputs unchanged and zeroes negative ones. It is almost trivially cheap and became the default for deep networks.

GELU and SiLU/Swish are smooth versions of ReLU: close to the identity for large positive inputs, close to zero for large negative ones, with a soft curve between that lets small negative values through. They are the standard choice in transformers, often inside gated variants like SwiGLU.

Softmax is different in kind: it acts on a whole vector, turning a list of scores into probabilities that sum to 1. Scores of [2.0, 1.0, 0.1] become [0.659, 0.242, 0.099]. It is used at the output of classifiers and language models, and inside attention.

Why ReLU took over: the slope

Training passes gradients backward through every layer, and at each layer the gradient is multiplied by the activation function's slope at that point. Over many layers those multiplications compound.

The sigmoid's slope is never more than 0.25 (at x = 0) and falls toward zero for large inputs, where the function flattens out — it saturates. Even in the best case, ten sigmoid layers multiply the gradient by:

0.25¹⁰ = 9.54 × 10⁻⁷

and twenty layers by about 10⁻¹². The early layers receive a gradient a million times weaker than the later ones and barely learn. This is the vanishing gradient problem, and it is why deep networks were so hard to train before around 2010.

Tanh is better — its slope reaches 1 at zero — but at an input of 1 its slope is already 0.42, and ten such layers give 0.000171.

ReLU's slope is exactly 1 for every positive input. Gradients pass through active units without shrinking at all. Combined with careful weight initialisation and normalisation layers, that is much of what made very deep networks trainable.

THREE BENDS, THREE SLOPES sigmoid tanh ReLU 0 maximum slope sigmoid 0.25 tanh 1 at 0, 0.42 at 1 ReLU 1 when active after 10 layers sigmoid 9.54 × 10⁻⁷ ReLU 1 gradients are multiplied by the slope at every layer on the way back
Saturating curves flatten at the ends, and a flat curve passes almost no gradient.

ReLU's own problem, and the fix

ReLU's slope is zero for negative inputs. A neuron whose weighted sum is negative for every input in the data outputs zero always, receives no gradient, and can never recover. These are dead neurons; a bad learning rate can kill a substantial share of a network.

Variants address this:

  • Leaky ReLU gives negative inputs a small slope, such as 0.01, so a gradient always flows.
  • GELU and SiLU curve smoothly through zero, passing a little gradient for moderately negative inputs and none only for very negative ones. Their smoothness also tends to make optimisation slightly better in large models.

Choosing one

  • Hidden layers of most networks: ReLU, or a smooth relative.
  • Transformers: GELU or SiLU, usually in a gated form (SwiGLU) in the feed-forward layers.
  • Binary output: sigmoid, giving a probability.
  • Multi-class output: softmax over the class scores.
  • Regression output: no activation — the raw weighted sum is the prediction.

What to take away

An activation function bends each layer's output, without which any stack of layers is equivalent to one linear layer. Sigmoid and tanh squash into fixed ranges and saturate; their small slopes compound during backpropagation, so ten sigmoid layers can shrink gradients to 9.54 × 10⁻⁷ of their size.

ReLU passes gradients unchanged through active units, which is a large part of why deep learning became practical. Its smooth successors, GELU and SiLU, are now standard in transformers, and softmax remains the way scores become probabilities at the output.