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 .
Which are equivariant, invariant or neither under circular shifts?
- ReLU is equivariant, because it acts on each entry alone.
- The sum is invariant, because a circular shift only reorders the entries.
- The first entry is neither. Shifting to changes it from to , and its output is a scalar that a shift cannot move.
- The centered vector is equivariant. The mean is invariant, and subtracting it is a pointwise operation.
- The cumulative sum is neither. For it gives , and for the shifted it gives . Shifting the first output gives again, not , so the map is not equivariant. The two outputs differ, so it is not invariant either.
(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.
Compute the three outputs. . The shift by one is , which gives . Every nonzero value moved to a skipped position, so the response vanished. The shift by two is , which gives . This is shifted circularly by one. A shift by in the input moves the output by . A shift by has no output counterpart at all.
The original gives
(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 .
Compute the feature maps and outputs with circular padding and with zero padding. . With circular padding, and . For this gives , where wraps around. For it gives , which is shifted circularly by one position, so the layer is equivariant. Both averages are , so the network is invariant to the shift.
Each unit is
With zero padding, . Now , because the contribution to is lost. The shifted input still gives . The averages are and , so the output changes. The pattern at the border lost part of its response to the padding. Shifting one more step to gives , again with average . Zero padding keeps the invariance for shifts in the interior and breaks it at the border.
See Also
- 5.2.1. Convolutional Layer, whose weight sharing gives equivariance.
- 5.2.2. Pooling, which turns equivariance into partial or full invariance.
- 4.4.2. Data Augmentation, for the invariances the architecture does not provide.
- 6.4. U-Net, an equivariant network for per-pixel outputs.