Positional Codes Keep Patches Placed
Positional codes add where to what, so permutation-blind attention still knows top-left from bottom-right.
Why Does This Exist?
Attention treats tokens as a bag. Shuffle patches and scores stay identical, but a cat scrambled is not a cat.
Codes restore order. They ground attention mechanisms, replace convolution geometry in convolutional layers stacks, and shape image classification maps.
Think of It Like This
Seat numbers on exam papers
Identical answer sheets need seat numbers to map to students. Content matches but seats decide identity.
Tokens are sheets, codes are seat numbers. Where it stops: wrong seat numbers misassign good answers, just as wrong codes misplace patches.
How It Actually Works
For patch and dim , 2D sine codes use with cosine on odd dims, where is width. Add to token . Learned codes train a table. Relative bias adds inside attention instead.
Worked example
, row , col . Dim 0 uses , so plus for . Dim 1 cosine gives plus for . Token becomes . Shuffle rows and the sums change, so attention follows place.
Watch Out For
Interpolating codes badly on larger tests
Train at then test at without resizing codes and maps smear. Symptom: accuracy drops on larger inputs only. Fix: bicubic-interpolate position tables to test grids before inference.
The Quick Version
- Attention is order blind, so codes add place to content.
- Sine plus cosine of row and column tag each patch deterministically.
- Fixture token shifts from 0.5 to 2.25 after adding codes.
- Learned tables work, relative bias generalizes better.
- Resize codes when testing larger than training.