Foundations to Frontier
Tests
H. Generative Models › H2 Autoregressive Models

PixelCNN, Gated PixelCNN, PixelCNN++

PixelCNN replaces PixelRNN's recurrence with masked convolutions. Every weight that would let a pixel see itself or a later pixel is zeroed, so the whole image is processed in one parallel pass during training, though sampling is still pixel by pixel. Stacking masked 3×3 convolutions leaves a blind spot above and to the right of each pixel. The Gated PixelCNN (van den Oord et al., 2016) fixes it with separate vertical and horizontal stacks and adds tanh ⊙ σ gates and class conditioning. PixelCNN++ (Salimans et al., 2017) replaces the 256-way softmax with a discretized logistic mixture and adds downsampling, shortcut connections and dropout, reaching 2.92 bits/dim on CIFAR-10. In the exercises you build mask A and mask B, trace receptive fields and blind spots (3, 10 and 28 blind pixels after 3, 5 and 8 layers), and evaluate the discretized logistic likelihood. You then train two 3-layer, 8-channel PixelCNNs on 8×8 digits with backpropagation written out by hand. The gated vertical/horizontal stacks saw 24 earlier pixels with none blind and reached 14.22 nats per image. The plain masked stack saw 21 pixels with 3 blind and reached 14.74. Both were worse than MADE (12.30) on such small images.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Convolutions that are not allowed to peek

A normal convolution looks at a pixel’s whole neighbourhood, including pixels below and to the right. To predict pixels in reading order, PixelCNN covers up those future pixels with a mask, like a sheet of paper slid down the page as you read. Because every pixel can then be predicted at once from the real image, training is fast. A side effect is a blind spot: stacked masks never reach some pixels to the upper right. The demo shows the receptive field and the blind spot, and how two separate stacks remove it.

Generative models lab
Target pixel red, receptive field green, yellow: nearby earlier pixels it cannot see. Stacked masked convolutions: 21 pixels in the field, 3 blind pixels inside its bounding box; the blind spot to the upper right grows with depth.
Why engineers care

PixelCNN-style priors are used for lossless and near-lossless compression of instrument data and as the prior over discrete codes in VQ-VAE pipelines.

📝 Quick check (Level 0)
0/3 answered
  1. 1. PixelCNN enforces the ordering with…
  2. 2. Training PixelCNN is…
  3. 3. The blind spot is fixed by…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Masked convolution: zero the kernel weights at the centre row’s right half and all rows below; mask A (first layer) also zeroes the centre, mask B (later layers) keeps it. For colour, mask channel groups so that G sees R and B sees R and G.
  • PixelCNN (van den Oord, Kalchbrenner and Kavukcuoglu, 2016): 7×7 mask-A layer, 15 residual 3×3 mask-B layers, 256-way softmax; CIFAR-10 3.14 bits/dim, faster to train than PixelRNN but worse.
  • Blind spot: stacked masks grow the field up and to the left but miss a wedge to the upper right that grows as L(L − 1)/2 pixels.
  • Gated PixelCNN (van den Oord et al., 2016): a vertical stack sees all rows above (n × n kernel cropped to the rows above); a horizontal stack sees the current row to the left plus the vertical stack’s output; gated units tanh(W_f ∗ x) ⊙ σ(W_g ∗ x); conditioning h added to both gates (class labels, face embeddings). CIFAR-10 3.03 bits/dim.
  • PixelCNN++ (Salimans, Karpathy, Chen and Kingma, 2017): discretized logistic mixture likelihood (neighbouring values share mass, 10 components instead of 256 logits), whole-pixel conditioning with linear dependence between channels, strided downsampling with U-Net-like shortcuts, dropout; CIFAR-10 2.92 bits/dim.
  • Later: PixelSNAIL (2018) added self-attention (2.85); VQ-VAE (2017) and VQ-VAE-2 (2019) used PixelCNN priors over discrete codes; causal transformers replaced convolutional priors from about 2020.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Mask A differs from mask B in…
  2. 2. PixelCNN++ replaced the softmax with…
  3. 3. Gated units compute…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Masks, fields and likelihoods

