Search

Search papers and lessons

Rectified Linear Unit (ReLU)

Level 3 · 13 min read

ReLU is the activation that outputs its input when positive and zero otherwise, a cheap bend that keeps gradients alive and made deep networks trainable.

On this page

  • Colour = topic mix
  • Neural network basics
  • Sign in to see your progress

TL;DR

The rectified linear unit, or ReLU, is the activation function ReLU(z)=max⁡(0,z)\text{ReLU}(z) = \max(0, z). A positive input passes through unchanged, and a negative input becomes zero. It is a bend with a corner at zero, and it is about as cheap to compute as an activation can be.

Its main virtue is the gradient. For positive inputs the slope is exactly 1, so error signals flow back through many layers without fading. That made deep networks practical to train once they were adopted widely in the early 2010s.

Where you'll see it: in the hidden layers of most convolutional image networks, in the feedforward blocks of many transformers, and as the default activation in textbook examples of deep learning.

Intuition

A switch that lets positive signals through

Think of a neuron that either speaks or stays quiet. If its weighted sum is negative, it stays silent and sends zero. If the sum is positive, it sends exactly that amount. The neuron is a one-sided ramp: flat on the left, a straight line on the right.

That shape does two jobs at once. The corner at zero is a nonlinearity, so stacked layers no longer collapse into one linear map (see Activation Function). The flat region is a kind of gate: an input that a unit does not care about produces no output, and many units in a layer end up silent for any given input. The network becomes sparse, which is one reason it is efficient and sometimes easier to interpret.

Why the slope matters more than the bend

Compare ReLU with the older sigmoid. The sigmoid squashes every input into the interval from 0 to 1, and it is nearly flat at both ends. Its slope is at most 0.25 anywhere, and it is close to zero for large positive or negative inputs.

During training, the error signal reaching an early layer is a product of slopes, one per layer. A product of many numbers below 0.25 is tiny, and the early layers learn almost nothing. This is the vanishing gradient problem. ReLU's slope on its active side is exactly 1, so the product does not shrink on the active path. The Derivative is the thing that matters, and ReLU is designed around it.

−3−2−101230123zoutput
  • ReLU
  • sigmoid
ReLU is zero on the left and a straight line on the right. The sigmoid saturates toward 0 and 1 at both ends, where its slope nearly vanishes.

The math

Definition

