L4.5 Where should instructions enter a shared model?
Builds on2 notes
One machine, different instructions
The same photograph can be brightened or turned into a night scene. The photograph alone does not specify which answer is wanted. Where does the instruction enter the network? Write this extra information as a condition :
- “Make this a night scene”: a category condition.
- “Generate from this description”: a sequence of tokens.
- “We are at step 300; perform the appropriate correction”: a scalar condition.
The last condition describes the algorithm’s state, rather than a user instruction. Shared weights perform different operations at different stages, so the stage must be supplied.
Conditions can enter the input, modulate intermediate features, or be queried through attention. These choices change computations and have different costs for different condition structures.
Where should an instruction enter?
Is one extra input coordinate enough?
What if that coordinate is a scalar requiring rapid variation?
DiscussionQ1
How does a conditional model differ mathematically from an unconditional one? If a scalar condition is appended as another input coordinate, what must the network learn along that coordinate?
Show the reasoning
Fix the learning objective first. Under squared-error regression with finite second moments, the ideal conditional predictor is
The condition refines the information available. Conditional variance cannot increase after averaging over the conditioning variables, but a particular subgroup can have larger variance. Finite models and data need not achieve lower error.
What must a raw-coordinate model learn? If the desired function varies rapidly with , the network must construct those oscillations along that input coordinate.
The frequency experiment in If MLPs Can Approximate Everything, Why Use Other Architectures? found training MSE increasing from to when the target changed from one to 32 cycles at fixed width and budget. This illustrates a spectral training bias in that setting; it can affect a condition coordinate just as it affects a spatial coordinate.
Two different responses are possible.
- Supply frequency features directly: replace or augment with sine and cosine features at several frequencies. A frequency already present can be selected by a linear layer.
- Give the condition another route: use it to modulate intermediate features, rather than treating it only as an ordinary input coordinate.
We measure the first choice and explain when the second fits the structure of the condition. Neither is universally necessary or superior.
Three injection mechanisms
Compare what each mechanism changes:
- Concatenation: . This is simple and natural for aligned inputs, such as an image and a pixelwise mask. Input-level conditioning must propagate through subsequent layers; concatenation can also be applied at intermediate layers.
- FiLM: compute layerwise scales and shifts from , then apply featurewise affine modulation. Global conditions such as class, time, or style can directly change intermediate computations.
- Cross-attention: current features supply queries; condition tokens supply keys and values. Different positions can consult different parts of the condition. Other mechanisms can also process variable-length inputs through encoders, but cross-attention provides direct content matching.
These mechanisms can coexist: FiLM for time, cross-attention for text, and concatenation for aligned auxiliary images. Condition structure suggests useful candidates:
| Condition | A mechanism worth comparing |
|---|---|
| Scalar time or strength | Embedding followed by FiLM or concatenation |
| Discrete category | Learned embedding followed by FiLM |
| Aligned image, mask, or edge map | Channelwise concatenation |
| Variable-length token sequence | Cross-attention |
Figure: The three mechanisms change different computations. A time embedding is a representation choice for scalar conditions.
Why embed a scalar first?
A sinusoidal embedding maps to multiple frequencies:
It replaces “construct this oscillation from a scalar” with “use an oscillation already supplied in the input.”
For example, if is already a feature, a linear layer can select it directly. This motivates Fourier features [2]; it does not imply that every network’s first-layer weights must grow as .
Check an important limitation: these integer frequencies give . If the endpoints need different answers, retain raw , include suitable noninteger frequencies, or use another endpoint-distinguishing representation.
Geometric frequencies cover several scales with only frequency pairs, or trigonometric coordinates. Their ratio spans from the first frequency to the last.
SupplementThe link to positional encodings and Fourier features
Transformer sinusoidal positional encodings also use sine/cosine pairs at geometric frequencies. The scalar denotes token position rather than time or condition strength. The exact frequency choices and the way features enter the model differ.
Coordinate-based networks use related Fourier features [2]. Their neural-tangent-kernel analysis explains how feature mappings can alter a model’s effective spectral bias. The result depends on architecture and regime; it is not a theorem that all unembedded networks have exponentially decaying frequency response or can never learn high frequencies.
The reusable idea is to compare multiscale frequency features when a scalar must influence the output at several scales, while checking endpoint collisions and the frequencies the task actually needs.
Three representations on one task
Choose a target with deliberately rapid dependence on the condition:
The dependence on has one cycle, while the dependence on has eight. Run 6000 Adam steps at for each setting:
concatenate scalar t input dim 2 h= 64 training MSE 0.0018
concatenate sinusoidal t input dim 13 h= 64 training MSE 0.0004
concatenate scalar t, wider input dim 2 h=128 training MSE 0.0010
The embedding reduces displayed error by about a factor of four with 11 additional input coordinates. Doubling hidden width approximately quadruples the dominant hidden-layer parameter count, yet gives a smaller improvement here. This is evidence for this representation under this task and budget, rather than a universal efficiency ranking.
Fixed embedding frequencies need no training, but they change the representation, downstream parameter count, and represented functions. Controlled comparisons are needed to explain an improvement.
This motivates common sinusoidal time embeddings. They are part of the model’s input representation, even when the frequency map has no learned parameters. Like feature scaling in Downhill in Fog: Direction, Step Size, and Gradient Descent, they can change optimization, though their effect is more than a simple change of units.
“No condition” needs training too
A conditional model sometimes needs to operate without an explicit condition:
- To generate with no requested category.
- To compare conditional and unconditional predictions, as guidance methods often do.
- To handle a user who provides no condition.
A null token can represent absence—for example, an all-zero class vector. But if conditions are always present in training, that null input was never constrained by the objective.
Use , with independent, equally likely binary classes. The unconditional squared-error predictor is therefore
Independently replace the condition with null with probability . Null examples then train this marginalized target. In general the average uses , not necessarily equal class weights.
def feats(x, c, mask): # mask=0 drops this condition
oh = np.zeros((len(x), 2)); oh[np.arange(len(x)), c] = 1.0
return np.c_[x, oh*mask[:,None]]
mask = (rng.random(N) > p).astype(float) # p is condition-dropout probability
dropout p=0.0 median null-prediction MSE against the class average: 1.3908
dropout p=0.1 median null-prediction MSE against the class average: 0.0660
dropout p=0.3 median null-prediction MSE against the class average: 0.0225
reference: always predicting 0 gives MSE 0.6575
With , the objective never constrains null behavior, and it performs poorly here. The function still has an output; there is no guarantee that it equals the class average. Other initializations or models need not fail by the same amount.
At , null error falls by roughly twenty-one times; lowers it further. Increasing also reduces the fraction of condition-present examples. This table measures only null quality, so it cannot establish the conditional-quality tradeoff or an optimal dropout probability.
A null condition is an input whose desired behavior must be learned or otherwise specified.
A deliberate violation: replace the trained null vector with an unseen vector. Its behavior is again unconstrained and may be poor. The issue is not which vector looks geometrically neutral; use the null representation whose behavior the training objective actually specifies.
Limits of the experiment: these are small scalar/class-conditioned networks. For token sequences, dropping the entire condition and dropping individual tokens teach different tasks. Tune while measuring both conditional and null quality, since they share parameters. Independent dropout is important for the intended marginalization; selective missingness can change the null target.
Injection, frequency, and dropout
Interactive demo: Increase condition frequency and compare raw-scalar and embedded training curves. The embedding’s advantage depends on the supplied frequencies and training setup.
Check your understanding
References
- Perez, E. et al. FiLM: Visual Reasoning with a General Conditioning Layer. AAAI 2018.
- Tancik, M. et al. Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains. NeurIPS 2020.
- Ho, J., Salimans, T. Classifier-Free Diffusion Guidance. 2022.