Concept units

Conditional generation: generating from a hint

Intermediate

Read these first if they are new to you

The idea in one paragraph

A conditional generative model takes two inputs: random noise, as before, and a condition that says what kind of example you want. The condition can be a label such as "cat", a sentence, or part of a shape. The model generates examples that fit the condition. Give it the same condition with new noise, and you get a different example that also fits. One model learns every condition at once, because the condition is just another input.

The condition picks which examples the model draws from

A plain generative model draws from the whole collection. A conditional one draws only from the examples that match a condition c. We write that distribution p(x | c), read "the distribution of x given c". The model turns noise into an example, as before, but the condition changes which function it applies.

Worked example: heights for two age groups

A model of heights in centimeters takes the condition "child" or "adult". For a child it uses mean 130 and spread 8; for an adult, mean 170 and spread 7. A sample is mean + spread × z, where z is a draw from the standard bell curve.

noise zcondition "child"condition "adult"
0.5130 + 8 × 0.5 = 134170 + 7 × 0.5 = 173.5
−1130 − 8 = 122170 − 7 = 163

Read down a column: the same condition with new noise gives a new example of the same kind. Read across a row: the same noise with a new condition gives a different kind of example.

Your turn. What does each condition give for the noise draw z = 2?

Answer

Child: 130 + 8 × 2 = 146 cm. Adult: 170 + 7 × 2 = 184 cm.

Same condition, new noise: a new example that still fits

In two dimensions, let the examples be points of two kinds: points on a circle and points along a line. The condition names the kind.

Samples (large dots) for the chosen condition, over the real examples (faint)

…

Try this. Choose Circle and press Draw again twice. What stays the same, and what changes? Then predict where the samples go with No condition, and check. Here the samples come from a hand-written rule for each shape, standing in for a trained model.
What you should find

With a condition, every sample lands on the named shape, but the samples change with each draw. With no condition, the samples land on both shapes, about half on each, because the two kinds are equally common in the collection.

Without a condition, the model draws from the whole collection. That is the same as first picking a condition at random, as often as it occurs in the collection, and then drawing an example for it. In symbols, p(x) is the mix of the p(x | c), each weighted by how common c is.

Training uses examples paired with their conditions

Each training example comes with its condition: a photo with its caption, a height with its age group, a complete shape with the part of it the model will be shown. The model receives the condition as extra input numbers, next to the noise. A label becomes a vector through an embedding. A sentence or a set of points can be read with cross-attention. Sometimes the condition is cut from the example itself, as in shape completion.

Models can follow a condition only loosely

A trained model does not always respect its condition. Its samples may fit the condition only roughly, or sometimes not at all. Classifier-free guidance is a common way to make a model follow its condition more strongly.

A conditional model gives many answers where a predictor gives one

A predictor (a regression model) returns one answer for each input. Trained with squared error, it returns the average of the answers it saw for that input (see loss). For the answers −1 and +1, predicting 0 gives squared errors 1 + 1 = 2, and predicting +1 gives 4 + 0 = 4. That works when one answer is right. It fails when several different answers are right, because their average may be none of them.

A path reaches a fork: generated paths take a branch; the average takes neither
left, −1 right, +1 average, 0 start
What to notice. Half the paths go left (−1) and half go right (+1). The average, 0, runs straight into the gap between the branches, where no path goes. A conditional generative model gives −1 or +1, each about half the time.

conditional_bell.py

import numpy as np

rng = np.random.default_rng(1)

# One model, two conditions: the condition picks the mean and the spread.
params = {"child": (130.0, 8.0), "adult": (170.0, 7.0)}

def generate(condition, n):
    mu, sigma = params[condition]
    return mu + sigma * rng.standard_normal(n)

print("child:", np.round(generate("child", 4), 1))
print("adult:", np.round(generate("adult", 4), 1))

# A fork in a path: it ends at -1 (left) or +1 (right), half the time each.
ends = rng.choice([-1.0, 1.0], size=1000)
print("single best guess (the average):", round(ends.mean(), 2))
print("generated ends:", ends[:6])
child: [132.8 136.6 132.6 119.6]
adult: [176.3 173.1 166.2 174.1]
single best guess (the average): -0.02
generated ends: [ 1.  1. -1. -1.  1.  1.]

Check yourself

  1. You give a conditional model the condition "adult" twice, with two different noise draws. Should the two outputs be equal?

    Answer

    No. Both should fit the condition, but they should differ, because different noise gives different examples of the same kind.

  2. A road splits into three lanes that end at −2, 0 and +2, and each is equally common. What does a squared-error predictor answer? Is that a real lane? Would it be one if only the lanes −2 and +2 existed?

    Answer

    It answers 0, the average. With three lanes, 0 happens to be a real lane. With only −2 and +2, the average is still 0, which is not a lane. Averaging fails exactly when the average is not one of the right answers.

  3. A conditional model knows three conditions. In the collection they occur 50%, 30% and 20% of the time. How do you draw a sample with no condition?

    Answer

    Pick a condition at random with probabilities 0.5, 0.3 and 0.2. Then draw a sample from the model with that condition.

Go further

Optional background, for readers with more time.

Where this shows up

Units that build on this one