On Understanding Attention-Based In-Context Learning for Categorical Data

Aaron T Wang, William Convertino, Xiang Cheng, Ricardo Henao, Lawrence Carin

Introduction

Researchers have long been fascinated by a Transformer's ability to "learn" from a few examples in its prompt without any weight updates—a phenomenon known as In-Context Learning (ICL). Recent theoretical work suggests that Transformers act like an optimization algorithm, specifically performing functional Gradient Descent (GD) on a hidden latent function.

However, most of this theory has been limited to regression tasks (predicting real numbers). Real-world applications like language modeling and image classification deal with categorical data (discrete tokens or classes). Categorical data introduces a non-linear "softmax" expectation into the GD update rule, which standard self-attention mechanisms struggle to represent across multiple steps. This paper bridges a major gap in Transformer theory by showing how ICL for categorical data can be mathematically viewed as performing functional GD, introducing a novel architecture that exactly implements multi-step GD for classification.

A GD-Based Attention Architecture

To tackle the discrete world of categories and tokens, the authors propose a specific architecture designed to implement functional GD for categorical outcomes exactly.

Proposed GD-based attention block featuring self-attention and cross-attention layers.

Their "GD-based" attention block consists of three main components:

  1. Dual-Head Self-Attention: One head calculates the function update (the gradient), while a second head "erases" the old expectation from the previous step to make room for an update.
  2. Cross-Attention Layer: This is a key innovation. It uses learned category embedding vectors as "keys" and "values" to compute the non-linear softmax expectation required for categorical GD.
  3. Scratch Space: The model uses specific dimensions in its hidden vector as a "workspace" to store and iteratively update the inferred latent function.

Beyond the architecture itself, the authors provide a theoretical proof showing that the parameters required to implement this GD procedure represent a stationary point in the loss landscape of a standard Transformer. In other words, when a Transformer is trained on categorical data, it is mathematically incentivized to "discover" this exact GD algorithm.

Experimental Validation

The framework was validated across three distinct settings to prove its versatility and theoretical soundness:

  • Synthetic Data: The authors used highly non-linear functions with 25 categories to compare "Trained Transformers" (learned from scratch) against "GD Transformers" (parameters hard-coded to perform GD). As training data increased, the performance and internal parameters of the standard Trained Transformer converged almost perfectly to the GD Transformer.
  • Image Classification: The model was tested on in-context image classification using ImageNet. Given "few-shot" examples of new image classes, it was tasked with classifying a query image using VGG-network features.
In-context image classification setup using functional GD on a Transformer forward pass.
  • Language Modeling: The framework was also applied to language generation, trained on the Tiny Stories and Children Stories datasets, which are designed to illuminate Transformer mechanisms.

Key Insights and Results

The functional GD perspective yields several profound insights into why Transformers are built the way they are:

  • Embeddings are Natural: The common practice of encoding tokens as learned embedding vectors isn't just a heuristic; it arises naturally from the GD perspective as the mathematically optimal way to represent categorical observations.
  • The Role of Cross-Attention: While modern "decoder-only" models (like GPT) often omit cross-attention, this paper suggests that cross-attention is the mathematically "pure" way to handle the non-linearities of categorical data in a GD framework.
  • Zero-Shot Competitiveness: On ImageNet, the GD-based model achieved Top-1 accuracy comparable to "linear probing" (a common benchmark), but without requiring any per-task fine-tuning.
  • Massive Efficiency Gains: The GD-based perspective allows for models that perform similarly to standard Transformers but with orders of magnitude fewer parameters. For instance, by adding a standard Feed-Forward (FF) layer to their GD model, the authors matched the performance of a standard Transformer on story generation while using only 8K parameters compared to the standard Transformer's 6M parameters.

Limitations and Future Work

The primary limitation acknowledged by the authors is that while the GD framework beautifully explains the attention mechanism, it does not fully account for the role of the Feed-Forward (FF) blocks found in standard Transformers. While empirical results showed that adding an FF layer significantly boosts performance, the GD-based mathematical justification for why the FF layer helps remains an open question.

Future work aims to integrate the FF layer more deeply into the functional data analysis perspective, potentially revealing even more efficient ways to design and interpret large language models.

Summary updated

BibTeX
@inproceedings{wang2025understanding,
  author = {Aaron T Wang and William Convertino and Xiang Cheng and Ricardo Henao and Lawrence Carin},
  title = {{On Understanding Attention-Based In-Context Learning for Categorical Data}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=7Daf4TMtX9},
}
Figure