Learning interpretable positional encodings in transformers depends on initialization

Takuya Ito, Luca Cocchi, Tim Klinger, Parikshit Ram, Murray Campbell, Luke J. Hearne · DOI

Abstract

In transformers, the positional encoding (PE) provides essential information that distinguishes the position and order amongst tokens in a sequence. Most prior investigations of PE effects on generalization were tailored to 1D input sequences, such as those presented in natural language, where adjacent tokens (e.g., words) are highly related. In contrast, many real world tasks involve datasets with highly non-trivial positional arrangements, such as datasets organized in multiple spatial dimensions, or datasets for which ground truth positions are not known. Here we find that the choice of initialization of a learnable PE greatly influences its ability to learn interpretable PEs that lead to enhanced generalization. We empirically demonstrate our findings in three experiments: 1) A 2D relational reasoning task; 2) A nonlinear stochastic network simulation; 3) A real world 3D neuroscience dataset, applying interpretability analyses to verify the learning of accurate PEs. Overall, we find that a learned PE initialized from a small-norm distribution can 1) uncover interpretable PEs that mirror ground truth positions in multiple dimensions, and 2) lead to improved generalization. These results illustrate the feasibility of learning identifiable and interpretable PEs for enhanced generalization.

Introduction

Transformers are fundamentally "permutation-invariant," meaning they treat a sequence of tokens as an unordered set unless explicitly provided with positional information. Standard solutions, such as the fixed sinusoidal encodings introduced in the original Transformer architecture, assume data follows a simple 1D linear progression. However, many real-world datasets—ranging from 2D image grids to 3D biological structures—possess complex geometries that are lost when data is flattened into a 1D vector for processing.

While learnable positional encodings (PEs) offer a potential solution by allowing models to discover these relationships, they often fail to generalize to out-of-distribution data or result in opaque, "black box" internal representations. This paper investigates how the initialization of learnable PEs determines whether a model merely memorizes training data or successfully recovers the underlying spatial structure of the input.

The Latin Square Task (LST) and the challenge of tokenizing 2D grid data into 1D sequences. Heatmaps (F-H) compare pairwise distances in 2D ground truth vs. 1D and 2D positional encodings.

Methods: The Role of Initialization

The researchers focused on the standard deviation ($\sigma$) of the initial PE weights, comparing two regimes from deep learning theory:

  1. The "Lazy" (NTK) Regime: Initializing with a large-norm distribution (large $\sigma$). In this state, weights change minimally during training, and the model tends to find solutions that lack structural depth, often failing to generalize.
  2. The "Rich" (Feature Learning) Regime: Initializing with a small-norm distribution (small $\sigma$). This encourages the model to learn structured, low-dimensional features that reflect the actual organization of the input.

To evaluate the interpretability of learned PEs, the authors used the Orthogonal Procrustes Transformation to align high-dimensional learned embeddings with known ground-truth coordinates, measuring how accurately the model "reconstructed" the data's original geometry.

Experimental Results

Relational Reasoning and Geometry Emergence

In the Latin Square Task (LST)—a 2D reasoning puzzle similar to Sudoku—the model was required to predict missing symbols in a 4x4 grid. Despite being fed flattened 1D sequences, models initialized in the "rich" regime ($\sigma=0.2$) spontaneously reconstructed the 2D grid geometry. These models achieved 95.6% accuracy, significantly outperforming standard 1D fixed encodings (78.1%) and nearly matching the performance of models provided with ground-truth 2D encodings (97.7%).

Transformer architecture with learnable PE (A) and the impact of initialization standard deviation ($\sigma$) on training/validation accuracy (B) and generalization performance (C).

Initialization as a Performance Driver

The study found a sharp phase transition in performance based on initialization. As shown in Figure 2, models initialized with $\sigma \leq 1.0$ maintained high generalization accuracy. However, as $\sigma$ increased beyond this threshold, validation performance plummeted, even though training accuracy remained high. This suggests that large-norm initializations trap the model in a "lazy" regime where it fails to learn the relational structure necessary for generalization.

Real-World Application: 3D Brain Imaging

The approach was further validated using Human Connectome Project (HCP) fMRI data. In this 3D neuroscience task, small-norm PEs did more than just learn physical coordinates; they successfully identified "functional" networks. The model grouped brain regions that work together (e.g., the visual system) even when those regions were physically distant, demonstrating that learnable PEs can recover complex, non-obvious organizational principles.

Conclusion

This research demonstrates that the ability of Transformers to learn interpretable and effective positional representations is highly sensitive to initialization. By using small-norm distributions, models can autonomously discover the underlying 2D or 3D geometry of their inputs, leading to superior generalization. These findings suggest that for many scientific and structural datasets, learnable PEs—if initialized correctly—are a powerful alternative to hand-coded 1D encodings.

Summary updated

BibTeX
@misc{ito2024learning,
  author = {Takuya Ito and Luca Cocchi and Tim Klinger and Parikshit Ram and Murray Campbell and Luke J. Hearne},
  title = {{Learning interpretable positional encodings in transformers depends on initialization}},
  year = {2024},
  eprint = {2406.08272},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.08272},
}
Figure