Our new X account is live! Follow @wizwand_team for updates
WorkDL logo mark

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

About

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. 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.

Jianfei Zhang, Bei Li, Jun Bai, Rumei Li, Yanmeng Wang, Chenghua Lin, Wenge Rong• 2025

Related benchmarks

TaskDatasetResultRank
Commonsense ReasoningHellaSwag
Accuracy87.68
1460
Commonsense Question AnsweringCommonsenseQA
Accuracy87.71
81
Sentiment AnalysisSST-5
Accuracy48.32
47
Natural Language InferenceMNLI
Accuracy77.29
36
Semantic ParsingSMCalFlow
Program Accuracy52.82
22
Semantic ParsingBreak
Accuracy42.21
18
In-Context Learning9-dataset Average (SST-5, MNLI, CMSQA, HellaSwag, GeoQ, NL2Bash, Break, MTOP, SMCalFlow) (test)
Accuracy68.08
15
Semantic ParsingNL2Bash
Accuracy45.19
14
ReasoningGeoquery
Accuracy84.64
14
Semantic ParsingMTOP
Accuracy47.74
14
Showing 10 of 10 rows

Other info

Code

Follow for update