Neurons, layers, activation functions, backpropagation — how neural networks learn.
Published September 21, 2026
A neural network is a flexible function with a very large number of adjustable numbers (parameters, or weights). Training adjusts those numbers so the function maps inputs to the right outputs: an image to "cat", a sentence to its sentiment, a sequence of words to the next word.
Neural networks aren't magic, and they aren't brains. They're compositions of simple arithmetic steps, tuned by calculus. That's worth understanding concretely, because it explains both their power and their failure modes.
A neuron takes inputs, multiplies each by a weight, adds a bias, and passes the result through an activation function:
z = w₁·x₁ + w₂·x₂ + w₃·x₃ + b (a weighted sum)
a = f(z) (a non-linear "activation")
With inputs such as a house's size, number of rooms and age, the weights express how much each input matters, and in which direction. On its own, one neuron is just a linear model: logistic regression, if f is the sigmoid.
Neurons are organized into layers. Each layer takes the previous layer's outputs as its inputs:
input features ─▶ hidden layer 1 ─▶ hidden layer 2 ─▶ output layer
(x) (h₁ = f(W₁x+b₁)) (h₂ = f(W₂h₁+b₂)) (ŷ)
Each layer transforms its input into a new representation. In an image network, early layers detect edges, middle layers detect textures and parts, and later layers detect whole objects. Nobody programs these features. They're learned because they help reduce the error.
Why activations must be non-linear: stacking linear layers (W₂(W₁x)) is just another linear function (Wx), so depth would add nothing. Non-linear activations let the network represent curved, complex decision boundaries.
| Activation | Formula | Where it's used |
|---|---|---|
| ReLU | max(0, z) | Default for hidden layers: cheap, and keeps gradients alive for positive inputs |
| GELU | a smooth ReLU-like curve | Transformers (GPT, BERT) |
| Sigmoid | 1 / (1 + e^(−z)) | Output for binary classification (a probability between 0 and 1) |
| Softmax | e^(zᵢ) / Σ e^(zⱼ) | Output for multi-class classification (probabilities that sum to 1) |
| Tanh | ranges from −1 to 1 | Older recurrent networks |
Computing a prediction is just matrix arithmetic, layer by layer:
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def forward(X, W1, b1, W2, b2):
H = np.maximum(0, X @ W1 + b1) # hidden layer with ReLU
return sigmoid(H @ W2 + b2) # output probability for a binary task
X holds a batch of examples (one per row), so the whole batch is processed at once. That's why GPUs, which are built for large matrix multiplications, made deep learning practical.
Training needs one number that says how wrong the predictions are:
(y − ŷ)².−[y·log ŷ + (1−y)·log(1−ŷ)]. It heavily penalizes confident wrong answers.−log(probability assigned to the correct class). It's also what language models minimize for the next token.Training repeatedly nudges every weight in the direction that reduces the loss:
∂loss/∂w for every weight: how much the loss would change if that weight moved slightly. Frameworks such as PyTorch and TensorFlow do this automatically (autodiff).w ← w − learning_rate × gradient.Using small random batches rather than the whole dataset is stochastic gradient descent (SGD). Modern optimizers improve on it: momentum smooths updates, and Adam / AdamW adapts the step size per parameter and is the common default.
The learning rate is the most important setting. Too high, and the loss bounces or explodes. Too low, and training crawls or stalls. Schedules that warm up and then decay the rate are standard for large models.
Networks with millions of parameters can memorize training data (overfitting): great training accuracy, poor results on new data. Always evaluate on held-out validation and test sets. The usual defences:
Vanishing and exploding gradients: in deep networks, gradients multiplied through many layers can shrink toward zero or blow up. ReLU-family activations, careful weight initialization, normalization and residual (skip) connections (output = layer(x) + x) are what made networks with dozens or hundreds of layers trainable.
| Architecture | Key idea | Typical data |
|---|---|---|
| MLP (fully connected) | Every input connects to every neuron | Small tabular problems (gradient-boosted trees often win on tabular data) |
| CNN | Small filters slide over the input, sharing weights across positions | Images, spatial signals |
| RNN / LSTM | Processes a sequence step by step, carrying state | Older approach to text, audio and time series |
| Transformer | Attention lets every position look at every other position in parallel | Text, code, images, audio: the basis of LLMs (see Transformers & Attention) |
The universal approximation theorem says a wide enough network with one hidden layer can approximate any continuous function. In practice, depth is what makes learning efficient: deep networks build complex features hierarchically from simpler ones, with far fewer parameters than a single enormous layer.
Q: What is backpropagation, in one sentence? A: It's the chain rule of calculus applied efficiently through the network's computation graph, from the loss backwards, to compute the gradient of the loss with respect to every weight in one backward pass.
Q: Why did ReLU replace sigmoid in hidden layers? A: The sigmoid saturates: for large positive or negative inputs its gradient is nearly zero, so gradients vanish in deep networks. ReLU has a constant gradient for positive inputs, is cheap to compute, and made deep networks train much faster.
Q: How do you tell if a model is overfitting, and what do you do? A: Training loss keeps falling while validation loss rises, or plateaus well above it. Get more data or augment it, add regularization (dropout, weight decay), simplify the model, or stop training earlier.
Q: What does the learning rate do, and how do you choose it? A: It sets the size of each weight update. Too large diverges, and too small is slow or gets stuck. Start from a known-good default for the optimizer (around 1e-3 for Adam on small models), try a small range of values, and use warm-up and decay schedules for large models.
Q: When is a neural network the wrong tool? A: With small tabular datasets, gradient-boosted trees are usually more accurate, faster and easier to interpret. Neural networks shine with large amounts of unstructured data (images, text, audio), or when a pre-trained model can be fine-tuned.