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

PixelRNN

PixelRNN (van den Oord, Kalchbrenner and Kavukcuoglu, 2016) models an image pixel by pixel in raster order, like NADE and MADE. Each pixel's colour channels are predicted with a 256-way softmax, and LSTMs carry information from everything already generated. Running an LSTM pixel by pixel takes n² sequential steps, so the paper uses two 2-D variants. The Row LSTM processes a whole row at once with convolutions, which gives a triangular receptive field. The Diagonal BiLSTM skews the image so that a convolution sweeps along diagonals and sees the whole causal context. Residual connections link up to 12 layers. It set the 2016 state of the art (3.00 bits/dim on CIFAR-10) and introduced the discrete softmax output and the masked convolutions used by PixelCNN. In the exercises you skew and unskew feature maps and compute the Row LSTM's receptive field (30 of 43 earlier pixels for pixel (5, 3) of an 8×8 image). You then train a one-layer row-recurrent network on 8×8 digits. Its recurrent state cut test NLL from 19.92 to 13.22 nats per image, and halved it in the bottom half of the image (9.10 → 4.55), where the digit's identity is already known from above.

Level 0: Foundation

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

An image as a long sentence of pixels

PixelRNN reads an image like a book, left to right and top to bottom, and before each pixel it guesses that pixel’s colour from everything read so far. A memory (an LSTM) carries the story along, so by the bottom of a digit the model already knows which digit it is drawing. Because going pixel by pixel is slow, PixelRNN processes whole rows or diagonals in parallel during training. The demo shows which earlier pixels can influence a chosen pixel in each variant.

Generative models lab
Pixel (5, 3) in red. Green: pixels that reach it; yellow: earlier pixels it cannot see. The Row LSTM sees 30 of the 43 earlier pixels: its triangular field misses pixels far to the side.
Why engineers care

The same idea, a recurrent or causal model over a raster of pixels, is used for lossless compression of images from space instruments, where every bit of downlink counts.

📝 Quick check (Level 0)
0/3 answered
  1. 1. PixelRNN generates pixels in…
  2. 2. Each colour value is predicted with…
  3. 3. The Diagonal BiLSTM sees…

Level 1: Beginner

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

By hand

  • Model: p(x) = Π_i p(x_i | x_before i), and within a pixel p(R) p(G | R) p(B | R, G); each is a 256-way softmax over intensities, which handles multimodal and sharp distributions better than a Gaussian.
  • Row LSTM: an LSTM runs down the rows; the input-to-state component is a masked 1-D convolution over the current row, and the state-to-state component is a kernel-3 convolution over the previous row’s states. All pixels of a row are computed in parallel, n sequential steps. Its receptive field is a triangle and misses context to the far sides.
  • Diagonal BiLSTM: skew the map so that row i shifts right by i; a column-wise LSTM with a kernel-2 state convolution then sweeps diagonals; two directions are combined (the right-to-left one shifted down by a row to stay causal). Full causal receptive field, 2n − 1 steps.
  • Masks: mask A in the first layer (a pixel cannot see itself, and colour G sees R only), mask B afterwards.
  • Results (2016): binarised MNIST 79.20 nats; CIFAR-10 3.00 bits/dim (Diagonal BiLSTM) vs 3.14 for the PixelCNN of the same paper; ImageNet 32×32 3.86 bits/dim.
  • Legacy: PixelCNN, Gated PixelCNN and later causal transformers replaced the recurrence because it is slow to train; the discrete softmax and channel masking remain standard.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The Row LSTM's receptive field is…
  2. 2. Skewing the input lets an LSTM sweep…
  3. 3. PixelRNN's CIFAR-10 result:

Level 2: Intermediate

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

Rows, diagonals and receptive fields

Exercise 1.

  • Skew and unskew a feature map.
  • Compute the one-layer Row LSTM receptive field of a pixel.
  • Compare it with the full causal context.

Practice.

  • Implement Row LSTM as a loop over rows with 1-D convolutions; the Diagonal BiLSTM as skew → column loop → unskew.
  • Use residual connections between layers; 7 to 12 layers in the paper.
  • Treat intensities as 256 classes; report bits per dimension.
  • Sampling is pixel by pixel and slow (n² network passes); cache states when generating.
  • For new work prefer PixelCNN or a causal transformer; PixelRNN is mainly of historical and teaching value.
import torch, torch.nn as nn
class RowRNN(nn.Module):          # minimal row-recurrent model for binary images
    def __init__(s, h=16):
        super().__init__(); s.inp = nn.Conv1d(2, h, 3, padding=1); s.state = nn.Conv1d(h, h, 3, padding=1); s.out = nn.Conv1d(h, 1, 1)
    def forward(s, x):            # x: (B, H, W); predicts every pixel from earlier ones
        B, H, W = x.shape; h = x.new_zeros(B, s.state.in_channels, W); logits = []
        left = nn.functional.pad(x, (1, 0))[..., :W]; above = nn.functional.pad(x, (0, 0, 1, 0))[:, :H]
        for i in range(H):
            h = torch.tanh(s.inp(torch.stack([left[:, i], above[:, i]], 1)) + s.state(h)); logits.append(s.out(h)[:, 0])
        return torch.stack(logits, 1)
