Skip to content
LIVE Agentic AI Workshop · 12 DecView workshop →

Explore Netsetos

My Learning
Join workshopAll courses →
Contact Support · info@netsetos.com
top of page
Search

Why GPT Learns From Its Mistakes

Jun 14
5 min read

Updated: 2 days ago

Imagine you're taking an exam


Question: what comes after "The cat sat on the ____"?


You answered


mat    90%
floor   8%
roof    2%

Your friend answered


mat    51%
floor  49%
roof    0%

You both picked mat. You're both right. Should you both get the same score?


"No. The first student clearly understood the answer better."

That is exactly the problem cross-entropy solves: it grades not just what you predicted, but how sure you were.


The story that inspired it: two weather forecasters


Long before neural networks, statisticians hit the same wall while scoring weather forecasts.


Day 1 — it rains


Forecaster A


60% chance of rain


Forecaster B


99% chance of rain


🌧️


Who deserves more credit? Forecaster B. He wasn't just correct — he was confidently correct.


Now flip it — no rain


☀️


Same two forecasts, but the sun comes out. Now who made the bigger mistake? Forecaster B again. He staked 99% on the wrong outcome.


The principle: Being wrong is acceptable. Being confidently wrong should be punished much more heavily.

Why accuracy isn't enough


Forecaster

Predicted

Actual

Accuracy

A (60%)

Rain

Rain

Correct ✓

B (99%)

Rain

Rain

Correct ✓


Accuracy collapses both forecasts to a single yes/no by thresholding at 50%. The moment a forecast crosses that line, the difference between 60% and 99% is thrown away. Accuracy discards confidence. Cross-entropy keeps it.


Quick check. Two models both classify a tumor as "malignant" and are right. Model 1 said 0.55, Model 2 said 0.98. Accuracy scores them identically. Which would you trust on the next case? That gap is precisely what cross-entropy measures.


Designing the perfect penalty system


Suppose we get to invent the grading rule from scratch. We want a penalty that depends on the probability the forecaster gave to the truth:


Confidence in truth

Desired penalty

99%

Almost zero

90%

Small

50%

Medium

10%

Large

1%

Huge


Which function naturally behaves like this? Let's test candidates.


Option 1 — Penalty = 1 − p


p

Penalty

99%

0.01

50%

0.50

1%

0.99


Too soft. Giving the truth only 1% probability barely costs more than giving it 50%. A near-blind guess should hurt far more.


Option 2 — Penalty = 1 / p


p

Penalty

99%

1.01

50%

2

1%

100


Too harsh, and it never reaches zero — even a perfect 100% forecast still pays a penalty of 1. We want a confident, correct answer to cost nothing.


Option 3 — The logarithm


The logarithm produces exactly the curve we sketched: flat near certainty, then it punishes harder and harder as confidence in the truth drops toward zero.


Confidence in truth        Penalty = -ln(p)

99%  █                       0.01
90%  ██                      0.10
70%  ███                     0.36
50%  █████                   0.69
10%  ███████████████        2.30
1%   ████████████████████   4.60

  • Correct and confident → tiny penalty

  • Correct but unsure → medium penalty

  • Confidently wrong → massive penalty


The formula finally appears


Loss = −log(p)


where p is the probability the model assigned to the correct answer. The minus sign is there because the log of a probability (a number between 0 and 1) is always negative, and we want a positive penalty.


ln(0.9)  = -0.10        -ln(0.9)  = 0.10
ln(0.5)  = -0.69        -ln(0.5)  = 0.69
ln(0.01) = -4.60        -ln(0.01) = 4.60

One thing to nail down: Machine-learning cross-entropy uses the natural log (ln, base e), not log₁₀. If you reproduce these numbers with a base-10 log you'll get smaller values (for example −0.30 instead of −0.69) — the shape is identical, only the scale changes.

Cross-entropy wasn't chosen because mathematicians liked logarithms. It was chosen because the logarithm naturally creates the right rewards and punishments for probabilistic predictions:


Reward confidence when correct. Punish confidence when wrong.

Where does softmax come in?


