AILearn AI
LearnGlossarySources
ID/EN
Learn AI — open material for learning AIEvery lesson cites its original sources.
⌬Neural Networks
  1. Neurons and Activation Functions
  2. Multilayer Perceptrons and Backpropagation
  3. Training Hyperparameters: Epochs, Learning Rate, Batch Size and More
  4. Optimizers: SGD, Momentum, RMSProp, Adam/AdamW and LR Schedules
  5. Regularization, Normalization and Initialization
Modules
  1. ◎ AI Foundations
  2. ∿ Classical Machine Learning
  3. ⌬ Neural Networks
  4. ↘ Training Results & Evaluation
  5. ▦ Deep Learning Architectures
  6. ✎ Large Language Models
  7. ◐ Image Generation
  8. ♪ Audio & Speech AI
  9. ◈ Multimodal, Video & 3D
  10. ↻ Reinforcement Learning
  11. ⚙ ML Development & MLOps
  12. ★ Advanced & Frontier Topics
Learn/Neural Networks

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.

Beginner~18 min·6 references
Key terms
Neuron (artificial)PerceptronActivation functionReLU (Rectified Linear Unit)Sigmoid (logistic function)SoftmaxVanishing (and exploding) gradient

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.

≈
A neuron is a tiny voting committee
Imagine deciding whether to go outside. Inputs: sunshine (x₁), temperature (x₂), whether it is raining (x₃). You care a lot about rain (large negative weight), a bit about sunshine (small positive weight). You add up the weighted opinions plus your general mood (the bias), and then a rule turns that score into a decision — “go” or “stay”, or a smooth “how keen am I”. That final rule is the activation function.

One artificial neuron

z = w₁x₁ + w₂x₂ + … + wₙxₙ + b = w·x + b a = f(z)
z is the pre-activation (a Dot product plus bias); f is the activation function; a is the neuron's output.
In words: “multiply each input by its weight, add everything up together with the bias, then pass that single number through the activation function.” The first part, z, is only a weighted score — a straight-line (linear) function of the inputs. The activation f bends it, and that bend is what lets networks of many neurons learn curved, complex patterns.
Symbols(hover a symbol in the formula)
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
Go outside? Inputs x = (sunshine 2, temperature 3, raining 1) Weights w = (0.5, 0.25, −2), bias b = −0.5 z = 0.5·2 + 0.25·3 + (−2)·1 + (−0.5) = 1 + 0.75 − 2 − 0.5 = −0.75 ReLU: a = max(0, −0.75) = 0 Sigmoid: a = 1/(1 + e^0.75) ≈ 0.321 (“32% keen”) Now the rain stops (x₃ = 0): z = 1 + 0.75 + 0 − 0.5 = 1.25 ReLU: a = 1.25 Sigmoid: a ≈ 0.777 The large negative rain weight flipped the decision.

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

W₂(W₁x + b₁) + b₂ = (W₂W₁)x + (W₂b₁ + b₂) = W′x + b′
Without an activation, two linear layers collapse into one linear layer. Depth would add nothing.
In words: “a linear layer applied to the output of another linear layer is just one bigger linear layer.” Multiply out the brackets and the two weight matrices merge into a single matrix W′, the biases into a single bias b′. So without a non-linear activation in between, stacking layers adds no expressive power — only extra parameters.
Symbols(hover a symbol in the formula)
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
2 inputs → 2 hidden units → 1 output, no activation W₁ = [[1, 2], [0, 1]], b₁ = (0, 1), W₂ = [1, −1], b₂ = 0.5, x = (1, 2) Two layers: W₁x = (1·1 + 2·2, 0·1 + 1·2) = (5, 2); + b₁ → (5, 3) W₂·(5, 3) = 5 − 3 = 2; + b₂ → 2.5 One layer: W′ = W₂W₁ = [1·1 + (−1)·0, 1·2 + (−1)·1] = [1, 1] b′ = W₂b₁ + b₂ = (0 − 1) + 0.5 = −0.5 W′x + b′ = 1 + 2 − 0.5 = 2.5 ← identical, for every x Now put a ReLU between the layers and try x = (−1, 0): hidden = ReLU((−1, 0) + (0, 1)) = ReLU(−1, 1) = (0, 1) → output 0 − 1 + 0.5 = −0.5 collapsed linear layer: 1·(−1) + 1·0 − 0.5 = −1.5 ← different: the ReLU broke the collapse

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

