4.5.1Generative Models

Generative vs discriminative models

1,770 words8 min readdifficulty · medium1 backlinks

WHY this matters: Discriminative models answer "which class?". Generative models answer "how is this data created?" – giving them superpowers like data synthesis, anomaly detection, and handling missing features.

Mathematical Foundation

The Probability Story

Deriving the Connection (From First Principles)

WHY start with Bayes' theorem? Because it's the bridge between generative and discriminative approaches.

Bayes' theorem states: P(yX)=P(Xy)P(y)P(X)P(y|X) = \frac{P(X|y) \cdot P(y)}{P(X)}

WHY each term?

  • P(Xy)P(X|y) likelihood – "if it's a cat, what features would we see?"
  • P(y)P(y) = prior – "how common are cats in general?"
  • P(X)P(X) = evidence – "how likely is this feature combination overall?"
  • P(yX)P(y|X) = posterior – "given these features, what class is it?"

The Key Insight: P(X)=yP(Xy)P(y)P(X) = \sum_{y'} P(X|y') \cdot P(y')

WHY this sum? Because the data XX could come from ANY class, so we marginalize (sum) over all possibilities.

This gives us: P(yX)=P(Xy)P(y)yP(Xy)P(y)P(y|X) = \frac{P(X|y) \cdot P(y)}{\sum_{y'} P(X|y') \cdot P(y')}

Discriminative approach: Model P(yX)P(y|X) directly with a function f(X)yf(X) \to y.

Generative approach: Model P(Xy)P(X|y) and P(y)P(y) separately, then compute P(yX)P(y|X) using Bayes.

Figure — Generative vs discriminative models

Detailed Examples

Comparison Table

Aspect Discriminative Generative
Models P(yX)P(y\|X) P(Xy)P(X\|y) and P(y)P(y)
Goal Decision boundary Data distribution
Examples Logistic Regression, SVM, Neural Nets Naive Bayes, GM, VAE, GAN
Data Efficiency More efficient (fewer params) Needs more data
Can Generate ❌ No ✅ Yes
Missing Features Struggles Handles via marginalization
Asymptotic Accuracy Higher (focuses on boundary) Lower (models everything)

WHY discriminative often wins? It only models the decision boundary, not the entire data distribution. Fewer parameters = less data needed = better generalization when data is abundant.

WHY generative is powerful? It understands the data's structure, enabling synthesis, semi-supervised learning, and robustness.

Recall Explain to a 12-Year-Old

Imagine you're learning to tell apart dogs and cats in photos.

Discriminative way: You learn rules like "if it has pointy ears and whiskers, it's probably a cat." You ONLY care about telling them apart. You can't draw a realistic cat or dog, but you're REALLY good at the yes/no decision.

Generative way: You study EVERYTHING about how cats and dogs look – how their fur grows, ear shapes, typical colors. Now you can DRAW realistic cats and dogs from scratch. You can also identify them (by comparing your drawing knowledge), but it takes more effort to learn.

So discriminative = faster learner for identification. Generative = deeper understanding, can create new examples.

Connections

#flashcards/ai-ml

What is the key difference between generative and discriminative models? :: Discriminative models learn P(yX)P(y|X) (decision boundaries), while generative models learn P(Xy)P(X|y) and P(y)P(y) (how data is generated).

Write Bayes' theorem for classification :: P(yX)=P(Xy)P(y)P(X)=P(Xy)P(y)yP(Xy)P(y)P(y|X) = \frac{P(X|y) \cdot P(y)}{P(X)} = \frac{P(X|y) \cdot P(y)}{\sum_{y'} P(X|y') \cdot P(y')}

Name three generative models
Naive Bayes, Gaussian Mixture Models (GMM), Variational Autoencoders (VAE), GANs
Name three discriminative models
Logistic Regression, Support Vector Machines (SVM), Neural Networks, Decision Trees
What is the prior P(y)P(y) in a generative model?
The probability of class yy before seeing any features (e.g., base rate of spam emails)
What is the likelihood P(Xy)P(X|y)?
The probability of observing features XX given that the true class is yy (how class yy generates data)
Why do discriminative models typically need less data?
They model only the decision boundary, not the full data distribution, requiring fewer parameters
What is one advantage of generative models over discriminative?
Can generate new synthetic data, handle missing features, perform semi-supervised learning
What does the "naive" in Naive Bayes assume?
Features are conditionally independent given the class: P(Xy)=iP(xiy)P(X|y) = \prod_i P(x_i|y)
In spam classification, what does a generative model learn?
P(wordsspam)P(\text{words}|\text{spam}) and P(wordsnot spam)P(\text{words}|\text{not spam}) – the word distributions for each class
How do you classify with a generative model?
Compute P(yX)P(Xy)P(y)P(y|X) \propto P(X|y) \cdot P(y) for each class and pick the highest
What is marginalization in the context of P(X)P(X)?
Summing over all possible classes: P(X)=yP(Xy)P(y)P(X) = \sum_y P(X|y) P(y)
Why does softmax appear in discriminative neural networks?
Converts raw scores (logits) into valid probabilities that sum to 1
If you only care about classification accuracy with large data, which is better?
Discriminative models (more sample-efficient, asymptotically more accurate)

Give one scenario where generative models excel :: When you need to generate new samples, perform density estimation, or leverage unlabeled data

Concept Map

models directly

models

models

combined via

combined via

yields

marginalizes over classes

example

example

enables

Bayes theorem

Discriminative model

Generative model

Posterior P y given X

Likelihood P X given y

Prior P y

Evidence P X

Logistic Regression

Naive Bayes

Data synthesis, anomaly detection

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Samjho ki tumhe photos mein se cats aur dogs ko identify karna hai. Do tarike hain.

Discriminative model (jaise Logistic Regression ya Neural Network) sirf yeh seekhta hai ki "boundary kahan hai" –agar pointy ears hain aur ch

Go deeper — visual, from zero

Test yourself — Generative Models

Connections