TL;DR
A generative model is a probability model of the data itself. Given a family of possible datasets, such as photographs of faces or sentences in English, it assigns each possible example a probability, with plausible examples getting high values and nonsense getting low ones. Because it can do this, it can do two things a simpler model cannot: score how typical a new example is, and draw new examples from the same distribution.
The alternative is a discriminative model, which only learns a rule for a label given an input, such as "is this email spam?". It can answer that one question well, but it has no picture of what emails look like in general.
Where you'll see it: in language models that predict the next word, in image generators that turn a text prompt into a picture, and in anomaly detectors that flag inputs the model finds unlikely.
Intuition
A model that knows what the data looks like
Think of a sommelier who can taste a wine and say whether it is a plausible Burgundy, or whether something is off. Now think of a sommelier who can also describe, from scratch, a wine that a Burgundy grower might plausibly make. The first skill is recognition. The second is generation. A generative model has both, because to generate convincingly it must already know which outcomes are likely and which are not.
A discriminative model is the first sommelier only, and only for one question. Show it a bottle and it answers "red or white?". It may do that very accurately, yet it could not tell you what a red wine is.
The distinction matters because the world is full of questions that are not labels. "Write a paragraph about rivers" has no fixed set of answers to pick from. To write one, the system must hold a distribution over paragraphs and sample from it. That is what a generative model does.
flowchart LR
subgraph Discriminative
X1[Input x] --> M1[Model learns p of y given x] --> Y1[Label y]
end
subgraph Generative
D[Training data] --> M2[Model learns p of x] --> S[Score any x]
M2 --> G[Sample new x]
end
Why learn the whole distribution at all
Learning the full distribution is harder than learning a boundary between classes. A boundary only has to be right where the classes meet. A full distribution has to say something sensible about every input, including inputs the model has never seen. The payoff is flexibility. Once you have , you can ask for samples, fill in missing pixels, estimate how surprising a new example is, or combine the model with a classifier. Most of the modern systems in this graph are one of these ideas, scaled up.
The math
Setup
Let be one example (an image as a vector of pixel values, or a sentence as a sequence of tokens). Let be the parameters of the model, which are the numbers the training process adjusts, such as the weights of a neural network. A generative model is a family of distributions indexed by . Each choice of gives one distribution over all possible .
A generative model is a parametric family of probability distributions over the space of data . For discrete data the density is replaced by a probability mass function. A trained generative model is one value of chosen so that fits the observed data.
Two properties are required. The values must be non-negative, and they must integrate (or, for discrete data, sum) to 1 over all . These constraints are what make the model a distribution, and they are also why designing one is hard. A neural network with arbitrary outputs will not satisfy them unless its architecture is built to.
Training by maximum likelihood
Suppose the training set is , assumed to be independent samples from the unknown true distribution. The standard training rule is maximum likelihood: choose to maximise the log-likelihood
Maximising pushes probability mass toward the training examples. Dividing by gives an average, and the expression then reads as an expected log-probability under the data, which is a cross-entropy between the data distribution and the model. Minimising the negative average log-likelihood is the same as minimising a cross-entropy, so most generative training is supervised learning on the examples themselves.
Factorising a complicated distribution
A distribution over a whole image or a whole sentence has enormous support, so it cannot be written down as a table. Models get around this by factorising it into a product of simpler conditionals using the chain rule of probability (built from conditional probabilities):
Each factor is a distribution over one token given the ones before it. This is the autoregressive view. A language model is exactly this, and the next-token prediction you train it on is the conditional factor. Other models make different structural choices. Some assume the pixels of an image are conditionally independent given a hidden code, which is the idea behind latent variable models. Others define the distribution through a sequence of noising steps that can be reversed, as in diffusion models.
Sampling
Once trained, drawing a new example is a matter of sampling from the learned distribution. For the autoregressive factorisation this is simple: draw from its conditional, then draw given , and so on. Each step is an ordinary draw from a categorical or Gaussian distribution. For latent models the procedure is to draw a hidden code from its prior and then draw the visible example given that code. The quality of the samples depends on how close the model is to the true distribution, and that is where the trade-offs in the next sections come from.
Generative models as classifiers
A generative model can also make predictions about labels. If the data is and the model gives for each class together with a prior , then Bayes' rule gives the posterior:
This is how naive Bayes classifiers work. The classifier is only as good as the class-conditional models, and a poor model of gives a poor classifier, even if the labels are easy to predict directly.
Worked examples
Take three training values , , . The model is with .
- For a Gaussian, the maximum likelihood estimates are the sample mean and the average squared deviation: .
- . Note that the divisor is , not . The maximum likelihood estimate is biased, and the unbiased version is a different estimator.
- The log-likelihood at the estimate is .
- Compare a poor setting, with the same variance. The squared deviations now sum to , giving .
- The estimate scores about 4.3 nats higher, which is why it is the one we keep. The plot below shows the same comparison as curves over the data.
- maximum likelihood: mean 5, σ ≈ 2.94
- poor choice: mean 0, same σ
A toy language model gives these conditional probabilities. The numbers are made up for illustration.
- By the chain rule, .
- Reorder the same words as "cat the sat". Suppose , , and . Then the probability is .
- The model rates the first ordering about 3000 times more likely. It has learned word order without being told any grammar rules.
- To generate, you would sample the first word from its distribution, then the second from its conditional given the first, and so on. Each run gives a different sentence, but the frequent patterns come out more often.
Classify a message as spam or not. Suppose the prior is and . A generative model of messages gives the likelihood of this particular message as and .
- Unnormalised scores: spam ; ham .
- Total: .
- Posterior: .
- The message is about 73% likely to be spam. Notice that the prior alone said 40%. The likelihoods moved it up because this message is about four times more typical of spam.
Tradeoffs and failure modes
Generative models are powerful, but the design choice you make sets what they are good at and where they fail.
| Situation | Problem | Better choice |
|---|---|---|
| You only need a label | Modeling the full distribution wastes capacity and can hurt accuracy on the boundary | Train a discriminative classifier directly |
| You need exact likelihoods and sampling | Many architectures cannot normalise the density, so the likelihood is intractable | Autoregressive models, flows, or models with tractable normalisation |
| You want fast, sharp samples | Some likelihood-based models produce blurry or slow samples | GANs for speed, diffusion models for quality |
| Data has several modes | Simple models collapse onto one mode and drop the rest | Mixtures, latent variable models, or models with richer hidden structure |
| The model must sample from a discrete space | Gradients do not pass through discrete draws | Reparameterised relaxations, or autoregressive training |
Some practical warnings:
- Likelihood is not the same as quality. A model can assign high likelihood to bland, averaged outputs while producing poor-looking samples, or the reverse. Always inspect samples as well as the training loss.
- Memorisation looks like good fit. A high-capacity model can push probability onto its training examples exactly. Held-out likelihood is the check.
- Generative training spends effort on details you may not need. To assign a probability to every pixel, the model must model fine texture, which a classifier never has to do.
- Density values are not probabilities of events. For continuous data, can exceed 1. Compare densities only across models that use the same units.
History
Early generative ideas came from statistics in the late 1800s. Karl Pearson fitted mixtures of normal curves to data in 1894, which was an early density model with several components. Ronald Fisher's work on maximum likelihood in the 1920s gave the training rule that most later models still use.
Naive Bayes classifiers, which are generative models with a conditional independence assumption, became common in the 1950s and 1960s for text and spam-like tasks. They worked well for their size and showed the benefit of modeling how the data was produced.
The modern wave started with neural density models. Restricted Boltzmann machines and deep belief networks, pushed by Geoffrey Hinton and colleagues in the mid-2000s, showed that deep networks could be pretrained as generative models. Variational autoencoders (2013) and generative adversarial networks (2014) then made large-scale generation practical. Autoregressive language models and diffusion models carried the idea to text and images in the following years. Those systems are generative models in the sense used here, even when their architectures look very different.
Check yourself
QuestionA model assigns probability 0.0001 to one sentence and 0.00002 to another. Which does it consider more plausible, and by what factor?Show answer
The first, by a factor of 5. Probabilities are compared by ratio, so . Because the model is a distribution over sentences, this comparison says which sentence it thinks is more typical of its training data.
QuestionWhat is the loss a generative model minimises during standard training, and why is it the same as maximising likelihood?Show answer
The average negative log-likelihood, . The logarithm is monotonic, so maximising the sum of log-probabilities over the data is the same as maximising the probability of the data. Negating turns the maximisation into a minimisation, and the result is the cross-entropy between the data distribution and the model.
QuestionYou want a model that answers whether an email is spam, and you have a generative model of emails. Why might a discriminative classifier still beat it on accuracy, and when could the generative model still be worth having?Show answer
A discriminative model spends all its capacity on the boundary between spam and ham, which is the only thing the task needs. A generative model also spends capacity on modeling every detail of email text, and any error there can hurt the classification. The generative model is still worth having when you also want to detect unusual emails, generate examples, or handle a new class without labelled data.
QuestionA language model assigns probability to a 20-token sentence. Why is it still a sensible way to measure quality, and what would you compare it with?Show answer
The probability of any one specific long sentence is tiny, because there are huge numbers of possible sentences. The sensible measure is the log-probability per token, or the perplexity, which normalises for length. Comparing two models on the same held-out text, the one with lower average negative log-probability per token is the better model of that text.
Sources
- Generative model (Wikipedia): https://en.wikipedia.org/wiki/Generative_model
- Bishop, Pattern Recognition and Machine Learning, §2: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/
- Goodfellow et al., Generative Adversarial Networks (2014): https://arxiv.org/abs/1406.2661
- Kingma and Welling, Auto-Encoding Variational Bayes (2013): https://arxiv.org/abs/1312.6114
- Goodfellow, Bengio, Courville, Deep Learning, Chapter 20: https://www.deeplearningbook.org/