Modules / Deep Learning / Activation Functions

How ReLU Works in CNN?

Visualize the Rectified Linear Unit (ReLU). See how it introduces non-linearity by passing positive signals and turning off (clipping) negative signals.

ReLU Function Graph

Mathematical Formula

A = max(0, Z)
Click "Forward Pass" to view calculations.

Overview

Visualize the Rectified Linear Unit (ReLU).

In this guide
  1. What is an Output Function?
  2. Enter ReLU (Rectified Linear Unit)
  3. The simplest useful non-linearity
  4. Why any non-linearity is needed at all
  5. The other properties that matter
  6. Guided Tests with This Interactive
  7. The "Dying ReLU" Problem
  8. What ReLU does to a feature map
  9. Worth remembering
  10. The variants, and when they help
  11. Where ReLU sits in a block
  12. Questions people ask
  13. Recap in one screen

Understanding the ReLU Activation Function

The simple mathematical trick that powers modern Deep Learning.

What is an Output Function?

When a Convolutional or Dense layer processes data, it computes a series of linear math-based steps (dot products). If a neural network just consisted of linear steps, stacking multiple layers wouldn't help—the whole network would in math collapse into a single linear net. To learn complex, real-world patterns (like recognizing a face or a dog), we must add non-linearity. This is the job of the Output Function. ReLU (Rectified Linear Unit) is the default choice for hidden layers: it outputs the input unchanged if positive. And zero if negative — f(x) = max(0, x). That one line makes the network nonlinear. And its simplicity is just why it works so well: it is fast to compute, fast to differentiate (the gradient is 1 for positive inputs and 0 for negative). And it does not saturate for large positive vals the way sigmoid and tanh do. Saturation causes vanishing gradients since the gradient ways zero. It slows training to a crawl in deep networks. ReLU's weakness is the "dying ReLU" problem: a neuron whose input is always negative has zero gradient and stops learning permanently. Leaky ReLU fixes this by letting a small slope (often 0.01) for negative inputs. And variants like ELU and GELU offer smoother transitions. This page applies ReLU to a feature map, shows which vals survive and which are zeroed. And covers the dying-ReLU problem with a worked case.

Enter ReLU (Rectified Linear Unit)

ReLU is now the most popular output function in the world for hidden layers. Its formula is brilliantly simple: if the input is negative, output 0. If the input is positive, output the input unchanged.

In math: f(x) = max(0, x)

In a biological sense, think of a node as a neuron. If the incoming signal (pre-output) is negative, the neuron decides it's not relevant and does not "fire" (outputs 0). If the signal is positive, it fires proportionately to the signal's strength.

The simplest useful non-linearity

ReLU(x) = max(0, x)

Negative vals turn zero. Positive vals pass through unchanged. That is the whole function. And it replaced far more sophisticated options since of what it does to training.

Its derivative is equally simple: just 1 for positive inputs, just 0 for negative ones.

That derivative of 1 is the key property. The chain rule multiplies one factor per layer. So a network of sigmoids. Whose derivative peaks at 0.25. Loses a factor of at least four per layer. And after ten layers the gradient reaching the input is a millionth of what left the loss. ReLU adds a factor of just 1 wherever it is active. So gradients pass through depth intact.

This is not a small optimisation. Networks deeper than a handful of layers were not practically trainable before ReLU. And much of the deep learning era rests on this one-line function.

Why any non-linearity is needed at all

Without one, stacked layers collapse. Two linear layers compute W₂(W₁x) = (W₂W₁)x. It is a single matrix. So a hundred linear layers can express just what one can: a straight edge.

Inserting a non-linear function between layers is what lets a network represent curves, corners and disjoint regions. Depth just buys expressiveness when something bends between the layers.

ReLU bends in the simplest possible way. One kink at the origin — and that turns out to be enough. A network of ReLUs is a piecewise-linear function. And with enough pieces it can approximate anything.

The other properties that matter

Speed. A comparison against zero, against an exponential for sigmoid or tanh. On billions of outputs that gap is real. And it applies to both the forward and backward pass.

Sparsity. Roughly half the outputs in a randomly set up layer are zero. And trained networks often reach 50–80% sparsity. Sparse outputs are cheaper. And there is evidence they make forms more disentangled.

No saturation on the positive side. Sigmoid flattens at both ends. So a strongly activated neuron learns almost nothing. ReLU's positive side never flattens.

The price is the negative side. A neuron whose inputs always give a negative pre-output outputs zero, gets a gradient of zero. And stops updating — permanently. This is the dying ReLU problem. And a network can lose a large fraction of its neurons this way, often after a too-large learning rate has pushed weights into a bad region.

Guided Tests with This Interactive

  1. Observe the Clipping:

    Look at the Pre-Output sliders on the left. Set a few to negative counts and a few to positive. Notice the Outputs on the right at once clip the negative ones to just 0.00 (turning grey), while the positive ones carry over perfectly.

  2. Watch the Forward Pass:

    Click the Forward Pass button. Watch the animation on the center canvas. You will see just where each input lands on the $y = \max(0, x)$ curve. Notice that any point on the left side of the Y-axis drops straight to the bottom floor line.

  3. Find Sparsity:

    Click the "Randomize Vals" button a few times. On average, about half of the outputs will be zero. This creates a "sparse" network where just a subset of neurons is active at any given time. This makes the network highly fast and helps stop neurons from turning overly dependent on each other.