Definition: Rectified linear unit
ReLU(z)=max⁡(0,z)={zif z>0,0if z≤0.\text{ReLU}(z) = \max(0, z) = \begin{cases} z & \text{if } z \gt 0, \\ 0 & \text{if } z \le 0. \end{cases}

Here zz is the pre-activation of a neuron (its weighted sum plus bias). The output is non-negative.

The derivative is

ddzReLU(z)={1if z>0,0if z<0.\frac{d}{dz}\text{ReLU}(z) = \begin{cases} 1 & \text{if } z \gt 0, \\ 0 & \text{if } z \lt 0. \end{cases}

At exactly z=0z = 0 the function has a corner and no derivative. Implementations choose a convention, usually 0, and the choice has little effect in practice because exact zeros are rare with real-valued inputs.

The neuron in a layer

A layer of ReLU units computes, for input vector x\mathbf{x} and weight matrix W\mathbf{W} with bias b\mathbf{b},

h=ReLU(Wx+b),\mathbf{h} = \text{ReLU}(\mathbf{W}\mathbf{x} + \mathbf{b}),

where ReLU applies elementwise. Each row of W\mathbf{W} is one neuron's weight vector, and each entry of h\mathbf{h} is that neuron's output.

The chain rule through a ReLU

Training with Backpropagation needs the gradient of the loss L\mathcal{L} with respect to the pre-activation zz. If a=ReLU(z)a = \text{ReLU}(z), the chain rule gives

∂L∂z=∂L∂a⋅ReLU′(z).\frac{\partial \mathcal{L}}{\partial z} = \frac{\partial \mathcal{L}}{\partial a} \cdot \text{ReLU}'(z).

So the upstream gradient passes through unchanged when z>0z \gt 0, and is blocked entirely when z<0z \lt 0. Nothing is multiplied or shrunk on the active side. This is the operation that replaces the sigmoid's factor of at most 0.25 in the backward pass.

Why sparsity appears

If the pre-activations are symmetric around zero, roughly half of the units in a layer are active for a typical input. The output of a ReLU layer is therefore a sparse vector with many exact zeros. Sparsity reduces the number of multiplications that matter and makes each unit respond to a narrower set of inputs.

Initialization matters

A ReLU zeroes half its input in expectation, so the variance of activations halves at every layer unless the weights compensate. This is why He initialization draws weights with variance 2/nin2/n_{\text{in}}, where ninn_{\text{in}} is the number of inputs to the layer. The factor of 2 restores the variance that ReLU removes. Using the older Xavier initialization, designed for symmetric activations such as tanh, with ReLU is a common mistake and makes deep networks harder to train.

Worked examples

Example
Forward and backward through one unit

A neuron has pre-activations z=−1.5z = -1.5, z=0.5z = 0.5 and z=2.0z = 2.0 for three different inputs.

  1. Forward: ReLU(−1.5)=0\text{ReLU}(-1.5) = 0, ReLU(0.5)=0.5\text{ReLU}(0.5) = 0.5, ReLU(2.0)=2.0\text{ReLU}(2.0) = 2.0.
  2. Local derivative: ReLU′(−1.5)=0\text{ReLU}'(-1.5) = 0, ReLU′(0.5)=1\text{ReLU}'(0.5) = 1, ReLU′(2.0)=1\text{ReLU}'(2.0) = 1.
  3. Suppose the upstream gradient ∂L/∂a\partial \mathcal{L}/\partial a is 33 for each input. The gradients with respect to zz are 00, 33 and 33.

The first input contributes nothing to learning through this unit. The other two pass the full signal of 3 backwards. The interpretation is that the unit learns only from inputs where it is active.

Example
A dead unit

A unit has weight w=1w = 1 on a single input and bias b=−5b = -5. The input xx ranges over [−2,2][-2, 2].

  1. Pre-activation: z=wx+b=x−5z = wx + b = x - 5. Over the range, zz runs from −7-7 to −3-3.
  2. Every zz is negative, so ReLU(z)=0\text{ReLU}(z) = 0 for every input in the range.
  3. The derivative is 00 everywhere in the range, so the gradient with respect to ww and bb is zero for every training example.

The weights never change, because the gradient is zero whatever the data. The unit is permanently dead on this data. This failure is the main practical weakness of ReLU, and the fixes are covered in the failure modes below.

Example
Gradients through twenty layers

Compare the best-case backward factor for a deep stack. For a sigmoid, the derivative is at most 0.250.25, at the centre of its range. For a ReLU, the derivative is 11 on the active side.

  1. Twenty sigmoid layers: 0.2520=(0.2510)2≈(9.5×10−7)2≈9.1×10−130.25^{20} = (0.25^{10})^2 \approx (9.5 \times 10^{-7})^2 \approx 9.1 \times 10^{-13}.
  2. Twenty ReLU layers, all active: 120=11^{20} = 1.

Even in the sigmoid's best case the gradient reaching the first layer is about a trillionth of the original, so the first layers barely learn. A ReLU network keeps the signal at full strength on any path where its units are active. The comparison is an upper bound for the sigmoid, since real sigmoid slopes are smaller than the maximum, and it is the reason networks with sigmoid hidden units were so hard to train at depth.

Tradeoffs and failure modes

ReLU is a good default for hidden layers. It is fast, sparse, and avoids saturation on the positive side. It is less suitable for the output layer when the target is a probability or a bounded quantity, where a sigmoid or softmax is needed.

SituationProblemBetter choice
Hidden layers of a deep networkDead units if pre-activations drift negative[[parametric-relu
Output that must be a probabilityReLU is unbounded and not a probabilitySigmoid or softmax
Transformer feedforward blocksSmooth curvature may train betterGELU or a gated variant
Very deep residual networksMostly fine with ReLU and normalizationKeep ReLU with [[residual-connection
Non-zero-centred outputsActivations are all non-negative, which can slow updatesBatch or layer normalization

The main failure mode is the dying ReLU. If a unit's pre-activation becomes negative for every input, its gradient is zero and it cannot recover, as in the dead-unit example. A large learning rate or a poorly scaled bias can push many units into this state. Leaky ReLU fixes this by giving the negative side a small slope α\alpha, typically 0.01, so the gradient is never exactly zero. Parametric ReLU learns α\alpha from data.

A second issue is that ReLU is not differentiable at zero, and its outputs are not zero-centred. Both are minor in practice, because the convention at zero rarely matters and normalization layers correct the offset. A third issue is that the output is unbounded, so a large activation can dominate the next layer, which normalization again helps with.

History

The rectifier idea is older than deep learning. Kunihiko Fukushima's visual pattern models in the late 1960s used threshold-like units of this kind, and the function was known to neuroscientists as a model of firing rates.

Its move into machine learning came in 2010 and 2011. Vinod Nair and Geoffrey Hinton showed that rectified units improved restricted Boltzmann machines, and Xavier Glorot, Antoine Bordes and Yoshua Bengio argued that rectifiers gave sparse representations that trained well in deep networks without pretraining. The decisive result was AlexNet in 2012, the image classifier by Alex Krizhevsky, Ilya Sutskever and Geoffrey Hinton, which used ReLU and trained much faster than tanh networks of similar size. It caught on because it worked, because its gradients did not vanish, and because it was cheap enough to run on GPUs.

Later variants followed: leaky ReLU, parametric ReLU in 2015, and smoother alternatives such as GELU in 2016 that now dominate transformers. ReLU remains the baseline that new activations are compared against.

Check yourself

QuestionWhat is ReLU(−3.2)\text{ReLU}(-3.2), ReLU(0)\text{ReLU}(0) and ReLU(4.5)\text{ReLU}(4.5), and what is the derivative at z=4.5z = 4.5?Show answer

ReLU(−3.2)=0\text{ReLU}(-3.2) = 0, ReLU(0)=0\text{ReLU}(0) = 0 and ReLU(4.5)=4.5\text{ReLU}(4.5) = 4.5. The derivative at 4.54.5 is 11, because the function is the identity on the positive side. The value at 00 is the boundary case: the function is zero there, and the derivative is conventionally taken as 0.

QuestionA ReLU unit has upstream gradient −2-2 and pre-activation z=−0.7z = -0.7. What gradient reaches its weights, and why does this matter for learning?Show answer

The gradient with respect to zz is −2×ReLU′(−0.7)=−2×0=0-2 \times \text{ReLU}'(-0.7) = -2 \times 0 = 0. Nothing reaches the weights, so this input does not change the unit. If this happens for every input, the unit is dead, and this is the reason leaky ReLU and careful initialization exist.

QuestionWhy does He initialization use variance 2/nin2/n_{\text{in}} instead of 1/nin1/n_{\text{in}} for ReLU layers?Show answer

A ReLU sets about half its inputs to zero, so the second moment of the activations is roughly halved at each layer. Doubling the weight variance compensates exactly for that loss, which keeps the scale of activations and gradients stable with depth. A 1/nin1/n_{\text{in}} scheme would shrink the signal by about half per layer, and across many layers the effect compounds.

QuestionYou train a 30-layer network with ReLU hidden units, and loss stops decreasing after a few hundred steps. The first layers' gradients are essentially zero, while the last layers still change. Name two plausible causes and a fix for each.Show answer

One cause is many dead units: check the fraction of units with zero activations on a batch, and if it is very high, lower the learning rate or switch to leaky or parametric ReLU. The other is poor initialization: if the weights are too small, or the layers are not normalized, the signal can shrink through depth; use He initialization and add batch or layer normalization with residual connections. Both fixes address the core symptom, which is that the early layers receive no usable gradient.

Sources