Skip to content
Zogle

← Articles

Research

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. We run it by hand on two tiny examples, then see what changes for an LLM.

By Shashank R AlurUpdated 7 min read

Loss curve going down as a model takes repeated steps
On this page

The one-line idea

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

An LLM is trained with the same method. It's this loop, repeated an enormous number of times.

Example 1Example 2
GoalFind the best value of one number wGuess the next word
WhySimplest case, every step is clearSame job an LLM does

Part 1: What is "loss"?

Loss is one number that says how bad the model is right now. Lower is better, and 0 is perfect.

Example 1. We want w to be 3 (the model doesn't know this). Loss = (w−3)2(w-3)^2.

w01234
Loss94101

Example 2. The model sees "the cat" and must pick the next word from cat, dog, sat, mat. The correct word is sat.

The model gives each word a score. Softmax turns scores into probabilities (between 0 and 1, adding up to 1). Higher score means higher probability.

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

Loss only looks at the probability of the correct word:

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

P("sat")0.990.90.250.10.01
Loss0.0100.1051.3862.3034.605

High probability on the right word means low loss. At the start, loss = −ln⁡(0.25)=1.386-\ln(0.25) = 1.386.

Part 2: What is the gradient?

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

Example 1, checked with real numbers. At w = 0 the loss is 9. At w = 0.01 it's (0.01−3)2=8.9401(0.01-3)^2 = 8.9401. The loss changed by -0.0599 for a +0.01 move, so the slope is about -6. The exact formula 2(w−3)2(w-3) gives 2(0−3)=−62(0-3) = -6. Same.

  • Negative gradient: increasing the number lowers the loss, so increase it.
  • Positive gradient: decrease the number.

Example 2. For softmax with this loss, the gradient of each score is:

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

Wordcatdogsatmat
Gradient0.250.250.25 − 1 = -0.750.25

sat has a negative gradient, so its score goes up. The wrong words have positive gradients, so their scores go down.

Part 3: The update

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, the step size we choose.

Example 1 (lr = 0.1)

Value
Old w0
Gradient-6
New w0 − 0.1 × (−6) = 0.6
New loss5.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:

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

Example 1 (lr = 0.1)

StepwLoss
009
10.6005.76
31.4642.36
102.6780.104
302.9960.00

Example 2 (lr = 1)

StepP("sat")Loss
00.251.386
10.4750.744
50.8420.172
200.9610.040
1000.9920.008

In both, the loss falls fast at first and slowly later. As the model improves, the gradient shrinks, so the steps shrink. In Example 2, going from 0.96 to 0.99 takes 80 more steps.

Part 5: The learning rate matters

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

lrwLossWhat 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 more than the last, loss explodes

Too small wastes time. Too big breaks training.

Part 6: How is an LLM different?

An LLM does Example 2, scaled up.

Example 2 (ours)An LLM
JobNext word from 4 optionsNext token from tens of thousands of options
Input"the cat"A long stretch of text
Where scores come from4 free numbers we adjust directlyA huge calculation with billions of adjustable numbers
Loss−ln(P(correct))Same
Data1 exampleAn enormous amount of text
Learning loopLoss, gradient, update, repeatSame

A token is a small chunk of text, often a word or part of a word.

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

The one real addition: the scores come from a long chain of calculations, so finding each number's gradient uses the chain rule again and again. That's called backpropagation, and it's still just "find each number's slope."

What this article does not cover

  • How the model computes its scores (in modern LLMs, a transformer). That's a separate topic.
  • Training tricks. Real training uses small batches and improved versions of gradient descent, but the core step is still "move against the gradient."
  • What happens after this stage. Models are often trained further.

Glossary

TermMeaning
LossOne number: how bad the model is right now
GradientSlope of the loss for one number inside the model
Gradient descentMoving numbers against their gradient to lower the loss
Learning rateSize of each step
SoftmaxTurns scores into probabilities that add up to 1
TokenA small chunk of text
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 it: change lr in Example 1 to 1.1 and watch the loss explode, like the table in Part 5.

Tags: gradient descent, LLM, loss, learning rate, next word prediction, beginner

Written by

Shashank R Alur

AI engineer exploring the foundations of machine learning, language models, and high-performance AI systems.

Cite this article

Shashank R Alur. "Gradient Descent: The One Loop Behind Every AI, From a Tiny Model to an LLM." Zogle, Oct 2026. https://zogle.org/research/gradient-descent-the-one-loop-behind-every-ai-from-a-tiny-model-to-an-llm

@article{alur2026gradient,
  author = {Shashank R Alur},
  title = {Gradient Descent: The One Loop Behind Every AI, From a Tiny Model to an LLM},
  journal = {Zogle},
  year = {2026},
  url = {https://zogle.org/research/gradient-descent-the-one-loop-behind-every-ai-from-a-tiny-model-to-an-llm}
}

Keep reading