Paper · 2022-02-25 · cs.CL, cs.AI
Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?
Analysis paper: replacing the labels of in-context demonstrations with uniformly random labels costs only 0-5% absolute on 26 classification and multi-choice datasets across 12 model/inference combinations (774M to 175B, incl. GPT-3). Ablations show the label space, the input text distribution and the input-label pair format drive the gains, not correct pairings.
In-context learningPrompt engineeringFew-shot learningpromptingin-context-learninganalysisevaluationfew-shot
arXivPDFCodeMarkdown11 min read
AI-compiled by claude-opus-5-5 on 2026-10-18, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.
In plain words
Large language models can do a new task when you paste a handful of worked examples into the prompt, for instance a few movie reviews each followed by "positive" or "negative", then a new review with the label left blank. This is called in-context learning. The natural assumption is that the model studies the examples and learns which inputs go with which labels.
This paper tests that assumption by deliberately lying to the model: the labels in the examples are replaced with random ones. Surprisingly, accuracy barely moves, across a dozen models up to GPT-3. So the model is mostly not learning the input-to-label rule from the examples; it already knows it from pretraining.
What the examples do provide, the authors find, is context: what kind of text to expect, which answer words are allowed, and the "input, then answer" pattern to copy. Swap the inputs for unrelated sentences, or the labels for random English words, or drop the pairing pattern, and performance falls much more. A model trained specifically to do in-context learning (MetaICL) relies on these shallow cues even more strongly.
For practitioners the takeaway is twofold. Unlabeled inputs paired with random labels already get close to the usual few-shot result. And in-context learning is unlikely to teach a model a task whose input-label relationship it did not pick up during pretraining.
TL;DR
An empirical study, not a new method. Using k=16 demonstrations, the authors compare no demonstrations, demonstrations with gold labels, and demonstrations whose labels are drawn uniformly at random from the label set, on 26 datasets with 6 decoder-only LMs (GPT-2 Large, MetaICL, GPT-J, fairseq 6.7B/13B, GPT-3), each with direct and channel scoring. Random labels lose only 0-5% absolute. Further variants isolate four aspects of the prompt (input-label mapping, input distribution, label space, pair format) and show the last three matter far more than the first. Relevant if you build few-shot prompts, design ICL evaluations, or need a stronger zero-shot baseline that uses only unlabeled inputs.
Problem
In-context learning (conditioning an LM on k input-label pairs and then a test input) beats zero-shot inference on many tasks, but it was not understood how the model uses the demonstrations or which of their properties produce the gain §1. Prior analyses were theoretical (ICL as Bayesian inference over latent concepts) or correlational (term frequency in pretraining data); the authors describe theirs as the first empirical study of why ICL beats zero-shot §2. Scope: classification and multi-choice tasks with real natural-language inputs only; generation is not studied [Limitation].
Method
All comparisons hold the model, the k training inputs and the prompt template fixed and change only one aspect of the demonstrations. Methods are written for the direct model; the channel model is obtained by swapping x and y §4.1, footnote.
Inference methods §3. Each LM is used with direct scoring, argmax over y in C of P(y | demos, x), and channel scoring, which models the input given the label (following Min et al., arXiv:2108.04106). C is the small set of candidate labels (for multi-choice, the answer options).
Main conditions §4.1:
- No demonstrations: \arg\max_{y \in \mathcal{C}} P(y \mid x).
- Demonstrations with gold labels: \arg\max_{y \in \mathcal{C}} P(y \mid x_1, y_1, \dots, x_k, y_k, x).
- Demonstrations with random labels: each x_i is paired with \tilde{y}_i sampled uniformly from \mathcal{C}; predict \arg\max_{y \in \mathcal{C}} P(y \mid x_1, \tilde{y}_1, \dots, x_k, \tilde{y}_k, x).
Four aspects of a demonstration §5 fig. 7: (1) input-label mapping, whether each x_i has its correct label; (2) distribution of the input text x_1 \dots x_k; (3) label space covered by y_1 \dots y_k; (4) format, i.e. presenting inputs and labels as pairs.
Variants (which aspects each keeps: F format, L label space, I input distribution, M mapping) §5.1–5.3 fig. 8–10 tab. 4:
| Variant | F | L | I | M | Construction | Source |
|---|---|---|---|---|---|---|
| Gold labels | yes | yes | yes | yes | standard ICL | §4.1 |
| Random labels | yes | yes | yes | no | \tilde{y}_i \sim \mathrm{Uniform}(\mathcal{C}) | §4.1 |
| a% correct labels | yes | yes | yes | partial | Algorithm 1 below | §4.2 alg. 1 |
| OOD + random labels | yes | yes | no | no | x_i replaced by k sentences from CC-News, length-matched to the test input | §5.1 app. B |
| Random English words | yes | no | yes | no | sample \mathcal{C}_\mathrm{rand} of English words with \lvert\mathcal{C}_\mathrm{rand}\rvert = \lvert\mathcal{C}\rvert, pair randomly with x_i | §5.2 |
| No labels (inputs only) | no | no | yes | no | condition on x_1 \dots x_k concatenated | §5.3 |
| Labels only | no | yes | no | no | condition on y_1 \dots y_k concatenated | §5.3 |
Constant label answer | (changed) | no | yes | no | every y_i = "answer" | app. C.3 |
| Test input as every x_i | (changed) | yes | yes | no | x_i = x, random \tilde{y}_i | app. C.3 |
Algorithm 1, forming demonstrations with a% correct labels alg. 1 app. B:
# Demonstrations with a% correct labels [alg. 1]
# Input: k gold pairs [(x_1, y_1) ... (x_k, y_k)], label set C, a in [0, 100]
# Output: list of k (input, label) pairs
def form_demonstrations(pairs, label_set, a):
demos = []
# number of pairs that keep their gold label [alg. 1]
num_correct = k * a / 100
# choose which positions stay correct, uniformly without replacement [alg. 1]
correct_positions = sample(range(1, k + 1), num_correct)
for i in range(1, k + 1):
x_i, y_i = pairs[i]
if i in correct_positions:
demos.append((x_i, y_i))
else:
# any label except the gold one [alg. 1]
demos.append((x_i, sample(label_set - {y_i})))
return demosPrompt construction and scoring, as implemented for GPT-3 in the official code gpt3.py (the GPT-2/MetaICL/GPT-J path lives in the MetaICL codebase, not this repo):
# Build per-option prompts and pick the lowest-loss option
# [code: gpt3.py#L23-L52] [code: gpt3.py#L76-L105]
def predict(demos, test_input, options, method):
context = ""
for x_i, y_i in demos:
# direct: input then label; channel: label then input [app. B]
# newline inside a pair, three newlines between pairs [app. B]
if method == "direct":
context += x_i + "\n" + y_i + "\n\n\n"
else:
context += y_i + "\n" + x_i + "\n\n\n"
losses = []
for option in options:
if method == "direct":
prompt = context + test_input + "\n"
target = option
else:
prompt = context + option + "\n"
target = test_input
# drop tokens from the left so prompt + target <= 1024 tokens
# [code: gpt3.py#L54-L61]
prompt = left_truncate(prompt, target, max_tokens=1024)
# mean negative log-prob over the target's tokens
# [code: gpt3.py#L88-L94]
losses.append(mean_token_nll(prompt, target))
return options[argmin(losses)]Random labels in the code: each demonstration's label is replaced by a uniform draw from that example's own option list (the label set for classification, the example's answer choices for multi-choice) create_data.py.
Hyperparameters
| Name | Value | Where | Notes |
|---|---|---|---|
| demonstrations k | 16 | §3 create_data.py | 4, 8, 16, 32 in the k ablation fig. 5 |
| sampling of demos | uniform from training data | §3 | same k-shot splits as MetaICL app. A |
| seeds | 5 (3 for fairseq 13B and GPT-3) | §3 | seeds 100, 13, 21, 42, 87 create_data.py |
| random-label distribution | uniform over C | §4.1 | true training distribution variant in app. C.2 |
| a (% correct) | 0, 25, 50, 75, 100 | fig. 4 create_data.py | |
| OOD corpus | CC-News | app. B | not released with the code (README) |
| OOD length matching | "similar length to the test input" | app. B | code: weight exp(-(len diff in words)^2 / 50) create_data.py |
| English word list | pypi english-words, 61,569 words | app. B | |
| templates | minimal (dataset label words as is) | §3 app. B tab. 3 | manual templates in ablation fig. 6 |
| separators: GPT-2 | space between input and label and between examples | app. B | |
| separators: MetaICL, GPT-J, GPT-3 | \n inside a pair, \n\n\n between pairs | app. B gpt3.py | |
| separators: fairseq | \n inside a pair and between pairs | app. B | |
| max context (GPT-3 code) | min(256 * k, 1024) tokens; 256 per example | test_gpt3.py gpt3.py | not stated in paper |
| GPT-3 engine | Davinci API, base (not instruct) version, assumed 175B | tab. 1 | |
| metric | Macro-F1 (classification), accuracy (multi-choice) | §3 | average over seeds per dataset, then macro-average over datasets |
Models tab. 1:
| Model | # Params | Public weights | Meta-trained |
|---|---|---|---|
| GPT-2 Large | 774M | yes | no |
| MetaICL | 774M | yes | yes |
| GPT-J | 6B | yes | no |
| fairseq 6.7B (dense) | 6.7B | yes | no |
| fairseq 13B (dense) | 13B | yes | no |
| GPT-3 | 175B (assumed) | no | no |
Evaluation data: 16 classification and 10 multi-choice datasets, all with under 10K training examples, evaluated on the HuggingFace development split §3 app. A tab. 2. Ablations use 5 classification (MRPC, RTE, Tweet_eval-hate, SICK, poem-sentiment) and 4 multi-choice (OpenbookQA, CommonsenseQA, COPA, ARC) datasets §4.2. Fairseq 13B and GPT-3 use MRPC, RTE, Tweet_eval-hate, OpenbookQA, CommonsenseQA, COPA §3.
Dataset statistics tab. 2:
| Dataset | Category | # Train | # Eval |
|---|---|---|---|
| financial_phrasebank | Sentiment analysis | 1,811 | 453 |
| poem_sentiment | Sentiment analysis | 892 | 105 |
| medical_questions_pairs | Paraphrase detection | 2,438 | 610 |
| glue-mrpc | Paraphrase detection | 3,668 | 408 |
| glue-wnli | NLI | 635 | 71 |
| climate_fever | NLI | 1,228 | 307 |
| glue-rte | NLI | 2,490 | 277 |
| superglue-cb | NLI | 250 | 56 |
| sick | NLI | 4,439 | 495 |
| hate_speech18 | Hate speech detection | 8,562 | 2,141 |
| ethos-national_origin | Hate speech detection | 346 | 87 |
| ethos-race | Hate speech detection | 346 | 87 |
| ethos-religion | Hate speech detection | 346 | 87 |
| tweet_eval-hate | Hate speech detection | 8,993 | 999 |
| tweet_eval-stance_atheism | Hate speech detection | 461 | 52 |
| tweet_eval-stance_feminist | Hate speech detection | 597 | 67 |
| quarel | Question answering | 1,941 | 278 |
| openbookqa | Question answering | 4,957 | 500 |
| qasc | Question answering | 8,134 | 926 |
| commonsense_qa | Question answering | 9,741 | 1,221 |
| ai2_arc | Question answering | 1,119 | 299 |
| codah | Sentence completion | 1665 | 556 |
| superglue-copa | Sentence completion | 400 | 100 |
| dream | Sentence completion | 6116 | 2040 |
| quartz-with_knowledge | Sentence completion | 2696 | 384 |
| quartz-no_knowledge | Sentence completion | 2696 | 384 |
Results
Author-reported. The per-model numbers appear only as bar charts fig. 3 fig. 11; the paper's text gives the aggregate figures below. Default setup: k=16, minimal templates, 5 seeds §3.
| Comparison | Setting | Finding | Source |
|---|---|---|---|
| Gold vs. random labels | nearly all 12 model/method pairs | drop of 0–5% absolute | §4.1 fig. 3 |
| Gold vs. random labels | average, classification / multi-choice | 2.6% / 1.7% absolute drop | §4.1 |
| Gold vs. random labels | MetaICL | 0.1–0.9% absolute drop | §4.1 |
| 0% correct labels | share of the gold-label gain over no-demos kept: MetaICL cls / MetaICL MC / GPT-J MC | 92% / 100% / 97% | §4.2 fig. 4 |
| 0% correct labels | GPT-J classification | nearly 10% drop vs. gold, still clearly above no-demos | §4.2 |
| Varying k | Channel MetaICL (cls), Direct GPT-J (MC), k = 4…32 | gold-random gap 0.8–1.6% (4.4% for cls at k=4); little gain beyond k=8 | §4.2 fig. 5 |
| Manual templates | same comparison with templates from prior work | same trend; manual templates not always better than minimal | §4.2 fig. 6 |
| OOD inputs + random labels vs. random labels | Channel MetaICL, Direct GPT-J, Channel GPT-J | 3–16% absolute drop; Direct GPT-J MC falls below no-demos; Direct MetaICL barely affected | §5.1 fig. 8 |
| Random English words vs. random labels | direct models | 5–16% absolute drop | §5.2 fig. 9 |
| Random English words vs. random labels | channel models | 0–2% drop, sometimes a gain | §5.2 fig. 9 |
| No labels / labels only | all models | close to or below no-demos | §5.3 fig. 10 |
| OOD + random labels (format kept) | Direct MetaICL, cls / MC | keeps 95% / 82% of the ICL gain | §5.3 |
| Random English words (format kept) | Channel MetaICL cls, Channel GPT-J cls, Channel MetaICL MC, Channel GPT-J MC | keeps 82% / 87% / 86% / 75% of the ICL gain | §5.3 |
| Random labels from true label distribution | Channel MetaICL / Channel GPT-J, classification | gap shrinks 1.9→1.3% / 5.0→3.5% | app. C.2 |
| Per-dataset gap | worst case | nearly 14% absolute on financial_phrasebank with GPT-J; a few hate-speech datasets also large | [Limitation] app. C.2 fig. 12 |
Constant label answer; test input as all x_i | — | both worse than random English words / most demo methods | app. C.3 |
Caveats the authors flag: direct GPT-2, direct GPT-J and direct fairseq 6.7B are not much better than chance on many classification datasets, and channel fairseq 13B does better with no demonstrations than with gold ones; these are discussed less §4.1, footnote.
Paper vs code
- Only data creation and GPT-3 inference are in the repo. Inference for GPT-2, MetaICL and GPT-J
(
test.py), themetaiclpackage and k-shot data building (_build_gym.py) come from merging the MetaICL repository (README). Fairseq models are not supported ("contact authors", README). So the scoring details below are verified only for the GPT-3 path. - Scoring is length-normalized. The paper writes argmax of P(y | …) §4.1; the GPT-3 code picks the option with the lowest mean per-token negative log-likelihood of the target gpt3.py gpt3.py. For channel, the target is the test input, conditioned on the option gpt3.py.
- OOD length matching. Paper: sentences of "similar length to the test input" app. B. Code: for each test example a fresh set of k sentences is drawn without replacement with probability proportional to exp(-(Δ word count)^2 / 50), and they keep the original demonstrations' (random) labels in order create_data.py. So OOD demonstrations differ per test input.
- Random English words. For classification, one bijection gold label → random word is drawn per seed, then demo labels are drawn uniformly among the new words, while the test options stay the original labels create_data.py create_data.py. For multi-choice, each demo gets fresh, distinct random words for its options create_data.py. The paper's single \mathcal{C}_\mathrm{rand} with \lvert\mathcal{C}_\mathrm{rand}\rvert = \lvert\mathcal{C}\rvert §5.2 matches only the classification case.
- a% correct. Code computes the correct count with integer division,
k * a // 100create_data.py; with k=16 all tested values are exact. - No labels sets each demo label to the empty string and labels only sets each input to the empty string create_data.py create_data.py; how the separators then render is decided by the MetaICL formatting code, which is not in this repo.
- Extra variants in the code not reported in the paper:
permutated_labels,random_english_words_gold_labelscreate_data.py create_data.py.
Pitfalls
- This is not a recipe for dropping labels in general. The gap is measured on classification and multi-choice with label words the LM already understands; the authors note synthetic tasks may rely on correct labels more, per-dataset gaps reach nearly 14% (financial_phrasebank, GPT-J), and later work cited in the paper found negated labels hurt substantially [Limitation] app. C.2.
- "Random" means uniform over C, not a shuffle. Uniform sampling can unbalance labels relative to the training distribution; sampling from the true label distribution narrows the gap further app. C.2. Implement whichever matches the experiment you are reproducing.
- Incorrect-label ablation samples from C minus the gold label, so at a=0 every label is wrong, which differs from random labels (where some are correct by chance) alg. 1.
- Direct and channel behave differently. Channel models are nearly insensitive to the label space (English-word labels cost 0–2%), direct models lose 5–16% §5.2; MetaICL direct ignores the input distribution §5.1. Do not generalize an ablation result from one scoring method to the other.
- Format changes hide inside "content" ablations. A constant label or a repeated input acts like a separator and changes the format, which is why those variants score worse app. C.3. When designing your own ablations, keep the pair structure intact.
- Weak baselines distort the picture. Several direct models are near chance on classification and channel fairseq 13B prefers no demonstrations §4.1, footnote; macro-averages include these.
- Seeds and averaging. Report per-dataset mean over seeds, then the macro-average over datasets §3; fairseq 13B and GPT-3 numbers use only 6 datasets and 3 seeds and are not comparable to the 26-dataset averages (use fig. 11 for cross-model comparison).
- Minimal templates are deliberate. The paper uses the raw dataset label words (e.g.
not_entailment) and multi-field inputs joined by[SEP]tab. 3; manual templates were not consistently better app. B. - Context length. With k=16 and 256-token examples, prompts hit the 1024-token cap and the GPT-3 code drops tokens from the left, i.e. the earliest demonstrations gpt3.py test_gpt3.py.
- Code license. The repository has no LICENSE file; treat it as all rights reserved and do not copy code from it.
Lineage
- Builds on: in-context learning as introduced with GPT-3 (arXiv:2005.14165); direct vs. channel scoring from Min et al. (arXiv:2108.04106); the MetaICL model, datasets and k-shot splits (arXiv:2110.15943).
- Compared against: no prior method; the baselines are no-demonstration and gold-label ICL with the same models §4.1.
- Related analyses named by the paper: ICL as implicit Bayesian inference (Xie et al.), correlation with pretraining term frequencies (Razeghi et al.), and demonstrations as task location (Reynolds and McDonell) §2 §6.
Source and license
Adapted from “Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?” by Sewon Min, Xinxi Lyu, Ari Holtzman, Mikel Artetxe, Mike Lewis, Hannaneh Hajishirzi, Luke Zettlemoyer (arXiv:2202.12837), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/Alrope123/rethinking-demonstrations at commit d8267faf528b (none (no LICENSE file in repository)). Any quoted code is shown under that license.