Start studying
Module 2 · Deep Learning Module contents
Module 2  ·  Card 5 / 25beginner

Activation Functions

An activation function adds non-linearity after a neuron's weighted sum.

Without one, stacking layers would collapse into a single linear function.

ReLU outputs the input if positive and zero otherwise, and is the usual default.

Sigmoid squashes into 0 to 1, and softmax turns a vector into probabilities.

The choice affects both how fast and how well the network trains.

Learn in depth ↓
Module 02 · Deep Learning · 25 concepts
In depth  /  15 lines

Activation Functions, in more detail

An activation function is applied to a neuron's weighted sum before it is passed on.

Its job is to bend the straight line the neuron would otherwise draw.

Stack two linear layers and the result is still linear, so depth would buy nothing.

One bend per layer is what makes deep networks able to model complicated things.

ReLU passes positive numbers through unchanged and turns negatives into zero.

That is almost free to compute and its gradient does not shrink for positive inputs.

Its weakness is that a neuron stuck outputting zero receives no gradient and can die.

Leaky ReLU lets a small negative slope through so the neuron keeps a way back.

GELU is a smoother version of the same shape and is standard in transformers.

Sigmoid squashes everything into the range zero to one.

It saturates at both ends, where the gradient is nearly zero and learning stops.

That saturation is why sigmoid fell out of use inside deep networks.

It survives at the output layer of a binary classifier, where a probability is wanted.

Softmax is the multi-class version, turning a row of scores into probabilities that sum to one.

Inside the network use ReLU or GELU, and choose the output activation to match the task.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Activation Functions

The four that matter, and their derivatives

What the forward pass sees is the function. What training sees is the derivative, and that is what decides whether a network learns.

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

The sigmoid squashes to the range 0 to 1. Its derivative peaks at 0.25 and falls towards zero in both directions.

tanh⁡(z)=ez−e−zez+e−z,tanh⁡′(z)=1−tanh⁡2(z)\tanh(z) = \frac{e^{z} - e^{-z}}{e^{z} + e^{-z}}, \qquad \tanh'(z) = 1 - \tanh^{2}(z)

Tanh is the same shape centred on zero, with a peak derivative of 1, which is why it beat sigmoid in hidden layers for years.

ReLU(z)=max⁡(0,z),ReLU′(z)=1[z>0]\mathrm{ReLU}(z) = \max(0, z), \qquad \mathrm{ReLU}'(z) = \mathbf{1}[z > 0]

ReLU is not a squash at all. Its derivative is exactly 1 on the positive side, so the gradient passes through unchanged however deep the stack is.

GELU(z)=z Φ(z)\mathrm{GELU}(z) = z \, \Phi(z)

GELU multiplies the input by the probability that a standard normal is below it, giving a smooth curve that is ReLU-like for large values and gently negative near zero.

Why the derivative decides everything

∂L∂a[1]∝∏l=2Lg′(z[l])\frac{\partial \mathcal{L}}{\partial \mathbf{a}^{[1]}} \propto \prod_{l=2}^{L} g'\big(z^{[l]}\big)

Backpropagation multiplies one derivative per layer, so a value below 1 at every layer shrinks the product geometrically.

With sigmoid at its best, ten layers multiply ten factors of at most 0.25, which is 10−610^{-6} before any weight is considered.

With ReLU those factors are 1 wherever the unit is active, and the product does not decay at all. That is the whole reason deep networks became trainable.

What ReLU costs

z<0  ⟹  ∂L∂z=0z < 0 \implies \frac{\partial \mathcal{L}}{\partial z} = 0

A unit pushed permanently negative receives no gradient, so it can never recover. It is dead for the rest of training.

LeakyReLU(z)=max⁡(αz,z),α≈0.01\mathrm{LeakyReLU}(z) = \max(\alpha z, z), \quad \alpha \approx 0.01

Leaky ReLU leaves a small slope on the negative side so there is always something to learn from.

ReLU is also not smooth at zero, and the subgradient used there is an arbitrary choice that in practice costs nothing.

Choosing one

  1. Hidden layers in a convolutional or fully connected network: ReLU, unless something is going wrong.
  2. Hidden layers in a transformer: GELU or SwiGLU, which is what every current model uses.
  3. Binary output: a single sigmoid, producing one probability.
  4. Multiclass output: softmax over the classes, not a sigmoid each.
  5. Regression output: nothing at all, so the network can print any real number.

The one that is not an activation function

Softmax takes a whole vector and returns a distribution over it, so it couples every unit to every other one.

The three above act on one number at a time, which is why they can be applied to a million-element tensor in one pass with no reduction.

Putting softmax in a hidden layer is almost always a mistake. It forces the layer's outputs to sum to one, which is a constraint nothing there asked for.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
Why does a neural network need non-linear activation functions?
Deep Learning

25 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close