Probability for Deep Learning
A classifier outputs a probability distribution, dropout is a random variable, a language model is a chain of conditional distributions, and training minimizes an expectation estimated from samples. This lesson collects the probability a deep-learning practitioner uses daily; the full treatment is in Math for ML: Probability and Probability Theory.
Distributions networks output
| Output | Distribution | Network head |
|---|---|---|
| binary label | Bernoulli() | one logit , |
| one of classes or tokens | categorical() | logits, |
| real value | Gaussian(, ) | a mean (and sometimes a log-variance) |
| sequence | product of conditionals | one categorical per position |
The logistic sigmoid squashes a real number into ; its inverse is the logit . The softmax generalizes it to classes, and the softmax of two logits is exactly .
The chain rule of probability
Any joint distribution factors as a product of conditionals:
An autoregressive language model learns each factor with one network that reads the prefix (Language Modeling as Next-Token Prediction). No independence assumption is made; the factorization is exact.
import numpy as np
# A toy "language" over {a, b} with a hand-made next-token model
def next_probs(prefix):
if not prefix: return {"a": 0.7, "b": 0.3}
return {"a": 0.2, "b": 0.8} if prefix[-1] == "a" else {"a": 0.6, "b": 0.4}
def seq_prob(s):
p = 1.0
for t in range(len(s)):
p *= next_probs(s[:t])[s[t]]
return p
from itertools import product
seqs = ["".join(s) for s in product("ab", repeat=3)]
probs = {s: seq_prob(s) for s in seqs}
for s in seqs: print(s, round(probs[s], 4))
print("total over all length-3 sequences:", round(sum(probs.values()), 10))aaa 0.028
aab 0.112
aba 0.336
abb 0.224
baa 0.036
bab 0.144
bba 0.072
bbb 0.048
total over all length-3 sequences: 1.0
Expectations, samples and dropout
Training minimizes the expected loss , estimated by mini-batch averages. Dropout multiplies each activation by an independent Bernoulli mask and rescales by ("inverted dropout") so that the expected activation is unchanged:
At test time dropout is switched off, and thanks to the rescaling no correction is needed.
import numpy as np
rng = np.random.default_rng(0)
h = np.array([1.0, 2.0, 3.0, 4.0])
p = 0.5
samples = np.array([(rng.uniform(size=4) > p) / (1 - p) * h for _ in range(100000)])
print("one dropout sample:", samples[0])
print("average over samples:", np.round(samples.mean(axis=0), 3), " original:", h)
print("variance added per unit:", np.round(samples.var(axis=0), 3), " theory h^2 p/(1-p):", h**2 * p / (1 - p))one dropout sample: [2. 0. 0. 0.]
average over samples: [1.002 1.994 2.983 4.001] original: [1. 2. 3. 4.]
variance added per unit: [ 1. 4. 9. 16.] theory h^2 p/(1-p): [ 1. 4. 9. 16.]
Bayes in deep learning
Bayes' theorem underlies weight decay (a Gaussian prior, see MAP Estimation), and calibrated probabilities matter whenever a model's confidence is acted on: a classifier that says 90% should be right about 90% of the time.
In software
torch.distributions(andjax.random) provideBernoulli,Categorical,Normalwithsample,log_probandentropy.nn.Dropout(p)implements inverted dropout and is disabled bymodel.eval().- Generation with an LLM is repeated sampling from the categorical next-token distribution (Sampling).
Exercises
Exercise 1: Sigmoid and softmax
Show that .
Answer
.
Solution
Divide numerator and denominator by : . A binary classifier with one logit is a 2-class softmax with the second logit fixed at 0.
Exercise 2: Sequence probability
Using the toy model of the first example, compute by hand.
Answer
.
Solution
.
Exercise 3: Dropout at test time (code)
A layer was trained with dropout without inverted scaling. What factor must test-time activations be multiplied by? Check by simulation.
Answer
.
Solution
Without rescaling, the training-time expected activation is , so test-time activations must be scaled by to match.
import numpy as np
rng = np.random.default_rng(1)
h = 5.0
print(round(np.mean((rng.uniform(size=200000) > 0.2) * h) / h, 3))0.801
Test yourself
A -class classifier's output distribution is usually…
- Gaussian
- categorical, via a softmax over logits
- uniform
- Poisson
Inverted dropout divides kept activations by so that…
- the variance is zero
- the expected activation equals the original activation
- more units are dropped
- test time needs dropout too
The factorization requires…
- independent tokens
- no assumption; it is the chain rule of probability
- a Gaussian model
- fixed-length sequences of length 2