Activation functions: sigmoid, tanh, ReLU, GELU, softmax

A neuron in a neural network multiplies its inputs by weights, adds them up, and then passes the sum through one last function. That function is the activation function. Drag along each function and its slope side by side, then see step by step why, without it, stacking layers only ever gives you one straight line.

Short answer. An activation function bends what a neuron computes (the weighted sum of its inputs). Without it, any number of layers acts like a single layer: one straight-line (linear) relation. Hidden layers usually use ReLU or GELU. The output layer depends on the task: nothing when you predict a number, sigmoid for yes or no, and softmax to pick one of many.

Pick a function, then drag across the picture or type a number from −5 to 5 into the x box. The orange dashed line in the top panel is the tangent at that point, and how steep it is shows up in the bottom panel. For Leaky ReLU the slope on the negative side is drawn as 0.1 so you can see it (the PyTorch default is 0.01).

Try this. ① The page opens on sigmoid at x = 2: value 0.881, slope 0.105. Set x to 0 and the slope is at its largest, 0.25. Set it to 5 and you get value 0.993, slope 0.007. The value is stuck near 1 and hardly moves. ② Switch to tanh. The slope at x = 0 is 1, but at x = 3 you get value 0.995, slope 0.01: the same thing happens. ③ On ReLU, set x = −2: both the value and the slope are 0. Switch to Leaky ReLU and you keep value −0.2, slope 0.1. ④ On GELU, x = −0.8 gives −0.169, a little below 0 (its lowest point is about −0.17). The slope at x = 2 is 1.085, more than 1.

Definitions

The text below explains with pictures and experiments. The textbook definitions are as follows. z is what one neuron gets by multiplying its inputs by weights, adding them up, and adding the bias.

activation function
The function f that a neuron applies to z, made from inputs x₁, …, xₙ, weights w₁, …, wₙ and bias b, to decide its output a. Every neuron in a layer applies the same f on its own.

a=f(z),z=∑j=1nwjxj+b

sigmoid
The function below, which sends every real number to a value between 0 and 1. Its derivative can be written with the function's own value, and its largest value is 1/4, at z = 0.

σ(z)=11+e−z

σ′(z)=σ(z)(1−σ(z))

tanh (hyperbolic tangent)
The hyperbolic tangent, which sends every real number to a value between −1 and 1. Written with the sigmoid it is 2σ(2z) − 1, and its derivative has its largest value 1 at z = 0.

tanh(z)=ez−e−zez+e−z=2σ(2z)−1

tanh′(z)=1−tanh2(z)

ReLU and Leaky ReLU (rectified linear unit)
ReLU is the function below. Its derivative is 1 for z > 0 and 0 for z < 0, and it has no derivative at z = 0. Leaky ReLU gives z when z > 0 and αz otherwise, where α is a small positive constant.

ReLU(z)=max(0,z)

GELU (Gaussian error linear unit)
The input z times Φ(z), the cumulative distribution function of the standard normal distribution, that is, the chance that a standard normal value is z or less.

GELU(z)=zΦ(z)

softmax
The function that takes K scores (logits) z = (z₁, …, zK) and gives the value below for each slot. All results are greater than 0 and add up to 1, so they are used as the probabilities of K classes.

softmax(𝐳)i=ezi∑j=1Kezj

1. What an activation function is

One neuron in a neural network multiplies its inputs by weights, adds them, adds a bias, and gets z = w₁x₁ + w₂x₂ + … + b. Then it applies a function f and sends a = f(z) to the next layer. That f is the activation function. It takes one number and returns one number, and every neuron in a layer applies the same f on its own.

The slope f′(z) matters as much as the value. During training, backpropagation sends the error backward from the output toward the input, and each neuron it passes through multiplies it by f′(z) at that point. If the slope is 0, nothing gets through. That is why lab ① shows the value and the slope together.

2. How the formula comes together: why it has to bend

