mlsanityData to 14 Oct 2026

How do Large Language Models Learn In-Context? Query and Key Matrices of In-Context Heads are Two Towers for Metric Learning

arXiv 2402.02872 ↗First posted 5 Feb 2024cs.CL cs.LGcited by 18

Not on the map. Why?

Abstract (arXiv)

We investigate the mechanism of in-context learning (ICL) on sentence classification tasks with semantically-unrelated labels ("foo"/"bar"). We find intervening in only 1% heads (named "in-context heads") significantly affects ICL accuracy from 87.6% to 24.4%. To understand this phenomenon, we analyze the value-output vectors in these heads and discover that the vectors at each label position contain substantial information about the corresponding labels. Furthermore, we observe that the prediction shift from "foo" to "bar" is due to the respective reduction and increase in these heads' attention scores at "foo" and "bar" positions. Therefore, we propose a hypothesis for ICL: in in-context heads, the value-output matrices extract label features, while the query-key matrices compute the similarity between the features at the last position and those at each label position. The query and key matrices can be considered as two towers that learn the similarity metric between the last position's features and each demonstration at label positions. Using this hypothesis, we explain the majority label bias and recency bias in ICL and propose two methods to reduce these biases by 22% and 17%, respectively.

Key facts. Cited by 18 papers in the map (data through 14 Oct 2026). Builds on 28 of its 50 references that are in the map. First posted on arXiv 5 Feb 2024.

Builds on

28 of 50 references.

References · 28

  1. 2020Language Models are Few-Shot Learnerscited by 32,419
  2. 2022Training language models to follow instructions with human feedbackcited by 15,971
  3. 2023LLaMA: Open and Efficient Foundation Language Modelscited by 14,132
  4. 2017Axiomatic Attribution for Deep Networkscited by 3,384
  5. 2022Locating and Editing Factual Associations in GPTcited by 2,691
  6. 2015Character-level Convolutional Networks for Text Classificationcited by 2,330
  7. 2022Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?cited by 1,434
  8. 2022Least-to-Most Prompting Enables Complex Reasoning in Large Language Modelscited by 1,407
  9. 2021Calibrate Before Use: Improving Few-Shot Performance of Language Modelscited by 1,392
  10. 2020Transformer Feed-Forward Layers Are Key-Value Memoriescited by 1,336
  11. 2021What Makes Good In-Context Examples for GPT-3?cited by 1,195
  12. 2022Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 smallcited by 1,165
16 more
  1. 2021Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order Sensitivitycited by 1,135
  2. 2021An Explanation of In-context Learning as Implicit Bayesian Inferencecited by 938
  3. 2023Progress measures for grokking via mechanistic interpretabilitycited by 839
  4. 2022What Can Transformers Learn In-Context? A Case Study of Simple Function Classescited by 715
  5. 2022What learning algorithm is in-context learning? Investigations with linear modelscited by 684
  6. 2022Transformer Feed-Forward Layers Build Predictions by Promoting Concepts in the Vocabulary Spacecited by 532
  7. 2023Dissecting Recall of Factual Associations in Auto-Regressive Language Modelscited by 528
  8. 2023Larger language models do in-context learning differentlycited by 394
  9. 2017The (Un)reliability of saliency methodscited by 333
  10. 2013Good Debt or Bad Debt: Detecting Semantic Orientations in Economic Textscited by 310
  11. 2013Direct and Indirect Effectscited by 280
  12. 2023Label Words are Anchors: An Information Flow Perspective for Understanding In-Context Learningcited by 236
  13. 2022Demystifying Prompts in Language Models via Perplexity Estimationcited by 216
  14. 2023What In-Context Learning "Learns" In-Context: Disentangling Task Recognition and Task Learningcited by 152
  15. 2023The Closeness of In-Context Learning and Weight Shifting for Softmax Regressioncited by 41
  16. 2023ICL-D3IE: In-Context Learning with Diverse Demonstrations Updating for Document Information Extractioncited by 38

Built on by

Cited by year (2026 to 14 Oct)

Citing papers per year: 2024: 7; 2025: 7; 2026: 4.

18 papers.

Sort:

By area:

  1. 2024Attention Heads of Large Language Models: A Surveycited by 67
  2. 2024Interpreting Arithmetic Mechanism in Large Language Models through Comparative Neuron Analysiscited by 31
  3. 2026Locate, Steer, and Improve: A Practical Survey of Actionable Mechanistic Interpretability in Large Language Modelscited by 27
  4. 2024Can In-context Learning Really Generalize to Out-of-distribution Tasks?cited by 23
  5. 2024Understanding Multimodal LLMs: the Mechanistic Interpretability of Llava in Visual Question Answeringcited by 11
  6. 2025How Do Large Vision-Language Models See Text in Image? Unveiling the Distinctive Role of OCR Headscited by 9
  7. 2024Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanismcited by 6
  8. 2025From Compression to Expression: A Layerwise Analysis of In-Context Learningcited by 6
  9. 2025CAFE: Retrieval Head-based Coarse-to-Fine Information Seeking to Enhance Multi-Document QA Capabilitycited by 4
  10. 2024From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and Whencited by 3
  11. 2025ConText: Driving In-context Learning for Text Removal and Segmentationcited by 3
  12. 2025BASIC: Boosting Visual Alignment with Intrinsic Refined Embeddings in Multimodal Large Language Modelscited by 2
6 more
  1. 2025Localizing Task Recognition and Task Learning in In-Context Learning via Attention Head Analysiscited by 2
  2. 2026Understanding the Dynamics of Demonstration Conflict in In-Context Learningcited by 2
  3. 2025Quantifying LLM Biases Across Instruction Boundary in Mixed Question Formscited by 1
  4. 2024Logit Separability-Driven Samples and Multiple Class-Related Words Selection for Advancing In-Context Learningcited by 0
  5. 2026In-Context Fixation: When Demonstrated Labels Override Semantics in Few-Shot Classificationcited by 0
  6. 2026AttnLink: Turning Attention into Schema Links for Text-to-SQLcited by 0