Exercise 1.

  • Build k×k masks A and B.
  • Propagate a receptive field backwards through stacked masked convolutions and see the blind spot.
  • Compute the gated activation and the PixelCNN++ discretized logistic likelihood of an 8-bit value.

Practice.

  • Implement masked convolutions by multiplying the weight by a fixed mask in every forward pass (or by shifting and cropping padded convolutions, which is faster).
  • Check causality numerically: changing pixel p must not change any logit at positions ≤ p.
  • Use the vertical/horizontal split, gates and residual connections; condition with a learned bias per class.
  • For 8-bit data prefer the discretized logistic mixture; report bits/dim.
  • Sampling needs one pass per pixel (and channel); cache activations (Fast PixelCNN++, 2017) for speed.
import torch, torch.nn as nn
class MaskedConv2d(nn.Conv2d):
    def __init__(self, cin, cout, k, kind):
        super().__init__(cin, cout, k, padding=k // 2)
        m = torch.ones_like(self.weight); c = k // 2
        m[:, :, c, c + (kind == "B"):] = 0; m[:, :, c + 1:] = 0
        self.register_buffer("mask", m)
    def forward(self, x): return nn.functional.conv2d(x, self.weight * self.mask, self.bias, padding=self.padding)
net = nn.Sequential(MaskedConv2d(1, 64, 7, "A"), nn.ReLU(), *[m for _ in range(5) for m in (MaskedConv2d(64, 64, 3, "B"), nn.ReLU())],
                    nn.Conv2d(64, 1, 1))
🐍 Exercise 1: masks, blind spots and the discretized logistic
Build PixelCNN masks, trace the receptive field of a masked stack, and evaluate the gated unit and the PixelCNN++ likelihood.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Verify causality by…
  2. 2. Class conditioning in Gated PixelCNN adds…
  3. 3. Fast PixelCNN++ speeds up…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the blind spot. Mask B lets position (i, j) read (i − 1, j − 1..j + 1) and (i, j − 1..j). Information can therefore move one column right only by also moving one row up. After L layers, the field u rows up reaches only u columns to the right of j, while the bounding box reaches L columns right; the wedge between, L(L − 1)/2 earlier pixels, can never be seen.

The two-stack fix. The vertical stack’s features at row i depend only on rows above i, and it has no right-hand restriction, so it covers the full width of the rows above. The horizontal stack covers the current row to the left and reads the vertical stack. Together they cover every earlier pixel within the window, with no blind spot.

Discretized logistic. For x in 0..255 rescaled to [−1, 1], P(x) = σ((x + 1/255 − μ)/s) − σ((x − 1/255 − μ)/s), with the edge bins taking the tails. Nearby values share mass, which gives smoother gradients and fewer parameters than a softmax, and a mixture of 5–10 logistics captures multimodality.

Gating. tanh(a) ⊙ σ(b) mimics LSTM gates; van den Oord et al. credited gating, with the blind-spot fix, for closing the gap to PixelRNN.

model CIFAR-10 bits/dim
PixelCNN (2016) 3.14
PixelRNN, Diagonal BiLSTM (2016) 3.00
Gated PixelCNN (2016) 3.03
PixelCNN++ (2017) 2.92
PixelSNAIL (2018) 2.85
📝 Quick check (Level 3)
0/3 answered
  1. 1. Blind pixels after L layers:
  2. 2. The vertical stack depends only on…
  3. 3. Best CIFAR-10 bits/dim in this family table:

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does removing the blind spot with gated vertical and horizontal stacks improve a small PixelCNN?

History.

  • A. van den Oord, N. Kalchbrenner and K. Kavukcuoglu, “Pixel recurrent neural networks” (2016): PixelCNN.
  • A. van den Oord et al., “Conditional image generation with PixelCNN decoders” (NeurIPS 2016): Gated PixelCNN.
  • T. Salimans, A. Karpathy, X. Chen and D. P. Kingma, “PixelCNN++” (ICLR 2017).
  • X. Chen et al., “PixelSNAIL” (2018); A. van den Oord, O. Vinyals and K. Kavukcuoglu, “Neural discrete representation learning” (VQ-VAE, 2017).

Exercise 2. The same 2000 training and 1000 test 8×8 digits as in NADE / MADE.

  • Plain PixelCNN: 3 masked 3×3 layers (A, B, B), 8 channels, ReLU, 1×1 read-out to a Bernoulli logit.
  • Gated stacks: 3 layers with the 8 channels split into a vertical half (rows above only) and a horizontal half (left neighbour in the row, itself, and the vertical half at the same and previous row); each layer computes tanh ⊙ σ of two masked convolutions, so it has about twice the weights.
  • Training: 200 Adam steps, batch 32, lr 0.015, im2col convolutions with backpropagation written by hand (checked by finite differences and by perturbing pixels to confirm causality).
  • Receptive field of the bottom-row pixel (7, 4), from exact mask propagation; the blind count is earlier pixels inside the field’s bounding box that cannot reach it.
model test NLL (nats/image) field (pixels) blind
plain PixelCNN 14.74 21 3
gated vertical + horizontal stacks 14.22 24 0
for reference: FVSBN / MADE-128 (h2-1) 12.90 / 12.30 63 0
layers plain: field / blind stacks: field / blind
2 11 / 1 12 / 0
4 33 / 3 36 / 0
6 49 / 3 52 / 0
  • Gated stacks won on every seed: 14.22 vs 14.74 here, 14.38 vs 15.04 and 14.46 vs 14.76 on seeds 1 and 2. The comparison is confounded: they have no blind spot, a larger field and gates with twice the weights.
  • Blind spot: present at every depth for the plain stack (capped at 3 here by the 8-pixel image edge; 3, 10 and 28 on a large image after 3, 5 and 8 layers).
  • Null result against full-context models: with only 3 layers, both PixelCNNs saw at most 24 of up to 63 earlier pixels and lost to FVSBN and MADE, which connect every earlier pixel. PixelCNN’s advantages are parameter sharing and parallelism on large images, not small ones.
  • Caveat: 8 channels and 200 steps; real PixelCNNs use 15 or more layers and hundreds of channels.
🐍 Exercise 2 (research): does removing the blind spot with gated vertical and horizontal stacks improve a small PixelCNN?
Train a plain masked-convolution PixelCNN and a gated vertical/horizontal-stack PixelCNN on 8×8 digits and compare test likelihood, receptive fields and blind spots.

PixelCNN in AI, robotics and aerospace

  • Priors over discrete codes: PixelCNN priors in VQ-VAE pipelines compress and generate imagery and spectra.
  • Lossless compression: PixelCNN-style models with arithmetic coding beat PNG and JPEG-LS on many image types, at a high compute cost.
  • Super-resolution and inpainting: conditional PixelCNNs (for example on low-resolution input) give sharp, diverse completions.

References

  1. A. van den Oord, N. Kalchbrenner and K. Kavukcuoglu (2016). “Pixel recurrent neural networks.” ICML.
  2. A. van den Oord, N. Kalchbrenner, O. Vinyals, L. Espeholt, A. Graves and K. Kavukcuoglu (2016). “Conditional image generation with PixelCNN decoders.” NeurIPS.
  3. T. Salimans, A. Karpathy, X. Chen and D. P. Kingma (2017). “PixelCNN++: improving the PixelCNN with discretized logistic mixture likelihood and other modifications.” ICLR.
  4. X. Chen, N. Mishra, M. Rohaninejad and P. Abbeel (2018). “PixelSNAIL: an improved autoregressive generative model.” ICML.