So far we've talked about a single probability p. But a model doesn't hand you a clean probability — it outputs raw, unbounded scores called logits, one per possible answer. For "the cat sat on the ____" there's a logit for mat, one for floor, one for every word in the vocabulary.


Softmax is the step that turns those raw scores into a clean probability distribution — every value between 0 and 1, all of them summing to 1:


softmax(z)ᵢ = e^zᵢ / Σⱼ e^zⱼ


Reading the formula piece by piece


You have a list of raw logits, one per word: z = [z_mat, z_floor, z_roof, ...]. These can be anything — positive, negative, huge or tiny. The formula has two moving parts.


Numerator — e^zᵢ (exponentiate). Raise e to this word's logit. It does two jobs at once:


  • Forces everything positive. Probabilities can't be negative, but logits can. Since e^x > 0 for any x (even e^−3 = 0.05), exponentiating guarantees every value is positive — no arbitrary floor needed.

  • Stretches the gaps. e^x grows fast, so a logit that's a little bigger becomes a probability that's a lot bigger. A small lead in the raw scores turns into a confident winner — that's how a model lands on 90% for mat instead of a wishy-washy 40%.


Denominator — Σⱼ e^zⱼ (normalise). Sum the exponentiated scores across every word in the vocabulary. Dividing each word's e^zᵢ by this total forces the whole list to sum to exactly 1 — the defining property of a probability distribution.


So the structure is just "exponentiate, then divide by the total."


A worked number


Three logits, z = [2.0, 1.0, 0.1] for [mat, floor, roof]:


Word

logit z

eᵃ

÷ total

mat

2.0

7.39

7.39 / 10.18 = 0.659

floor

1.0

2.72

2.72 / 10.18 = 0.242

roof

0.1

1.11

1.11 / 10.18 = 0.099



Σ = 10.18

sums to 1.0 ✓


mat's logit was only twice floor's, but after softmax it gets nearly three times the probability. That's the exponential stretching the lead.


Why the exponential at all?: Why not just divide each logit by the sum of logits and skip the e? Because logits go negative — dividing negatives by a sum gives nonsense, like negative "probabilities" or a denominator near zero that blows up. The exponential cleanly maps the entire real line onto positive numbers before normalising, and its derivative is well-behaved, which matters when gradients flow back through it during training.

Exponentiating makes every score positive and stretches the gap between big and small logits; dividing by the total normalises them into probabilities. The two pieces then snap together:


The full picture.


  • Softmax takes the model's raw logits and produces a probability for every word.

  • Cross-entropy reaches into that vector, picks out the probability of the true next word, and scores it with −ln(p).


Together: Loss = −ln( softmax(z)_correct )


Enter GPT


This is the exact grading system that trains GPT. At every position in the text, the model produces logits over the whole vocabulary, softmax turns them into probabilities, and cross-entropy compares the probability it gave to the word that actually came next.


If the real next word was mat and the model said 90%, the loss is tiny — nudge the weights only slightly. If it confidently said roof and the truth was mat, the loss is large — push the weights hard. Repeat across trillions of tokens and the model is steadily dragged toward putting high probability on the words that really follow.


The whole idea in one line: GPT doesn't learn by being told the right word. It learns by being penalised in proportion to how surprised it was by the right word — and surprise is just −ln(p).

Whether you're forecasting rain or predicting the next token, the rule never changes: reward confidence when correct, punish confidence when wrong. That single principle, wrapped in a logarithm, is what lets a language model learn from every mistake it makes.


Want to go further? Our free GenAI Engineering course shows how LLM apps are built and run: RAG, agents, MCP and deployment.

 
 
 

Recent Posts

See All
RAG Evaluation Metrics Explained: A Complete Guide

Imagine you build a smart assistant that answers questions using your company's documents. You test it on three questions, the answers look great, and you ship it. A week later you change one small se

 
 
 
The Transformer, drawn from scratch

Start here · the whole article on one map Type a few words and an AI guesses what comes next. Here is the entire journey those words take inside the machine - every stop is a short chapter below, buil

 
 
 

Comments


bottom of page