Here is a tiny network: one input x, two hidden neurons, one output y. The weights are fixed at W₁ = (1, −1), b₁ = (−1, −1), W₂ = (1, 0.5), b₂ = 0 (a scaled-down toy that was not trained). Across is x, and up is what each layer outputs.

  1. First layer: h = W₁x + b₁. From x it makes h₁ = x − 1 and h₂ = −x − 1. Both are straight lines.
  2. Second layer: y = W₂(W₁x + b₁) + b₂. y = 1·h₁ + 0.5·h₂. Multiply straight lines by numbers and add them, and you get a straight line again.
  3. Merge: y = (W₂W₁)x + (W₂b₁ + b₂). Open the brackets: W₂W₁ = 1·1 + 0.5·(−1) = 0.5 and W₂b₁ + b₂ = −1.5, so y = 0.5x − 1.5. That is exactly one layer with weight 0.5 and bias −1.5. A matrix times a matrix is just another matrix, so even ten layers merge into one this way. Why: doing "multiply and add" (a linear map) any number of times is still a single "multiply and add".
  4. Put f in between: y = W₂·f(W₁x + b₁) + b₂. With ReLU between the layers, h₁ bends at x = 1 and h₂ bends at x = −1. So y is flat at 0 in the middle (−1 to 1) and rises on both sides. Now you cannot merge it into a single (W₂W₁). f is not a straight line, so the brackets do not open.

Try this. At steps ② and ③ the black dots at x = −3, 0, 3 read −3, −1.5, 0, and in ③ the orange dashed line (one layer) sits exactly on the black line. At step ④ the same spots read 1, 0, 2. The gray dashed line is the straight line you get without f.

ReLU bends in only one place, but with more neurons you get more bends, and pieces of straight lines can follow a curve. Chapter 6 of the textbook Deep Learning notes that the theorem saying a wide enough hidden layer can get as close as you like to a broad range of functions (the universal approximation theorem) has been proved for a wide class of activation functions, including ReLU.

3. The five functions and their traits

FunctionFormulaOutput rangeLargest slopeNotes
Sigmoidσ(z) = 1 ÷ (1 + e^(−z))0 to 10.25 (z = 0)Saturates at both ends. Always positive
tanhtanh(z) = 2σ(2z) − 1−1 to 11 (z = 0)A stretched, shifted sigmoid. Centered on 0. Saturates at both ends
ReLUmax(0, z)0 and up1 (z > 0)Cheapest to compute. Slope 0 on the negative side
Leaky ReLUz if z > 0, else α·zno limit1 (z > 0), α below 0Keeps a small slope α on the negative side
GELUz · Φ(z)about −0.17 and upabout 1.13 (z = √2)A smooth version of ReLU. Dips a little below 0 near 0

Φ(z) is the chance that a standard normal distribution gives a value of z or less. The GELU paper explains it this way: ReLU lets an input through or blocks it based on its sign, while GELU weights the input by its value (by Φ(z)). The slopes are: σ′(z) = σ(z)·(1 − σ(z)), tanh′(z) = 1 − tanh(z)², and for ReLU 1 on the positive side and 0 on the negative side.

4. Work it out by hand

Feed x = −3, 0 and 3 into the tiny network from section 2. First find the first-layer values z₁ = x − 1 and z₂ = −x − 1, then compare y without f against y with ReLU.

xz₁ = x − 1z₂ = −x − 1no f: z₁ + 0.5·z₂ReLU: f(z₁) + 0.5·f(z₂)
−3−42−4 + 1 = −30 + 1 = 1
0−1−1−1 − 0.5 = −1.50 + 0 = 0
32−42 − 2 = 02 + 0 = 2

Without f you get −3, −1.5, 0: each time x goes up by 3, y goes up by 1.5, so all three sit on one line (y = 0.5x − 1.5). With ReLU you get 1, 0, 2: y drops by 1 from −3 to 0 and rises by 2 from 0 to 3, so no single straight line can pass through them.

5. Saturation and dead ReLUs

Saturation: the slope sticks to 0

When the input to sigmoid or tanh is very large or very small, the value sticks to 1 (or 0, or −1) and barely changes. This is called saturation. Where it saturates, the slope is almost 0 (0.007 for sigmoid at x = 5), so the signal sent back by backpropagation nearly vanishes. The slope of sigmoid is at most 0.25, so after ten sigmoid neurons the activation slopes alone multiply to at most 0.25¹⁰ ≈ 0.00000095. When the early layers get slopes this small and stop learning, it is called the vanishing gradient problem.

