Autoregressive Models: PixelCNN, PixelRNN, and Sequential Generation
3754 words
19 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Autoregressive Models: PixelCNN, PixelRNN, and Sequential Generation ## 🎯 Learning Objectives - Understand the autoregressive factorization of the data likelihood - Implement causal/masked convolutions for efficient image generation - Compare PixelRNN (sequential) and PixelCNN (parallel) architectures - Explain h...

Autoregressive Models: PixelCNN, PixelRNN, and Sequential Generation
🎯 Learning Objectives
- Understand the autoregressive factorization of the data likelihood
- Implement causal/masked convolutions for efficient image generation
- Compare PixelRNN (sequential) and PixelCNN (parallel) architectures
- Explain how autoregressive models handle long-range dependencies
- Analyze the trade-offs between autoregressive and latent variable models
📋 Prerequisites
- Convolutional Neural Networks: Basic CNN operations
- Sequence Models (optional): RNN/LSTM basics
- Probability: Chain rule of probability, conditional distributions
- Generative Models Overview (Week 1): Taxonomy of generative models
1. 📖 Core Content
1.1 Intuition: What is an Autoregressive Generative Model?
Imagine you're writing a sentence. You start with the first word, then based on that word, you choose the second, then based on the first two, you choose the third, and so on. This is natural — language is inherently sequential.
Autoregressive models apply the same logic to any type of data, including images: generate one piece at a time, conditioning on everything generated so far.
For an image, this means generating pixel by pixel (or sub-pixel by sub-pixel):
Why does this matter? Autoregressive models:
- Provide tractable exact likelihoods (no approximation or lower bound needed)
- Have stable training (just maximum likelihood, no adversarial training)
- Achieve state-of-the-art density estimation on many benchmarks
- Power large language models (GPT, LLaMA) — the dominant paradigm in NLP The downside: sequential generation is slow — for a 256×256 image, you must generate 65,536 pixels one at a time, each requiring a forward pass through a neural network.
1.2 The Autoregressive Factorization
By the chain rule of probability, any joint distribution over N variables can be factorized as:
This is mathematically exact — no approximation! The challenge is modeling the conditional distributions p(xi∣x<i), which become increasingly complex as i grows.
In practice: We parameterize each conditional with a neural network that receives all previous variables as input.
Worked Example 1: Autoregressive Modeling of Binary Sequences
Consider a simple dataset of 4-bit binary sequences: [0,0,0,0], [0,0,0,1], [0,0,1,0], [0,0,1,1].
Step 1: Factorize the joint distribution:
Step 2: Model each conditional. For simplicity, use lookup tables:
- p(x1=1)=0/4=0, p(x1=0)=4/4=1
- p(x2=1∣x1=0)=2/4=0.5, p(x2=0∣x1=0)=2/4=0.5
- p(x3=1∣x1=0,x2=0)=2/4=0.5, p(x3=0∣x1=0,x2=0)=2/4=0.5
- p(x4=1∣x1=0,x2=0,x3=0)=1/2=0.5, p(x4=0∣x1=0,x2=0,x3=0)=1/2=0.5
- p(x4=1∣x1=0,x2=0,x3=1)=1/1=1, p(x4=0∣x1=0,x2=0,x3=1)=0 Step 3: Compute likelihood of a sequence. For [0,0,0,0]:
Step 4: Generate new sequences by ancestral sampling:
- Sample x1∼p(x1) → always 0
- Sample x2∼p(x2∣x1) → 0 or 1 equally likely
- Sample x3∼p(x3∣x1,x2) → 0 or 1 equally likely
- Sample x4∼p(x4∣x1,x2,x3) → depends on previous values This small example illustrates the core mechanism: sequential factorization with conditional distributions.
1.3 PixelRNN: Sequential Processing with Recurrent Networks
PixelRNN (Van den Oord et al., 2016) was one of the first neural autoregressive models for images.
Architecture: Process pixels left-to-right, top-to-bottom (raster scan order). For each pixel, an LSTM/RNN maintains a hidden state that summarizes all previously generated pixels.
(Diagram)
Forward pass for a single pixel at position (i,j):
Problem: Sequential processing means O(N) sequential steps for N pixels. For a 256×256 image, that's 65,536 sequential LSTM steps — extremely slow.
Variants:
- Row LSTM: Process entire rows at once using a convolutional LSTM
- Diagonal BiLSTM: Process along diagonals for better coverage Despite improvements, PixelRNN remains too slow for practical image generation at high resolutions.
1.4 PixelCNN: Parallelizable with Masked Convolutions
PixelCNN (Van den Oord et al., 2016) revolutionized autoregressive image generation by using masked convolutions instead of recurrent connections.
1.4.1 The Key Insight
A standard convolution at position (i,j) looks at a neighborhood around (i,j). This is bidirectional — the kernel sees pixels both before and after the current position in the raster order.
PixelCNN uses masked convolutions that only see pixels before the current position in the raster order.
1.4.2 Mask Type A and Type B
(Diagram)
Mask A: Applied to the first layer. The center pixel (current position) is masked out — it cannot see itself.
Mask B: Applied to subsequent layers. The center pixel is included — later layers can use information about the current pixel that was computed from previous layers.
1.4.3 Implementation
A masked convolution is a standard convolution with a binary mask applied to the kernel:
python# runnable import torch import torch.nn as nn import torch.nn.functional as F class MaskedConv2d(nn.Module): def __init__(self, mask_type, in_channels, out_channels, kernel_size=3): super().__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=kernel_size//2) self.register_buffer('mask', self.conv.weight.data.clone()) _, _, h, w = self.mask.size() self.mask.fill_(1) # Center of kernel center_y, center_x = h // 2, w // 2 if mask_type == 'A': # Center pixel is masked (for first layer) self.mask[:, :, center_y, center_x] = 0 # Mask out future pixels (below center row) self.mask[:, :, center_y+1:, :] = 0 # Mask out future pixels (same row, to the right of center) self.mask[:, :, center_y, center_x+1:] = 0 def forward(self, x): self.conv.weight.data *= self.mask return self.conv(x)
1.4.4 PixelCNN Architecture
(Diagram)
Key design choices:
- Stack of masked convolutions: Typically 10-20 layers to build a large receptive field
- Gated activations: y=tanh(Wf∗x)⊙σ(Wg∗x) improves performance
- Residual connections: Help train deeper networks
- 256-way softmax: Pixel values (0-255) treated as categorical, not continuous
Worked Example 2: Receptive Field Growth in PixelCNN
Consider a PixelCNN with 3×3 kernels and L layers (without dilation).
Layer 1: Each pixel sees its 3×3 neighborhood (k=3). Layer 2: Each pixel sees a 5×5 region (k=3+(3−1)=5). Layer L: Each pixel sees a (2L+1)×(2L+1) region.
For L=15 layers: receptive field is 31×31.
Problem: For a 256×256 image, many pixels far away are invisible. This limits the model's ability to capture global structure.
Solution: Use dilated convolutions (like PixelCNN++ or Gated PixelCNN) to grow the receptive field exponentially.
Worked Example 3: Computing the Loss for Autoregressive Models
For an autoregressive model with 256-way softmax per pixel:
This is the standard cross-entropy loss. For a batch of images, we compute the negative log-likelihood of each pixel's true value under the predicted distribution, sum over all pixels, and average over the batch.
1.5 Gated PixelCNN and PixelCNN++
Gated PixelCNN
Adds gated activation units and vertical + horizontal stacks:
- Vertical stack: Sees all pixels above the current row (captures context)
- Horizontal stack: Sees pixels to the left on the same row (captures local detail) The two stacks are combined via gating:
where ∗ is the masked convolution, ⊙ is element-wise multiplication, and σ is the sigmoid.
PixelCNN++
Improvements over the original PixelCNN:
- Discretized logistic mixture instead of 256-way softmax (better likelihood, fewer outputs)
- Downsampling for larger receptive fields
- Dropout for regularization
- Multi-scale architecture: Generate at low resolution first, then upsample with conditioning The discretized logistic mixture loss models each pixel's value as a mixture of logistic distributions, discretized to 256 levels:
This requires only K×3 outputs per pixel (mixing coefficients π, means μ, scales s) instead of 256, making it much more efficient.
1.6 Autoregressive Models Beyond Images
| Domain | Model | Ordering | Conditioning |
|---|---|---|---|
| Text | GPT, LLaMA | Left-to-right (token) | Previous tokens via self-attention |
| Audio | WaveNet | Time steps | Previous audio samples via dilated convs |
| Video | Video Pixel Networks | Frame-by-frame, then pixel | Previous frames + pixels |
| Molecular graphs | GraphRNN | Node-by-node | Previous nodes and edges |
1.7 Edge Cases & Gotchas
- Pixel ordering matters: Raster scan is arbitrary — different orderings produce different models. Some orderings (e.g., S-curve) capture different dependencies.
- Blind spots: Standard PixelCNN can't see pixels directly above due to masking. This creates a "blind spot" in the receptive field.
- Training/Sampling mismatch: During training, the model sees ground-truth context. During sampling, it sees its own predictions, which may have different statistics (exposure bias).
- High computational cost: Each pixel generation requires a full forward pass, making high-resolution autoregessive generation impractical.
- Color channels: For RGB images, pixels within the same location must also be ordered (e.g., R→G→B).
1.8 Why This Matters
Autoregressive models are the foundation of large language models (GPT-4, Claude, LLaMA). The same principle — predict the next token given all previous tokens — is used in virtually every modern text generation system.
In the context of this course:
- Week 4 (Diffusion Models): Diffusion offers faster sampling for images, but autoregressive models provide better density estimation
- BSDA5004 (LLMs): Autoregressive transformers are the dominant paradigm in NLP
- Practical trade-off: Use autoregressive for text (where sequential generation is natural), use diffusion for images (where parallel generation matters) Autoregressive image models like PixelCNN++ were state-of-the-art before diffusion models surpassed them, but the core principle (chain rule factorization) remains fundamental to generative modeling.
2. 📐 Key Formulas / Concepts
| Concept | Formula / Description | Notes |
|---|---|---|
| Chain rule | $p(x_{1:N}) = \prod_{i=1}^N p(x_i\ | x_{<i})$ |
| Masked Conv | K⊙M where M masks future pixels | Enables parallel training |
| Mask A | Center pixel masked (first layer) | Prevents trivial identity shortcut |
| Mask B | Center pixel visible (deeper layers) | Allows information flow within pixel |
| Gated activation | y=tanh(Wf∗x)⊙σ(Wg∗x) | Improves gradient flow |
| NLL loss | $\mathcal{L} = -\sum \log p(x_i\ | x_{<i})$ |
| Discretized logistic | $p(x) = \sum \pi_k \cdot \text{logistic}(x\ | \mu_k, s_k)$ |
3. ⚠️ Common Pitfalls
Pitfall 1: Confusing Autoregressive Models with RNNs
Mistake: Thinking all autoregressive models use RNNs.
Why: Early autoregressive models (PixelRNN) used RNNs, leading to the assumption that autoregressive = recurrent.
Correct approach: Autoregressive refers to the factorization (chain rule), not the architecture. PixelCNN uses convolutions with masking to achieve the same factorization without recurrence. GPT uses transformers (self-attention with masking). The architecture choice affects speed, parallelism, and receptive field.
Pitfall 2: Forgetting That Training and Sampling Have Different Computation Graphs
Mistake: Assuming training-time parallelism (can compute p(xi∣x<i) for all i in one forward pass) also works at sampling time.
Why: During training, all pixels are available, so masked convolutions process the entire image in one forward pass. During sampling, pixels are generated one at a time, requiring N sequential passes.
Correct approach: Training is O(1) parallel, sampling is O(N) sequential. Always design your application around this constraint — use autoregressive models when density estimation matters more than sampling speed.
Pitfall 3: Ignoring the Blind Spot in PixelCNN
Mistake: Assuming masked convolutions capture all pixels above and to the left.
Why: The standard masking strategy prevents a pixel from seeing pixels directly above it in the same column (because those are "above" the center row but masked out by the future-pixel mask).
Correct approach: Use the vertical stack + horizontal stack design (Gated PixelCNN). The vertical stack masks only below the current row, allowing all pixels above to contribute. The horizontal stack handles same-row left context.
Pitfall 4: Treating Pixel Values as Continuous Instead of Categorical
Mistake: Modeling pixel intensities (0-255) as real numbers with MSE loss.
Why: Pixel values are discrete (256 levels). A Gaussian output distribution assumes continuous values and penalizes all errors equally, even when the predicted distribution is multimodal.
Correct approach: Use either a 256-way softmax (treat each level as a separate class) or a discretized logistic mixture (continuous distribution binned into 256 discrete buckets). The latter captures multi-modality (e.g., a pixel could be either dark or bright) that MSE cannot represent.
4. 📝 Practice Questions
Q1: Why are autoregressive models considered "exact likelihood" models while VAEs are not?Autoregressive models use the chain rule: p(x)=∏p(xi∣x<i), which is mathematically exact — no approximations. Each conditional is directly modeled and the joint is the product.VAEs introduce a latent variable z: p(x)=∫p(x∣z)p(z)dz. This integral is intractable, so VAEs optimize a lower bound (ELBO) instead of the exact likelihood. The gap between the ELBO and the true log-likelihood is the KL divergence between the approximate posterior q(z∣x) and the true posterior p(z∣x), which is generally > 0.Autoregressive models pay for exactness with sequential sampling; VAEs pay with approximate likelihood but gain fast parallel sampling. Q2: For a 64×64 grayscale image, how many sequential neural network evaluations are needed to generate one image with (a) PixelCNN, (b) DDPM with 1000 steps, (c) GAN?(a) PixelCNN: 64×64=4096 sequential evaluations (one per pixel). (b) DDPM: 1000 sequential evaluations (one per denoising step). (c) GAN: 1 evaluation (generator transforms noise z to image in one forward pass).Note: PixelCNN's per-pixel evaluations can be sped up with caching (reusing hidden states), but still requires O(N) steps. This comparison highlights why autoregressive models are impractical for high-resolution image generation compared to diffusion models or GANs. Q3: Explain why Mask A (center pixel excluded) is necessary in the first layer of PixelCNN.If the center pixel were visible in the first layer, the model could trivially copy the input pixel value to the output. The first layer convolution at position (i,j) would receive the pixel value xi,j as input and could simply pass it through. This would allow the model to predict p(xi,j∣x<i,j) with xi,j itself in the conditioning set, which is a form of "label leakage" — the model would learn the identity mapping and never learn true conditional distributions.Mask A prevents this by zeroing out the center of the convolution kernel, ensuring the first layer's representation of pixel (i,j) depends only on neighbors before it in raster order. Mask B in subsequent layers can include the center because the information flowing through is already processed (not raw pixel values). Q4: Why does PixelCNN++ use a discretized logistic mixture instead of 256-way softmax?
- Parameter efficiency: A mixture of K logistics requires 3K parameters per pixel (K mixing weights, K means, K scales). With K=5, that's 15 parameters. The 256-way softmax requires 256 parameters per pixel. 15 < 256.
- Smoothness: The logistic distribution naturally imposes smoothness over pixel intensities — nearby values (e.g., 127 and 128) should have similar probabilities. The softmax treats each level independently.
- Multi-modality: A mixture of logistics can represent complex, multi-modal pixel distributions (e.g., a pixel that is either very dark or very bright) better than a single unimodal distribution.
The discretized logistic is obtained by integrating the logistic CDF over each integer bin: P(x=v)=F(v+0.5∣μ,s)−F(v−0.5∣μ,s). Q5: Design a simple autoregressive model for a 4×4 binary image (each pixel is 0 or 1). How many parameters would a full conditional model (no weight sharing) have?The number of possible previous pixel configurations for position i is 2i−1. For i=16 (last pixel), there are 215=32768 possible contexts. A full table would have:20+21+22+...+215=216−1=65535 entriesEach entry stores a single Bernoulli probability p(xi=1∣x<i), so 65535 parameters. This demonstrates why weight sharing (via neural networks) is essential — the naive table approach is exponential in the number of pixels.For comparison, a simple PixelCNN with 4 masked conv layers (8 filters each) would have roughly 4×(9×82)≈2304 parameters, a fraction of the 65535 needed for the full table. Q6: GPT is an autoregressive text model. How does it differ from PixelCNN in terms of (a) the factorization, (b) the conditioning architecture, (c) the output distribution?(a) Factorization: Both use the chain rule. GPT: p(token1,...,tokenT)=∏p(tokent∣token<t). PixelCNN: p(pixel1,...,pixelN)=∏p(pixeli∣pixel<i). The concept is identical.(b) Conditioning architecture: GPT uses Transformer decoder blocks with causal self-attention (each token attends to all previous tokens). PixelCNN uses masked convolutions. Transformers have larger receptive fields (any token can attend to any previous token) while convolutions have limited local receptive fields.(c) Output distribution: GPT uses a softmax over the vocabulary (typically 50K+ tokens). PixelCNN uses a 256-way softmax or discretized logistic mixture over pixel intensities.Despite different architectures, both implement the same core idea: sequential next-element prediction. Q7: For a PixelCNN with 12 layers of 3×3 masked convolutions (no dilation), what's the receptive field of the final layer?With L layers of k×k convolutions (no dilation), the receptive field is:RF=1+L⋅(k−1)For L=12, k=3: RF=1+12⋅2=25.The receptive field is 25×25 pixels. For a 64×64 image, this covers about 15% of the image. For a 256×256 image, only about 1% of the image is visible.This limited receptive field is why PixelCNN struggles with global structure — it cannot see far enough. Dilation or downsampling (as in PixelCNN++) is needed for larger images. Q8: What is "teacher forcing" in the context of autoregressive models? Why is it both helpful and harmful?Teacher forcing means during training, the model receives ground-truth context x<i to predict xi, but during generation, it receives its own previously-generated samples as context.Helpful because: Training is parallelizable (all N conditionals can be computed simultaneously), gradients are well-behaved (no sampling noise), and training converges faster.Harmful because: There's a mismatch between training and inference (exposure bias). A small error during generation can compound — if the model generates x2 incorrectly, then x3 is conditioned on a wrong context, leading to cascading errors. This is why techniques like scheduled sampling (mixing ground-truth and generated context during training) are sometimes used. Q9: In PixelCNN, why is it important to have a separate "vertical stack" and "horizontal stack" (Gated PixelCNN)?A single masked convolution has a blind spot: pixels directly above the current position (in the same column) are masked out. This happens because:
- Future pixels (below center row) are masked
- Future pixels on the same row (right of center) are masked
- But pixels ABOVE the center row are NOT masked (they're past context)
- However, the single mask zeros out the entire top-right quadrant and the center-right part of the middle row, which includes some pixels above
The vertical stack handles ALL pixels above the current row, while the horizontal stack handles pixels to the left on the same row. The two stacks process information independently and are combined via gating. This split design eliminates the blind spot entirely. Q10: How does an autoregressive model handle RGB color channels at a single pixel location?For RGB images, the three color channels at a single location (i,j) must be ordered. The standard approach (used in PixelCNN) is to process them as R → G → B within each pixel location.The factorization becomes:p(xi,j,R,xi,j,G,xi,j,B∣x<(i,j))=p(xi,j,R∣x<(i,j))⋅p(xi,j,G∣x<(i,j),xi,j,R)⋅p(xi,j,B∣x<(i,j),xi,j,R,xi,j,G)This means the model first predicts the red channel (conditioned on all previous pixels), then green (conditioned on previous pixels + red at current pixel), then blue (conditioned on previous pixels + red + green at current pixel).Architecturally, this is achieved by having separate output heads for R, G, B, where G's head receives R's output and B's head receives R and G's outputs. Q11: Compare autoregressive models and normalizing flows as exact likelihood methods.
| Aspect | Autoregressive | Normalizing Flows |
|---|---|---|
| Likelihood | Exact (chain rule) | Exact (change of variables) |
| Training | Simple (MLE of conditionals) | Simple (MLE via Jacobian) |
| Sampling | Sequential ( O(N) ) | Parallel ( O(1) via inverse) |
| Architecture | Masked conv/attention | Invertible layers (coupling, 1×1 conv) |
| Computational cost (sampling) | O(N) sequential passes | O(N) parallel (one forward pass) |
Normalizing flows offer parallel sampling (sample z∼N(0,I), transform to x in one pass) while maintaining exact likelihood. However, flows require designing invertible transformations with tractable Jacobians, which constrains architecture choices. Autoregressive models have more architectural freedom but pay for it with sequential sampling.In practice, flows are rarely used for high-resolution images because the invertibility constraint makes them parameter-inefficient compared to autoregressive or diffusion models. Q12: Given PixelCNN's slow sampling, propose a hybrid approach that combines autoregressive models with another generative model type.Hybrid: Autoregressive prior + Diffusion decoder
- High-level structure: Use an autoregressive model to generate a low-resolution (e.g., 8×8) image
- Super-resolution: Use a diffusion model (or upsampling network) conditioned on the low-res output to generate the full-resolution (256×256) image
This leverages:
- Autoregressive strength: Excellent density estimation for low-res (few pixels, manageable sequential cost)
- Diffusion strength: Fast, high-quality upsampling (only 20-50 steps in pixel space)
Another approach: Autoregressive latent model (e.g., VQ-VAE + PixelCNN):
- Compress the image to discrete latent codes via VQ-VAE (e.g., 32×32 grid of codes)
- Use PixelCNN on the latent grid (1024 sequential steps instead of 65536)
- Decode the latent grid back to pixels in one forward pass
This is the basis of models like DALL·E, VQ-GAN, and Parti — combining the best of both worlds.
5. 🔗 Cross-References
- Previous: DDIMs (Week 5) — Accelerated diffusion sampling
- Next: Conditional Generation (Week 7) — Adding conditioning to generative models
- Related: Evaluation Metrics (Week 8) — How to measure autoregressive model quality
- External: Van den Oord et al., "Pixel Recurrent Neural Networks" (ICML 2016)
- External: Van den Oord et al., "Conditional Image Generation with PixelCNN Decoders" (NIPS 2016)
- External: Salimans et al., "PixelCNN++: Improving the PixelCNN with Discretized Logistic Mixture Likelihood" (ICLR 2017) Join Discord PreviousDDIMsNextConditional Generation