FunctionFormulaRangeDerivative behaviourTypical use
Step1 if z > 0 else 0{0, 1}0 everywhere (undefined at 0) — no gradient signalOriginal 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 saturatesZero-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 sideDefault for MLPs and CNNs
Leaky ReLUmax(αz, z), α ≈ 0.01(−∞, ∞)1 or α — never exactly zeroWhen many ReLUs “die”
ELUz if z > 0 else α(e^z − 1)(−α, ∞)Smooth, negative outputs push mean toward 0Some CNNs / MLPs
GELUz·Φ(z)(≈ −0.17, ∞)Smooth ReLU-like curve with a small negative dipTransformers (BERT, GPT-2/3, ViT)
SiLU / Swishz·σ(z)(≈ −0.28, ∞)Smooth, non-monotonic near 0EfficientNet; SwiGLU in LLaMA-style LLMs
Softplusln(1 + e^z)(0, ∞)Derivative is σ(z) — a smooth ReLUWhen an output must be positive (e.g. a variance)
Φ is the standard normal CDF. GELU is from Hendrycks & Gimpel[4].

Softmax: turning scores into probabilities

softmax(z)ᵢ = e^(zᵢ) / Σⱼ e^(zⱼ)
Example: logits (2.0, 1.0, 0.1) → probabilities (0.659, 0.242, 0.099). They are positive and sum to 1.
In words: “the probability of class i is e to the power of its score, divided by the sum of e to the power of every score.” Exponentiating makes every score positive and exaggerates the differences between them; dividing by the total makes the outputs add up to 1, so they can be read as probabilities over the classes.
Symbols(hover a symbol in the formula)
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
Logits z = (2.0, 1.0, 0.1) for (cat, dog, bird) 1. Exponentiate: e^2.0 = 7.389 e^1.0 = 2.718 e^0.1 = 1.105 2. Sum: 7.389 + 2.718 + 1.105 = 11.212 3. Divide: 7.389/11.212 = 0.659 2.718/11.212 = 0.242 1.105/11.212 = 0.099 Check: 0.659 + 0.242 + 0.099 = 1 ✓ Double the logits (4, 2, 0.2) → (0.864, 0.117, 0.019): more confident Subtract the max (0, −1, −1.9) → same (0.659, 0.242, 0.099): libraries do this to avoid overflow Cross-entropy loss if the true class is cat: −ln 0.659 ≈ 0.417

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.

Loading interactive…
Plot of f(x) and its derivative f′(x). Drag the cursor to read values; use “compare” to overlay functions.
  1. 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”.
  2. 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.
  3. 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”.
  4. 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.
  5. 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.
WhereGood default
Hidden layers of MLPs / CNNsReLU (Leaky ReLU if many units die)
Hidden layers of TransformersGELU, or SwiGLU (SiLU-gated)
Regression outputNone (identity / linear)
Binary or multi-label outputSigmoid (one per label)
Multi-class output (one correct class)Softmax
knobLeaky ReLU negative slope α
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.
python
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])
The worked example from above in PyTorch. All of these activations live in torch / torch.nn[6].
!
Common pitfalls
Watch out for these:
  • Double softmax: PyTorch's nn.CrossEntropyLoss expects 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

1.
What happens if you remove all activation functions from a 5-layer network?
2.
Why do deep networks with sigmoid in every hidden layer often train slowly?
3.
A ReLU neuron outputs 0 for every training example. What is true?
4.
Logits are (3, 1, 1). Roughly what does softmax give for the first class?
5.
Which output activation fits predicting house prices?
Score: 0/5

References

  1. [1]
    Deep Learning — Chapter 6: Deep Feedforward Networks
    BookGoodfellow, Bengio & Courville2016deeplearningbook.org
  2. [2]
    CS231n: Neural Networks Part 1 — Setting up the Architecture
    CourseStanford CS231ncs231n.github.io
  3. [3]
    The perceptron: A probabilistic model for information storage and organization in the brain
    PaperRosenblatt1958doi.org
  4. [4]
    Gaussian Error Linear Units (GELUs)
    PaperHendrycks & Gimpel2016arxiv.org
  5. [5]
    Understanding the difficulty of training deep feedforward neural networks
    PaperGlorot & Bengio2010proceedings.mlr.press
  6. [6]
    torch.nn — Non-linear activations
    DocsPyTorchdocs.pytorch.org
← Previous
Overfitting and the bias-variance trade-off
Next →
Multilayer Perceptrons and Backpropagation