Part VI ยท Prompt engineering
Ch 19. Learning from examples in the prompt
Show, donโt tell
Write โhot -> coldโ and โbig -> smallโ on two lines, then โhappy ->โ, and a model will usually answer sad. Nobody told it the task was opposites; it worked that out from the examples, without any more training. This is in-context learningin-context learning: A model picking up a task from examples in its prompt, without any change to its weights. Ch 19, and giving a few examples like this is called few-shot prompting.[1]
Write โhot -> coldโ and โbig -> smallโ on two lines, then โhappy ->โ, and a model will usually answer sad. Nobody told it the task was opposites. It worked it out from the examples, during a single forward pass, without changing a single weight. This is in-context learningin-context learning: A model picking up a task from examples in its prompt, without any change to its weights. Ch 19, and giving a few examples like this is called few-shot prompting.[1][1]
Here it is for two tasks: opposites, and a rule nobody could guess, where fruits are โblipโ and animals are โzorkโ. Change how many examples the prompt has:
With no examples, Qwen2.5-1.5B-Instruct gives sad about a 1% chance; with four examples, 96%. The made-up rule works too, though less reliably.
With no examples, Qwen2.5-1.5B-Instruct gives sad about 1% for โhappy ->โ; with four, 96%. For the made-up rule, the right label goes from almost nothing to between 22% and 78%. Not every word gets easier: โlight -> darkโ became less likely as examples were added.
The much smaller GPT-2 mostly fails: it repeats the word it was given, or copies the last answer it saw. Bigger models are much better at this.[1]
GPT-2 small mostly fails. For opposites it usually just repeats the word it was given, and for the made-up rule it answers โzorkโ for everything, the label of the last example it saw. In-context learning gets much better as models get bigger.[1][1]
Answers longer than one token
GPT-2 splits the made-up labels in two: โblipโ is โ blโ + โipโ, and โzorkโ is โ zโ + โorkโ. The chance of a whole answer is the product of its tokensโ chances, each given everything before it:
For โhorse ->โ after four examples, thatโs 0.69 ร over 0.99, about 0.69: once GPT-2 has written โ zโ, the โorkโ is close to certain. So the whole choice is made at the first token, โ zโ or โ blโ, and after four examples GPT-2 picks โ zโ for the fruits too.
Do the labels have to be right?
You might expect wrong examples to teach a wrong rule. A study of 12 models found that mixing up the examplesโ labels at random barely mattered: what the examples taught was which labels to use and what the answer should look like.[2] But bigger models do follow labels that are wrong in a consistent way.[3] Here, eight short reviews are labeled positive or negative before 24 new ones. Pick how the eight are labeled:
If examples teach a rule, wrong examples should teach a wrong one. A study of 12 models, GPT-3 among them, found otherwise: swapping the examplesโ labels at random barely hurt. What mattered was that the examples showed which labels to use, what the inputs look like, and the layout of the answers.[2][2] A later study found that size changes this. Given examples with every label flipped, small models stuck to what they had learned in training, while the largest flipped their answers too, and mainly large models could learn labels with no meaning, like foo and bar for positive and negative.[3][3] Here, eight short reviews are labeled before 24 new ones. Pick how the eight are labeled:
Without examples, Qwen2.5-1.5B-Instruct gives positive or negative only about a quarter of its chances; with any eight examples, nearly all of them. That is the format at work. With half the labels wrong, it still labels all 24 reviews correctly. But flip every label and it follows the flipped labels for 23 of 24 reviews. GPT-2 small gets 21 right with the right labels, and with flipped or made-up labels it gives every review the same one.
Without examples, Qwen2.5-1.5B-Instruct puts only 27% of its probability on โ positiveโ or โ negativeโ, and GPT-2 almost none; with any eight examples, Qwen puts over 99% there and GPT-2 over 90%. That is the label set and the format at work. With half the labels wrong, Qwen still labels all 24 reviews by their real sentiment: labels that contradict each other teach it nothing, so it falls back on what it knows. Flip every label, though, and it follows the flipped labels for 23 of 24 reviews, and with foo and bar it uses the made-up labels correctly 23 times. In that study, it took far larger models to follow flipped labels; Qwen is a much newer model, on a much easier task. GPT-2 small gets 21 right with the right labels and 17 with half wrong, and with flipped or made-up labels it gives every review the same label.
How the model copies a pattern
How does the model use the examples? Part of the answer is a kind of attention head that finds earlier places where the current token appeared and looks at what came right after it. On โa b โฆ aโ, it looks back at โbโ. These induction heads may do much of the work of learning from examples.[4] GPT-2 small has one doing exactly this on the prompts above:
What does the model do with the examples? Part of the answer is a kind of attention head that finds earlier places where the current token appeared and looks at whatever came right after it. On โa b โฆ aโ, it looks back at โbโ. These induction heads appear during training at the same moment models suddenly get better at learning from their context, and they may be how most in-context learning works.[4][4] GPT-2 small has one doing exactly this on the prompts above:
From the final arrow, it looks almost entirely at the earlier answers, the words that followed the earlier arrows. The rest of the network can then use what it found to write an answer of the same kind. Copying is also why examples can work too well: a model can pick up their exact words, length, or quirks along with the rule, so examples that differ from each other in everything but the rule teach the rule best.
Two heads make an induction head
An induction head canโt do this alone, because a tokenโs query and key say nothing about which token came before it. An earlier head supplies that. A previous-token head copies each tokenโs predecessor into it, so the position of the answer โcoldโ also carries a note that it came after an arrow. The induction headโs query at the final arrow then matches the positions that came after arrows, which are the answers, and its values copy those answers forward.[4] That takes two layers of attention, one after the other, so a one-layer model canโt form one. Head 5.0 in the widget sends 98% of the final arrowโs attention to the four answers to the opposites, rising from 5% on the first to 35% on the last.
Show or tell?
You can also just say what you want, with no examples. For a familiar task, that is often enough. Examples help most when the answer has to look a certain way, or uses labels or a style the model canโt guess. They make every prompt longer, though.
Instructions alone, called zero-shot prompting, are often enough for familiar tasks like summarizing or translating. Examples earn their place when the answer has to follow an exact format, or uses labels or a style the model canโt guess from a description, as the reviews above showed. They also cost tokens on every call, though a fixed set at the start of the prompt can be cached (Ch 26). The next chapter is about the other half: telling the model what to do.
Recipe card
Recap
- A few examples in the prompt can teach a model a new task, with no change to its weights.
- Examples teach the label set and the format; with labels that contradict each other, a model falls back on what it knows, while consistently wrong labels can pull a capable model along.
- Attention heads that look at โwhat came after this token last timeโ help copy the pattern.
- Bigger models learn from examples much better; small ones often just repeat the last answer.
- A whole answerโs chance is the product of its tokensโ chances; an induction head relies on an earlier previous-token head.
Next question: chat models come with a system prompt. Does telling the model who to be change what it can do?
Sources
- Tom B. Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020.
- Sewon Min et al. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?. EMNLP 2022.
- Jerry Wei et al. (2023). Larger language models do in-context learning differently. arXiv.
- Catherine Olsson et al. (2022). In-context Learning and Induction Heads. Transformer Circuits Thread.
Sources
- Tom B. Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020.
- Sewon Min et al. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?. EMNLP 2022.
- Jerry Wei et al. (2023). Larger language models do in-context learning differently. arXiv.
- Catherine Olsson et al. (2022). In-context Learning and Induction Heads. Transformer Circuits Thread.
Sources
- Tom B. Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020.
- Sewon Min et al. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?. EMNLP 2022.
- Jerry Wei et al. (2023). Larger language models do in-context learning differently. arXiv.
- Catherine Olsson et al. (2022). In-context Learning and Induction Heads. Transformer Circuits Thread.