Benefits of Transformer: In-Context Learning in Linear Regression Tasks with Unstructured Data

Yue Xing, Xiaofeng Lin, Namjoon Suh, Qifan Song, Guang Cheng · DOI

Abstract

In-Context Learning (ICL) allows Transformers to solve new tasks via prompts without weight updates. While previous theoretical work often assumes "structured" data where inputs and labels share tokens, real-world data is typically "unstructured." This study explores the architectural requirements for ICL in an unstructured linear regression setting. We demonstrate that a single-layer Transformer fails this task, whereas a two-layer architecture—leveraging causal masks and positional encodings—successfully matches inputs to labels. Our findings reveal that the first layer serves as a critical pre-processor, restructuring data for the subsequent layer's predictive mechanism.

Introduction

Large Language Models (LLMs) exhibit the remarkable ability of In-Context Learning (ICL), performing tasks based on a few examples provided in the prompt. Most theoretical frameworks simplify this by assuming "structured data," where each token contains both an input ($x_i$) and its corresponding label ($y_i$). However, natural language is inherently unstructured; for instance, in the sequence "Apple: Red, Banana: Yellow," the object and its attribute are distinct tokens.

This paper addresses how the Transformer architecture bridges the gap between these separate tokens. Specifically, it investigates which components—layers, attention masks, or positional encodings—are essential for ICL to function in a realistic, unstructured environment.

Methods and Datasets

The researchers focus on a fundamental ICL task: Linear Regression. They analyze shallow Transformers (1 or 2 layers) using an "unstructured" prompt format ($E_2$) where inputs ($x_i$) and labels ($y_i$) are separated into adjacent tokens: $(x_1, 0), (0, y_1), (x_2, 0), (0, y_2) \dots (x_q, 0)$.

The study employs synthetic datasets where inputs $x$ are drawn from a Gaussian distribution $N(0, I_d)$, and labels $y$ are generated via a noiseless linear model $y = x^\top\theta$. The methodology combines mathematical proofs of architectural limitations with controlled simulations ($d=5$ dimensions, $D=30$ examples) to test the impact of layers, masks, and Positional Encoding (PE).

Key Insights: The Role of Architecture

A central finding is the failure of the single-layer Transformer. Mathematically, a one-layer model cannot effectively "grab" a label $y_i$ from a separate token and associate it with a query $x_q$, even if it recognizes the similarity between $x_q$ and $x_i$.

In contrast, a two-layer model succeeds by utilizing the first layer as a pre-processor. Layer 1 uses attention to "mix" information between the $x_i$ and $y_i$ tokens, effectively re-structuring the data. This allows Layer 2 to perform regression as if the data were originally structured.

Attention heatmaps for Layer 1 and Layer 2, illustrating the pre-processing role of the first layer.

Furthermore, a look-ahead (causal) attention mask is vital to prevent the model from "cheating" by looking at future labels during training, which would otherwise lead to poor generalization. Positional Encoding (PE) also plays a crucial role by providing unique signatures for example pairs, helping the attention mechanism precisely match $x_i$ with its corresponding $y_i$.

Key Results

Simulations confirm the theoretical predictions. Models with only one layer maintain a high, flat error rate (MSE $\approx 1.0$), failing to learn from context. Two-layer models, however, show a significant decrease in error as the number of in-context examples ($D$) increases.

Squared error vs. in-context examples for various Transformer configurations.

The addition of Positional Encoding significantly enhances performance, reducing the Mean Squared Error (MSE) from approximately 0.48 to 0.19 in the two-layer setting. The authors prove that for a two-layer Transformer, the prediction error converges at a rate of $O(d/D)$, matching the efficiency of models trained on structured data.

Limitations and Future Work

While providing deep insights into unstructured ICL, this study is limited to linear regression and shallow architectures. Real-world LLMs handle non-linear tasks with much deeper networks. Additionally, the theoretical proofs assume infinite training data, overlooking the generalization gap inherent in finite datasets. Future research could explore how multiple attention heads and deeper hierarchies facilitate more complex data matching or apply these findings to "Chain-of-Thought" reasoning.

Summary updated

BibTeX
@misc{xing2024benefits,
  author = {Yue Xing and Xiaofeng Lin and Namjoon Suh and Qifan Song and Guang Cheng},
  title = {{Benefits of Transformer: In-Context Learning in Linear Regression Tasks with Unstructured Data}},
  year = {2024},
  eprint = {2402.00743},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.00743},
}
Figure