Exercises — In-context learning mechanisms
This page tests the machinery from the parent note: the Bayesian task-inference view, the attention-as-one-GD-step toy model, and the empirical quirks (recency, label-space, saturation). Every problem has a collapsible full solution — try first, then reveal.
Before we start, we fix the vocabulary used throughout, so no symbol appears unexplained.
Level 1 — Recognition
L1.1 — Is this in-context learning?
Two students describe how they got an LLM to classify emails as spam/not-spam.
- Student A: "I collected 5000 labelled emails and ran training that adjusted the network's weights until it stopped improving."
- Student B: "I pasted 4 example emails with their labels into the prompt, then a 5th email, and read off the answer. I never touched the weights."
Which one is doing in-context learning? Name the single defining feature that decides it.
Recall Solution
Student B is doing ICL. The single defining feature is: the weights never change — the task is learned purely by conditioning on demonstrations in the prompt during one forward pass. Student A is doing fine-tuning: the weights are permanently updated by gradient descent. Number of examples (5000 vs 4) is a red herring — the deciding line is weight update vs no weight update.
L1.2 — Name the shot count
Classify each prompt by its (the shot count):
Translate to French: "hello" →(no examples given, just the instruction and query)dog → animal; then: rose → ?A → 1; B → 2; C → 3; then: D → ?
Recall Solution
- → zero-shot (no demo pairs, only instruction + query).
- → one-shot (one demo
dog → animal). - → three-shot (three demos
A→1, B→2, C→3). Rule: counts completed input–output pairs, not the query and not any instruction line.
Level 2 — Application
L2.1 — Compute a two-concept posterior
The model is unsure between two tasks after seeing demos:
- = "sentiment" with prior ,
- = "topic-labelling" with prior .
Two demos arrive. Under their joint likelihood is . Under it is . Compute the posterior using Step 1 of the parent's derivation.
Recall Solution
Bayes with a flat prior: Numerator . Denominator . So Two demos already push belief in the true task from to — that is the posterior beginning to collapse.
L2.2 — One gradient step by hand
Use the toy model from the parent note, starting at , learning rate , with one demo where and (a scalar). Predict on the query .
Recall Solution
Where the update comes from (one-sentence reminder). We train the linear model by gradient descent on the squared-error loss . Its gradient is ; evaluated at the start this is , so one step gives Step — build . With one demo: Step — predict. Cross-check via the "attention form." Substitute back into : This is the same shape as a single (linear) attention head — it weights each demo answer by the similarity between that demo's input and the query, exactly the equivalence the parent note derives. Numerically: , so . Identical to the matrix route. ✓
Level 3 — Analysis
L3.1 — Why does accuracy saturate?
The parent claims the log-posterior-ratio grows like . Suppose per demo the true concept beats a rival by nats (natural-log units, defined in the notation box), and start from equal priors. Give the posterior odds after demos, and explain in one sentence why gains "plateau".
Recall Solution
Log-odds accumulate. With two concepts, the log-odds is . From the parent's Step 3 it equals (equal priors ⇒ start at ) Exponentiate to get odds. Odds . Convert odds back to probability. For two mutually exclusive concepts , so odds inverts to — this is exactly the sigmoid from the definition box.
| odds | ||
|---|---|---|
| 1 | 2 | |
| 2 | 4 | |
| 3 | 8 | |
| 10 | 1024 | |
| Why it plateaus (one sentence): the odds grow exponentially, but the probability is squashed into , so going moves accuracy only from to — the KL "budget" is nearly spent and extra demos barely help. The figure makes this S-curve explicit. |

