Skip to content
ZogleAGI Lab

Neural network fundamentals

Gradient Descent: The One Loop Behind Every AI, From a Tiny Model to an LLM

Gradient descent is the loop that trains almost every AI model, including large language models. We run it on two tiny examples with real numbers: finding one best number, and teaching a model to pick the right next word. Then we see what changes when the same loop is scaled up to an LLM.

9 min read

Cover image for “Gradient Descent: The One Loop Behind Every AI, From a Tiny Model to an LLM”

The one-line idea

Every AI model is a calculation with adjustable numbers inside it. Training means changing those numbers until the model's mistakes get smaller. The method that does the changing is called gradient descent.

An LLM (large language model, the kind behind chat assistants) is trained with this same method. It is not a different trick. It is this loop, repeated an enormous number of times.

This article shows the loop on two tiny examples side by side, then explains what is different for an LLM.

Example 1Example 2
GoalFind the best value of one number wwTeach a model to guess the next word
Why this oneThe simplest case, so every step is clearThis is the same job an LLM does

Part 1: What is "loss"?

To improve something, you first need to measure how bad it is right now. That measurement is a single number called the loss. Lower is better, 0 means perfect.

Example 1. We want ww to be 3 (the model does not know this). We measure the loss as the squared distance from 3:

L(w)=(w−3)2L(w) = (w-3)^2

ww01234
Loss94101

The loss is 0 only at w=3w=3. The model's job is to find that value.

Example 2. The model sees the words "the cat" and must guess the next word out of four options: cat, dog, sat, mat. The correct next word is sat.

The model gives each word a score (any number). To compare words fairly, scores are turned into probabilities (numbers between 0 and 1 that add up to 1). The formula is called softmax:

probability of a word=eits scoresum of escore over all words\text{probability of a word} = \frac{e^{\text{its score}}}{\text{sum of } e^{\text{score}} \text{ over all words}}

You do not need to memorise it. See what it does:

Scores (cat, dog, sat, mat)Probabilities
0, 0, 0, 00.25, 0.25, 0.25, 0.25
-0.25, -0.25, 0.75, -0.250.175, 0.175, 0.475, 0.175

Equal scores give equal probabilities. A higher score gives a higher probability.

The loss only cares about the probability given to the correct word:

L=−ln⁡(probability of the correct word)L = -\ln(\text{probability of the correct word})

Probability of "sat"0.990.90.250.10.01
Loss0.0100.1051.3862.3034.605

Giving the right word a high probability means low loss. Starting with all scores at 0, the loss is −ln⁡(0.25)=1.386-\ln(0.25) = 1.386.


Part 2: What is the gradient?

We know how bad the model is. Now: which way should we change the number to make the loss smaller?

The gradient answers this. It is the slope: if I increase this number a tiny bit, how much does the loss change?

Example 1: check with real numbers. At w=0w=0 the loss is 9. Move ww up by 0.01:

w=0w=0w=0.01w=0.01
Loss9(0.01−3)2=8.9401(0.01-3)^2 = 8.9401

Loss changed by −0.0599-0.0599 for a +0.01+0.01 change, so the slope is about −5.99-5.99, roughly -6.

The exact formula is 2(w−3)2(w-3), which at w=0w=0 gives 2(0−3)=−62(0-3) = -6. Matches.

  • Negative gradient: increasing the number lowers the loss, so increase it.
  • Positive gradient: decrease the number.
  • Big gradient: the loss is very sensitive to this number.

Example 2. For softmax with this loss, the gradient of each word's score has a simple form:

gradient=probability−(1 for the correct word, else 0)\text{gradient} = \text{probability} - (1 \text{ for the correct word, else } 0)

At the start (all probabilities 0.25):

Wordcatdogsatmat
Gradient0.250.250.250.250.25−1=−0.750.25 - 1 = -0.750.250.25

The correct word ("sat") has a negative gradient, so raise its score. The wrong words have positive gradients, so lower their scores.


Part 3: The update (the "descent")

Move each number against its gradient:

new value=old value−lr×gradient\text{new value} = \text{old value} - \text{lr} \times \text{gradient}

lr is the learning rate, a step size we choose. This is the "descent" in gradient descent: the loss goes down because every step moves against the slope.

Example 1 (lr = 0.1)

Value
Old ww0
Gradient-6
New ww0−0.1×(−6)=0.60 - 0.1 \times (-6) = 0.6
New loss(0.6−3)2=5.76(0.6-3)^2 = 5.76 (was 9)

Example 2 (lr = 1)

Wordcatdogsatmat
Old score0000
Gradient0.250.25-0.750.25
New score-0.25-0.250.75-0.25
New probability0.1750.1750.4750.175

New loss: −ln⁡(0.475)=0.744-\ln(0.475) = 0.744 (was 1.386). Both examples improved after one step.


