TechByteByByte

Generative Modeling From First Principles

The foundational distinction between discriminative and generative modeling that underlies every model family in this course — starting Level 2, from data through learned patterns to sampled new outputs.

#Generative AI#AI#Generative Modeling#Level 2

Start with the simple idea

A discriminative model separates or predicts categories. A generative model learns enough about the data pattern to create a new example.

Simple learning path: problem → intuition → mechanism → example → limits

What you will learn

  • Explain Generative Modeling From First Principles in plain language.
  • Follow its mechanism step by step.
  • Connect a small example to a real AI system.
  • Recognize its strengths, limits, and common mistakes.

How this appears in current AI systems

GPT, Gemini, and Claude generate text with learned token patterns. The same generative idea also appears in image, audio, and video model families, even when their internal mechanism is different.

Official grounding: OpenAI documents its current text-generation API and Google documents the current Gemini model catalog. These pages verify available capabilities; exact model names and limits can change.

When this knowledge helps

Use Generative Modeling From First Principles when it matches the problem described below. Before choosing it, check the task, available data, quality target, cost, response time, privacy, and safety needs; popularity alone is not a reason to use it.

1. The question this module answers

Module 2 introduced the idea of learning a distribution. This module formalizes it properly, and introduces the single most important conceptual distinction for understanding every model family covered in Level 2: discriminative modeling vs. generative modeling. Every model family from here forward — autoregressive, VAEs, GANs, diffusion — is a different technical approach to being a generative model.


2. The Problem

Your ML course taught you to build models that predict a label or a value from an input. That’s a really different mathematical objective than building a model that can produce new data. This module makes that difference precise.


3. Discriminative Modeling — What Your ML Course Actually Taught

Discriminative modeling learns: P(label | input)

"Given this input, what's the probability of each possible label?"
Input: an email

Discriminative model

P(spam | email) = 0.92
P(not spam | email) = 0.08

The model learns a boundary or mapping between inputs and outputs. It never needs to understand how to produce a realistic email — only how to distinguish between categories of emails it’s shown.


4. Generative Modeling — The Different Objective

Generative modeling learns: P(data)  or  P(data | context)

"What does realistic data from this domain actually look like?"
Training data: thousands of emails

Generative model

Learns what makes an email "look like an email" -- structure,
vocabulary, patterns

Can then SAMPLE new outputs: "generate a plausible email"

This is a fundamentally different, and in some ways harder, objective: a discriminative model only needs to find a boundary between categories; a generative model needs to understand the data well enough to actually produce new, realistic examples of it.


5. Intuition — The Art Critic vs. The Art Forger

Analogy: a discriminative model is like an art critic who can reliably tell you whether a painting is a genuine Picasso or not, without being able to paint one themselves. A generative model is like a forger who has studied Picasso’s work so thoroughly they can produce an entirely new painting in his style — really new, really consistent with the learned patterns, but never simply copied.

Both require real skill and real learning from examples — but they’re learning fundamentally different things: the critic learns to distinguish, the forger learns to produce.

Analogy: The Weather Forecaster (Joint vs. Conditional Probability) Think of discriminative vs. generative math in terms of two different weather prediction approaches:

  • Conditional Probability P(YX)P(Y|X) (Discriminative): The forecaster looks out the window, sees dark, heavy clouds (XX), and says: “There is an 85% chance of rain (YY).” They don’t model how the wind blew the clouds here, or what humidity levels are; they just map the cloud input directly to a binary rain/no-rain outcome label.
  • Joint Probability P(X,Y)P(X, Y) (Generative): The forecaster builds a full, high-fidelity physical simulator of the Earth’s atmosphere. This simulator models humidity, heat convection, wind, cloud formation (XX), and rain (YY) simultaneously.
    • Because they understand the entire weather generation system, they can run simulations to generate synthetic rainy days, dry windy days, or humid mornings from scratch.

📊 Visual Chart: Decision Boundary vs. Joint Distribution Cloud

Here is the mathematical difference in how the two model types view a dataset:

