Equivariance and Invariance

A function is equivariant (or covariant) to a transformation if transforming the input transforms the output in the same way:

It is invariant to if transforming the input leaves the output unchanged:

An image classifier should be invariant to translation, since a shifted mountain is still a mountain. A segmentation network should be equivariant, since its label map must move with the image. The parts of a CNN treat translation differently:

  • Convolution with stride is equivariant: shifting the input shifts every feature map by the same amount. This holds exactly for circular shifts with circular padding, and away from the border otherwise.
  • Pointwise operations, such as adding the bias and applying the ReLU, act on every position alike and keep equivariance.
  • Strided convolution and pooling are equivariant only to shifts by a multiple of the stride. Max pooling gives partial invariance to small shifts that keep each maximum inside its window.
  • Global average pooling averages each feature map over all positions. It is invariant to circular shifts, and to any shift that keeps the pattern away from the border.

A stack of equivariant layers is equivariant, and an invariant layer at the end makes the whole network invariant. Nothing in a standard CNN builds in rotation, scale or reflection. The network learns those from the variation in its training data, mostly supplied by data augmentation.

Intuition

Weight sharing runs the same detector at every position. A detector for a vertical edge fires wherever a vertical edge is, so moving the edge moves the response. This is equivariance, and it means the network does not have to learn the same pattern again at every location. A classifier then has to forget where the pattern was. Pooling does this. Global average pooling forgets position completely, while a max pooling forgets only shifts within its window.

The guarantees are exact only in idealized settings. Zero padding treats the border differently from the interior, and a stride of cannot represent a one-pixel shift. Real CNNs are therefore only approximately equivariant. A shift of a single pixel can change the prediction of a trained classifier noticeably.

Properties

(Composition) Equivariant layers compose into an equivariant network, and an invariant layer anywhere after them makes the network invariant.

If and are both equivariant to , then

If is invariant to , then . A single layer that is not equivariant, placed before the invariant one, removes the guarantee for the whole network. A single strided layer limits the whole network to shifts by a multiple of its stride. Layers after the invariant one, such as the final dense classifier, keep the invariance. Their input no longer changes under the shift.

(Borders and Strides) Translation equivariance is exact only for circular shifts under circular padding, and with stride only for shifts by multiples of .

Zero padding replaces the values beyond the border with zeros. When a pattern moves toward the edge, part of it falls into the padding. The edge units then respond differently from units in the interior. A stride of breaks equivariance in a different way. Shifting the input by one pixel does not shift a stride-2 output by a whole unit. The layer is equivariant only to shifts by multiples of , and the output then moves by of the shift. A one-pixel shift can even move a feature from a sampled position to a skipped one. Its response then disappears from the output.

Not Taught in Lecture

Prince mentions in the chapter notes that max-blur-pooling (Zhang, 2019) restores much of the lost shift equivariance. It blurs each map before subsampling it.

(Weight Sharing Is What Gives Equivariance) Among linear layers on a circular signal, the shift-equivariant ones are exactly the convolutions.

Not Taught in Lecture

Lecture and Prince state that convolution is equivariant. The converse below is standard linear algebra.

A linear layer on a length- signal is equivariant to circular shifts exactly when , where is the cyclic shift matrix. Entry by entry, this says with indices taken modulo . Every diagonal is then constant and wraps around, so is a circulant matrix. A circulant matrix is a circular convolution with the kernel stored in its first row. A locally connected layer gives each row its own weights, so it is not equivariant. For linear layers on circular signals, equivariance and weight sharing are therefore equivalent.

(Only Translation Is Built In) A convolution is not equivariant to rotation, scaling or reflection.

A kernel tuned to vertical edges does not respond to horizontal ones. Rotating the input by therefore does not rotate the feature maps. It changes which kernels respond. A network becomes approximately invariant to such transformations only by seeing transformed examples during training. This is the role of data augmentation. Group-equivariant CNNs build some rotations and reflections into the architecture. They share weights across rotated copies of each kernel, but they are not standard.

Derivation

Write a stride-1 1D layer as , so . Let shift the input by , so . Applying the layer to the shifted input gives

In operator form, . The output is shifted by , which is equivariance. The step uses only that the same appear at every position . The bias adds the same everywhere and the activation acts on each alone, so neither changes the argument. The indices must exist. With circular padding they always do, since they wrap around. With zero padding they do whenever the shifted pattern stays away from the border.

Examples

(Easy) Sorting Functions by Their Symmetry

Consider five functions of a vector : the elementwise ReLU, the sum , the first entry , the centered vector , and the cumulative sum .

(Medium) A Stride Breaks Equivariance

A layer with a kernel and stride keeps the entries at positions , and of a length- signal. It is applied to and to its circular shifts by one and by two positions.

(Hard) Invariance Needs Circular Padding

A tiny network applies a 1D convolution with kernel , no bias and stride to an input of length . A ReLU and a global average over the four positions follow. The input is compared with its shift .

See Also