A 2D Semantic-Aware Position Encoding for Vision Transformers

Xi Chen, Shiyang Zhou, Muqi Huang, Jiaxu Feng, Yun Xiong, Kun Zhou, Biao Yang, Yuhui Zhang, Huishuai Bao, Sijia Peng, Chuan Li, Feng ShiarXiv 2025 · DOI

Abstract

Vision Transformers (ViTs) typically rely on fixed, coordinate-based position encodings (PE) borrowed from Natural Language Processing, which ignore the actual visual content of the image. This paper introduces $\text{SaPE}^2$, a 2D semantic-aware position encoding that dynamically adjusts positional representations based on the visual similarity of patches. By linking spatial position to semantic content rather than rigid grids, $\text{SaPE}^2$ significantly boosts image classification accuracy and enhances the model's ability to distinguish foreground from background.

Introduction

In a Vision Transformer, an image is partitioned into a grid of patches, flattened into a sequence, and processed via self-attention layers. Because self-attention is permutation-invariant, explicit Position Encodings (PE) are required to preserve spatial structure. Historically, vision models have utilized Absolute Position Encoding (APE) or Relative Position Encoding (RPE). However, these coordinate-based methods are strictly geometric, informing the model of fixed spatial offsets without regard for image semantics. This rigid approach limits generalization across resolutions and hinders the processing of repetitive textures or spatially distant but semantically related regions.

Comparison of Absolute PE, Relative PE, and the proposed SaPE² within a Vision Transformer architecture.

Methodology

The proposed 2-Dimensional Semantic-Aware Position Encoding ($\text{SaPE}^2$) calculates the "distance" between patches based on their semantic similarity rather than physical coordinates. The method involves four key steps:

  1. Semantic Gating: Inspired by Contextual Position Encoding (CoPE), the model calculates a "gate value" between 0 and 1 for patch pairs using the dot product of their Query and Key vectors. If two patches are semantically similar, the gate opens.
  2. 2D Decomposition: To manage complexity, $\text{SaPE}^2$ decomposes semantic distances independently along the horizontal (x-axis) and vertical (y-axis) directions.
  3. Continuous Interpolation: Since semantic distances are continuous values, the method interpolates between learnable integer embeddings to create smooth positional representations.
  4. Attention Bias: The Euclidean distance between these semantic position vectors is added as a bias directly into the self-attention mechanism.
Detailed breakdown of acquiring semantic-aware position information along the x and y axes.

Experimental Results

The authors evaluated $\text{SaPE}^2$ on standard benchmarks using a ViT-Small architecture. The addition of semantic-aware encoding yielded significant quantitative improvements over standard baselines:

  • CIFAR-10: The baseline ViT (with standard APE) achieved 87.41% Top-1 accuracy. Upgrading to $\text{SaPE}^2$ + APE pushed this to 93.98%.
  • CIFAR-100: The baseline achieved 66.54%, while the $\text{SaPE}^2$ + APE model reached 72.23%.
  • Comparison with Baselines: $\text{SaPE}^2$ outperformed other advanced position encodings adapted for vision, including 2D RoPE (89.98%) and CoPE (92.54%) on CIFAR-10.

Discussion and Insights

Key findings from the study include:

  • Perceptual Distance: Allowing the model to assign similar positional representations to semantically related regions helps it group foreground objects together and separate them from the background, even when they are physically distant.
  • Complementary Encodings: The researchers found that combining APE (providing rigid structural scaffolding) with $\text{SaPE}^2$ (providing flexible, content-aware grouping) yielded the best results.
  • Key-Centric Integration: Applying $\text{SaPE}^2$ to the Key (K) matrix proved more effective than applying it to the Query (Q) matrix, as it enriches the contextual map that Queries search against.

Conclusion

While $\text{SaPE}^2$ offers significant performance gains, it introduces quadratic computational complexity ($O(N^2)$) relative to the number of patches. Furthermore, current implementation requires training from scratch. Future work will focus on efficient fine-tuning strategies to integrate $\text{SaPE}^2$ into existing pre-trained models without requiring a full retrain.

Summary updated

BibTeX
@misc{chen2025d,
  author = {Xi Chen and Shiyang Zhou and Muqi Huang and Jiaxu Feng and Yun Xiong and Kun Zhou and Biao Yang and Yuhui Zhang and Huishuai Bao and Sijia Peng and Chuan Li and Feng Shi},
  title = {{A 2D Semantic-Aware Position Encoding for Vision Transformers}},
  year = {2025},
  eprint = {2505.09466},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2505.09466},
}
Figure