graph TD
    classDef predictive fill:#e74c3c,stroke:#333,stroke-width:1px,color:#fff;
    classDef generative fill:#2ecc71,stroke:#333,stroke-width:1px,color:#fff;

    subgraph Discriminative ["Discriminative Boundary: P(Y|X)"]
        DrawLine["Draw a line dividing circles from triangles"]:::predictive
        OutPredict["If new point is on left -> 'circle label'"]:::predictive
    end

    subgraph Generative ["Generative Distribution Cloud: P(X, Y)"]
        ModelSpace["Model the density cloud of circle coordinates"]:::generative
        OutGen["Generate brand-new circle points inside the cloud space"]:::generative
    end

6. Formalizing the Distinction

Discriminative:      models the DECISION BOUNDARY between classes
                    -- learns P(y | x): given input x, what's the
                    label y?

Generative:              models the DATA DISTRIBUTION itself --
                       learns P(x) or P(x | context): what does
                       realistic data actually look like?
DiscriminativeGenerative
LearnsBoundary between categoriesThe structure of the data itself
Typical outputA label, a number, a probabilityNew data (text, image, audio…)
Your ML course exampleSpam classifier(Not typically covered — this course’s focus)
Can it generate new data?No, not directlyYes, that’s its whole purpose
Can it classify?Yes, directlySometimes indirectly, but not its primary design

7. A Real Developer Example

Task: build a system that both flags spam AND suggests better subject
lines for legitimate marketing emails.

Spam flagging:               a DISCRIMINATIVE task -- classify
                            email as spam/not spam, choosing from a
                            fixed set of 2 labels

Subject line suggestion:        a GENERATIVE task -- produce NEW
                              text (a subject line) that didn't
                              exist as a pre-defined option

A real system needs BOTH kinds of models, chosen deliberately based
on which objective actually matches each specific sub-task -- exactly
Module 1's "predictive vs generative, chosen deliberately" lesson,
now grounded in the precise mathematical distinction this module
introduces.

8. A Simple Agentic AI Connection

An agent deciding whether a user’s message requires escalation (choosing from a fixed set: escalate / don’t escalate) is doing discriminative-style reasoning. The same agent drafting the escalation message itself is doing generative-style reasoning.

Real agent architectures (Module 29) constantly blend both — recognizing which objective a given sub-decision actually calls for is a really useful design skill.


9. How Is This Used in AI?

🤖 How Is This Used in AI?

Every generative model family covered in the rest of this course — autoregressive models (Module 6), VAEs (Module 7), GANs (Module 8), diffusion models (Module 9) — is a different technical strategy for achieving this same generative objective: learning P(data), well enough to sample really new, realistic examples from it.


10. Real-World Applications

  • Discriminative: spam filters, fraud detection, medical diagnosis classification, sentiment analysis
  • Generative: text generation, image generation, music composition, code generation, data augmentation (generating synthetic training examples for other models)

11. When to Use Which

  • Use discriminative modeling when the task really is choosing among known, fixed categories or predicting a specific value
  • Use generative modeling when the task requires producing new content, or when you need to understand/sample from the underlying structure of the data itself (including, notably, generating synthetic training data for other models)

12. Common Mistakes

Incorrect idea

Assuming a generative model is “just a more powerful” discriminative model.

Why it is incorrect

They solve really different mathematical problems — a generative model isn’t automatically better at classification; in fact, purpose-built discriminative models are often more efficient and accurate for pure classification tasks.

Incorrect idea

Using a generative model where a discriminative one would be simpler and sufficient.

Why it is incorrect

If a task is really just “choose from these 3 fixed labels,” a discriminative approach is usually simpler, cheaper, and more reliable — Module 35 of this course covers this decision directly.

Incorrect idea

Forgetting that some generative models CAN also be used for classification-like tasks indirectly

Why it is incorrect

(e.g., comparing how probable different labels make the data appear) — the distinction is about the primary learning objective, not an absolute, unbreakable wall.