🐍 Exercise 1: skewing and receptive fields
Skew and unskew a feature map, and compute which earlier pixels a one-layer Row LSTM can see.
📝 Quick check (Level 2)
0/3 answered
  1. 1. PixelRNN sampling needs about…
  2. 2. Layers in PixelRNN are connected with…
  3. 3. For new work, prefer…

Level 3: Advanced

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

Theory

Receptive field of the Row LSTM. In one layer, h(i, j) sees x(i, j − 1) and h(i − 1, j − 1..j + 1). Unrolling u rows up gives the columns j − 1 − u .. j − 1 + u: a triangle that misses the upper-right and far-left context near the pixel’s row. Stacking layers widens the triangle but never fills the region to the right of the current pixel in the row just above at distance > 1 in one step.

Diagonal BiLSTM. After skewing, pixel (i, j) sits at column i + j; a column-wise LSTM processes one anti-diagonal per step, and with a kernel-2 state convolution each position sees the positions above and to the left in the original image. Combining a left-to-right and a right-to-left pass gives the full causal context in a single layer.

Why the softmax. A 256-way softmax makes no assumption about shape, can be multimodal, and gives exact discrete likelihoods; the price is 256 outputs per channel and no notion that 127 and 128 are close (PixelCNN++ later fixed this with a discretised logistic mixture).

variant sequential steps (n×n) receptive field
raster LSTM n² full
Row LSTM n triangle
Diagonal BiLSTM 2n − 1 full
PixelCNN (next topic) 1 (training) growing square, blind spot
📝 Quick check (Level 3)
0/3 answered
  1. 1. Skewed width of an n×n map:
  2. 2. A drawback of the 256-way softmax:
  3. 3. Row LSTM steps for an n×n image:

Level 4: Expert

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

How much does a recurrent state, which carries information from far above, add to a model that only sees nearby pixels?

History.

  • A. Graves and J. Schmidhuber, “Offline handwriting recognition with multidimensional recurrent neural networks” (2009).
  • L. Theis and M. Bethge, “Generative image modeling using spatial LSTMs” (2015).
  • A. van den Oord, N. Kalchbrenner and K. Kavukcuoglu, “Pixel recurrent neural networks” (ICML 2016 best paper).

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

  • Inputs at each pixel: the 3 pixels to its left and the 3 pixels above it (j − 1, j, j + 1).
  • Local context: h = tanh(features · A + b), Bernoulli logit from h; 16 units; no recurrence.
  • Row RNN: the same plus a tanh recurrence down the rows, h_i = tanh(F_i A + shift(h_(i−1)) U + b), with a kernel-3 state convolution: a one-layer, gate-free simplification of the Row LSTM.
  • Training: 200 Adam steps (batch 64, lr 0.03), backpropagation through the 8 rows; gradients checked by finite differences.
model test NLL (nats/image) top 4 rows bottom 4 rows
local context 19.92 10.81 9.10
Row RNN 13.22 8.67 4.55
for reference: FVSBN / MADE-128 (h2-1, same data) 12.90 / 12.30 – –
  • The recurrent state mattered: NLL fell by 6.7 nats, and the bottom half halved (9.10 → 4.55): by then the state knows the digit.
  • Top half: the gain was smaller (10.81 → 8.67), because there is less above to remember.
  • Null result against simpler full-context models: on 8×8 images, FVSBN and MADE, which connect every earlier pixel directly, did slightly better than this tiny one-layer Row RNN. Recurrence pays off on large images, where direct connections to every earlier pixel are infeasible.
  • Seeds 1 and 2: local 20.13 and 20.10; Row RNN 13.31 and 13.47; bottom half 9.23 → 4.45 and 9.22 → 4.51.
  • Caveat: no LSTM gates, one layer and 200 steps; the paper’s 12-layer Diagonal BiLSTM is far stronger.
🐍 Exercise 2 (research): how much does a recurrent state, which carries information from far above, add to a model that only sees nearby pixels?
Train the same local network with and without a recurrent state that runs down the image rows, and compare test NLL in the top and bottom halves of the digits.

PixelRNN in AI, robotics and aerospace

  • Lossless compression: autoregressive pixel models give near-optimal code lengths for instrument images with arithmetic coding.
  • Inpainting and gap filling: conditional sampling fills missing scan lines in satellite imagery (for example striping artefacts).
  • Historical role: PixelRNN’s discrete softmax and masking ideas carried into PixelCNN, WaveNet and image transformers.

References

  1. A. van den Oord, N. Kalchbrenner and K. Kavukcuoglu (2016). “Pixel recurrent neural networks.” ICML.
  2. L. Theis and M. Bethge (2015). “Generative image modeling using spatial LSTMs.” NeurIPS.
  3. A. Graves and J. Schmidhuber (2009). “Offline handwriting recognition with multidimensional recurrent neural networks.” NeurIPS.