- The dataset (Figure 30.4): three shapes in eight colors, with random size, position and rotation.
- Autoencoders (Figure 30.5): an encoder, a bottleneck and a decoder. You inspect the embedding through reconstructions, nearest neighbors and a probe of each layer.
- K-means (Figure 30.11): clustering as a discrete representation.
- Contrastive learning (Figure 30.14): the InfoNCE loss, where the choice of data augmentation decides whether the embedding encodes color or shape.
Code
Code
The colored-shapes dataset
Each image shows one of three shapes (circle, square or triangle) in one of eight colors, with random size, position and rotation. Shape and color vary independently of each other, and a good representation keeps the two apart.Autoencoders
An autoencoder passes each image through a low-dimensional bottleneck and back, and is trained to reconstruct its input by minimizing over the encoder and the decoder . The bottleneck has to keep what the decoder needs to redraw the image, so it becomes a compact representation of the image. You train a small convolutional autoencoder and then look at what its embedding captures.Code
Code
Reconstructions and nearest neighbors
The reconstructions show that the bottleneck keeps the shape, the color and the rough pose of each object. The nearest neighbors of an image in the embedding space usually share both its shape and its color, so the embedding has organized the data by the two factors that generated it.Code
Code
What each layer encodes
A 1-nearest-neighbor probe measures what a layer keeps. It labels each test image with the label of the closest training image in that layer’s features, once for shape and once for color. Shape accuracy rises with depth, from 78% on the raw pixels to 98% after the sixth convolution, while color accuracy falls from 79% to 65%. The deeper layers keep the more abstract factor and discard low-level appearance, which is the crossing the book reports in Figure 30.5b. The crossing appears only with the book’s architecture, six convolutional layers that keep some spatial detail, and a long training run. A shallower model, or one that downsamples each image to a single vector, does not show it.Code
Code

K-means clustering
Clustering gives a discrete representation: each point is summarized by the index of its nearest code vector. K-means minimizes by block coordinate descent. It alternates two steps: it assigns each point to its nearest mean, then moves each mean to the average of the points assigned to it. The figure shows the first iterations on a two-dimensional dataset with (Figure 30.11).Code
Code
Contrastive learning and the choice of augmentation
Contrastive learning pulls together the embeddings of two augmented views of the same image, a positive pair, and pushes apart the embeddings of different images. The InfoNCE loss does this: where is the other view of , the are views of other images in the batch, and is a temperature. The embedding becomes invariant to whatever the augmentation changes, so the choice of augmentation decides which factor survives. Here the embedding has two dimensions () and lies on the unit circle, so you can plot it directly:- Cropping alone () changes which part of the shape is visible but keeps the color, and the embedding groups the images by color.
- Color jitter () destroys the color but keeps the shape, and the embedding groups the images by shape.
Code
Code


Summary
A representation describes an image by the factors that produced it, here shape and color, and each method in this section exposes those factors differently. The autoencoder learns an embedding without labels. Its nearest neighbors share shape and color, and its deeper layers trade color for shape. K-means describes each point by a cluster index, which is a discrete representation. Contrastive learning makes the choice explicit: the augmentation you pick sets the invariance you get, so cropping keeps color and color jitter keeps shape. Modern self-supervised encoders depend on the same choice, and so does CLIP, where the two views of a pair are an image and its caption.Found an error on this page? Report an issue.

