MoonSay /Tutorials/Machine Learning Interview #6
Contents
  1. 01Introduction
  2. 02Two ways to learn
  3. 03Bayes’ theorem
  4. 04What generative models create
  5. 05What discriminative models do
  6. 06Who wins? Small data versus big data
  7. 07Beyond accuracy
  8. 08Side by side
  9. 09Interview questions and model answers
  10. 10Summary
  11. References

02Two ways to learn: the painter and the critic

2.1Three distributions

Every labelled data set is a sample from a joint distribution p(x,y). By the product rule it factorizes in two ways:

p(x,y)\;=\;\underbrace{p(y)}_{\text{prior}}\;\underbrace{p(x\mid y)}_{\text{class model}}\;=\;\underbrace{p(x)}_{\text{inputs}}\;\underbrace{p(y\mid x)}_{\text{posterior}}. \tag{1}

The two sides suggest the two ways to learn.

Definition 1 (Generative and discriminative classifiers). A generative classifier models p(x,y)=p(y)\,p(x\mid y): a prior over classes and a model of how inputs are distributed within each class. It can generate examples x from each class y, and it classifies with Bayes’ theorem. A discriminative classifier models only p(y\mid x), or a direct map from x to y; it can only discriminate between classes (Murphy, 2022, §9.4).

More broadly, a generative model is any model of how the data are distributed, p(x) or p(x\mid\text{condition}), from which we can sample: a language model is a model of p(\text{text}), an image generator a model of p(\text{image}\mid\text{prompt}). The prompt plays the role of y.

2.2The cats and dogs

Figure 1 shows both views on a toy problem with two features, snout length and ear pointiness. The generative model (a) fits one Gaussian to the 13 cats and one to the 13 dogs. Because it has a full description of each class, it can draw new animals from it (the stars): this is what “generative” means. The discriminative model (b) fits logistic regression, which learns only where cats turn into dogs. It reports p(\text{cat}\mid x)=0.66 for the new photo at x=(0.16,0.30), but it has no idea what a cat looks like away from the boundary.

The painter and the critic on illustrative data (the same points as in the companion video). (a) A generative model: one Gaussian per class, drawn as its 1 and 2 standard-deviation ellipses, and new animals sampled from the model. (b) A discriminative model: logistic regression’s p(\text{cat}\mid x), with the boundary p=0.5 and the contours 0.1 and 0.9. Computed for this tutorial.
Figure 1. The painter and the critic on illustrative data (the same points as in the companion video). (a) A generative model: one Gaussian per class, drawn as its 1 and 2 standard-deviation ellipses, and new animals sampled from the model. (b) A discriminative model: logistic regression’s p(\text{cat}\mid x), with the boundary p=0.5 and the contours 0.1 and 0.9. Computed for this tutorial.

2.3More examples of the two views

Language. To tell English from French, a generative approach learns a model of English text and a model of French text and asks which one makes a sentence more likely. A discriminative approach learns cues that separate them (“the”, “les”, accents) and nothing else.

Elephants and dogs. The classic example of the Stanford CS229 notes: a discriminative algorithm such as logistic regression looks for a line that separates elephants from dogs; a generative algorithm builds a model of what elephants look like and a model of what dogs look like, and matches a new animal against both (Ng and Ma, 2023).

Medicine. A generative model describes how blood tests are distributed among healthy and sick patients, which also tells a doctor that a particular result is unusual for both. A discriminative model gives the probability of disease given the test results, which is often all that is needed for the decision.

2.4The posterior can be much simpler than the classes

Modelling p(x\mid y) means modelling everything about the inputs, including details that do not matter for the decision. In Figure 2, class 1 has two bumps, so its density needs at least two components. The posterior p(y\mid x), however, is a single smooth step at x\approx0.77: the left bump, far from class 2, does not influence the decision at all. A discriminative model never spends parameters on it. This is the main argument for discriminative models (Bishop, 2006, §1.5.4), (Murphy, 2022, §9.4.1).

Complicated class densities, simple posteriors (equal priors). (a) Class 1 has two bumps. (b) The posteriors are a single step; the left bump of class 1 makes no difference to the decision. In the spirit of Bishop (2006, Fig. 1.27); computed for this tutorial.
Figure 2. Complicated class densities, simple posteriors (equal priors). (a) Class 1 has two bumps. (b) The posteriors are a single step; the left bump of class 1 makes no difference to the decision. In the spirit of Bishop (2006, Fig. 1.27); computed for this tutorial.

Pause and think. A model gives you p(y\mid x) for every x. Can you compute p(x) from it? Can you compute p(x\mid y)?

Show the answer

No. By (1), p(x,y)=p(x)\,p(y\mid x), and p(y\mid x) says nothing about p(x): any input distribution is compatible with the same posterior. Without p(x) we cannot get p(x,y) or p(x\mid y), and so we cannot generate inputs.