Reading the figure: the horizontal axis is the number of demos ; the vertical axis is the posterior probability on the true task. The teal S-curve rises fast for small (orange arrow: big early gains) then flattens toward (teal arrow: diminishing returns). The dashed plum line marks the level; the dotted plum line shows the first that clears it — , computed in L5.1.
L3.2 — Flipped labels
A student flips all labels in a 4-shot sentiment prompt (Positive↔Negative) and finds accuracy barely drops. They conclude "labels are useless." Critique this using the parent's label-space vs label-correctness distinction. When would flipping actually break the model?
Recall Solution
The demos carry three signals: (a) input distribution, (b) label space (the set {Positive, Negative} and the → format), (c) the exact input→label mapping.
Flipping labels destroys only (c) but preserves (a) and (b). For an easy, high-prior task like sentiment, the model recovers the correct mapping from its pretraining prior, so it ignores the corrupted (c) and rides on (a)+(b) — hence the small drop.
When flipping breaks it: for a novel/arbitrary task the prior cannot solve (e.g. cat → 7, dog → 3 with a made-up rule). There the model has no fallback mapping, so it must trust (c); corrupting the labels then genuinely wrecks accuracy. So "labels are useless" is false in general — it's a special case of strong priors.
Level 4 — Synthesis
L4.1 — Design a probe for "conditioning, not learning"
You want to demonstrate that ICL is transient conditioning (activations), not a real weight update. Design a two-part experiment and state, for each part, what result would confirm the conditioning hypothesis.
Recall Solution
Part 1 — Reversibility. Run the model on a -shot prompt, get answer . Then run the same query in a fresh prompt with no demos. If the model reverts to zero-shot behaviour (the demo-induced skill vanishes), the adaptation was transient → conditioning confirmed. A weight update would persist across prompts; conditioning does not. Part 2 — Ordering sensitivity. Permute the demo order and re-run. If predictions flip with order (recency/majority bias), the model is conditioning on token positions, not integrating examples into a stable learned function → conditioning confirmed. True GD on a fixed dataset is order-insensitive at convergence; ICL is not. Together: reversibility + order-sensitivity are signatures of conditioning inside a single forward pass, matching the parent's Story 2 caveat that the "virtual GD step" lives in activations only.
L4.2 — Reconcile the two stories
The parent gives Story 1 (Bayesian task location) and Story 2 (attention ≈ one GD step). Argue in a short paragraph that they are not competing theories, using the toy linear result .
Recall Solution
Both stories describe the same computation from different angles. Story 2's attention output is a similarity-weighted vote of the demo answers: query-like demos ( large) dominate. That weighting is exactly what Story 1 calls "the posterior selecting which examples/concept apply." The dot-product similarity plays the role of the per-demo likelihood; summing over demos plays the role of marginalising over evidence. So the forward pass simultaneously (i) implements a GD-like update (Story 2 mechanics) and (ii) behaves like Bayesian task-inference (Story 1 semantics). The model already contains the algorithm; the context selects and steers it — one process, two vocabularies.
Level 5 — Mastery
L5.1 — Derive the saturation curve closed-form
Assume equal priors between and one rival , and a constant per-demo expected log-likelihood gap nats. Derive as a function of , show it is a sigmoid in , find its value at , its limit as , and the at which it first exceeds when .
Recall Solution
Log-odds. From the parent's Step 3, log-odds (equal priors ⇒ first term ). Convert to probability. With two concepts, using the sigmoid from the definition box with : — a sigmoid in .
- At : (no evidence ⇒ 50/50). ✓
- As : (posterior collapses onto ). ✓ Threshold. Need . Invert the sigmoid (take log-odds of both sides): . With : So five demos first push confidence past — and every demo after that is deep in the flat part of the sigmoid, giving the plateau you saw in L3.1.
L5.2 — Weighted-vote breakdown of the toy predictor
Given demos and , and query , use to compute the prediction, and identify which demo dominates and why.
Recall Solution
Similarities: ; . Weighted vote: Domination: demo 1 contributes , demo 2 only . Demo 1 wins because the query points mostly along 's direction (its first coordinate, , is large), so its dot-product similarity is 3× that of demo 2. This is Story 1's "query-relevant demos dominate the posterior" and Story 2's "attention weights by key–query similarity" — the same number, two readings.
Recall One-line self-test
Every numeric answer on this page: , , odds table, , threshold — all confirmed in the machine-check block. If your hand-work disagreed, re-open the matching solution.