Sitemap
Anandha L Ranganathan

Anandha L Ranganathan

ReLU

--

ReLU(x)=max(0,x)

That’s the whole function. If the number is positive, keep it. If it’s negative, replace it with zero. Done.

ReLU is an activation function applied in Neurons inside the NeuralNet. In neuron, you first form the pre-activation

In a neuron you first form the pre-activation

 Z = W.X +b  

and then you apply ReLU .

The name stands for Rectified Linear Unit — three words that each earn their place, which I’ll come back to at the end.

Most people’s reaction on first seeing this is some version of “that can’t be it.” It looks like it does almost nothing. No curves, no e^x, no division. You could implement it in your head.

And yet it’s the reason depth became trainable at all. Before ReLU, networks deeper than a handful of layers were basically untrainable. After it, they weren’t. It wasn’t the only thing that had to go right — GPUs, more data, dropout, better initialization, and later residual connections all mattered — but it was the one that removed the wall.

Here’s the claim I want to defend with arithmetic:

Remove ReLU from a 50-layer network and you don’t get a slightly worse 50-layer network. You get a 1-layer network. Not metaphorically — algebraically, provably, the same function.

Let’s show that.

First, a worked example

Feed ReLU a list of six numbers. In a real network these input would be the outputs of a layer, before the activation is applied. They’re called pre-activations.

Press enter or click to view image in full size

Notice two things.

The positive numbers are untouched. Not squashed, not scaled, not compressed. 7.2 comes out as 7.2. This matters enormously and we’ll come back to it.

Half the outputs are now exactly zero. Three of six. In a real network with random weights it’s roughly half the neurons, on every input. Your layer’s output just became sparse — mostly zeros.

The analogy: a one-way valve

ReLU is a diode. Or if you prefer plumbing, a one-way valve.

Push water forward through a one-way valve and it passes through at full pressure — the valve doesn’t restrict it at all. Try to push water backward and it slams shut. Nothing gets through. Not “a reduced amount.” Nothing.

That’s ReLU exactly. Positive signal passes through at full strength, undistorted. Negative signal is blocked completely.

Hold onto the “at full strength” part. The functions ReLU replaced — sigmoid and tanh — were more like a narrow nozzle: they let signal through but squashed it into a small range no matter how hard you pushed. That squashing turned out to be fatal, for reasons we’ll get to.

Why ReLU exists at all: layers collapse without it

This is the part most tutorials skip, and it’s the only part that really explains ReLU.

A neural network layer does two things: multiply by some weights, add a bias. In one dimension, that’s just a straight line:

output = w · input + b.

Let’s stack two of them and see what happens.

  • Layer 1: h = 3x + 1
  • Layer 2: y = 2h − 5

Feed in x = 2:

Layer 1:  h = 3(2) + 1 = 7
Layer 2: y = 2(7) − 5 = 9

Output: 9.

Now let’s do the algebra instead of the arithmetic. Substitute layer 1 into layer 2: call this the singleLine formulae.

y = 2(3x + 1) − 5
= 6x + 2 − 5
= 6x − 3

y = 6x − 3. One line. One layer. Check it: 6(2) − 3 = 9. Same answer.

Let’s verify across several inputs: Here the singleLine and twoLayerOutput values are same.

Press enter or click to view image in full size

Identical, every time. Your two-layer network was never a two-layer network. It was 6x − 3 wearing a costume.

And this doesn’t stop at two. Stack a hundred layers and you can collapse them the same way, one substitution at a time, until you’re left with a single y = wx + b. All those millions of parameters buy you exactly two numbers’ worth of expressive power.

The real version. With matrices instead of single numbers it’s the same story: two layers W₂(W₁x + b₁) + b₂ collapse to one layer with W = W₂W₁ and b = W₂b₁ + b₂. I ran a 2×2 example and got the collapsed weights [[2, 11], [−4, 11]] and bias [−7, −3] — which reproduce the two-layer output exactly. Nothing about the collapse depends on the dimensions.

The 50-layer version, actually run

I promised 50 layers, so here it is. I built a network with 8 inputs, 49 hidden layers of width 64, and 3 outputs — 200,451 parameters — with no activation functions at all. Then I collapsed all 50 weight matrices into a single W and b by repeated substitution, which leaves 27 parameters (a 3×8 matrix plus a 3-vector).

Running 2,000 random inputs through both:

Press enter or click to view image in full size

The disagreement is floating-point rounding noise accumulated over fifty matrix multiplications. The two functions are the same function.

Get Anandha L Ranganathan’s stories in your inbox

Join Medium for free to get updates from this writer.

200,451 parameters bought exactly 27 parameters’ worth of expressive power. The other 200,424 were decoration.

Now insert ReLU between every pair of layers and change nothing else. The same network diverges from that collapsed line by up to 6.62 on the same inputs — the same size as the outputs themselves. On one sample input the linear version gives [−0.882, 3.053, 0.384] and the ReLU version gives [−0.070, −0.016, −0.134]. Not a small correction. A different function.

Now put ReLU in the middle

Same two layers. One change: apply ReLU to layer 1’s output before passing it on.

  • Layer 1: h = ReLU(3x + 1)
  • Layer 2: y = 2h − 5
Press enter or click to view image in full size