Part 4: Repeat

Training is this loop, repeated:

  1. Measure the loss
  2. Compute the gradient
  3. Update the numbers
  4. Go back to 1

Example 1 (lr = 0.1)

StepwwLoss
009
10.6005.76
31.4642.36
102.6780.104
302.9960.00

Example 2 (lr = 1)

StepProbability of "sat"Loss
00.251.386
10.4750.744
50.8420.172
200.9610.040
1000.9920.008

Notice in both: the loss falls fast at the start and slowly later. As the model improves, the gradient gets smaller, so steps get smaller. In Example 2, getting from 0.96 to 0.99 takes 80 more steps.


Part 5: The learning rate matters a lot

Example 1, same start (w=0w=0), different learning rates, after 30 steps:

Learning rateww after 30 stepsLoss after 30 stepsWhat happened
0.011.3642.678Too slow. Still far from 3
0.12.9960.00Good
0.92.9960.00Jumps back and forth over 3, still converges
1.1-709.1507,128Each jump overshoots by more than the last, so the loss explodes

The learning rate is something the person training the model has to choose. Too small wastes time. Too big breaks training.


Part 6: So how is an LLM different?

An LLM does Example 2, scaled up. Here is what changes and what does not.

Example 2 (ours)An LLM
JobGuess the next word from 4 optionsGuess the next token from tens of thousands of options (a token is a small chunk of text, often a word or part of a word)
Input"the cat"A long stretch of text before the guess
Where scores come from4 free numbers we adjust directlyA huge calculation with billions of adjustable numbers inside
Loss−ln⁡(probability of correct word)-\ln(\text{probability of correct word})Same idea
Data1 exampleAn enormous amount of text
Learning loopLoss, gradient, update, repeatSame

How training text becomes examples: take a piece of text, hide the next word, ask the model to guess it, measure the loss, update. Every position in every piece of text is a free example, because the correct answer is already in the text.

The one real addition is that the scores come from a long chain of calculations, not from free numbers. Finding the gradient for every adjustable number in that chain uses the chain rule again and again. That process has its own name, backpropagation, but it is still just "find each number's slope."


What this article does not cover

  • How the model computes its scores. That part (in modern LLMs, an architecture called a transformer) is a separate topic.
  • Training tricks. Real training updates using small batches of data at a time, and usually uses improved versions of gradient descent. The core step is still "move against the gradient."
  • What happens after. Models are often trained further after this stage, which this article does not describe.

Glossary

TermMeaning
LossOne number: how bad the model is right now
GradientSlope of the loss with respect to a number inside the model
Gradient descentRepeatedly moving numbers against their gradient to lower the loss
Learning rateSize of each step
ScoreAny number the model gives a word
SoftmaxTurns scores into probabilities that add up to 1
TokenA small chunk of text, often a word or part of one
BackpropagationComputing the gradient for every number in a multi-step calculation

Full code

python
import numpy as np

# Example 1: find w that minimises (w-3)^2
w = 0.; lr = 0.1
for step in range(1, 31):
    grad = 2*(w - 3)
    w -= lr*grad
    if step in (1, 3, 10, 30):
        print(step, round(w, 4), round((w-3)**2, 4))

# Example 2: guess the next word (cat, dog, sat, mat), correct = sat
softmax = lambda z: np.exp(z)/np.exp(z).sum()
scores = np.zeros(4); correct = 2; lr = 1.0
for step in range(101):
    p = softmax(scores)
    loss = -np.log(p[correct])
    if step in (0, 1, 5, 20, 100):
        print(step, p.round(3), round(loss, 4))
    grad = p.copy(); grad[correct] -= 1     # probability minus (1 for correct word)
    scores -= lr*grad

Try: change lr in Example 1 to 1.1 and watch the loss explode, like the table in Part 5.

  • gradient descent
  • LLM
  • loss
  • learning rate
  • next word prediction
  • beginner

More from the lab

  • Cover image for “What Is a Neuron? Trying 7 Kinds on 3 Tiny Puzzles”
    Experiment · EXP-001

    What Is a Neuron? Trying 7 Kinds on 3 Tiny Puzzles

    A neuron is a tiny calculator: it takes numbers in, adds them up with weights, and gives one number out. We try 7 kinds of neuron, from the simplest to a small group of neurons, on three tiny puzzles (AND, OR, XOR). The kind of neuron barely mattered. What mattered was having more than one neuron working together.

    6 min read

    FoundationalCompleted
  • Neural Networks From Zero: Two Tiny Examples, Every Step Explained

    Neural Networks From Zero: Two Tiny Examples, Every Step Explained

    Where neural networks are used, why almost every use reduces to predicting a number or choosing a category, and how a single neuron learns both. Every term is defined before it is used, and every step is shown with real numbers.

    15 min read