Maps to: CMU 11-711 Spring 2026, Lecture 12 (02/19), slides
anlp-s2026-12-multimodal-ii.pdf; course code12_multimodal_ii/vqvae.ipynb. Readings: A. van den Oord, O. Vinyals, K. Kavukcuoglu, Neural Discrete Representation Learning (VQ-VAE, NeurIPS 2017); P. Esser, R. Rombach, B. Ommer, Taming Transformers for High-Resolution Image Synthesis (VQGAN, CVPR 2021). Additional: M. Chen et al., Generative Pretraining from Pixels (Image GPT, ICML 2020); A. Ramesh et al., Zero-Shot Text-to-Image Generation (DALL·E, ICML 2021); Chameleon Team (Meta), Chameleon: Mixed-Modal Early-Fusion Foundation Models (2024); D. Kingma, M. Welling, Auto-Encoding Variational Bayes (2013). Sources: the public slides and notebook, the readings, and the papers cited in each section. Every number labelled “measured” comes fromcode/lecture12/, run on the author’s machine (Apple M4 Pro, Python 3.13, PyTorch 2.14, one CPU thread). Code:code/lecture12/— four demos, each under two minutes. Reading time: ~100 minutes for the prose; ~25 more for the demos.
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Name the four paradigms of generative modeling (autoregressive, VAE, GAN, diffusion) and say what each learns and how each samples.
- Explain why modeling pixels autoregressively works in principle and fails in practice: sequence length and the low information per token. Compute both.
- Derive the ELBO (importance sampling + Jensen), explain its two terms as reconstruction and rate, and explain why the reparameterization trick is needed.
- Explain the VQ-VAE: nearest-codebook quantization, the codebook and commitment losses, the straight-through gradient, dead codes. Explain why VQGAN adds perceptual and adversarial losses.
- Describe early-fusion mixed-modal models (DALL·E, Chameleon): image tokens in the LM’s vocabulary, text-to-image by next-token prediction, reranking. Name their main weakness, information lost in the tokenizer.
0.2 Prerequisites and scope
- Prerequisites:
- Lecture 3: language modeling as ; bits and entropy.
- Lecture 9: sampling, top-k, the KV cache.
- Lecture 11: Shape World, ViT, CLIP,
parse(). - Lecture 1, demo 05: the score-function (REINFORCE) gradient.
- Scope: multi-to-image and multi-to-multi with discrete image tokens.
- Not in scope:
- Diffusion and flow models: Lecture 15, which the slides name as the route to state-of-the-art image quality.
- GANs, beyond their role in VQGAN.
- Video and audio tokenizers (the same ideas apply).
0.3 Section → code map
| Section | Idea | Demo in code/lecture12/ |
|---|---|---|
| §2.1 | sequence lengths and attention cost | 01_pixels.py Part A |
| §2.2 | information per token, exactly | 01_pixels.py Part B (+ image_tokens.py) |
| §2.3 | a pixel Transformer vs the exact entropy; where the bits go; samples | 01_pixels.py Parts C–D |
| §3.1–3.2 | the ELBO; importance-weighted bounds; β; reparameterization | 02_vae.py |
| §3.3–3.4 | VQ-VAE: codes, fidelity, usage, straight-through, grid size | 03_vqvae.py |
| §4 | text → image with one Transformer; sampling; CLIP reranking | 04_text_to_image.py |
0.4 Notation
| Symbol | Meaning |
|---|---|
| an image; its tokens (pixels or codes) | |
| a latent variable (continuous for the VAE, discrete codes for the VQ-VAE) | |
| , | prior and decoder |
| encoder (approximate posterior) | |
| codebook vectors; encoder output, its quantized version | |
| stop-gradient: identity forward, zero gradient backward | |
| entropy in bits |
1. Generating images: the goal and the paradigms
1.1 Where we are
Lecture 11 built models whose input includes images and whose output is text. Products now go further. A model like GPT-4o takes text and images in and puts text and images out (“remove the bridges from this photo”). The slides’ goal is concrete:
- an image tokenizer that turns an image into tokens;
- an image de-tokenizer that turns tokens back into an image;
- a single Transformer for text and images, which reads and writes both.
Each of the three demands is a design decision with a measurable cost, and the lecture is about those costs.
1.2 Four paradigms
| paradigm | learns | generates by |
|---|---|---|
| autoregressive | , one dimension (pixel, token) at a time | sampling the dimensions in order |
| VAE | an encoder and a decoder , trained to reconstruct through a compressed | sampling from a prior, decoding |
| GAN | a generator that fools a discriminator trained to spot fakes | sampling , one forward pass |
| diffusion | to undo small steps of added noise | starting from noise, denoising step by step (Lecture 15) |
The autoregressive route is the one an LM already knows. The question of this lecture is what it should treat as a “token” of an image.
2. Attempt 1: pixels as tokens
2.1 The idea, and the sequence-length problem
Treat an image as a sequence of pixel values in raster order (row by row). Each value is in , or an index into a color palette. Then train a language model:
This lineage is real and successful at small scale:
- PixelRNN / PixelCNN (van den Oord et al., 2016): LSTMs and masked convolutions.
- Image Transformer (Parmar et al., 2018): local self-attention blocks.
- Image GPT (Chen et al., 2020): GPT-2 on pixel sequences. Its features rivaled contrastive methods on linear probes (CIFAR-10: 96.3% for iGPT-L).
The slides name two challenges. The first is sequence length. Measured in demo 01 Part A:
| sequence | tokens | attention scores per head and layer |
|---|---|---|
| 1024×1024 RGB, one token per channel value | 3,145,728 | 9.9 × 10¹² (≈ 40 TB in fp32) |
| 256×256, one token per pixel | 65,536 | 4.3 × 10⁹ (17 GB) |
| VQGAN / DALL·E tokens for a 256×256 image (32×32) | 1,024 | 1.0 × 10⁶ |
| Shape World 24×24 pixels | 576 | 3.3 × 10⁵ |
| Shape World at 12×12 | 144 | 2.1 × 10⁴ |
| Shape World as VQ tokens (§3.3) | 36 | 1.3 × 10³ |
Image GPT therefore trained on 32×32 to 64×64 images, with a 9-bit (512-color) palette found by k-means, and still needed “memory-efficient attention patterns”. At our tiny scale the same wall appeared. A training step of the same 2-layer model took about 9× longer on 576 tokens than on 144 (demo 01; times vary by machine), so demo 01 models 12×12 images, as Image GPT downsampled.
2.2 The information problem, measured exactly
The second challenge: “each pixel carries relatively little semantic information compared to a word token”. Shape World lets us compute this exactly. A one-object scene is a shape (4), a color (5), a size (2) and a position (196 or 324 positions). Enumerating every scene the sampler can produce (10,400 of them, with their probabilities) gives:
| representation | bits per image | bits per token (average) |
|---|---|---|
| 24×24 pixels (576 tokens) | 13.30 | 0.023 |
| 12×12 palette pixels (144 tokens) | 12.91 | 0.090 |
| VQ tokens (36) | ≤ 13.30 | ≤ 0.37 |
| the scene itself (shape, color, size, x, y) | 13.30 | 2.66 |
At 12×12 some scenes look identical: 8,452 distinct pictures, with 12.91 bits of entropy. A pixel model must spend most of its computation predicting tokens that are almost certain. A word in a caption carries orders of magnitude more information than a pixel. This is the deeper reason for tokenizers: put more meaning into each position.
2.3 Measured: a pixel Transformer on 144 tokens
Demo 01 trains Lecture 5’s Transformer (2 layers, ) on 20,000 12×12 palette images, for 2,000 steps of 32 images. Because the exact entropy is known, we can say how far from perfect the model is. No model can average below 12.91 bits per image.
| training step | 500 | 1,000 | 1,500 | 2,000 |
|---|---|---|---|---|
| test bits per image | 18.60 | 16.69 | 15.67 | 15.29 |
| excess over the entropy | 5.69 | 3.78 | 2.76 | 2.38 |
Where do the bits go?
| pixels | share of the tokens | mean bits per token | share of all bits |
|---|---|---|---|
| background | 92.1% | 0.036 | 31.0% |
| the first colored pixel (raster order) | 0.7% | 7.51 | 49.1% |
| other object pixels | 7.2% | 0.294 | 19.9% |
61.9% of all pixel tokens cost less than 0.01 bit. Half the information of an image is decided at a single token, the first colored pixel. That token says where the object starts and in which color; the rest is nearly determined. A pixel LM thus spends 144 sequential steps to make roughly one decision that matters.
Samples (demo 01 Part D). Of 500 samples drawn at temperature 1, 35.4% are an exact picture of a shape, checked against every possible rendering. Colors are balanced among the valid ones (30–43 per color). One wrong pixel among 144 spoils an image, and errors compound over sequential steps. Lecture 9 made the same argument about tail mass in text. Drawing all 500 took about a second with the KV cache: 144 sequential steps per image.
3. Attempt 2: learn a tokenizer
3.1 The VAE: compress, then generate
Intuition. An image’s information fits in far fewer numbers than its pixels (§2.2). So learn to compress an image into a short code , and to decompress back into an image. If the codes also follow a simple distribution, generation is: sample a code, decompress.
The model (Kingma & Welling, 2013). Assume data arises as:
- sample a latent ;
- produce with a decoder .
An encoder , a Gaussian whose mean and variance are produced by a network, approximates the intractable posterior .
The ELBO. The slides derive it in four lines:
- The second equality is importance sampling: multiply and divide by .
- The inequality is Jensen’s ().
- The gap between the two sides is exactly .
Training maximizes this evidence lower bound; the loss is its negative, . Its terms:
- reconstruction: decode a sampled and score ;
- regularization: keep each encoding close to the prior, so that samples from the prior look like encodings.
The reparameterization trick. The expectation is over , which depends on the encoder’s parameters. To get a gradient through the sample, write with . The randomness then sits in , and is a differentiable function of .
Measured (demo 02; one-object images; a 16-dimensional latent; the decoder a Gaussian with σ = 0.1 per pixel channel).
- Term by term. With : reconstruction nats, KL = 25.1 nats, ELBO = 2147.7 nats. These are continuous densities, so only differences matter.
- Jensen’s gap is real. Importance-weighted bounds (Burda et al., 2016) with samples climb toward :
| importance samples | 1 | 10 | 100 |
|---|---|---|---|
| bound on (nats) | 2148.0 | 2154.3 (+6.3) | 2156.2 (+8.2) |
- Why the trick matters. For one image, the gradient of with respect to was estimated 1,000 times with one sample each:
| estimator | variance per coordinate |
|---|---|
| reparameterization | 135 |
| score function (REINFORCE, Lecture 1): | 2.58 × 10⁸ |
| score function with a mean baseline | 1.62 × 10³ |
The score-function estimator multiplies by nats. Every sample’s estimate is therefore huge, and the samples cancel only on average: 1.9 million times the variance. A baseline helps (12× instead), but reparameterization differentiates through the sample. It is why VAEs train at all.
3.2 Rate, distortion, and why VAE samples disappoint
The KL term has a second reading. It is the rate: how many nats the code carries about the image (Alemi et al., 2018). Shape World tells us what the rate should be: a one-object image holds 13.30 bits. Demo 02 trains three VAEs, weighting the KL by (β-VAE, Higgins et al., 2017):
| β | rate (KL, bits) | distortion (MSE) | reconstruction parses exactly | prior samples valid |
|---|---|---|---|---|
| 0.25 | 56.9 | 0.00267 | 27.6% | 12.0% |
| 1 | 36.3 | 0.00245 | 23.0% | 39.6% |
| 4 | 20.0 | 0.00257 | 22.6% | 42.4% |
Three lessons:
- The code is inefficient. Even at β = 4 it uses 20 bits for 13.3 bits of content, and the extra rate does not buy exact reconstructions.
- β trades rate for sample quality. A low β writes information into the code that the Gaussian prior never produces. Prior samples then land between the training codes, and decode into nothing (12.0% valid).
- Reconstructions are blurry (23% parse exactly). The decoder is trained with a pixel-wise Gaussian (L2) likelihood. When a detail is uncertain, the L2-optimal output is the average of the possibilities: a blur.
Demo 02’s interpolation shows one more property. Decoding the point halfway between the codes of a purple circle and a red square gives a green square. A straight line in latent space is not a straight line in the world.
These are the classic VAE complaints: blurry outputs, and samples worse than reconstructions. They motivate discrete codes: a prior over a finite set of codes can be an LM, which is far more flexible than .
3.3 The VQ-VAE: a discrete tokenizer
From continuous to discrete (van den Oord et al., 2017). Keep the encoder and the decoder, but force the code through a codebook :
- The encoder maps the image to a grid of vectors . Ours is 6×6 vectors for a 24×24 image, so each vector summarizes a 4×4-pixel cell.
- Quantize: replace each vector by its nearest codebook vector, with . The posterior is one-hot: iff .
- The decoder reconstructs from .
The tokens are the integers : an image becomes 36 integers from a vocabulary of 64.
The loss (the slides):
- Reconstruction: as in the VAE (we use MSE).
- Codebook loss: moves each chosen codebook vector toward the encoder outputs assigned to it. It is k-means done by gradient descent.
- Commitment loss (): keeps the encoder near its chosen codes, so its outputs do not drift away from the codebook.
- Straight-through gradient. The argmin has no gradient, so the decoder’s gradient with respect to is copied to (the slides’ “Copy” arrow). In code:
z_q = z_e + (z_q - z_e).detach(). The value is ; the gradient is ‘s. - No KL term. The KL to a uniform prior over codes is a constant here. The prior is fitted afterwards: PixelCNN in the paper, a Transformer in VQGAN and DALL·E.
3.4 Measured: what the tokenizer keeps and loses
The standard tokenizer (demo 03: 6×6 grid, , 3,000 steps). Reading the reconstructions of 1,000 test images back with parse():
| round trip image → 36 tokens → image | color | shape | size | arrangement | exact |
|---|---|---|---|---|---|
| standard tokenizer | 100% | 80.9% | 99.6% | 100% | 80.8% |
Colors, sizes and arrangements survive; shapes do not always. The errors are specific: a cross comes back as a circle 109 times, a triangle as a square 77 times. Demo 03 Part C prints one such reconstruction. The arms of a small cross come back as a soft blob, the L2 average of the shapes its codes could stand for.
How the codes are used.
- 74.0% of all cells are pure background, and a handful of codes cover them: the top 8 codes are all background codes.
- The 33 most frequent codes cover 90% of all tokens. The course notebook found the same concentration on MNIST: 36 of 128 codes covered 90%.
- Among the codes, 21 mostly encode background and 43 mostly encode one color or another. 34 codes have a single content in at least 90% of their cells.
- Dead codes. A code no encoder output selects gets no gradient and stays dead. The notebook’s MNIST run used 81 of 128 codes in its first epoch. Our training re-seeded 34 dead codes from current encoder outputs, and all 64 codes end up in use.
Equal budget, three designs (1,000 steps):
| design | tokens per image | shape | exact | MSE |
|---|---|---|---|---|
| standard: 6×6 grid, straight-through | 36 | 59.2% | 59.2% | 0.00125 |
| 12×12 grid (finer cells) | 144 | 99.7% | 99.7% | 0.00026 |
| 6×6 grid, no straight-through | 36 | 0.0% | 0.0% | 0.01459 |
- Tokens per image is the dial between fidelity and sequence length. 144 tokens reconstruct almost perfectly, but an LM over them faces 4× longer sequences: back toward §2.1’s problem. DALL·E chose 32×32 = 1,024 tokens for 256×256 images; VQGAN usually 16×16.
- Without the straight-through gradient the encoder never learns from the reconstruction loss, and nothing useful reaches the decoder. “Copy the gradient” is what gets through the argmin.
3.5 VQGAN: sharper tokens
Esser et al. (2021) kept the VQ-VAE’s structure but changed what the decoder is rewarded for:
- Perceptual loss: compare deep features (LPIPS) of the image and its reconstruction, not pixels. A blur that is close in L2 is far in features.
- Adversarial loss: a patch discriminator is trained to tell real patches from reconstructed ones, and the decoder (“generator” ) is trained to fool it: . Averaged blurs are easy to spot, so the decoder must commit to one sharp possibility.
- Then a Transformer prior: tokenize lots of images () and train an autoregressive Transformer, . With or instead of 65,536 pixels, megapixel synthesis became feasible.
Our attempt at a cheap perceptual loss was a failure, and an instructive one. Matching Lecture 11’s CLIP features, or up-weighting object pixels, made fidelity worse (18–21%). A perceptual loss is only as good as the features it compares. LPIPS features are calibrated to human judgments; our 3-layer CLIP’s are not.
4. One Transformer for text and images
4.1 The recipe
Once images are tokens, “multimodal” is a vocabulary question:
- Add the image codes to the LM’s vocabulary, together with markers
<img>and</img>. - Train on sequences that mix them. Text-to-image is
caption <img> codes </img>; captioning is the reverse order. - Generate an image by sampling image tokens after
<img>, restricted to image codes, then de-tokenize.
Systems built this way:
- DALL·E (Ramesh et al., 2021):
- A discrete VAE with 8,192 codes turns a 256×256 image into 1,024 tokens.
- A 12B-parameter Transformer models 256 text tokens followed by the 1,024 image tokens, trained on 250M image–text pairs.
- It drew 512 samples per caption and reranked them with CLIP.
- Chameleon (Meta, 2024):
- Early fusion: one tokenizer and one Transformer for interleaved text and images, in both directions.
- Trained on ~10T tokens; generates documents that mix text and images.
- It needed stability tricks for mixed-modal training (query–key normalization, among others).
4.2 Measured: text → image in Shape World
Demo 04 adds the 64 codes of demo 03’s tokenizer to the 46 Shape World words, 110 tokens in all. It trains Lecture 5’s Transformer (, 3 layers, 3,000 steps of 32 sequences) on <s> caption <img> 36 codes </img> </s>, with one next-token loss on every token. A two-object sequence is 49 tokens long, and drawing an image takes 36 sampling steps.
For 300 new captions (sizes always stated), each generated image is parsed back and compared with the caption. The ceiling is the true image of each caption, sent through the tokenizer and back. No generator on these tokens can do better on average.
| valid | color | shape | size | arrangement | exact | |
|---|---|---|---|---|---|---|
| ceiling: true image through the tokenizer | 100% | 100% | 81.3% | 100% | 100% | 81.3% |
| generated, temperature 1 | 85.7% | 81.0% | 34.0% | 48.7% | 91.4% | 26.0% |
| … one-object captions | 92.1% | 87.9% | 54.3% | 67.9% | — | 45.7% |
| … two-object captions | 80.0% | 75.0% | 16.2% | 31.9% | 91.4% | 8.8% |
| top-k, k = 4 | 89.7% | 86.3% | 38.3% | 53.3% | 92.6% | 26.7% |
(“Arrangement” is scored over valid two-object samples: which color is left of, or above, which.)
Four observations:
- The pipeline works, within limits. Valid scenes 85.7% of the time, colors right 81%. Shape and size are where generation fails, as for every model in Lectures 11–12: they are fine detail in token space.
- The arrangement is easy. Where things go is coarse in token space (which cells are non-background), so the arrangement of valid two-object samples is right 91.4% of the time, even though exact matches are rare (8.8%).
- Top-k helps a little. Truncating to the 4 most likely codes per step raises validity (85.7% → 89.7%) by cutting the unlikely, broken codes — Lecture 9’s argument, in images.
- The ceiling is the tokenizer’s. Even a perfect generator stops at 81.3% exact, because the tokens cannot represent every cross. This is the slides’ “Con: information loss from the image tokenizer / de-tokenizer”.
4.3 Reranking with CLIP
DALL·E sampled many images per caption and kept the ones CLIP scored highest: best-of-N with a learned verifier, an idea Lecture 23 generalizes. Demo 04 draws 8 images per caption and keeps CLIP’s favorite (CLIP from Lecture 11):
| valid | color | shape | size | arrangement | exact | |
|---|---|---|---|---|---|---|
| first sample | 85.7% | 81.0% | 34.0% | 48.7% | 91.4% | 26.0% |
| CLIP’s pick of 8 | 91.3% | 91.3% | 57.7% | 72.3% | 97.8% | 52.7% |
| … one-object captions | 96.4% | 96.4% | 83.6% | 87.9% | — | 79.3% |
| … two-object captions | 86.9% | 86.9% | 35.0% | 58.8% | 97.8% | 29.4% |
- Reranking doubles exact matches (26.0% → 52.7%). For a perfect reranker, one that knows which samples are exact, it would be 70.3%: the best of 8 is often right.
- Does CLIP’s relation-blindness hurt? Lecture 11 showed CLIP cannot tell “X left of Y” from “Y left of X”. Here the arrangement rose too (91.4% → 97.8%), but only along with everything else, because CLIP picks samples whose colors and shapes are right.
- The direct test. Look at captions whose 8 samples include the right colors in both arrangements. That happened for only 11 captions, and CLIP chose the right arrangement in 8. With 11 cases, an exact binomial test gives p = 0.23: too few to tell from a coin flip. Lecture 14 is about exactly this kind of question: how many cases one needs before a difference means something.
4.4 Pros and cons, and what comes next
- Pros: one generative model for everything. It is autoregressive, everything is tokens, and every trick for LMs carries over: scaling, KV caches, sampling, instruction tuning, and generation in both directions.
- Cons:
- Information loss from the tokenizer: our ceiling was 81.3%, and real tokenizers struggle with text in images, faces and fine textures.
- Many sequential steps per image: 1,024 for DALL·E.
- Errors that compound token by token.
State-of-the-art image generation therefore moved to diffusion (Lecture 15). Diffusion refines all pixels (or continuous latents) in parallel over a few dozen steps, and needs no discrete bottleneck. Hybrids — discrete diffusion over tokens, autoregression with continuous tokens — are an active research area.
5. Summary
- Generative paradigms: autoregressive (one dimension at a time), VAE (compress + decode), GAN (fool a discriminator), diffusion (denoise). An LM is the autoregressive kind; the question is what an image token is.
- Pixels as tokens: correct in principle, wasteful in practice.
- Sequences explode: 3.1M tokens for a 1024×1024 RGB image.
- Information per token is tiny: measured exactly, 0.023 bits per pixel.
- A pixel Transformer reached 15.29 bits per image against a 12.91-bit floor, put half its bits on one token, and drew valid pictures 35.4% of the time.
- VAE: ELBO = reconstruction − KL (rate), derived by importance sampling + Jensen.
- Importance weighting tightened the bound by 8.2 nats.
- Reparameterization cut the gradient variance by a factor of ~2 million against REINFORCE.
- β trades rate (56.9 → 20.0 bits) for sample validity (12% → 42%).
- Outputs are blurry, because the L2-optimal output averages possibilities.
- VQ-VAE: nearest-codebook quantization, codebook + commitment losses, straight-through gradient.
- Measured: 36 tokens per image keep colors, sizes and arrangements but lose some shapes (80.8% exact).
- 144 tokens keep almost everything (99.7%) at 4× the sequence length.
- Without straight-through, nothing is learned.
- VQGAN sharpens tokens with perceptual + adversarial losses.
- Early-fusion generation (DALL·E, Chameleon): image codes join the vocabulary; text → image is next-token prediction.
- Measured: 26.0% exact (ceiling 81.3%); CLIP reranking of 8 samples, 52.7%.
- The tokenizer’s information loss is the price; diffusion (Lecture 15) avoids it.
Common pitfalls
- Comparing bits per image without an entropy floor. “15.3 bits” means nothing until you know the floor is 12.9.
- Judging generators by eye. Measure faithfulness with a parser, a verifier, or at least a classifier, attribute by attribute.
- Forgetting the tokenizer ceiling. Before blaming the generator, reconstruct the true images through the tokenizer.
- Too few tokens per image. The tokenizer blurs exactly the details captions mention (here, shapes).
- Dead codes. Monitor codebook usage; re-seed unused codes or use EMA updates.
- Removing or mis-wiring the straight-through estimator. The encoder silently stops learning (0% fidelity).
- A VAE with a small β, judged by its reconstructions. Its prior samples can be far worse (12% valid).
- Drawing conclusions from a handful of cases. 8 of 11 is not evidence (p = 0.23).
Exercises
-
Sequence arithmetic. A 512×512 RGB image is modeled (a) per channel value, (b) per pixel with a palette, (c) as VQ tokens with downsampling factor 16. How many tokens each? By what factor does attention memory shrink from (a) to (c)?
Answer
(a) 512·512·3 = 786,432; (b) 262,144; (c) (512/16)² = 32² = 1,024. Attention scores scale with $T^2$: (786,432 / 1,024)² = 768² ≈ 590,000× fewer. -
The ELBO gap. Show that . Why does the importance-weighted bound with samples tighten as grows?
Answer
ELBO $= \mathbb{E}_q[\log p(x, z) - \log q(z \mid x)] = \mathbb{E}_q[\log p(z \mid x) + \log p(x) - \log q(z \mid x)] = \log p(x) - D_{\mathrm{KL}}(q \,\|\, p(z \mid x))$. The IWAE bound $\mathbb{E}[\log \frac{1}{K}\sum_k w_k]$ with $w_k = p(x, z_k)/q(z_k \mid x)$ is Jensen applied to an average of $K$ weights. The average concentrates around its mean $p(x)$ as $K$ grows, so the Jensen gap shrinks (Burda et al., 2016). Measured: +6.3 nats at K = 10, +8.2 at K = 100. -
Reparameterization vs score function. For and , write both one-sample estimators of . How does each depend on the constant ?
Answer
Reparameterized: $z = \mu + \varepsilon$, estimator $f'(z) = 2z$; it does not depend on $c$. Score function: $f(z)\,\frac{d}{d\mu}\log \mathcal{N}(z; \mu, 1) = (z^2 + c)(z - \mu)$; its mean is unaffected by $c$ (since $\mathbb{E}[z - \mu] = 0$), but its variance grows like $c^2$. That is why demo 02's estimator with $\log p(x \mid z) \approx 2{,}173$ had ~2 million times the variance, and why subtracting a baseline (an estimate of $c$) helps. -
Commitment loss. What goes wrong if , or if the codebook loss is removed?
Answer
With β = 0 the encoder's outputs are not tied to the codebook. They can grow or drift (the decoder only sees $z_q$), so assignments flip constantly and training is unstable. Without the codebook loss the codebook vectors get no gradient at all: the straight-through path skips them. They stay at their random initialization, and the encoder must chase fixed points. The two terms split the work: codes move toward encoder outputs, encoder outputs stay near codes. -
Dead codes. A codebook of 512 is initialized uniformly in while encoder outputs have norm ~5. Explain why most codes may never be selected, and give two fixes.
Answer
All codes start near the origin; the nearest code to every encoder output is whichever few lie closest to the data's direction. Only those get codebook-loss gradients, so they move toward the data while the rest stay behind, never nearest to anything. Fixes: re-seed unused codes from random current encoder outputs (as here: 34 re-seeded); initialize the codebook by k-means on encoder outputs; update codes by exponential moving averages; or use lower-dimensional, normalized codes. -
The tokenizer ceiling. In demo 04, 26.0% of first samples are exact, the tokenizer ceiling is 81.3%, and CLIP’s pick of 8 reaches 52.7%. A colleague proposes a 4× larger Transformer. Another proposes a 12×12 tokenizer. What does each change, and at what cost?
Answer
A larger Transformer can close part of the gap between 26.0% (or 52.7%) and the 81.3% ceiling, but never exceed the ceiling, and it costs compute per step. A 12×12 tokenizer raises the ceiling to ~99.7% (demo 03), but every image becomes 144 tokens instead of 36. Training and sampling cost ~4× more per image in our timing (66 vs 18 ms per step), and the generator has more tokens to get right, so its exact rate may not rise at first. Which helps more depends on whether the bottleneck is the generator (here, yes: 26% ≪ 81%) or the tokenizer. -
Reranking as best-of-N. If each of independent samples is exact with probability , what is the best possible reranked accuracy? With and , compare with the measured perfect-reranker score of 70.3% and explain the difference.
Answer
$1 - (1 - p)^N = 1 - 0.74^8 \approx 0.91$. The measured 70.3% is lower because samples are not independent across captions. Some captions are hard for the model (two objects, small crosses), with per-caption $p$ near 0; others are easy, with $p$ near 1. Averaging $1 - (1 - p_i)^N$ over a spread of $p_i$ gives less than plugging in the average $p$. Lecture 23 returns to this coverage-vs-difficulty effect.
Related assignment (concepts only)
- Assignment 2 (end-to-end NLP system building) is in progress. If your system generates structured outputs, the lesson of §4 applies: build an automatic checker for each attribute you care about, and measure the “ceiling” of every lossy component before tuning the model on top. These notes do not provide assignment code.
Further reading
- A. van den Oord, O. Vinyals, K. Kavukcuoglu, “Neural Discrete Representation Learning” (VQ-VAE), NeurIPS 2017 (course reading).
- P. Esser, R. Rombach, B. Ommer, “Taming Transformers for High-Resolution Image Synthesis” (VQGAN), CVPR 2021 (course reading).
- D. Kingma, M. Welling, “Auto-Encoding Variational Bayes”, ICLR 2014; Y. Burda, R. Grosse, R. Salakhutdinov, “Importance Weighted Autoencoders”, ICLR 2016; I. Higgins et al., “β-VAE”, ICLR 2017; A. Alemi et al., “Fixing a Broken ELBO”, ICML 2018.
- A. van den Oord, N. Kalchbrenner, K. Kavukcuoglu, “Pixel Recurrent Neural Networks”, ICML 2016; N. Parmar et al., “Image Transformer”, ICML 2018; M. Chen et al., “Generative Pretraining from Pixels”, ICML 2020.
- A. Ramesh et al., “Zero-Shot Text-to-Image Generation” (DALL·E), ICML 2021; Chameleon Team, “Chameleon: Mixed-Modal Early-Fusion Foundation Models”, 2024.
- I. Goodfellow et al., “Generative Adversarial Nets”, NeurIPS 2014; R. Zhang et al., “The Unreasonable Effectiveness of Deep Features as a Perceptual Metric” (LPIPS), CVPR 2018.
- A. Razavi, A. van den Oord, O. Vinyals, “Generating Diverse High-Fidelity Images with VQ-VAE-2”, NeurIPS 2019.
Preview of Lecture 13 — Evaluation and benchmarks
We have now measured a lot of things: zero-shot accuracy, VQA by question type, faithfulness by attribute, bits against an entropy floor. Lecture 13 steps back to ask how evaluation is done in general:
- loss vs task metrics;
- what makes a good benchmark;
- exact match, pass@k, ROUGE and their failure modes;
- LLM-as-a-judge and human preference arenas;
- whether a benchmark score can be trusted (contamination, robustness).
Lecture 14 then asks how sure we can be of a number like “8 of 11”.
术语表 (Glossary, English → 中文)
| English | 中文 |
|---|---|
| generative model / generative modeling paradigm | 生成模型 / 生成建模范式 |
| autoregressive model / raster order | 自回归模型 / 光栅顺序 |
| pixel / palette / color quantization | 像素 / 调色板 / 颜色量化 |
| bits per dimension / bits per image | 每维比特数 / 每图比特数 |
| entropy / information per token | 熵 / 每词元信息量 |
| variational autoencoder (VAE) | 变分自编码器 |
| latent variable / prior / approximate posterior | 隐变量 / 先验 / 近似后验 |
| evidence lower bound (ELBO) / Jensen’s inequality | 证据下界 / 詹森不等式 |
| importance sampling / importance-weighted bound (IWAE) | 重要性采样 / 重要性加权下界 |
| KL divergence / rate / distortion | KL 散度 / 码率 / 失真 |
| β-VAE | β-变分自编码器 |
| reparameterization trick / score-function estimator | 重参数化技巧 / 得分函数估计器 |
| generative adversarial network (GAN) / generator / discriminator | 生成对抗网络 / 生成器 / 判别器 |
| diffusion model | 扩散模型 |
| vector quantization (VQ) / VQ-VAE / codebook | 向量量化 / 向量量化变分自编码器 / 码本 |
| codebook loss / commitment loss | 码本损失 / 承诺损失 |
| stop-gradient / straight-through estimator | 停止梯度 / 直通估计器 |
| dead code / codebook usage | 死码 / 码本利用率 |
| image tokenizer / de-tokenizer | 图像分词器 / 反分词器 |
| reconstruction fidelity / tokenizer ceiling | 重建保真度 / 分词器上限 |
| perceptual loss / adversarial loss (VQGAN) | 感知损失 / 对抗损失 |
| early fusion / mixed-modal / joint vocabulary | 早期融合 / 混合模态 / 联合词表 |
| text-to-image generation | 文生图 |
| reranking / best-of-N | 重排序 / 多选一 |
| faithfulness | 忠实度 |