Skip to content
E
Educatum

Can you articulate the distinctions between generative models, designed to produce novel data instances, and discriminative models, which aim to classify or categorize data?

Curated by Educatum AI teamLast updated:
Generative Models
Direct Answer

Generative models learn the data distribution p(x) or joint distribution p(x, y) to model how data is generated and synthesize new instances. Discriminative models directly learn the conditional distribution p(y|x) or a decision boundary to classify inputs without modeling p(x). Generative models support sampling, density estimation, and Bayes-rule classification, whereas discriminative models focus capacity on the decision boundary and typically achieve lower asymptotic error on supervised tasks.

Study Card: Generative vs. Discriminative Models

Direct Answer

Generative models learn the underlying probability distribution of the data—either the marginal p(x)p(x) (unsupervised) or the joint distribution p(x,y)=p(x∣y)p(y)p(x, y) = p(x \mid y)p(y) (supervised)—allowing them to generate novel data instances similar to the training set. Discriminative models directly model the conditional distribution p(y∣x)p(y \mid x) or learn a decision boundary fθ(x)f_\theta(x) that maps inputs to labels without modeling p(x)p(x).

Using Bayes’ rule, a supervised generative model can also perform classification:

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

In classic supervised learning (Ng & Jordan, 2001), generative classifiers such as Naive Bayes or Gaussian Discriminant Analysis often converge faster in sample complexity when labeled data is scarce, whereas discriminative models (such as Logistic Regression or SVMs) avoid distributional assumptions on p(x∣y)p(x \mid y) and typically achieve lower asymptotic error when labeled data is abundant.

Key Terms

  • Generative Model: A model that learns p(x)p(x) or the joint distribution p(x,y)p(x, y) so it can synthesize new samples (x′,y′)(x', y') or x′∼pθ(x)x' \sim p_\theta(x) and estimate data likelihoods.
  • Discriminative Model: A model that directly learns p(y∣x)p(y \mid x) or a separating hyperplane/decision boundary without estimating p(x)p(x).
  • Bayes’ Theorem: The identity connecting joint/class-conditional generative modeling p(x∣y)p(y)p(x \mid y)p(y) to discriminative posterior prediction p(y∣x)p(y \mid x).
  • Decision Boundary: The hypersurface in feature space separating different classes in a classification problem.

Example

Consider classifying images of cats and dogs:

  • A discriminative model (like logistic regression, a CNN classifier, or an SVM) learns features and weights that separate cats from dogs directly, outputting p(y=cat∣x)p(y = \text{cat} \mid x) without learning how to draw a complete cat image.
  • A generative model (like a Variational Autoencoder, Diffusion Model, or GAN) learns the full distribution over pixel patterns of cats and dogs, enabling it to synthesize entirely new cat or dog images from latent noise or class conditioning.

Similarly, in NLP, an autoregressive language model estimating p(x1,…,xT)=∏t=1Tp(xt∣x<t)p(x_1, \dots, x_T) = \prod_{t=1}^T p(x_t \mid x_{<t}) is generative, whereas a spam classifier predicting p(spam∣x)p(\text{spam} \mid x) is discriminative (note that modern generative LLMs can also perform classification zero-shot via prompting).

Code Implementation

# Runnable comparison: Logistic Regression (discriminative) vs. Gaussian Generative Model (QDA-style)

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Generate synthetic binary classification dataset
X, y = make_classification(
    n_samples=200, n_features=2, n_informative=2, n_redundant=0, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)

# 1. Discriminative Model: directly models p(y | x)
discriminative_model = LogisticRegression()
discriminative_model.fit(X_train, y_train)
y_pred_disc = discriminative_model.predict(X_test)
print("Discriminative accuracy:", round(accuracy_score(y_test, y_pred_disc), 4))


# 2. Generative Model: fits class-conditional Gaussians p(x | y) and class priors p(y)
class GaussianGenerativeModel:
    def __init__(self, reg=1e-6):
        self.reg = reg
        self.classes = None
        self.priors = {}
        self.class_means = {}
        self.class_covariances = {}

    def fit(self, X, y):
        self.classes = np.unique(y)
        n_samples, n_features = X.shape
        for c in self.classes:
            X_c = X[y == c]
            self.priors[c] = len(X_c) / n_samples
            self.class_means[c] = np.mean(X_c, axis=0)
            cov = np.cov(X_c, rowvar=False)
            self.class_covariances[c] = cov + self.reg * np.eye(n_features)

    def _log_likelihood(self, X, c):
        mean = self.class_means[c]
        cov = self.class_covariances[c]
        inv_cov = np.linalg.inv(cov)
        sign, logdet = np.linalg.slogdet(cov)
        diff = X - mean
        quad = np.sum((diff @ inv_cov) * diff, axis=1)
        d = X.shape[1]
        return -0.5 * (d * np.log(2 * np.pi) + logdet + quad)

    def predict(self, X):
        log_posteriors = [
            np.log(self.priors[c]) + self._log_likelihood(X, c) for c in self.classes
        ]
        best_idx = np.argmax(np.vstack(log_posteriors), axis=0)
        return self.classes[best_idx]

    def generate_sample(self, c, n=1, rng=None):
        rng = rng or np.random.default_rng(42)
        return rng.multivariate_normal(self.class_means[c], self.class_covariances[c], n)


generative_model = GaussianGenerativeModel()
generative_model.fit(X_train, y_train)
y_pred_gen = generative_model.predict(X_test)
print("Generative (Bayes rule) accuracy:", round(accuracy_score(y_test, y_pred_gen), 4))

rng = np.random.default_rng(0)
for c in generative_model.classes:
    new_sample = generative_model.generate_sample(c, n=1, rng=rng)
    print(f"Generated sample for class {c}: {new_sample}")
  • Types of Generative Models: Autoregressive LLMs, Diffusion Models, VAEs, GANs, and Normalizing Flows—and how they trade off likelihood tractability, sample quality, and sampling speed.
  • Types of Discriminative Models: Logistic Regression, SVMs, Random Forests, and discriminative neural classifiers, along with their calibration and margin properties.
  • Semi-Supervised & Unlabeled Data: Generative models can leverage abundant unlabeled p(x)p(x) data alongside scarce labels, whereas purely discriminative losses require labeled pairs (x,y)(x, y).
  • Evaluation Metrics: Discriminative models use accuracy, F1, or AUROC; generative models require distributional/likelihood metrics such as Perplexity, Fréchet Inception Distance (FID), or Precision/Recall for distributions.
  • Out-of-Distribution (OOD) Detection: Generative density estimation offers a principled mechanism to flag atypical inputs, though high-dimensional likelihood pitfalls must be handled carefully.
View all →

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