Generative and discriminative tasks
Two tasks, one catalog
A fictional garden and kitchen catalog needs both topic labels and short titles. A classifier selects a label such as garden or kitchen. A generator produces a sequence such as "Spring seed guide". The first output has a fixed label set; the second has many possible acceptable answers. A fixed template is often enough for predictable catalog titles. A language model adds flexibility but also the possibility of invented claims.
Generative and discriminative modeling
In statistical modeling, a discriminative classifier estimates a decision boundary or a conditional distribution P(label | input). A generative model may learn a distribution over data, or a joint distribution P(input, label) that can also support classification. Conditional generation models learn distributions over outputs given inputs. Task and training objective are different axes: a text generator can produce a class label, and a generative classifier need not produce prose. Compare methods based on required output, error cost and available data.
Build a classical baseline
This deterministic keyword classifier is not a trained model. It counts distinct words from two tiny, fixed vocabularies. Lowercasing and whitespace splitting are explicit toy policies, not robust language processing. Equal scores, including two zeros, trigger review rather than force a label. Input is a plain title string without punctuation.
def topic(title): words = set(title.lower().split()) garden = len(words & {"seed", "soil", "plant"}) kitchen = len(words & {"bread", "oven", "recipe"}) if garden == kitchen: return "review" return "garden" if garden > kitchen else "kitchen"
print(topic("Seed and soil"))print(topic("Bread oven"))print(topic("Seed recipe"))Expected lines: garden, kitchen, review. The third title contains one word from each vocabulary. Repetition does not increase a score because the code uses a set. Punctuation, synonyms and unseen languages can defeat this baseline; test them separately.
Evaluate generation against a task contract
For a synthetic title, require the word seed, allow only a declared vocabulary, and limit length to three words. Evaluate each constraint separately so a reviewer can diagnose failure. This deliberately narrow lexical checker cannot measure factual accuracy, grammaticality, copyright or semantic equivalence. In a real catalog, compare claims to approved product records, use human review for uncertain claims, and test diverse examples. A valid title need not exactly match one reference title.
def title_report(title, allowed, required, max_words): words = title.lower().split() tokens = set(words) return { "length_ok": 0 < len(words) <= max_words, "missing": sorted(set(required) - tokens), "unexpected": sorted(tokens - set(allowed)), }
allowed = ["spring", "seed", "guide", "soil"]print(title_report("Spring seed guide", allowed, ["seed"], 3))print(title_report("Free seed guarantee", allowed, ["seed"], 3))The first report has length_ok True and both lists empty. The second has length_ok True and missing empty but unexpected ['free', 'guarantee']. Both meet the length limit; only the first passes all three checks. An invented guarantee is especially problematic even when the title is fluent.
Measure the right outcome
For topic classification, use held-out labeled examples and the metrics from Chapter 18, including per-class precision and recall and the rate of review. For generation, combine contract satisfaction, factual grounding, usefulness and safety review; report how many outputs passed and the failure categories. Diversity is useful only when variations remain acceptable. Evaluate a deterministic template under the same conditions before selecting a model.
Common mistakes and practice
Do not treat every generated sentence as a fact, or compare exact-match accuracy across tasks with different valid outputs. Do not select a model just because it is generative. Practice: design two acceptable titles using the allowed vocabulary and explain why a reference-string match would reject one. Compare a keyword classifier and a template against a generative candidate; state when to abstain. Next: foundation models and modalities.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Generative and discriminative tasks quiz
Apply the chapter concepts to synthetic content tasks.
Which catalog task is explicitly a closed-set classification task?
What does topic("Seed recipe") return?
What does the title report say about "Free seed guarantee"?
Why might exact matching against one reference title underestimate generation quality?
Which experiment best measures whether a generator adds value over a template?