MoonSay /Tutorials/Machine Learning Interview #16
Contents
  1. 01Introduction
  2. 02Bayes’ rule, part by part
  3. 03A generative classifier
  4. 04The naive assumption
  5. 05Every pixel votes
  6. 06Why it still works
  7. 07Interview follow-ups
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Naive Bayes/Chapter 03 of 10

03A generative classifier

Chapter 2 used one pixel as the evidence. Now the evidence is all 64 pixels, and Bayes’ rule becomes a way to build a whole classifier.

3.1Learn how each class generates its features

Definition 1 (Generative and discriminative classifiers, Murphy, 2022, §9.4). A generative classifier models how each class generates its features, P(x\mid c), together with the prior P(c); it classifies by flipping these around with Bayes’ rule (1). A discriminative classifier models the class given the features, P(c\mid x), directly.

Naive Bayes is generative; logistic regression (Episode 15) is discriminative. The name “generative” is literal: once we know P(c) and P(x\mid c), we could generate new images, by first drawing a digit and then drawing pixels for it. Naive Bayes is a method for classification: the answer is one of a fixed set of classes, not a number (regression) and not a grouping of unlabeled data (clustering). For our digits it computes the posterior of each of the ten digits given all the pixels,

P(c\mid x)=\frac{P(x\mid c)\,P(c)}{\sum_{c'}P(x\mid c')\,P(c')},\qquad x\in\{0,1\}^{64}. \tag{4}
The data of Alpaydin and Kaynak (1998). (a) One training image as ink counts from 0 to 16. (b) The same image after the on/off rule (on if the count is 8 or more). (c) Three training images of each digit, on/off: images of the same digit differ in many pixels. Computed for this tutorial, with the same data as the companion video.
Figure 2. The data of Alpaydin and Kaynak (1998). (a) One training image as ink counts from 0 to 16. (b) The same image after the on/off rule (on if the count is 8 or more). (c) Three training images of each digit, on/off: images of the same digit differ in many pixels. Computed for this tutorial, with the same data as the companion video.

3.2The prior is easy, the likelihood is not

The prior is a count. The maximum likelihood estimate is the share of each digit in the training data, \hat\pi_c=N_c/N (Murphy, 2022, §9.3.2); for our digits each is about a tenth, since there are 122 to 128 training images of each.

The likelihood of a whole image is another matter. Sixty-four on/off pixels make 2^{64}\approx1.8\times10^{19} possible images, about eighteen billion billion. To estimate P(x\mid c) by counting, we would need to see each of those images many times for each digit, but we have about 126 images of each digit. Almost every possible image has a count of zero, and so do the images we actually need: images of the same digit differ in many pixels (Figure 2c), and only 17 of the 540 test images appear exactly anywhere in the training data. Counting whole images would give a new image probability zero under every digit, and the posterior (4) would be 0/0.

Pause and think. A discriminative model only needs P(c\mid x). Why can a generative model be the harder thing to learn?

Show the answer

It has to describe how every pixel pattern arises for every class, a distribution over 2^{64} images, while the decision only needs the boundary between classes; discriminative models do not “waste effort” on the features (Murphy, 2022, §9.4.1). In return, generative models are easy to fit by counting once we make an assumption (next chapter) (Murphy, 2022, §9.4.2).

So the prior is easy, but P(x\mid c) cannot be counted. How do we estimate the likelihood?