Towards Understanding How Transformers Learn In-context Through a Representation Learning Lens

Ruifeng Ren, Yong Liu · DOI

Abstract

Pre-trained large language models based on Transformers have demonstrated remarkable in-context learning (ICL) abilities. With just a few demonstration examples, the models can implement new tasks without any parameter updates. However, it is still an open question to understand the mechanism of ICL. In this paper, we attempt to explore the ICL process in Transformers through a lens of representation learning. Initially, leveraging kernel methods, we figure out a dual model for one softmax attention layer. The ICL inference process of the attention layer aligns with the training procedure of its dual model, generating token representation predictions that are equivalent to the dual model's test outputs. We delve into the training process of this dual model from a representation learning standpoint and further derive a generalization error bound related to the quantity of demonstration tokens. Subsequently, we extend our theoretical conclusions to more complicated scenarios, including one Transformer layer and multiple attention layers. Furthermore, drawing inspiration from existing representation learning methods especially contrastive learning, we propose potential modifications for the attention layer. Finally, experiments are designed to support our findings.

Introduction

In-Context Learning (ICL) is a cornerstone of the success of Large Language Models (LLMs), allowing them to adapt to new tasks via few-shot prompting without explicit parameter updates. Despite its empirical success, the underlying mechanism remains elusive. While previous theories have suggested that ICL might be an implicit form of gradient descent, these explanations often rely on simplified "linear attention" mechanisms or specific weight initializations. This work aims to provide a more general and realistic explanation for standard softmax attention, framing the ICL process as a form of representation learning.

The Dual Model of Softmax Attention

The core contribution of this work is the derivation of a "dual model" for the softmax attention layer using kernel methods. By applying Mercer’s Theorem, the authors demonstrate that the standard attention mechanism can be interpreted as an operation within a high-dimensional feature space.

Equivalence between the softmax attention layer and its trained dual model.

As illustrated in Figure 1, the forward pass (inference) of a Transformer layer on a sequence of demonstration examples is mathematically equivalent to performing a single step of Stochastic Gradient Descent (SGD) on this dual model. In this framework, the Transformer doesn't just compute an output; it effectively "trains" a mini-model on the fly using the context provided in the prompt.

A Representation Learning Perspective

Viewing ICL through the lens of representation learning allows for a deeper understanding of how Transformers process information. The authors propose that the "Key" ($W_K$) and "Value" ($W_V$) projections of a token can be viewed as two different "augmentations" of the same underlying data point.

The dual model viewed as a representation learning framework where $W_K$ and $W_V$ serve as projections.

In this view, the Transformer’s objective during ICL is to learn a mapping that aligns these augmentations. This perspective reveals that standard ICL is essentially a form of contrastive learning that lacks explicit negative samples—it focuses on pulling similar representations together without necessarily pushing dissimilar ones apart. Based on this insight, the authors derived a generalization error bound, proving that the ICL error decreases at a rate of $1/\sqrt{N}$ as the number of demonstration tokens $N$ increases.

Proposed Modifications and Results

Inspired by the representation learning framework, the authors proposed several structural modifications to the attention mechanism:

  1. Regularization: Introducing a penalty term to the dual model's weights.
  2. Non-linear Augmentations: Replacing simple linear projections for Keys and Values with non-linear Multi-Layer Perceptrons (MLPs).
  3. Negative Sampling: Explicitly identifying and "pushing away" irrelevant tokens.

These modifications were tested on synthetic tasks and real-world NLP benchmarks from GLUE (CoLA, MRPC, STS-B, and RTE). On the RTE task, the "Global Best" model (combining augmentations and negative samples) improved accuracy to 71.12 from a baseline of 68.23. On CoLA, regularization improved scores from 56.82 to 61.42. These results suggest that the standard linear projections in Transformers may be a bottleneck for complex representation learning.

Conclusion

This research provides a novel theoretical bridge between Transformer inference and representation learning. By showing that ICL is equivalent to training a dual model, the authors offer a path toward more interpretable and efficient Transformer architectures. Future work will focus on refining negative sampling strategies and scaling these insights to larger decoder-only models.

Summary updated

BibTeX
@inproceedings{ren2024towards,
  author = {Ruifeng Ren and Yong Liu},
  title = {{Towards Understanding How Transformers Learn In-context Through a Representation Learning Lens}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=dB6gwSDXKL},
}
Figure