Conditional Positional Encoding (CPE) is a method that does not store positional encodings as a fixed table, but instead generates them on the fly with a small convolution over the tokens surrounding each token. Designed for image Transformers (ViT, DeiT), it was posted on arXiv by Chu et al. in 2021 and accepted at ICLR in 2023. The component that produces CPE is called the PEG (Positional Encoding Generator), and a ViT with a PEG inserted is called CPVT.
ViT cuts an image into patches to form a sequence of tokens and adds learned positional embeddings to convey their order. This approach has two problems specific to images.
- Weak on image sizes different from training: The table of positional embeddings only covers the image size used in training (for example, 14×14 = 196 entries for 224×224). For larger images the table is interpolated, but accuracy drops without fine-tuning.
- Does not behave consistently under translation: Since a different vector is added at each position, when an object in the image moves, the positional vectors added to it also change. The property of convolutional neural networks that "if the object moves, the output moves by the same amount" (translation equivariance) breaks down.
CPE solves both problems by generating the positional encoding from each token's neighborhood. As an analogy, instead of handing each person an address on a map (absolute position), each person is asked to look at "who is to my left and who is to my right." Since it only looks at the surroundings, it works the same way even when the venue (image) gets larger. Also, people standing by the wall of the venue can tell they are at the edge because "there is no one next to them," so absolute position is conveyed to some extent as well.