13. Limitations

  • This module presents the discriminative/generative distinction as fairly clean for teaching purposes — in practice, some modern systems and techniques blend both objectives in more complex ways
  • Understanding this distinction conceptually doesn’t yet explain HOW any specific generative model family actually implements “learning P(data)” — that’s exactly what Modules 6-9 cover, each with a really different technical strategy

14. Quick Reference — The Whole Idea in One Diagram

Discriminative:      Input -> Model -> P(label | input) -> Prediction

Generative:              Data -> Model learns P(data) -> Sample ->
                       New, realistic output

Every generative model family in this course (autoregressive, VAE,
GAN, diffusion) is a DIFFERENT technical strategy for the same
underlying generative objective.

15. Code — Discriminative vs. Generative Objectives, Side by Side

🎯 Target of this example: make the mathematical distinction from Section 6 directly observable — one call that returns a probability- like classification (discriminative-style), and one call that produces really new content (generative-style), using the exact same underlying model to highlight that it’s the task framing, not the model itself, that determines which objective is in play.

Example 1 — Simple

import anthropic

client = anthropic.Anthropic()

email = "URGENT: You've won $1,000,000! Click here NOW to claim your prize!"

# DISCRIMINATIVE-style prompt: choose from a FIXED set of categories
disc_response = client.messages.create(
    model="claude-sonnet-4-6", max_tokens=10, temperature=0,
    messages=[{"role": "user", "content":
               f"Classify as Spam or Not Spam: {email}"}]
)
print("Discriminative output:", disc_response.content[0].text)

# GENERATIVE-style prompt: produce really NEW content
gen_response = client.messages.create(
    model="claude-sonnet-4-6", max_tokens=60,
    messages=[{"role": "user", "content":
               "Write a realistic, SHORT example of a marketing email "
               "subject line for a legitimate 20% off sale."}]
)
print("Generative output:", gen_response.content[0].text)

Expected Output:

Discriminative output: Spam

Generative output: "Your 20% Off Ends Tonight -- Don't Miss Out!"

What we conclude from this example: the discriminative call’s output is always exactly one of two fixed words — it’s modeling a decision boundary. The generative call’s output is new text that never existed as a fixed option beforehand — it’s modeling what a realistic subject line looks like well enough to produce a new one. Same underlying model, really different objective per call.

Example 2 — Intermediate

import anthropic

client = anthropic.Anthropic()

def compare_objectives(topic: str) -> dict:
    """Runs the SAME topic through both a discriminative-style task
    and a generative-style task, to directly compare their outputs."""

    # Discriminative: is this topic more suited to B2B or B2C marketing?
    disc = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=10, temperature=0,
        messages=[{"role": "user", "content":
                   f"Is '{topic}' more suited to B2B or B2C marketing? One word."}]
    )

    # Generative: write an actual marketing tagline for the topic
    gen = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=40,
        messages=[{"role": "user", "content":
                   f"Write a short marketing tagline for: {topic}"}]
    )

    return {"topic": topic, "discriminative_result": disc.content[0].text.strip(),
            "generative_result": gen.content[0].text.strip()}

result = compare_objectives("cloud-based project management software")
print(result)

Expected Output:

{
  'topic': 'cloud-based project management software',
  'discriminative_result': 'B2B',
  'generative_result': 'Keep your team aligned, wherever work happens.'
}

What we conclude from this example: for the exact same topic, the discriminative result is a fixed category (a boundary decision), while the generative result is entirely new phrasing — this directly demonstrates Section 6’s table: discriminative models a boundary, generative models the structure of plausible content itself.

Example 3 — Production Grade

import anthropic
from enum import Enum
from dataclasses import dataclass

client = anthropic.Anthropic()

class ObjectiveType(Enum):
    DISCRIMINATIVE = "discriminative"
    GENERATIVE = "generative"

@dataclass
class TaskResult:
    objective: ObjectiveType
    output: str

