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

07Beyond accuracy: what only a generative model can do

7.1Missing features

Suppose a photo hides the snout, so only ear pointiness x_2 is known. A generative model simply marginalizes the missing feature (Murphy, 2022, §9.4.3):

p(y\mid x_2)\;\propto\;p(y)\int p(x_1,x_2\mid y)\,dx_1\;=\;p(y)\,p(x_2\mid y). \tag{24}

For a Gaussian class model the marginal p(x_2\mid y) is again Gaussian, with the corresponding entries of \mu_y and \Sigma_y; for naive Bayes one just leaves out the factor of the missing feature. A discriminative model p(y\mid x_1,x_2) has no principled answer: it needs x_1, and filling in a guess is a separate modelling decision.

Worked example 5 (Ears only). In the Gaussian class models of Figure 1(a), ear pointiness is \mathcal N(0.695,\,0.111^2) for cats and \mathcal N(0.323,\,0.103^2) for dogs. For a photo with ear pointiness 0.55 the two densities are 1.524 and 0.351, and with equal priors (24) gives p(\text{cat}\mid x_2=0.55)=1.524/(1.524+0.351)=0.813.

7.2Outliers and novelty

A discriminative model always answers: every input lands on one side of the boundary. A generative model also knows p(x)=\sum_c\pi_c\,p(x\mid y=c), so it can notice an input that is unlike anything it was trained on.

Worked example 6 (A fox). Show both models of Figure 1 an animal with a long snout and very pointy ears, x=(0.95,0.95), say a fox. Logistic regression says p(\text{cat}\mid x)=0.38, “a dog, with 62\% probability”. The generative model’s posterior is no better (it says “cat” with probability 1.00, because the fox is less extreme for the cat model than for the dog model), but its density gives the fox away: p(x)=1.6\times10^{-8}, against at least 0.70 for every one of the 26 training animals (median 3.7). The fox is 6.3 standard deviations (in Mahalanobis distance) from the cats and 8.3 from the dogs. The lesson: detect outliers with p(x), not with the posterior.

7.3More that the painter can do

  • Generate. Sample new examples from p(x\mid y): data augmentation, simulation, and everything in Section 4.
  • Add a class without retraining. Each class model is fitted on its own (5), so a new class only needs its own model; in a discriminative model all parameters interact (Murphy, 2022, §9.4.2).
  • Use unlabelled data. Unlabelled inputs still carry information about p(x)=\sum_c\pi_cp(x\mid y=c), which a generative model can use (semi-supervised learning, for example with the EM algorithm).

7.4What the critic does better

  • Accuracy with enough data (Section 6).
  • Arbitrary features. Correlated, engineered or learned features are no problem, because their distribution is never modelled.
  • Calibrated probabilities. Naive Bayes counts correlated evidence several times. If a feature is copied k times, (8) adds its weight k times and the posterior is pushed towards 0 or 1; logistic regression simply spreads the same total weight over the copies. Naive Bayes’ probabilities are therefore often too extreme, and logistic regression’s better calibrated (Murphy, 2022, §9.4.1).

Pause and think. For the fox, the generative posterior says “cat” with probability 1.00. Is that a contradiction with the claim that generative models can detect outliers?

Show the answer

No. The posterior only compares the classes with each other, p(\text{cat}\mid x)\propto\pi_{\text{cat}}\,p(x\mid\text{cat}), and the fox is less unlikely as a cat than as a dog. Detection uses the size of p(x) itself, which the posterior divides away.