Look at the bottom two rows. For positive inputs the network still behaves like 6x − 3 — ReLU wasn’t blocking anything, so nothing changed. But once 3x + 1 goes negative, ReLU clamps it to zero and the output flatlines at −5 instead of continuing down to −9, −21, and beyond.

You can find the exact turning point. 3x + 1 = 0 when x = −1/3. So the function is:

  • y = 6x − 3 for x > −1/3
  • y = −5 for x ≤ −1/3

That’s a bend. A kink — a point where the function is continuous but its slope jumps, from 0 on the left to 6 on the right. And a bend is something no single straight line can ever produce, no matter how you choose w and b. The collapse is broken. Your two-layer network is finally, genuinely, two layers.

That’s what ReLU is for. Not to “add nonlinearity” as an abstract virtue — to stop your layers from algebraically eating each other.

A warning worth absorbing: forgetting an activation function is silent. Nothing crashes, the loss goes down, the model trains. You’ve just shipped linear regression with two hundred thousand extra parameters.

ReLU units are hinges

One ReLU gives you one bend. Combine a few and you can build shapes.

Take three ReLUs, offset from each other, and add them with these weights:

Let’s evaluate it by hand at x = 1.5:

ReLU(1.5)     = 1.5      → contributes  +1.5
ReLU(1.5 − 1) = 0.5 → contributes −1.0 (weight −2)
ReLU(1.5 − 2) = 0 → contributes 0
total = 0.5
Press enter or click to view image in full size
Press enter or click to view image in full size

That’s a triangle — up from 0 to a peak of 1 at x = 1, back down to 0 at x = 2, flat everywhere else. Built out of three copies of the world’s dumbest function.

Look at the last row before you move on. At x = 10 the three terms are 10, −18 and 8. Three large numbers that sum to exactly zero. The triangle isn’t flat out there because the units have finished and gone quiet — every one of them is still running at full volume, and they cancel.

That’s why the middle weight is −2 and not −1. Each ReLU is an instruction: from my threshold onward, change the slope by my weight. The slope at any point is the running total of every instruction switched on so far:

Press enter or click to view image in full size

If you find that mechanism familiar, it’s the same one as progressive income tax. With a 0% band to 10,000, 10% to 30,000, and 25% above that, your total tax is exactly 0.10·ReLU(I − 10000) + 0.15·ReLU(I − 30000) — and the weights are 0.10 and 0.15, the increments in the marginal rate, not the rates themselves. At an income of 40,000 both the formula and the bracket-by-bracket calculation give 4,500.

This is the whole game. Every ReLU contributes one hinge. Stack enough hinges and you can approximate any shape you like, the way a paper fan approximates a curve with straight folds. The network’s job during training is learning where to put the hinges — the thresholds are the biases and the slope changes are the weights.

It also tells you exactly what kind of function a ReLU network is: piecewise linear. Straight pieces joined at bends. Never a true curve — just enough straight segments, small enough, to be indistinguishable from one at the scale you care about. (Strictly the pieces are affine, not linear: 6x − 3 doesn’t pass through the origin. “Piecewise linear” is the universal term anyway.)

One consequence worth knowing: a ReLU network has zero second derivative almost everywhere. If you ever need genuine curvature — physics-informed networks, for instance — this is a real obstruction, not a technicality.

Why ReLU beat sigmoid: the gradient survives

Before roughly 2011, the standard activations were sigmoid (an S-curve squashing everything into 0 to 1) and tanh (the same shape, squashing into −1 to 1). Both were smooth, differentiable everywhere, and looked more biologically plausible. Both also made deep networks impossible to train.

Here’s why, and you need one idea first.

Networks learn by backpropagation: an error signal travels backward from the output through every layer, and each layer multiplies it by its own local slope. Ten layers means the signal gets multiplied by ten slopes in a row.

Now — sigmoid’s slope never exceeds 0.25. That’s its maximum, right at the center. Everywhere else it’s smaller, and out in the tails it’s nearly zero.

So multiply 0.25 by itself repeatedly:

Press enter or click to view image in full size

And remember 0.25 is the best case. Realistically a sigmoid neuron sits somewhere in its tails: sigmoid(6) has a slope of about 0.0025. Multiply that ten times and you’re at 10⁻²⁶.

The derivative, and the awkward question at zero

ReLU’s slope is trivial:

Press enter or click to view image in full size

Note what’s missing: x = 0. The function has a sharp corner there and the derivative genuinely does not exist — the slope is 0 approaching from the left and 1 approaching from the right, and there’s no third answer that reconciles them.

And finally

Rectified Linear Unit.

Three words, and each one is doing real work:

Rectified. Borrowed straight from electrical engineering. A rectifier is a circuit that converts alternating current to direct current by blocking the negative half of the waveform and letting the positive half through. That is exactly max(0, x). The archetypal rectifier is a diode.

Linear. The positive half is a genuine straight line — y = x, slope exactly 1. No squashing, no saturation, no curve. This is the word that distinguishes ReLU from everything it replaced: sigmoid and tanh are nonlinear everywhere, which is what made their slopes shrink and gradients die. ReLU is linear on the part it lets through.

Unit. Old neural-network vocabulary for a single neuron — “hidden units,” “output units.” Strictly, the unit is the neuron and max(0, x) is its activation function, so calling the function “ReLU” .

--

--