Unit 1 · Foundations
Lesson 04 · Available to read
Neural Networks Refresher: Layers, Weights and Gradient Descent
The minimum neural-network machinery you need: neurons, layers, loss, softmax, cross-entropy, gradient descent and backpropagation.
LLMs are neural networks, so before we open up the transformer we need a working toolkit: what a neuron computes, how layers stack, how the network turns scores into probabilities, how we measure error, and how training reduces it. This lesson is a compact refresher — the Math section's Unit 10 has slower, more detailed versions of several of these ideas.
Learning from data
Traditional software is written as rules. Machine learning instead defines a model with adjustable parameters and tunes those parameters until the model's outputs match examples.
- Supervised learning uses labelled examples: (photo, "cat").
- Self-supervised learning creates labels from the data itself. LLM pretraining is self-supervised: the "label" for every position in a text is simply the next token, so no humans are needed to annotate it.
The artificial neuron
A neuron takes a vector of inputs, computes a weighted sum plus a bias, and passes the result through a nonlinear activation function:
output = activation(w · x + b)
Worked example: inputs x = (1, 2), weights w = (0.5, −0.25), bias b = 0.1.
- weighted sum z = 0.5·1 + (−0.25)·2 + 0.1 = 0.1
- with ReLU activation (which outputs max(0, z)): output = 0.1
The weights and bias are the parameters — the numbers training will adjust.
Layers are matrices
Put many neurons side by side and they form a layer. Each neuron has its own weight vector; stacking them as rows of a matrix W means a whole layer is one matrix multiplication:
h = activation(W x + b)
A network is layers feeding layers. A layer that maps 4,096 inputs to 16,384 outputs has 4,096 × 16,384 ≈ 67 million weights — and this is why LLMs spend almost all their compute multiplying matrices on GPUs.
Why the nonlinearity? Without it, two layers W₂(W₁x) collapse into a single matrix (W₂W₁)x — stacking would add nothing. Activations such as ReLU (max(0, z)) or the smoother GELU used in many transformers let deep networks represent complicated, curved functions.
From scores to probabilities: softmax
The last layer of a language model produces one raw score — a logit — per vocabulary token. Softmax turns logits into a probability distribution:
softmax(zᵢ) = e^(zᵢ) / Σⱼ e^(zⱼ)
Example with three tokens and logits (2, 1, 0):
- e² ≈ 7.389, e¹ ≈ 2.718, e⁰ = 1; sum ≈ 11.107
- probabilities ≈ (0.665, 0.245, 0.090)
All positive, summing to 1, and the biggest logit still wins — but every option keeps some probability.
Measuring error: loss and cross-entropy
A loss function turns "how wrong was the model?" into one number. For next-token prediction the standard loss is cross-entropy: the negative log of the probability the model gave to the correct token.
loss = −ln(p_correct)
- If the correct token got 0.665 → loss = −ln(0.665) ≈ 0.41
- If the correct token got only 0.090 → loss ≈ 2.41
- If it got 1.0 → loss = 0 (perfect)
Confident wrong answers are punished heavily, which pushes the model to put probability where the truth is.
Gradient descent: walking downhill
Picture the loss as a landscape over all parameter values. The gradient — a vector of partial derivatives — points uphill. Gradient descent repeatedly takes a small step the other way:
w ← w − η · ∂loss/∂w
where η (eta) is the learning rate. Example: w = 3, ∂loss/∂w = 2, η = 0.1 → new w = 3 − 0.2 = 2.8.
In practice we estimate the gradient on a small random batch of examples (stochastic gradient descent), and use smarter variants such as Adam, which adapts the step size for each parameter.
Backpropagation: who is to blame?
A network may have billions of parameters. Backpropagation computes all their gradients efficiently by applying the chain rule backwards from the loss, layer by layer, reusing intermediate results. One backward pass costs roughly twice as much as the forward pass — not billions of times more.
The training loop
Every neural network, including every LLM, trains with the same loop:
- Forward pass — run a batch through the network to get predictions.
- Loss — compare predictions with the targets.
- Backward pass — backpropagate to get every parameter's gradient.
- Update — take a gradient-descent step.
- Repeat — for an LLM, hundreds of thousands of steps over trillions of tokens.
Generalization and overfitting
The goal is not to do well on the training data but on new data. A model that memorises its training set overfits: training loss keeps falling while loss on a held-out validation set starts rising. We watch validation loss, and use more data, regularisation or early stopping to keep the model general. Huge LLMs trained for about one pass over enormous datasets overfit less than you might expect — but memorising rare or repeated text is still a real issue (Lesson 10).
The takeaway
A neural network is a stack of matrix multiplications and nonlinearities; training repeats forward pass → loss → backpropagation → gradient-descent update billions of times until the parameters make good predictions.
Practice
0 of 8 attempted
- 1Calculate
A neuron has inputs x = (2, −1, 3), weights w = (0.5, 1, −0.5), bias b = 1 and ReLU activation. What is its output? What would the output be if the bias were −1?
Saved in this browser
- 2Calculate
Compute the softmax of logits (1, 1, 0). Then compute the cross-entropy loss if the correct answer is the third option.
Saved in this browser
- 3Calculate
A parameter w = 0.8 has gradient ∂loss/∂w = −0.5. With learning rate η = 0.2, what is w after one gradient-descent step? Did w go up or down, and why?
Saved in this browser
- 4Explain in your own words
Explain in your own words why a deep network needs nonlinear activation functions.
Saved in this browser
- 5Recall
List the four steps of the training loop in order.
Saved in this browser
- 6Multiple choice
What does backpropagation compute?
- 7Apply
You train a model and see training loss steadily falling while validation loss falls, flattens, then rises. What is happening, and name two things you could do.
Saved in this browser
- 8Explain in your own words
Why is LLM pretraining called self-supervised?
Saved in this browser