def route_task(task_description: str, objective: ObjectiveType, prompt: str) -> TaskResult:
    """A router that makes the objective EXPLICIT for each task,
    applying different generation settings appropriate to each --
    e.g., temperature=0 for discriminative-style consistency,
    higher temperature for generative-style variety."""
    temperature = 0 if objective == ObjectiveType.DISCRIMINATIVE else 0.7
    max_tokens = 10 if objective == ObjectiveType.DISCRIMINATIVE else 60

    response = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=max_tokens, temperature=temperature,
        messages=[{"role": "user", "content": prompt}]
    )
    return TaskResult(objective=objective, output=response.content[0].text.strip())

tasks = [
    ("Spam classification", ObjectiveType.DISCRIMINATIVE,
     "Classify as Spam or Not Spam: 'Win a free iPhone now!!!'"),
    ("Subject line generation", ObjectiveType.GENERATIVE,
     "Write a short, realistic marketing email subject line for a book sale."),
]

for label, objective, prompt in tasks:
    result = route_task(label, objective, prompt)
    print(f"[{label} | {result.objective.value}] -> {result.output}")

Expected Output:

[Spam classification | discriminative] -> Spam
[Subject line generation | generative] -> "Turn the Page: 30% Off Your
Next Great Read"

What we conclude from this example: explicitly routing each task through an ObjectiveType, and deliberately choosing different generation settings (temperature, token limits) per objective, shows this distinction isn’t just academic — it directly informs real engineering decisions about how to configure each call appropriately for its actual purpose.


16. Interview Questions

Q: What is the fundamental difference between discriminative and generative modeling?

Ans: Discriminative modeling learns P(label | input) — the boundary or mapping between inputs and a fixed set of possible labels or values, exactly what most of your Machine Learning course covered. Generative modeling learns P(data) or P(data | context) — the underlying structure of the data itself, well enough to sample entirely new, realistic examples from it. They’re solving really different mathematical objectives, not just different applications of the same idea.

Q: Why can’t you simply use a generative model wherever a discriminative model is needed, assuming the generative model is “more powerful”?

Ans: They solve different problems — a generative model isn’t automatically better at classification just because it can also produce new content. A purpose-built discriminative model is often simpler, more efficient, and more directly accurate for a pure classification task, since it’s optimized specifically for finding a decision boundary rather than modeling the full structure of the data.

Q: Explain the art critic vs. art forger analogy for discriminative vs. generative modeling.

Ans: An art critic (discriminative model) can reliably distinguish a genuine painting from a fake without being able to paint one themselves — they’ve learned a decision boundary. An art forger (generative model) has studied the style so thoroughly they can produce an entirely new painting consistent with it — they’ve learned the underlying structure and patterns well enough to generate new, plausible examples. Both require genuine learning from data, but they’re learning fundamentally different things.

Q: How would you decide whether a given sub-task within a larger AI system calls for a discriminative or generative approach?

Ans: I’d ask whether the task involves choosing among a fixed, known set of outputs (discriminative — like classifying urgency or routing a request to one of several categories) or producing new content that isn’t a predefined option (generative — like drafting a message or generating a description). Real systems very often need both, applied to different sub-tasks within the same overall pipeline, so the key skill is correctly identifying which objective each specific piece of the problem actually requires.


17. What You Should Remember

  • Discriminative modeling learns P(label | input) — a decision boundary between fixed categories. Generative modeling learns P(data) — the underlying structure of the data itself, enabling new samples.
  • These are really different mathematical objectives, not a “more powerful” vs. “less powerful” version of the same thing.
  • Every generative model family in this course (Modules 6-9) is a different technical strategy for achieving this same generative objective.

18. Quick Practice

For each of these, identify whether it’s fundamentally a discriminative or generative task: (1) determining whether an image contains a cat, (2) generating a caption for that image, (3) detecting whether a transaction is fraudulent, (4) writing a fraud investigation report.

19. Next Step

Next: Module 6 — Autoregressive Generation — the first specific generative model family covered in depth, and the one you already have the strongest foundation for from your LLM course.

Author
TechByteByByte Editorial Team
Reviewed by
TechByteByByte Admin
Published
Last reviewed