A language model does not output a word. It reads and writes tokens, words or pieces of words, and for the next token it outputs a probability for every choice. Here each token is a whole word, in the toy example from Softmax, Temperature and Decoding.
Figure 1. One step of a language model is a probability for every word.
Each idea in this table is taught in the section listed.
Which word comes next is a random variable, a value chance picks. So is whether dropout zeroes a unit, and so is the rounding error of an ADC, the analog-to-digital converter, the chip that turns a microphone voltage into numbers. A list of values gets a chance per value, the PMF (probability mass function).
next word
chance
running total
mat
0.431
0.431
floor
0.194
0.625
sofa
0.130
0.754
bed
0.096
0.850
roof
0.064
0.915
the other five
0.085
1
An ADC outputs only evenly spaced levels; the gap between neighbouring levels is one stepΔ. It rounds each voltage to the nearest level, so its error is equally likely anywhere in a width Δ centred on 0. A value on a line like this gets a PDF (probability density function) instead. Its flat shape and the bell curve are taught in Common Probability Distributions; here only their areas and running totals matter.
Chances add to 1; for a value on a line, the area under the density is 1.
ΣxP(x) =1
Capital X names the random variable and lowercase x one value it can take. P(x) is the chance that X takes the value x; Σx adds it over every value. The ten words add to 1, so when one gets likelier the others lose the same amount.
∫ p(x) dx =1
∫ … dx measures the area under the density p(x), a height, not a chance. The rounding error is flat at height one over Δ across a width Δ, so its area is 1. Only ranges get chances, so a narrow density can rise above 1. Figure 2 draws the word chances (top) and the flat density (middle).
The running total of chance from the left, the CDF (cumulative distribution function).
F(x) =P(X ≤ x)
x is any value of X, used as a cut point. For a list, F adds the PMF up to x; for a line, it is the area under p up to x. It climbs from 0 to 1 and never goes down. Top-p 0.9, a decoding rule, keeps the likeliest words until their total reaches 0.9: five words, up to roof. The rounding error’s running total reads 0.5 at 0, its centre. The standard bell curve is the hump-shaped density centred on 0 that noise often follows, taught in Common Probability Distributions, section 5. Its running total is written Φ (capital phi), the bottom panel of Figure 2; it reads 0.841 at 1.
An activation function sits after each layer of a network. ReLU, the simplest, passes positive inputs and zeroes negative ones. GELU, used inside models such as BERT and GPT-2, is a smooth ReLU, taught with the other activations in Multilayer Perceptrons. It keeps 0.841 of an input of 1 and turns −1 into −0.159.
GELU weights each input by the bell’s running total at that input.
GELU(x) =x Φ(x)
Here x is one input value, a number fed to the activation. Φ(x) is the bottom curve of Figure 2: near 0 for very negative x and near 1 for large x. So GELU passes large inputs almost unchanged, nearly zeroes very negative ones and bends smoothly in between, where ReLU has a sharp corner at 0.
In dropout, each unit is dropped with chance 0.1, and a kept unit is divided by 0.9, one minus the drop chance (inverted dropout). Take one unit whose output, its activation, is an illustrative 1.5.
outcome
value
chance
dropped
0
0.1
kept
1.5 divided by 0.9, 1.667
0.9
average, 0 × 0.1 + 1.667 × 0.9
1.5
The expected value, also called the mean, is the long-run average: each value times its chance, all added.
E[X] =Σx x P(x)
Each value x is weighted by its chance and the weighted values are added. The table’s average is 1.5, the activation itself and the balance point of Figure 3, so nothing needs rescaling at test time. In training, an expected loss, the error score training pushes down, is estimated by averaging the loss over a batch of examples (cross-entropy, policy gradients).
Figure 3. Each value weighted by its chance; they balance at the mean, 1.5.
Spread is the variance, the average squared distance from the mean; its square root is the standard deviationσ. Attention in Transformers scores a token’s query vector, what it looks for, against another token’s key vector, what it offers, entry by entry.
one query entry times one key entry, independent, each mean 0, variance 1
0
1 × 1 = 1 per product; a score sums 64 products, so 64 before dividing by √64 (section 5)
The variance measures how far a value tends to land from its mean.
Var(X) =E[(X − E[X])²]
X − E[X] is how far one value lands from the mean; squaring counts both sides; E averages. The standard deviation σ is its square root. The ADC page calls the table’s 0.0833 mV² the noise power; Bias and Variance splits a model’s error this way. Figure 4 draws the squared distance and its average for the rounding error.
Figure 4. The average squared distance is the variance; its root is σ = 0.289 mV.
3 Joint, marginal and conditional
A speech or silence detector reads one audio frame, a slice of sound a few tens of milliseconds long, and asks whether it is loud. Write s for speech or silence and o for what is observed, loud or quiet. With illustrative rates, half the frames are speech. A speech frame is loud with chance one half, since speech has pauses, and a silent frame with chance one sixth, from noise bursts.
One table of joint chances gives how often two things happen together, each alone, and one given the other.
P(o) =ΣsP(s, o)
P(s, o), the joint, is one cell of the table in Figure 5. Adding a column over s gives the marginal: the chance of loud is 0.333. When s is never seen, this sum is how a model scores what it does see: the hidden s is speech or silence in a hidden Markov model, the cluster that made a point in a Gaussian mixture model (GMM), and the alignment that says which audio frame spells which letter in Connectionist Temporal Classification (CTC), a speech-recognition loss. Expectation maximization (EM) fits models with hidden values like these.
P(o | s) =P(s, o)P(s)
The bar reads given: the conditional keeps one row and rescales it to add to 1. Loud given speech is 0.5, marker 1 in Figure 5. Swapping the roles gives P(s | o): speech given loud is 0.75, marker 2. So P(loud | speech) and P(speech | loud) are different numbers.
P(s, o) =P(o | s)P(s)
The product rule says chance first picks speech or silence, s, then loud or quiet given it, o. Half the frames are speech and half of those are loud, which gives the 0.25 cell.
A language model scores a whole reply the same way, one token at a time (decoding). In Figure 1 the four rare words moon, piano, Tuesday and “of” together get about 0.042. If every token had Figure 1’s chances, whatever came before, each would avoid them with chance 0.958. Models often keep the natural log of a chance, written ln. The ln of a number is the power you raise e ≈ 2.718 to in order to get that number, so ln 0.431 is −0.84.
reply
chance
“mat” after “the cat sat on the”
0.431, ln −0.84
20 tokens with no rare word
0.95820 = 0.42
100 tokens with no rare word
0.958100 = 0.014
The chain rule of probability builds a joint one variable at a time, each given everything before it.
wt is token t of T. The dots … stand for the tokens in between and ⋯ for the factors in between. Each factor is the product rule again, and the history behind the bar grows by one token per factor. Many chances below 1 multiplied together shrink fast: the chance of a reply that avoids every rare word falls from 0.42 at 20 tokens to 0.014 at 100. The log of the product is a sum of per-token logs, so models add log chances, like −0.84 for mat, instead of multiplying tiny numbers. Figure 6 draws the fall.
Figure 6. Multiplying 0.958 once per token shrinks the chance fast.
a bigram model, which looks one word back; the hidden states of an HMM
none
P(wt)
independent data points, which do not affect each other (section 5), so the joint is a product and its log a sum (likelihood, naive Bayes)
Backpropagation has a chain rule too, the calculus one for derivatives: same name, different rule.
4 Bayes rule flips a conditional
An always-on keyword spotter listens for a wake word, often with a small gated recurrent network (gated RNN). Each firing without the word is a false alarm, and on-device spotters are judged by false alarms per hour. When the spotter fires, is the word there? Write A for the cause, the word, and B for the evidence, a firing. The table’s rates are illustrative.
piece
value
the word is in 1 clip in 100, P(A)
0.01
the spotter fires on a clip with the word, P(B | A)
0.9
it fires on a clip without it, P(B | not A)
0.09
firings with the word, 0.9 × 0.01
0.009
false alarms, 0.09 × 0.99
0.0891
share of firings that hold the word, 0.009 out of 0.009 + 0.0891
0.092
Bayes rule turns the chance of the evidence given a cause into the chance of the cause given the evidence.
P(A | B) =P(B | A)P(A)P(B)
P(B | A), the likelihood, is how often the spotter fires when the word is there. P(A), the prior, is how common the word is before any firing. P(A | B), the posterior, is the product rule written both ways and solved for the cause. A rare word keeps the numerator small, so the posterior is only the table’s 0.092; Figure 7 counts it out in clips.
P(B) =P(B | A)P(A)+ P(B | not A) P(not A)
The denominator adds every way to see a firing: real firings with the word, then false alarms without it, in gray as in Figure 7. With 99 clips in 100 empty, the false alarms outnumber the real firings about ten to one.
To pick only the most likely cause, compare the numerators, since the denominator is the same for every cause. Naive Bayes picks the topic of a forum post, its class, this way, by an argmax, the choice with the largest score, the rule greedy decoding uses for words.
Counting clips instead of chances, the frequency formats of Gigerenzer and Hoffrage (1995), makes the flip visible.
Figure 7. 1,000 clips; the bar splits the firings.
A rare cause needs strong evidence. Put the speech or silence detector of section 3 in a quiet room where only 1 frame in 100 is speech (illustrative). The odds of speech are its chance divided by the chance of silence, and each loud frame multiplies them by 3, since a speech frame is loud with chance one half and a silent frame with chance one sixth. Each frame is scored on its own here; an HMM lets speech or silence carry over from step to step. The odds start at 1 to 99; dividing the odds by 1 plus the odds gives the chance back.
loud frames
odds of speech, times 3 each frame
chance of speech
0
0.0101
1%
1
0.030
3%
2
0.091
8%
3
0.27
21%
4
0.82
45%
5
2.45
71%
Figure 8. Speech chance after loud frames. 1After five, speech is likelier.
The snippet computes the keyword table’s posterior. prior is the chance of the word, fire_if_word and fire_if_none are the two firing rates, and fire is the denominator.
1The prior, the likelihood and the false-alarm rate of Figure 7.
2Every way to fire, the Bayes card’s second line.
3The posterior, the Bayes card’s first line.
5 Independence and correlation
Dropout draws each unit’s keep mask, a 1 that keeps the unit or a 0 that zeroes it, on its own (Srivastava et al. 2014). Knowing one mask says nothing about the other, so the two masks are independent.
Two words in a post are not independent. The 20 Newsgroups dataset collects posts from Usenet topic forums, such as comp.windows.x, about the windowing system of Unix computers, and rec.sport.hockey. “Port” and “motif”, a window toolkit, both show up in comp.windows.x posts. Take 20 illustrative posts, 10 from each forum. In the table, X and Y are a pair’s two yes-or-no variables, unit 1 and unit 2 kept, or port and motif in a post, and x and y are their yes values.
Figure 9. Equal rows mean independent. 1Dependent.2Independent in one topic.
pair
P(x)P(y)
P(x, y)
two keep masks
0.9 × 0.9 = 0.81
0.81
port and motif, all 20 posts
0.3 × 0.3 = 0.09
0.15
port and motif, comp.windows.x only
0.5 × 0.6 = 0.3
0.3
Independence turns a whole table into two short lists, because the joint becomes a product.
P(x, y) =P(x) P(y)
Knowing X no longer changes the chances of Y, so every row of the table has the same shape. The two masks pass, the matching rows of Figure 9. Port and motif across all 20 posts fail, 0.15 against 0.09, so they are dependent.
P(x, y | c) =P(x | c) P(y | c)
c is the class. When the product holds inside each class, X and Y are conditionally independent given c, as port and motif are within comp.windows.x. Naive Bayes assumes this for all words; CTC treats each frame’s prediction as independent of the others given the audio.
Correlation asks a weaker question, whether two values rise together along a straight line. Take one second of a 440 Hz tone, 440 cycles per second, sampled at 16 kHz, 16,000 numbers per second.
pair
correlation
independent
a sample and the next one
0.985
no; a signal against its own shifted copy is its autocorrelation
the tone and microphone noise
about 0
yes
the tone and its square
0
no, the tone fixes its square
Figure 10. Correlation 10.985,2about 0, 30, yet the tone fixes its square exactly.
Sums also need the covariance: how much two values rise and fall together along a line.
sum
variance
a query-key score, the dot product of a query and a key of length dk = 64, so 64 products with covariance 0 between them, each of variance 1
64, so σ = 8; divided by √64 = 8, back to 1
illustrative pair, variances 1 and 1, covariance 0.5
3, not 2
Covariance measures how two values move together; correlation is covariance without units; covariance decides how variances add.
Cov(X, Y) =E[(X − E[X])(Y − E[Y])]
Each bracket is a distance from its own mean; the product is positive when both land on the same side. Var(X) is Cov(X, X).
ρ =Cov(X, Y)σX σY
Dividing by both standard deviations gives the correlationρ (rho), from −1 to 1. It is the cosine between the samples of X and the samples of Y, each list with its mean subtracted. Independent implies ρ = 0; ρ = 0 does not imply independent (Goodfellow et al. 2016, sec. 3.8). Figure 10 shows all three cases.
Var(X + Y) =Var(X) + Var(Y)+ 2 Cov(X, Y)
When X and Y are uncorrelated, the covariance term is 0, so the variances simply add: the 64 products give 64. That is why attention divides its scores by √dk, the square root of the key length (Vaswani et al. 2017).
P(fires | word) is not P(word | fires), so never flip the conditional. In Figure 7 the spotter fires on 9 of the 10 clips with the word, yet only 9 of its 98 firings hold the word.
Zero correlation does not mean independent: the tone and its square, Figure 10.
A density above 1 is not a bug; only areas must stay at or below 1.
A row or slice is a conditional only after you rescale it by its total.
One zero count in a small table wipes out a whole product; smoothing, adding a small count to every cell, fixes it (Naive Bayes).
each word’s chance given the topic, multiplied over the post’s words, by conditional independence
posterior P(A | B)
each topic’s chance after reading the words; the argmax is the predicted topic
8 References
Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. Sec. 1.2 (probability rules). PDF.
Gigerenzer, G., and Hoffrage, U. (1995). How to improve Bayesian reasoning without instruction: frequency formats. Psychological Review, 102(4), 684 to 704.
Goodfellow, I., Bengio, Y. and Courville, A. (2016). Deep Learning, Chapter 3, Probability and Information Theory. MIT Press.