Task Vectors in In-Context Learning: Emergence, Formation, and Benefits

Liu Yang, Ziqian Lin, Kangwook Lee, Dimitris Papailiopoulos, Robert D Nowak

TL;DR

This paper investigates how transformers learn to perform new tasks with just a few examples (in-context learning) by focusing on "task vectors" – internal representations that capture task-specific information. They introduce a new training loss (TVP-loss) that explicitly encourages the formation of these task vectors, leading to more robust and generalizable models, especially for out-of-distribution tasks.

What Problem Does This Paper Address?

Transformers have shown an impressive ability to perform new tasks with only a few examples provided in their input context (in-context learning or ICL). While research suggests that this ability relies on internal "task vectors" that encode task-specific knowledge, how these vectors emerge and function within the model remains unclear, especially due to the complexity of large pre-trained models. This paper aims to demystify this by studying task vector formation in a controlled environment: training transformer models from scratch on simpler, synthetic tasks. They want to understand when and how these task vectors form and how to encourage their formation for better performance and robustness.

Methods

The authors investigate task vector emergence by training transformer models from scratch on synthetic datasets (linear regression, sinusoidal regression, discrete token offset prediction). They then propose a novel training mechanism called the Task Vector Prompting Loss (TVP-loss). This loss is added to the standard training objective and explicitly encourages the model to form a strong, localized task vector at a specific layer. This is achieved by penalizing the model if it cannot perform the task using only the extracted task vector (zero-shot) when presented with a query. They also explore various input formats and task vector extraction methods to understand what conditions best facilitate task vector emergence.

Datasets

The paper uses synthetic datasets for training and evaluation:

  • Linear Regression: f(x) = w^T x
  • Sinusoidal Regression: f(x) = sin(0.5 * w^T x + b)
  • Discrete Token Offset Prediction: f(x) = (a * x + b) mod C

They also evaluate their method on:

  • GINC Dataset: A benchmark for in-context learning based on factorial Hidden Markov Chains.
  • RegBench Dataset: A benchmark for in-context learning based on probabilistic Finite Automata.
  • CrossFit Benchmark: Used for evaluating performance on natural language tasks.

Key Insights and Ideas

  • Task vectors naturally emerge: Even in small models trained from scratch on simple tasks, task vectors can emerge, though they might be weak and non-locally encoded.
  • Input format matters: The way input data is presented to the model significantly impacts task vector emergence. The (*xy) format (where input and output alternate) is found to be crucial for reliable task encoding in trained-from-scratch models.
  • TVP-loss is effective: The proposed TVP-loss explicitly guides the model to form strong, localized task vectors, significantly improving task vector prompting performance.
  • Robustness and generalization: Models trained with TVP-loss show improved robustness to out-of-distribution (OOD) prompts and better generalization, especially in compositional tasks.
  • Layer localization: The optimal layer for task vector formation can vary depending on the task and model depth, with shallower models often benefiting from penultimate layers and deeper models showing more distributed encoding.

Key Results

  • Models trained with TVP-loss achieve comparable in-context learning (ICL) performance to vanilla models while significantly improving task vector prompting (TVP) performance, often matching ICL performance.
  • On synthetic tasks, TVP-loss enhances OOD generalization, particularly for complex tasks like sinusoidal regression and discrete token offset.
  • For the RegBench dataset, TVP-loss consistently outperforms vanilla training, showing improved ICL and TVP accuracy, especially under OOD conditions.
  • The paper demonstrates that task vectors can be successfully extracted and used for zero-shot prediction, even when the model is trained from scratch.

Limitations and Future Work

  • Controlled setting: The primary experiments are conducted on synthetic datasets. While this allows for controlled investigation, it's important to see how these findings translate to larger, more complex real-world tasks.
  • Hyperparameter sensitivity: The effectiveness of TVP-loss can depend on hyperparameter choices, such as the weight of the TVP-loss and the range of layers considered for task vector localization.
  • Interpretability of task vectors: While the paper shows TVP-loss improves task vector formation, further work could delve deeper into the semantic meaning and interpretability of these learned task vectors.
  • Computational cost: While TVP-loss improves performance, it adds to the training complexity. Future work could explore more efficient ways to achieve similar benefits.

Summary updated

BibTeX
@inproceedings{yang2025task,
  author = {Liu Yang and Ziqian Lin and Kangwook Lee and Dimitris Papailiopoulos and Robert D Nowak},
  title = {{Task Vectors in In-Context Learning: Emergence, Formation, and Benefits}},
  booktitle = {COLM},
  year = {2025},
  url = {https://openreview.net/forum?id=lODGn1Rp5t},
}
Figure