NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Learning Task Representations from In-Context Learning

Baturay Saglam, Xinyang Hu, Zhuoran Yang, Dionysis Kalogerias, Amin Karbasi

Introduction

Large Language Models (LLMs) have demonstrated a remarkable capacity for In-Context Learning (ICL), where they adapt to new tasks—ranging from translation to complex mathematical reasoning—simply by processing a few examples within a prompt. Despite its success, the internal mechanism by which a model "understands" and encodes these tasks remains largely opaque. While previous research proposed "task vectors" as numerical representations of these internal states, these formulations often prove brittle, failing to generalize across different data modalities or requiring manual tuning of model layers.

Structure of an In-Context Learning (ICL) prompt, illustrating the relationship between examples, queries, and completions.

Researchers at Yale have addressed this gap by introducing the Learnable Task Vector (LTV). This framework provides a principled, automated, and modality-agnostic method for extracting and utilizing internal task encodings, significantly outperforming existing baselines.

The Learnable Task Vector (LTV) Framework

The LTV approach is grounded in two primary hypotheses: first, that task-specific information is predominantly carried by attention heads rather than feed-forward networks; and second, that different attention heads contribute variably as "specialists" for specific tasks.

Unlike standard methods that rely on simple activation averaging, LTV represents a task as a weighted sum of attention head activations across the model's layers. These weights are not predefined but are learned causally through gradient descent. By freezing the LLM's parameters and optimizing only a small set of weights (one per attention head), the LTV can effectively "steer" the model's behavior. To ensure the LTV captures the task essence rather than just the prompt text, the researchers trained it on "shuffled" prompts with randomized labels, forcing the vector itself to provide the necessary task guidance.

Experimental Evaluation

The study rigorously tested LTV across two distinct domains to prove its versatility:

  1. Natural Language Processing (NLP): The authors evaluated LTV on 15 benchmarks, including sentiment analysis, named entity recognition (NER), and translation.
  2. Functional Regression: Using a benchmark for learning functions like linear equations, sparse linear functions, and 2-layer ReLU neural networks, the team tested the model's ability to handle numerical patterns.

A critical component of the regression testing was the Out-of-Distribution (OOD) length benchmark. This tested whether the task vector could maintain performance on sequences significantly longer than those encountered during the model's pre-training—a scenario where standard ICL typically fails.

Key Results and Insights

The LTV framework demonstrated superior performance and robustness across nearly all tested scenarios.

Dominance in Language Tasks

In a comprehensive evaluation of 15 language tasks across both zero-shot and few-shot settings (30 total evaluations), LTV achieved the highest accuracy in 29 out of 30 cases. Notably, in zero-shot settings where the model receives no examples, LTV provided a massive performance boost. For instance, on the AG News classification dataset, a vanilla transformer scored 0% accuracy, while the LTV-integrated model reached 46.9%.

Rescuing Regression Performance

In the OOD length benchmarks, LTV proved its resilience. While standard LLMs and competing methods like Function Vectors (FV) or In-Context Vectors (ICV) saw performance degrade as sequence lengths increased, LTV restored performance to near-optimal levels.

Mean squared error across linear, sparse linear, and 2-layer NN regression tasks, showing LTV's ability to maintain low error rates even as sequence length increases.

Modality Agnosticism and Efficiency

The most significant finding is that the same mathematical framework—weighted attention heads—successfully encodes both linguistic tasks and non-linear regressions. This suggests that task encoding is a fundamental, unified property of the transformer architecture. Furthermore, LTV proved more efficient than traditional fine-tuning methods like LoRA. By optimizing only 96 weights (in a 12-layer model) rather than thousands of parameters, LTV achieved better results in OOD scenarios, suggesting that steering existing internal representations is often more effective than retraining them.

Conclusion and Future Directions

The Learnable Task Vector offers a powerful "white-box" lens into the internal workings of LLMs. By identifying and weighting the specific attention heads responsible for task execution, LTV provides a robust, modality-independent method for task representation. This research marks a significant step toward making the "black box" of in-context learning more transparent and controllable.

Summary updated

BibTeX
@inproceedings{saglam2025learning,
  author = {Baturay Saglam and Xinyang Hu and Zhuoran Yang and Dionysis Kalogerias and Amin Karbasi},
  title = {{Learning Task Representations from In-Context Learning}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure