Neurons and Activation Functions
The building block of every neural network: a weighted sum followed by a non-linear activation. Why the non-linearity matters, and how sigmoid, tanh, ReLU, GELU and softmax behave.
Every neural network — from a 3-neuron toy to a large language model — is built from the same tiny unit: the artificial Neuron (artificial). It takes some numbers in, multiplies each by a weight, adds them up with a bias, and passes the result through an Activation function. Understanding this one unit, and especially the activation function, explains a surprising amount of why deep networks train well or badly.
One artificial neuron
- z
- pre-activation: the weighted sum plus bias — one number, can be any real value
- x₁
- the 1st input value (a Feature, e.g. “sunshine” in the analogy above)
- x₂
- the 2nd input value
- xₙ
- the last (n-th) input — a neuron can have any number n of inputs
- w₁
- weight on input 1: how strongly (and in which direction, + or −) x₁ pushes the score; learned during training
- w₂
- weight on input 2
- wₙ
- weight on the last input
- …
- “and so on” — the same pattern continues for all inputs in between
- b
- bias: a learned offset added at the end; shifts the score up or down even when all inputs are 0
- w·x
- Dot product of the weight Vector w and the input vector x — short notation for w₁x₁ + … + wₙxₙ
- f(z)
- the Activation function f applied to z, e.g. ReLU max(0, z) or Sigmoid (logistic function) 1/(1 + e^(−z))
- a
- activation: the neuron's output, passed on to the next layer
▸Worked example
Worked example: inputs x = (2, −1), weights w = (0.5, 1.5), bias b = 0.25. Then z = 0.5·2 + 1.5·(−1) + 0.25 = −0.25. With ReLU the output is max(0, −0.25) = 0; with sigmoid it is 1/(1+e^0.25) ≈ 0.438. Same weighted sum, very different outputs — the activation function decides what the neuron “says”.
From the perceptron to modern neurons
The Perceptron (Rosenblatt, 1958)[3] used a hard step function: output 1 if z > 0, else 0. It can learn any linearly separable rule (AND, OR) with a simple update rule, but a single perceptron cannot learn XOR, because no single straight line separates XOR's classes. Minsky and Papert's 1969 analysis of this limit cooled the field for years. The fix was to stack neurons into layers — and to replace the step with smooth functions so that Gradient descent and Backpropagation can work: the step's derivative is zero almost everywhere, so it gives gradient descent nothing to follow.
Why the non-linearity is essential
- x
- input Vector
- W₁
- weight matrix of layer 1, shape (hidden × inputs)
- b₁
- bias vector of layer 1 (one number per hidden unit)
- W₂
- weight matrix of layer 2, shape (outputs × hidden)
- b₂
- bias vector of layer 2
- W₂W₁
- matrix product of the two weight matrices — itself just one matrix, shape (outputs × inputs)
- W′
- the single equivalent weight matrix, W′ = W₂W₁
- b′
- the single equivalent bias, b′ = W₂b₁ + b₂
▸Worked example
No matter how many linear layers you stack, the result is still a straight line (or flat plane) — the same power as Linear regression or Logistic regression. Putting a non-linear f between layers lets each layer bend and fold the space, so the network can represent curved decision boundaries. With enough hidden units, a network with one hidden layer and a suitable non-linearity can approximate any continuous function on a bounded region (the “universal approximation theorem”)[1].
The activation zoo
| Function | Formula | Range | Derivative behaviour | Typical use |
|---|---|---|---|---|
| Step | 1 if z > 0 else 0 | {0, 1} | 0 everywhere (undefined at 0) — no gradient signal | Original perceptron |
| Sigmoid (logistic function) σ | 1/(1+e^(−z)) | (0, 1) | σ(1−σ), at most 0.25; ≈0 for |z| > 5 (saturates) | Binary / multi-label outputs, LSTM gates |
| tanh | (e^z − e^(−z))/(e^z + e^(−z)) | (−1, 1) | 1 − tanh², at most 1; also saturates | Zero-centred hidden units, RNN states |
| ReLU (Rectified Linear Unit) | max(0, z) | [0, ∞) | 1 for z > 0, 0 for z < 0 — no saturation on the positive side | Default for MLPs and CNNs |
| Leaky ReLU | max(αz, z), α ≈ 0.01 | (−∞, ∞) | 1 or α — never exactly zero | When many ReLUs “die” |
| ELU | z if z > 0 else α(e^z − 1) | (−α, ∞) | Smooth, negative outputs push mean toward 0 | Some CNNs / MLPs |
| GELU | z·Φ(z) | (≈ −0.17, ∞) | Smooth ReLU-like curve with a small negative dip | Transformers (BERT, GPT-2/3, ViT) |
| SiLU / Swish | z·σ(z) | (≈ −0.28, ∞) | Smooth, non-monotonic near 0 | EfficientNet; SwiGLU in LLaMA-style LLMs |
| Softplus | ln(1 + e^z) | (0, ∞) | Derivative is σ(z) — a smooth ReLU | When an output must be positive (e.g. a variance) |
Softmax: turning scores into probabilities
- softmax(z)ᵢ
- the i-th output of Softmax applied to the whole score vector z: predicted probability of class i (between 0 and 1)
- zᵢ
- raw score (logit) of class i — any real number
- e^
- “e to the power of” — the exponential function exp(·), e ≈ 2.718; the result is always positive
- Σⱼ
- sum over all classes j = 1 … C (C = number of classes)
- zⱼ
- logit of class j — j runs over every class in the sum
▸Worked example
Softmax is used on the output layer for multi-class Classification: it exponentiates the raw scores (Logits) and normalises them. Because of the exponential, the largest score gets a disproportionate share — a gap of 1.0 in logits means about 2.7× more probability. It is paired with Cross-entropy (log loss) loss. Sigmoid is the two-class special case: softmax over (z, 0) gives σ(z).
Saturation, vanishing gradients and dead ReLUs
During training, the gradient flowing backwards is multiplied by f′(z) at every layer. Sigmoid's derivative is at most 0.25, so through 10 sigmoid layers the signal can shrink by up to 0.25¹⁰ ≈ 10⁻⁶ — the Vanishing (and exploding) gradient problem that made deep sigmoid networks hard to train[5]. ReLU's derivative is exactly 1 for positive inputs, so gradients pass through unchanged; this is a big reason ReLU made deep networks practical. Its weakness: for z < 0 the gradient is exactly 0. If a neuron's bias drifts so negative that it is never positive on any input (often after a too-large Learning rate step), it stops learning for good — a dead ReLU. Leaky ReLU, ELU, GELU and SiLU keep a small gradient there.
- Sigmoid saturation: select sigmoid and put the cursor at x = 0, then at x = 5. f′ drops from 0.25 to about 0.007 — a 35× weaker learning signal for a neuron whose input is just “large”.
- tanh vs sigmoid: compare them. tanh′(0) = 1, four times sigmoid′(0), and tanh is centred on 0 — that is why tanh usually trains faster than sigmoid in hidden layers.
- ReLU's two regimes: select ReLU and sweep the cursor across 0. The derivative jumps from 0 to 1. Everything left of zero is the “dead zone”.
- GELU / SiLU vs ReLU: overlay them between −3 and 3. They are nearly identical for large |x| but smooth around 0 and dip slightly below zero, so negative inputs still get a small gradient.
- Step: select the step function and look at f′ — it is flat zero. This is exactly why gradient-based training needs smooth (or piecewise-linear) activations.
| Where | Good default |
|---|---|
| Hidden layers of MLPs / CNNs | ReLU (Leaky ReLU if many units die) |
| Hidden layers of Transformers | GELU, or SwiGLU (SiLU-gated) |
| Regression output | None (identity / linear) |
| Binary or multi-label output | Sigmoid (one per label) |
| Multi-class output (one correct class) | Softmax |
- What it is
- The slope used for negative inputs: f(z) = αz when z < 0. With α = 0 you get plain ReLU.
- ↑ If you increase it
- Negative inputs keep more gradient, so fewer units die; but as α → 1 the function becomes linear and the network loses its non-linearity.
- ↓ If you decrease it
- Behaves more like ReLU: sparser activations, but more risk of dead units.
- Typical values
- 0.01 (PyTorch default); 0.1–0.3 in some GANs.
import torch
import torch.nn as nn
x = torch.tensor([2.0, -1.0])
w = torch.tensor([0.5, 1.5])
b = 0.25
z = w @ x + b # weighted sum: tensor(-0.2500)
print(torch.relu(z)) # tensor(0.)
print(torch.sigmoid(z)) # tensor(0.4378)
print(nn.GELU()(z)) # tensor(-0.1003)
print(nn.SiLU()(z)) # tensor(-0.1094)
logits = torch.tensor([2.0, 1.0, 0.1])
print(torch.softmax(logits, dim=0)) # tensor([0.6590, 0.2424, 0.0986])- Double softmax: PyTorch's
nn.CrossEntropyLossexpects raw logits and applies log-softmax itself. Adding a softmax layer before it squashes gradients and slows learning. - ReLU on a regression output makes negative predictions impossible.
- Sigmoid in every hidden layer of a deep network: slow training due to saturation — prefer ReLU/GELU.
- Many dead ReLUs (a layer that always outputs 0): lower the learning rate, check initialization, or switch to Leaky ReLU/GELU.
▸Go deeper: biology, history and the universal approximation theorem
The artificial neuron is only loosely inspired by biology. McCulloch and Pitts (1943) modelled a neuron as a threshold logic unit; Rosenblatt's perceptron added learnable weights[3]. Real neurons communicate with timed spikes, have complex dendrites and are far from a weighted sum — modern deep learning borrows the name, not the mechanism[1].
The universal approximation theorem (Cybenko 1989; Hornik 1991) says a wide-enough single hidden layer can represent a good approximation — it says nothing about how many units are needed or whether gradient descent will find the weights. In practice deeper networks reach the same accuracy with far fewer parameters, which is why we go “deep”[1][2].
Quiz
References
- [1]Deep Learning — Chapter 6: Deep Feedforward NetworksBookGoodfellow, Bengio & Courville2016deeplearningbook.org
- [2]CS231n: Neural Networks Part 1 — Setting up the ArchitectureCourseStanford CS231ncs231n.github.io
- [3]The perceptron: A probabilistic model for information storage and organization in the brainPaperRosenblatt1958doi.org
- [4]Gaussian Error Linear Units (GELUs)PaperHendrycks & Gimpel2016arxiv.org
- [5]Understanding the difficulty of training deep feedforward neural networksPaperGlorot & Bengio2010proceedings.mlr.press
- [6]torch.nn — Non-linear activationsDocsPyTorchdocs.pytorch.org