2D sinusoidal position encoding is a fixed mathematical position representation that extends the original $1D$ sinusoidal encoding from "Attention Is All You Need" to two spatial dimensions — encoding each patch's $(x, y)$ grid position using independent sine and cosine functions along each axis and concatenating them to provide deterministic, parameter-free spatial information to Vision Transformers.
What Is $2D Sinusoidal Position$ Encoding?
- Definition: A position encoding method that separately encodes the row $(y)$ and column $(x)$ coordinates of each patch using $1D$ sinusoidal functions, then concatenates the two encodings to form a complete $2D$ position representation.
- Formula: For patch at position $(x, y)$ with embedding dimension $D: \mathrm{PE}(x, y) = [\mathrm{PE}_x(x), \mathrm{PE}_y(y)]$ where each PE uses alternating sin/cos at geometrically increasing frequencies.
- 1D Component: $\mathrm{PE}(pos, 2i) = \sin(pos / 10000^{2i/d}), \mathrm{PE}(pos, 2i+1) = \cos(pos / 10000^{2i/d})$, where $i$ indexes the embedding dimension and $d$ is half the total embedding dimension (D/2 for each axis).
- Origin: The $1D$ version was introduced in the original Transformer paper (Vaswani et al., 2017) for sequential data — the $2D$ extension adapts it for the grid structure of images.
Why $2D Sinusoidal Encoding$ Matters
- No Learnable Parameters: Unlike learned position embeddings, sinusoidal encoding is completely determined by a mathematical formula — it requires zero training and adds zero parameters to the model.
- Deterministic: The position encoding is fixed and reproducible — no variability from random initialization or training dynamics.
- Extrapolation Potential: The mathematical structure theoretically supports positions beyond the training range, offering better generalization to unseen sequence lengths than learned embeddings.
- Unique Position Representation: The combination of multiple frequency sine and cosine functions guarantees that every $(x, y)$ position maps to a unique vector, and relative positions can be expressed as linear transformations of the encoding.
- Baseline Reference: Serves as a strong parameter-free baseline against which learned position encodings are compared.
How $2D Sinusoidal Encoding$ Works
Step 1 — Separate Axes:
- Split the D-dimensional embedding into two halves: $D/2$ for the x-axis, D/2 for the y-axis.
Step 2 — Encode Each Axis:
- For the x-coordinate: Apply 1D sinusoidal encoding with $D/2$ dimensions.
- For the y-coordinate: Apply 1D sinusoidal encoding with $D/2$ dimensions.
- Each uses sin for even indices and cos for odd indices.
Step 3 — Concatenate:
$- \mathrm{PE}(x, y) = concat[\mathrm{PE}_{\sin_{\cos}}(x$, D/2), $\mathrm{PE}_{\sin_{\cos}}(y$, D/2)].
- Result: D-dimensional vector encoding the full $2D$ position.
Frequency Design:
- Low frequencies (early dimensions): Encode coarse position — distinguishes "left half" from "right half."
- High frequencies (later dimensions): Encode fine position — distinguishes adjacent patches.
- Geometric frequency spacing (1, $1/10000^{2/d}$, ...) provides logarithmically uniform coverage of spatial scales.
Mathematical Properties
- Unique Encoding: Each $(x, y)$ pair maps to a distinct D-dimensional vector with high probability due to the irrational frequency ratios.
- Relative Position as Linear Transform: $\mathrm{PE}(pos+k)$ can be expressed as a linear transformation of $\mathrm{PE}(pos)$ — this means the model can learn relative position relationships through linear attention operations.
- Bounded Values: All encoding values lie in [-1, 1], providing numerical stability.
- Orthogonality: Position vectors for distant positions tend to be more orthogonal, providing natural distance encoding.
2D Sinusoidal vs. Other Position Encodings
| Property | $2D Sinusoidal$ | Learned | Relative Bias | CPE |
|---|---|---|---|---|
| Parameters | 0 | $N \times D$ | $(2M-1)^2$ | Conv params |
| Resolution Flexible | Good | Poor (interpolation) | Good | Excellent |
| Translation Invariant | No | No | Yes | Yes |
| Extrapolation | Moderate | Poor | Limited | Good |
| Implementation | Simple formula | Embedding table | Index lookup | Conv layer |
| Training Stability | Perfect (fixed) | May overfit | Stable | Stable |
Usage in Vision Transformers
- Original ViT: Dosovitskiy et al. tested sinusoidal position encoding and found it performed comparably to learned embeddings — marginal differences in most settings.
- MAE (Masked Autoencoders): Uses $2D$ sinusoidal position encoding as the default, demonstrating it works well for self-supervised pretraining.
- DeiT: Uses learned position embeddings by default but sinusoidal is a viable alternative.
- ViT for Detection: DETR and variants use sinusoidal encoding for both spatial and learned components of position information.
When to Use $2D Sinusoidal$ Encoding
- Limited Training Data: When there isn't enough data to learn meaningful position embeddings.
- Parameter Budget: When model size must be minimized (edge deployment, mobile).
- Self-Supervised Pretraining: Works well as a stable, fixed reference point during unsupervised training.
- Multi-Resolution Inference: When the model must handle varying input resolutions without fine-tuning.
$2D$ sinusoidal position encoding is the mathematical foundation of spatial awareness in transformers — by encoding latitude and longitude with multi-frequency sine and cosine waves, it provides every patch with a precise, unique grid coordinate that requires no learning and no parameters, proving that sometimes the simplest solution is also one of the best.