Learning interpretable positional encodings in transformers depends on initialization

Takuya Ito, Luca Cocchi, Tim Klinger, Parikshit Ram, Murray Campbell, Luke Hearne

TL;DR

Transformers struggle with data that has complex spatial or network structures (like 2D grids or brain connectivity) because standard positional encodings (PE) are designed for 1D sequences. This paper demonstrates that by initializing learnable PEs with a small norm, transformers can autonomously discover the underlying geometry of the data, leading to significantly better generalization and highly interpretable representations.

What Problem Does This Paper Address?

Transformers are inherently "permutation invariant"—they treat a sequence of tokens like a bag of words unless we explicitly tell them where each token sits. The standard solution is Positional Encoding (PE), usually a fixed 1D sinusoidal signal.

While 1D PEs work for text, they fail for datasets with non-trivial arrangements, such as 2D images, 3D brain scans, or complex biological networks. In these cases, "closeness" in a 1D flattened sequence doesn't reflect actual physical or functional proximity. While we could learn PEs from scratch, doing so often leads to poor generalization. The authors investigate how the initialization of these learnable PEs determines whether a model simply "memorizes" the training data or actually learns the underlying spatial structure.

Methods

The researchers used a standard encoder-only transformer and replaced the fixed PE with a learnable parameter initialized from a multivariate Normal distribution: $\mathcal{N}(0, \sigma I)$. The key variable was $\sigma$ (the norm of the initialization).

To evaluate if the model actually "learned" the right geometry, they used:

  • Orthogonal Procrustes Transform: A method to rotate and align the learned high-dimensional PE embeddings with known "ground truth" coordinates to see if they match.
  • Attention Map Analysis: Comparing the model's internal attention patterns to those of a model equipped with perfect ground-truth spatial information.
  • Network Science Metrics: Using modularity and clustering statistics to see if learned PEs for brain regions naturally grouped into known functional networks.

Datasets

  1. Latin Square Task (LST): A synthetic 2D relational reasoning task (similar to Sudoku). The model must infer a missing symbol in a 4x4 grid. This tests if the model can recover 2D coordinates from a flattened 1D input.
  2. Nonlinear Stochastic Network (NMAR): A simulation of 15 nodes organized into three distinct clusters.
  3. Human Connectome Project (HCP): Real-world resting-state fMRI data from 100 participants. The task was to predict masked brain activity across 360 cortical regions, which are known to be organized into 12 functional networks.

Key Insights and Ideas

  • The "Rich" vs. "Lazy" Regime: The paper applies deep learning theory to PEs. Large-norm initializations ($\sigma > 1.0$) put the model in a "lazy" regime (Neural Tangent Kernel), where it converges but learns random, high-dimensional representations that fail to generalize. Small-norm initializations ($\sigma \approx 0.1-0.2$) trigger a "rich" feature-learning regime, where the model is forced to find structured, low-dimensional representations—essentially "discovering" the grid or network.
  • Interpretability Equals Generalization: There is a near-perfect correlation between how well the learned PE matches the ground-truth geometry and how well the model performs on unseen data.
  • Geometry from Scratch: Even when the model has zero prior knowledge of 2D or 3D space, a small-norm initialization allows it to reconstruct the spatial relationships of tokens just by observing their interactions.
Figure 1: A-D) The Latin Square Task (LST). The LST involves the presentation of a 2D 4-by-4 grid populated with A) four possible symbols, blank spaces and a single target probe (”?”). The agent’s aim is to solve for the target square with the rule that each shape can only appear once in every row and column. The reasoning complexity required for an LST puzzle can be manipulated by varying the number of distinct vectors that must be integrated to solve the problem. Examples of B) 1-, C) 2-, and D) 3-vector LST puzzles (left) and their solutions (right). E) Performing the LST is intuitive in 2D. However, when formatting the task sequence for neural networks, the input must be flattened into 1D. The LST is significantly more challenging when row and column information is lost. F) The pairwise distances between token positions according to rows and columns provides the “ground truth” of how tokens relate to each other in 2D space. A successful PE would preserve the isometry (i.e., pairwise distance relationships) of the (x,y) grid coordinates. G) Naively using the 1D sinusoidal PE (Vaswani et al., 2017) would provide incorrect token-wise position information, since it only considers the closeness of tokens in 1D. H) In contrast, recomputing absolute positions in 2D (with sines and cosines in the embedding dimensions) would preserve the position information of a 2D grid, even after flattening the sequence into 1D.

Key Results

  • LST Performance: Learnable PEs with $\sigma=0.2$ achieved 95.6% accuracy, nearly matching the "ground truth" 2D fixed PE (97.7%) and vastly outperforming the standard 1D fixed PE (78.1%).
  • Initialization Sensitivity: As shown in Figure 2, generalization performance drops sharply as the initialization norm $\sigma$ increases, even though all models converged during training.
  • Brain Network Recovery: In the fMRI experiment, only small-norm PEs successfully recovered the modular organization of the human brain. The learned embeddings for regions within the same functional network (e.g., the visual system) were automatically placed closer together in the embedding space.
Figure 2: Generalization performance on the LST task. Small $\sigma$ values lead to high accuracy, while large $\sigma$ values (the "lazy" regime) result in poor generalization despite successful training convergence.

Limitations and Future Work

  • Linear Superposition: The current approach adds the PE to the token embedding ($token + PE$). The authors suggest that learning nonlinear PE functions ($PE(token)$) could provide even greater expressive power.
  • Optimizer Interactions: The study found that the Adam optimizer can sometimes interfere with very small initializations ($\sigma < 0.1$) due to its adaptive learning rates, suggesting that optimizer choice is a critical hyperparameter for feature learning.
  • Dynamic Sequences: It remains unclear how this approach scales to tasks where the number of tokens or their relative positions change dynamically (e.g., length generalization).
Figure 5: In brain imaging data, small-norm initialized PEs (C, D) were the only ones to recover the modular functional network structure of the brain from flattened 1D input.

Summary updated

BibTeX
@misc{ito2024learning,
  author = {Takuya Ito and Luca Cocchi and Tim Klinger and Parikshit Ram and Murray Campbell and Luke 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