Selecting Demonstrations for Many-Shot In-Context Learning via Gradient Matching

Jianfei Zhang, Bei Li, Jun Bai, Rumei Li, Yanmeng Wang +2 more
6/5/2025
cs.CL

No confident code implementation found for this paper.

Abstract

In-Context Learning (ICL) empowers Large Language Models (LLMs) for rapid task adaptation without Fine-Tuning (FT), but its reliance on demonstration selection remains a critical challenge. While many-shot ICL shows promising performance through scaled demonstrations, the selection method for many-shot demonstrations remains limited to random selection in existing work.

Show full abstract

Since the conventional instance-level retrieval is not suitable for many-shot scenarios, we hypothesize that the data requirements for in-context learning and fine-tuning are analogous. To this end, we introduce a novel gradient matching approach that selects demonstrations by aligning fine-tuning gradients between the entire training set of the target task and the selected examples, so as to approach the learning effect on the entire training set within the selected examples. Through gradient matching on relatively small models, e.g., Qwen2.5-3B or Llama3-8B, our method consistently outperforms random selection on larger LLMs from 4-shot to 128-shot scenarios across 9 diverse datasets. For instance, it surpasses random selection by 4% on Qwen2.5-72B and Llama3-70B, and by around 2% on 5 closed-source LLMs. This work unlocks more reliable and effective many-shot ICL, paving the way for its broader application.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{zhang2025selecting,
  title  = {Selecting Demonstrations for Many-Shot In-Context Learning via Gradient Matching},
  author = {Jianfei Zhang and Bei Li and Jun Bai and Rumei Li and Yanmeng Wang and Chenghua Lin and Wenge Rong},
  year   = {2025},
  eprint = {2506.04579},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2506.04579v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2506.04579.svg)](https://codeofpaper.com/paper/2506.04579)

Discussion