Contents
09Interview questions and model answers
Q1What is the difference between a generative and a discriminative model?
A generative model learns how the data are produced, the joint distribution p(x,y)=p(y)\,p(x\mid y) (or p(x) without labels), so it can sample new data and classify with Bayes’ theorem. A discriminative model learns only p(y\mid x) or a direct map from x to y: the boundary between classes. A painter versus a critic. Examples: naive Bayes and Gaussian discriminant analysis versus logistic regression and support vector machines.
Q2How does a generative model classify? Work an example.
With Bayes’ theorem, p(y\mid x)=p(x\mid y)\,p(y)/p(x), picking the class with the largest p(x\mid y)\,p(y). If p(x\mid\text{cat})=0.30, p(x\mid\text{dog})=0.05 and the priors are equal, p(\text{cat}\mid x)=0.15/0.175=0.857. With a prior of 0.2 for cats it drops to 0.6.
Q3Is naive Bayes generative or discriminative? And logistic regression? How are they related?
Naive Bayes is generative: it models p(y) and p(x\mid y)=\prod_dp(x_d\mid y). Logistic regression is discriminative. For binary features the naive Bayes posterior is exactly \sigma(w^\top x+b) with w_d=\log\frac{\theta_{d1}(1-\theta_{d0})}{\theta_{d0}(1-\theta_{d1})}, the same form as logistic regression; they form a generative–discriminative pair. The difference is the fit: naive Bayes maximizes the joint likelihood (counting), logistic regression the conditional likelihood (convex optimization).
Q4Show that Gaussian classes with a shared covariance give a logistic posterior.
The log-odds is \log\frac{\pi_1\mathcal N(x\mid\mu_1,\Sigma)}{\pi_0\mathcal N(x\mid\mu_0,\Sigma)}; the normalizing constants and the quadratic terms x^\top\Sigma^{-1}x cancel, leaving w^\top x+b with w=\Sigma^{-1}(\mu_1-\mu_0) and b=-\frac12\mu_1^\top\Sigma^{-1}\mu_1+\frac12\mu_0^\top\Sigma^{-1}\mu_0+\log\frac{\pi_1}{\pi_0}. So p(y=1\mid x)=\sigma(w^\top x+b). With different covariances the boundary is quadratic. The converse is false: a logistic posterior does not imply Gaussian classes.
Q5Which one needs less data? Which is more accurate?
Ng and Jordan: naive Bayes approaches its asymptotic error with on the order of \log n examples (n features), logistic regression needs on the order of n, but logistic regression’s asymptotic error is lower. So the generative model often wins with small data and the discriminative one with large data. On handwritten 4s and 9s the curves cross at about 23 examples; with 200 the errors are 1.7\% and 0.8\%. Bias–variance: strong assumptions mean high bias, low variance.
Q6Can a discriminative model generate data?
No. p(y\mid x) contains no information about p(x), and without p(x) there is no p(x,y) to sample from. To generate, you need a model of the inputs. (A GAN uses a discriminator to train a generator, but the discriminator itself cannot produce samples.)
Q7Which one can handle missing features or detect outliers?
The generative model. A missing feature is marginalized, p(y\mid x_2)\propto p(y)\,p(x_2\mid y); an outlier has a tiny p(x). A discriminative model always outputs a label, even for an input unlike anything it has seen. Note that the outlier test must use p(x), not the posterior.
Q8How does a diffusion model work?
The forward process adds Gaussian noise over T steps; in closed form x_t=\sqrt{\bar\alpha_t}\,x_0+\sqrt{1-\bar\alpha_t}\,\epsilon. A network \epsilon_\theta(x_t,t) is trained to predict the added noise with the loss \mathbb E\|\epsilon-\epsilon_\theta(x_t,t)\|^2. To generate, start from pure noise and repeatedly subtract a little predicted noise, x_T\to x_{T-1}\to\dots\to x_0. DDPM uses T=1000; fast deterministic samplers need tens of steps, and latent diffusion runs the process in a compressed latent space.
Q9Compare diffusion and flow matching. Why can flow matching sample in fewer steps?
Both move samples from a simple noise distribution to the data distribution. Diffusion defines the path by a noise schedule; viewed as an ODE its trajectories are curved. Flow matching trains a velocity field on straight lines x_t=(1-t)x_0+t\,x_1 with target x_1-x_0, and the learned flow is much straighter. Euler’s error grows with curvature and is zero on a straight constant-speed path, so fewer, larger steps suffice. In a one-dimensional example, with four Euler steps the error was 0.118 for flow matching against 0.307 for diffusion. Pairing noise and data without crossings (rectified flow) makes the paths straight.
Q10What is a GAN, and what is its optimal discriminator?
A generator turns noise into samples, a discriminator classifies real versus fake, and they play \min_G\max_D\,\mathbb E\log D(x)+\mathbb E\log(1-D(G(z))). For a fixed generator the optimal discriminator is D^\star(x)=p_{\text{data}}(x)/(p_{\text{data}}(x)+p_g(x)), Bayes’ theorem with equal priors, and at that point the game minimizes the Jensen–Shannon divergence between data and generator, -\log4+2\,\mathrm{JSD}, which is smallest when p_g=p_{\text{data}}.