Dead ReLU: a neuron that always outputs 0

On the positive side the slope of ReLU is always 1, so it does not saturate there. But for a negative input both the value and the slope are 0. If a neuron's z becomes negative for every example, it always outputs 0, its slope is always 0, and the weights in front of it never change again. This is called a dead ReLU. It can happen when a bias drifts far below 0, or when the learning rate is so large that the weights jump a long way in one step. Leaky ReLU and GELU keep some slope on the negative side, with no stretch where the slope is exactly 0, so this happens less.

6. The output layer: sigmoid and softmax

Pick the output activation to match the shape of the answer. If you predict any number, such as a house price, apply nothing. For a single yes or no, sigmoid gives a probability between 0 and 1 (the same shape as logistic regression). To pick one of many, softmax gives a probability for each option.

Softmax takes several scores and turns them into softmax(z)ᵢ = e^(zᵢ) ÷ (e^(z₁) + e^(z₂) + …). Every result is above 0 and they add up to 1. For scores (2, 1, 0): e² ≈ 7.389, e¹ ≈ 2.718, e⁰ = 1, total 11.107, so the probabilities are (0.665, 0.245, 0.09). A bigger score gives a bigger probability, and adding the same number to every score changes nothing. For training it is usually paired with cross-entropy. PyTorch's CrossEntropyLoss expects the scores before softmax (logits), so you do not add your own softmax at the end of the model.

7. When to use which

Hidden layers

OptionGoodBadUse it when
ReLUCheap. Slope 1 on the positive side, so slopes shrink less in deep networksSlope 0 on the negative side. Neurons can dieThe default first choice. Most networks, including convolutional ones
Leaky ReLUKeeps a slope on the negative side, so neurons do not dieOne more number (α) to choose. Not always better than ReLUMany neurons always output 0 with ReLU
GELUSmooth. No stretch where the slope is exactly 0A bit more work to compute than ReLUFollowing a transformer-style design
tanhCentered on 0, kept between −1 and 1Saturates at both ends. Vanishing slopes in deep networksInside recurrent networks (such as LSTM), or when values must stay in −1 to 1
SigmoidBetween 0 and 1, good as a gate ("how far to open")Slope at most 0.25, saturates, always positiveRarely in deep hidden layers. Gates in an LSTM

Output layer

OptionGoodBadUse it when
None (as is)Can output any numberDoes not keep values in a rangePredicting a number, like a price or a temperature (regression)
SigmoidA separate probability from 0 to 1 for each outputThe outputs do not add up to 1One yes or no, or several labels that can be true at once
SoftmaxThe probabilities add up to 1Only right when exactly one option is correct. Costly with a huge number of optionsPicking one of many (multi-class classification), picking the next word

Where it is used

FAQ

What happens if I remove the activation function?

However many layers you stack, the whole thing acts like a single layer. Multiplying and adding twice can always be rewritten as multiplying and adding once (W₂(W₁x) = (W₂W₁)x). So the network can only learn a straight-line (linear) relation between input and output.

ReLU has a corner at 0, so it has no derivative there. Is that a problem?

No. During training a value rarely lands exactly on 0, and libraries just use the slope from one side (0 or 1) at that point. Chapter 6 of the textbook Deep Learning describes this. The lab on this page uses 0 at 0.

Which one should I pick for hidden layers?

Start with ReLU. It is cheap and works well most of the time. If you are following a known design such as a transformer, use the function that design uses (often GELU). If many neurons output 0 all the time during training, try Leaky ReLU. Sigmoid and tanh are rarely used in the hidden layers of deep networks.

Words

Plain words used hereTextbook term
The function that bends a neuron's outputactivation function, nonlinearity
The weighted sum zpre-activation, logit (at the output layer)
Straight line (only multiply and add)linear, affine transformation
The added number bbias
Value stuck at an end, slope near 0saturation
Slopes shrinking toward the early layersvanishing gradient
A neuron that always outputs 0dying ReLU, dead neuron
Scores to probabilities that add up to 1softmax
A wide layer can imitate almost any functionuniversal approximation theorem
Predicting a number, picking one optionregression, classification

Sources

← The training loop: one step, one epochVanishing and exploding gradients →