Skip to content
E
Educatum

Difference between generative and discriminative model

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Generative models learn the full joint probability distribution P(X, Y) = P(X|Y)P(Y), enabling them to synthesize new examples and classify via Bayes' theorem. Discriminative models learn the conditional distribution P(Y|X) or a direct decision boundary without modeling the input distribution P(X), often achieving higher predictive accuracy when labeled data is abundant.

Study Card: Generative vs. Discriminative Models

Direct Answer

Generative and discriminative models differ in what probability distribution they estimate for prediction tasks. Generative models learn the joint distribution P(X,Y)=P(X∣Y)P(Y)P(X, Y) = P(X \mid Y)P(Y) (or P(X)P(X) in unsupervised generation), modeling how features XX are generated conditioned on each class YY, and classify via Bayes’ theorem:

P(Y=y∣X=x)=P(X=x∣Y=y) P(Y=y)∑y′P(X=x∣Y=y′) P(Y=y′)P(Y = y \mid X = x) = \frac{P(X = x \mid Y = y)\,P(Y = y)}{\sum_{y'} P(X = x \mid Y = y')\,P(Y = y')}

Because they model P(X)P(X), generative models can sample synthetic data, handle missing features, and detect out-of-distribution inputs. Discriminative models directly learn the conditional posterior P(Y∣X)P(Y \mid X) (e.g., Logistic Regression, neural classifiers) or map X→YX \to Y via a decision boundary (e.g., SVMs). By focusing capacity strictly on separating classes without spending parameters modeling P(X)P(X), discriminative models typically achieve lower asymptotic classification error when sufficient labeled data is available and generative assumptions (such as Naive Bayes feature independence) do not hold.

Key Terms

  • Generative Model: Models the joint distribution P(X,Y)P(X, Y) (e.g., Naive Bayes, Gaussian Mixture Models, VAEs, Diffusion Models, autoregressive language models).
  • Discriminative Model: Models P(Y∣X)P(Y \mid X) or a decision function f(X)→Yf(X) \to Y directly (e.g., Logistic Regression, SVMs, Random Forests, discriminative CNNs).
  • Joint Probability (P(X,Y)P(X, Y)): Probability of observing input features XX and label YY together.
  • Conditional Probability (P(Y∣X)P(Y \mid X)): Posterior probability of label YY given observed input features XX.

Example

Consider classifying 1,000 images (800 dogs, 200 cats). A generative model (such as Gaussian Naive Bayes or a class-conditional Gaussian model) learns separate distributions describing what dog feature vectors look like (P(X∣dog)P(X \mid \text{dog})) and what cat feature vectors look like (P(X∣cat)P(X \mid \text{cat})) alongside class priors P(Y)P(Y), then applies Bayes’ rule—and can sample synthetic feature vectors from those distributions. A discriminative model (such as Logistic Regression) ignores how typical dog or cat images look internally and solely optimizes a hyperplane separating cats from dogs.

Code Implementation

import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate synthetic data for binary classification
X, y = make_classification(
    n_samples=1000, n_features=2, n_informative=2,
    n_redundant=0, n_clusters_per_class=1, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)

# Generative Model: Gaussian Naive Bayes (models P(X|Y) and P(Y))
gnb = GaussianNB()
gnb.fit(X_train, y_train)
gnb_accuracy = np.mean(gnb.predict(X_test) == y_test)
print(f"Generative Model (GaussianNB) Accuracy: {gnb_accuracy:.4f}")

# Discriminative Model: Logistic Regression (models P(Y|X) directly)
lr = LogisticRegression()
lr.fit(X_train, y_train)
lr_accuracy = np.mean(lr.predict(X_test) == y_test)
print(f"Discriminative Model (Logistic Regression) Accuracy: {lr_accuracy:.4f}")

# Plot decision boundaries side by side
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
grid = np.c_[xx.ravel(), yy.ravel()]

plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
Z_gnb = gnb.predict(grid).reshape(xx.shape)
plt.contourf(xx, yy, Z_gnb, cmap=plt.cm.RdBu, alpha=0.8)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.RdBu, edgecolors='k')
plt.title('Gaussian Naive Bayes (Generative)')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')

plt.subplot(1, 2, 2)
Z_lr = lr.predict(grid).reshape(xx.shape)
plt.contourf(xx, yy, Z_lr, cmap=plt.cm.RdBu, alpha=0.8)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.RdBu, edgecolors='k')
plt.title('Logistic Regression (Discriminative)')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')

plt.tight_layout()
plt.show()
  • Ng & Jordan (2001) Sample Complexity Tradeoff: When comparing Naive Bayes (generative) and Logistic Regression (discriminative), Naive Bayes converges faster to its asymptotic error (O(log⁡d)O(\log d) samples), whereas Logistic Regression typically achieves lower asymptotic error once training data is large enough.
  • Semi-Supervised Learning & Unlabeled Data: Generative models can naturally exploit unlabeled inputs XX via EM or latent-variable objectives to refine P(X)P(X) and improve class separation when labels are scarce.
  • Calibration & Out-of-Distribution Detection: Because generative models explicitly estimate P(X)P(X), low likelihood P(x)P(x) signals out-of-distribution inputs, whereas discriminative classifiers can be overconfident far from the training manifold.
  • Modern Deep Generative Models: VAEs, GANs, Diffusion Models, and autoregressive Transformers learn rich data distributions P(X)P(X) or P(X∣prompt)P(X \mid \text{prompt}) for text, image, and audio synthesis.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.