Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanism

Anhao Zhao, Fanghua Ye, Jinlan Fu, Xiaoyu Shen

Abstract

In-Context Learning (ICL) allows Large Language Models (LLMs) to perform tasks using a few demonstrations without parameter updates. However, the underlying mechanism remains debated, with theories split between "task recognition" and "learning from examples." This paper, presented at EMNLP 2024, introduces a unified 2D coordinate system based on Cognition (task identification) and Perception (example similarity). Using a novel metric, Peak Inverse Rank (PIR), the researchers demonstrate how model behavior shifts across four distinct quadrants, reconciling existing theories and explaining the varying importance of label correctness and shot count.

Introduction

Since the emergence of GPT-3, ICL has been fundamental to LLM utility. Research has largely diverged into two camps: one arguing that demonstrations merely cue latent pre-trained knowledge ("Task Recognition"), and another suggesting models explicitly learn mappings from provided examples ("Learning from Examples"). This study proposes that these views are not contradictory but rather represent different dimensions of a single operational space.

The 2D Coordinate System

The researchers propose a framework defined by two orthogonal axes:

  • Y-axis (Cognition): Represents the model's ability to recognize the underlying task (e.g., identifying a sequence as sentiment analysis).
  • X-axis (Perception): Represents the presence of demonstrations highly similar to the target query.
Figure 1: The 2D coordinate system mapping Cognition vs. Perception across four quadrants of model behavior.

These axes define four quadrants:

  1. Q1 (High Cognition, High Perception): The model leverages both pre-trained knowledge and similar examples. It may enter a state of "confusion" if labels are incorrect.
  2. Q2 (High Cognition, Low Perception): The model relies primarily on pre-trained knowledge; label correctness matters less here.
  3. Q3 (Low Cognition, Low Perception): ICL typically fails, often resulting in positional bias or "attention sink" behavior.
  4. Q4 (Low Cognition, High Perception): The model acts as a "copy machine," relying heavily on the labels of similar examples.

Methodology: Quantifying Cognition with PIR

To measure "Cognition," the authors developed Peak Inverse Rank (PIR). Utilizing the Logit Lens technique, they project internal hidden states into the vocabulary space. If a model recognizes a task (e.g., country-to-capital), the task-representative token (e.g., "capital") should appear with high rank in the internal distribution. PIR tracks the maximum rank of this token across all layers. PIR values near 1.0 indicate strong task recognition, while values near 0 suggest the model is performing the task without "understanding" it.

Key Findings and Results

The study evaluated eight models, including GPT-2, Llama-2, and Falcon-40B, across various classification and knowledge-based datasets.

The Impact of Label Correctness

A critical finding is that the importance of label correctness depends on the quadrant. In Q2 (High Cognition), models are robust to incorrect labels because they rely on internal knowledge. However, in Q4 (Low Cognition), models default to copying labels from similar samples, making correctness vital. As shown in the experimental results, providing similar examples with incorrect labels can significantly degrade performance compared to scenarios with no similar examples at all.

Figure 4: Accuracy comparison showing how similar examples with incorrect labels significantly degrade performance across various models.

Scaling and Attention

Larger models demonstrate superior "Perception," becoming more adept at identifying and copying labels from similar examples even when the underlying task is not fully understood. Conversely, in Q3 (Low Cognition/Perception), models exhibit a strong positional bias, often blindly predicting the label of the very first example provided.

Conclusion

By mapping ICL behavior onto a 2D coordinate system of Cognition and Perception, this research provides a unified framework for understanding LLM performance. It clarifies why certain prompting strategies work in some contexts but fail in others, offering a roadmap for more effective prompt engineering and a deeper understanding of the internal mechanics of large language models.

Summary updated

BibTeX
@inproceedings{zhao2024unveiling,
  author = {Anhao Zhao and Fanghua Ye and Jinlan Fu and Xiaoyu Shen},
  title = {{Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanism}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.689},
}
Figure