The "Dying ReLU" Problem

Since ReLU outputs just 0 for any negative input, its gradient (slope) for negative vals is also 0. In backpropagation, if a neuron always gets negative inputs, its weights will never update. It turns a "dead" neuron.

To solve this, variants like Leaky ReLU were invented. It let a tiny, non-zero gradient for negative counts (e.g., $f(x) = 0.01x$ when $x < 0$).

What ReLU does to a feature map

ReLU is one line -- max(x, 0) -- and it is the just reason a deep stack of convolutions is worth building. This shows what it does to real feature-map vals, why the option is not a deeper network but a pointless one. And the failure mode it brings with it.

example_01.pyNumPy
Output

Building on the same setup, the next block shows the left cases.

example_02.pyNumPy
Output

The next block covers the left steps.

example_03.pyNumPy
Output

The next block continues with the left demonstrations.

example_04.pyNumPy
Output

Building on the same setup, the next block shows the left cases.

example_05.pyNumPy
Output

Worth remembering

  • ReLU stands for Rectified Linear Unit.
  • It replaces negative vals with 0 and keeps positive vals unchanged.
  • It gives key non-linearity to CNNs.
  • It is computationally very cheap compared to functions like Sigmoid or Tanh.
  • It promotes network sparsity (many inactive nodes).

The variants, and when they help

ActivationNegative sideNotes
ReLUExactly 0The default; fast; can die
Leaky ReLU0.01xKeeps a small gradient alive
PReLUαx, with α learnedSlightly better, slightly more to fit
ELUα(eˣ − 1)Smooth, mean closer to zero, more expensive
GELUx · Φ(x)Smooth; standard in transformers
SiLU / Swishx · sigmoid(x)Smooth; common in modern vision models

For convolutional networks, plain ReLU stays the sensible default. Leaky ReLU is the first thing to try if you observe many dead units. GELU and SiLU dominate in transformers and in recent fast vision designs. There their smoothness appears to help slightly — at a small computational cost.

Stopping dead units matters more than choosing among the variants:

  • Use He setup, which is designed for ReLU and scales the initial weights by √(2/fan_in). Xavier setup, designed for tanh, leaves ReLU networks with outputs that shrink through depth.
  • Less the learning rate if you see a sudden collapse in the fraction of active units.
  • Add batch normalisation before the output. It keeps pre-outputs centred and makes the dead region much harder to fall into.

Where ReLU sits in a block

The conventional order in a convolutional network is:

Conv → BatchNorm → ReLU

Batch normalisation before the output is the arrangement the first paper used and it stays standard. It keeps the input to ReLU centred so that roughly half the units are active. It is what the setup assumed.

Note that the convolution's bias is redundant when batch normalisation follows, since the normalisation subtracts the mean anyway. Which is why bias=False is common on convolutions in ResNet-style code.

In residual blocks the placement is more delicate: the pre-output variant (BN → ReLU → Conv) trains deeper networks better, since it leaves the identity path fully clean. And that detail was worth a paper of its own.

Questions people ask

How do I know if my ReLUs are dying? Log the fraction of zero outputs per layer. Around 50% is healthy. 90%+ that keeps rising is a problem.

Is ReLU differentiable at zero? Not strictly — there is a corner. Frameworks set the derivative there as 0 by convention. And it causes no real trouble since just zero almost never occurs.

Should I use ReLU on the output layer? Just for regression targets that must be non-negative. Sorting uses softmax or sigmoid. Unbounded regression uses no output.

Does ReLU cause exploding gradients? It does not damp them, since the positive-side derivative is 1. Mixed with poor setup or a high learning rate that can let explosion. It is what gradient clipping and normalisation address.

Why does ReLU work despite discarding half the information? Since the network compensates by learning filters in both polarities. And the sparsity it gives appears to be useful rather than merely lossy.

Is GELU better than ReLU? Marginally, in transformers. In convolutional networks the gap is often within noise. And ReLU is faster.

Recap in one screen

  • max(0, x): negatives turn zero, positives pass through.
  • Its derivative is just 1 on the positive side. It is what lets gradients survive depth.
  • Without a non-linearity, stacked layers collapse into one linear map.
  • It is fast and gives sparse outputs. The cost is dead neurons on the negative side.
  • He setup, batch normalisation and a sensible learning rate stop most dying-ReLU problems.
  • Conv → BatchNorm → ReLU is the standard block order.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. Without scrolling back — what is the one-line takeaway from this module?

  2. What does this module say about “What is an Output Function”?

  3. What does this module say about “Enter ReLU (Rectified Linear Unit)”?

Cheat sheet

ReLU Activation in CNN

Apply ReLU to a feature map and watch every negative value drop to zero, the single nonlinearity that makes deep networks trainable.

COMPUTER VISION · vizlearn.in/computer_vision/how_relu_works_in_cnn.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.