Chapter 6 of 8
Probability and softmax: turning scores into choices
A model does not output a decision, it outputs raw scores. To choose a next token it must turn those scores into probabilities: positive numbers that sum to 1. The function that does this is softmax, and it sits at the output of every language model. In this lab you implement softmax and a sampler, then prove the output is a genuine probability distribution and that softmax preserves the order of the scores.
The lab: read it, then run it
#!/usr/bin/env python3
"""
LAB F6: Probability and softmax. Turning scores into choices.
A model outputs raw scores. To pick a next token it needs PROBABILITIES: positive
numbers that sum to 1. The softmax function does exactly that, and it is used at
the output of every language model. You implement softmax and sampling here and
prove the output is a real probability distribution.
Run: python3 modules/academy-content/labs/foundations/f6-probability-softmax.py
"""
import sys, math
def softmax(scores):
m = max(scores) # subtract the max for numerical safety
exps = [math.exp(s - m) for s in scores]
total = sum(exps)
return [e / total for e in exps]
def sample(probs, r): # r is a number in [0, 1)
acc = 0.0
for i, p in enumerate(probs):
acc += p
if r < acc:
return i
return len(probs) - 1
scores = [2.0, 1.0, 0.1, -1.0]
probs = softmax(scores)
print("STEP 1-2: softmax turns scores into probabilities")
for s, p in zip(scores, probs):
print(f" score {s:5.1f} -> prob {p:.3f}")
# a fixed, seedless pseudo-random walk so the sample is reproducible in the lab.
picks = [sample(probs, (i * 0.1) % 1.0) for i in range(10)]
print("")
print(f"STEP 3: sampling 10 times (reproducible): {picks}")
# STEP 4: the invariants. Probabilities are all >= 0 and sum to 1, and the highest
# score got the highest probability (softmax preserves order).
total = sum(probs)
ok = (abs(total - 1.0) < 1e-9) and all(p >= 0 for p in probs) and (probs.index(max(probs)) == scores.index(max(scores)))
print("")
print(f"STEP 4: probs sum to 1, all >= 0, and top score got top prob: {'YES' if ok else 'NO'}")
if not ok:
sys.exit(1)
print("")
print("Softmax is the output of every LLM. Next: derivatives, the key to learning.")
Runnable lab
f6-probability-softmax.pyImplement softmax and sampling, and prove the result is a valid distribution.
Proves: probs sum to 1, all >= 0, and top score got top prob: YES
Runs in your browser via Pyodide. First run loads the runtime once; no install, no server.
Your softmax passed the tests that define a probability distribution, and the highest score kept the highest probability. This is the exact function at the output of the GPT you build in the LLM course. Next: derivatives, the idea that lets a model improve.
Check your understanding
1. What must a probability distribution satisfy?
2. What does softmax do?
3. Does softmax change which option is most likely?