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

05What discriminative models do

5.1Logistic regression

Logistic regression models the posterior directly (Cox, 1958), (Murphy, 2022, §10.2):

p(y=1\mid x)=\sigma(w^\top x+b)=\frac1{1+e^{-(w^\top x+b)}}. \tag{21}

Taking the log-odds, \log\frac{p(y=1\mid x)}{p(y=0\mid x)}=w^\top x+b: the model says that the log-odds is linear in x, and nothing about how x itself is distributed.

The loss. Write \mu_n=\sigma(w^\top x_n+b) and absorb b into w by appending a constant feature 1 to each x_n. The negative log conditional likelihood of labels y_n\in\{0,1\} is the binary cross-entropy:

\mathrm{NLL}(w)=-\sum_{n=1}^N\big[y_n\log\mu_n+(1-y_n)\log(1-\mu_n)\big]. \tag{22}

The gradient, step by step. The sigmoid satisfies \sigma'(a)=\sigma(a)(1-\sigma(a)), so \partial\mu_n/\partial w=\mu_n(1-\mu_n)\,x_n. Then

\begin{aligned} \frac{\partial}{\partial w}\big[y_n\log\mu_n+(1-y_n)\log(1-\mu_n)\big]&=\Big[\frac{y_n}{\mu_n}-\frac{1-y_n}{1-\mu_n}\Big]\mu_n(1-\mu_n)\,x_n\\ &=\big[y_n(1-\mu_n)-(1-y_n)\mu_n\big]x_n=(y_n-\mu_n)\,x_n, \end{aligned}

and therefore

\nabla_w\mathrm{NLL}(w)=\sum_{n=1}^N(\mu_n-y_n)\,x_n,\qquad \nabla_w^2\,\mathrm{NLL}(w)=\sum_{n=1}^N\mu_n(1-\mu_n)\,x_nx_n^\top. \tag{23}

The gradient is the prediction error times the input, the same form as in linear regression. The Hessian is a sum of positive semidefinite matrices, so the loss is convex: gradient descent or Newton’s method finds the global minimum. Unlike the generative fit (5), there is no closed form. If the classes are perfectly separable, \|w\| grows without bound, which is why a penalty such as \lambda\|w\|^2 is usually added; the logistic regression of Example 3 uses one, which explains its smaller weights.

5.2Other discriminative models

Support vector machines (Cortes and Vapnik, 1995) also learn a linear boundary (or a nonlinear one with kernels), but with the hinge loss and a maximum margin; they output a decision, not a probability. Decision trees (Quinlan, 1986) split the input space by questions such as “is the snout longer than 0.5?”, and random forests average many of them (Breiman, 2001). Neural network classifiers end in a softmax, p(y=c\mid x)=e^{a_c}/\sum_{c'}e^{a_{c'}}, the multi-class version of (21), applied to features the network learns itself. All of them model the boundary and nothing else, which is also why they can use arbitrary features (word embeddings, polynomial terms, outputs of other models) without having to describe how those features are distributed (Murphy, 2022, §9.4.1).

5.3Where they are used

Most deployed classifiers are discriminative: spam filters (today mostly discriminative, although the classic one was naive Bayes), photo labelling, reading medical images, credit scoring and fraud detection. A real case at large scale: Google’s ad click prediction system, described by McMahan et al. (2013), predicts the probability that an ad is clicked with logistic regression trained online, at very large scale.

5.4Counting parameters

The discriminative model also needs fewer parameters. With M features, logistic regression has M+1 parameters. Gaussian discriminant analysis with a shared covariance has 2M for the means, M(M+1)/2 for the covariance and 1 for the prior, M(M+5)/2+1 in total, growing quadratically with M (Bishop, 2006, §4.3.2). For the M=64 pixels of the digits that is 65 against 2{,}209; with separate covariances it is 4{,}289, and Bernoulli naive Bayes needs 2M+1=129.

Pause and think. Logistic regression’s gradient (23) is \sum_n(\mu_n-y_n)x_n. Which training examples contribute little to it once the model is good?

Show the answer

Those it already predicts confidently and correctly, where \mu_n\approx y_n. The gradient is driven by the misclassified and uncertain examples near the boundary, a soft version of the support vectors of a support vector machine.