All courses

In-context learning as implicit task inference

Explain ICL as inferring a latent task or format from the examples and then predicting under it. Use this to explain why the format of examples can matter more than whether their labels are correct.

Builds on: In-context learning

▶ Watch this lesson Start the whole course

What the animation says

The wrong-label puzzle

Give a base model four examples: a review, then its sentiment label. Then a new review, with its label left blank. Now replace all four labels with random ones. Accuracy on the new review usually drops by only a few points. If the model were learning the rule from these examples, wrong labels should wreck it. So why do they barely hurt?

The prompt as evidence

The examples aren't ignored. They mostly locate a task the model already knows. Pretraining mixed many kinds of documents: review sites, quizzes, word lists, translation tables. To predict the next token, the model has to guess which kind it is reading. So it treats the prompt as evidence about a hidden task: the input type, the label set, the layout, and the mapping. It learned the sentiment mapping during pretraining. Every token of every example is evidence for which task this is.

Bayes' rule over tasks

Write this as Bayes' rule. For each candidate task, the posterior is proportional to the prior times the likelihood of the examples. The prior is how common that kind of document was in pretraining. Given the task, the examples are treated as independent, so their likelihoods multiply. Then the prediction averages over tasks, weighted by each task's posterior.

Two tasks, one template

The template, label set and input type single out one family of tasks among many. They carry most of the evidence. Label correctness only separates tasks within that family. So take two tasks that share the template. S is real sentiment: a label matches the review ninety percent of the time. R is arbitrary labels: a match only half the time. Prior odds are ninety-nine to one for S. With two of four labels wrong, how likely is S now?

Counting the evidence

Work in log odds of S against R. The prior gives a start of four point six. Each correct label adds the log of point nine over point five: plus point five nine. Each wrong label adds the log of point one over point five: minus one point six one. Two of each sum to minus two point oh four, leaving two point five six. That's odds of about thirteen to one, so S has probability point nine three. The query is labelled correctly with probability point eight seven, versus point nine oh with all labels correct.

Sweep the wrong labels

This code sweeps from zero to four wrong labels: same two tasks, same prior. This line multiplies the label probabilities. The next one turns prior times likelihood into a posterior. Accuracy comes out point nine oh, point nine oh, point eight seven, point seven four, point five six. Accuracy is one half plus point four times the probability of S, so point seven means the tasks are tied. Under R, the query label is a coin flip, not inverted, so accuracy never drops below one half. Tap the first row where task R has become more likely than S.

Why do wrong labels barely hurt?

Give a base model 4 examples like `Review: dull plot` / `Sentiment: negative`, then a new review. Replace the 4 labels with random ones, and accuracy on the new review usually drops only a few points. Keep the correct labels but turn the layout into run-on prose, and accuracy can drop a lot. If the model were learning the input→label rule from the examples, the opposite should happen. So what is it doing?

The examples point at a task

Pretraining text mixes many kinds of documents: review sites with ratings, quizzes, word lists, translation tables. To predict the next token well, the model must guess which kind it is reading. So it treats the prompt as evidence about a **latent task**: the kind of input, the allowed labels, the layout, and the mapping. It already knows the sentiment mapping from pretraining. The examples mostly *locate* that task; they do not teach it.

ICL as Bayesian task inference

$$p(y\mid P)=\sum_c p(y\mid c,P)\,p(c\mid P),\qquad p(c\mid P)\propto p(c)\prod_{i=1}^{n}p(e_i\mid c)$$ $P$: the prompt. $y$: the continuation. $c$: a candidate latent task. $e_i$: the $i$-th example. $p(c)$: the **prior**, how common that kind of document was in pretraining. $p(e_i\mid c)$: the **likelihood**, how probable example $i$ is if the task is $c$. The product assumes the examples are independent given the task. This is **Bayes' rule**: the posterior $p(c\mid P)$ is proportional to prior × likelihood. The prediction then averages over tasks, weighted by the posterior. Every token of $e_i$ is evidence. A consistent template and label words single out one task family among many. Label correctness only separates tasks within that family.

Two wrong labels out of four

Both tasks below share the template, so only label correctness separates them. S: real sentiment, a label matches the review with prob 0.9. R: arbitrary labels, prob 0.5. Work in log-odds $\ln\frac{p(S)}{p(R)}$. Each example adds its log likelihood ratio $\ln\frac{p(e_i\mid S)}{p(e_i\mid R)}$. Prior odds 99:1 give a start of $\ln 99=4.60$. Correct label: $\ln(0.9/0.5)=+0.59$. Wrong label: $\ln(0.1/0.5)=-1.61$. Evidence: $2(0.59)-2(1.61)=-2.04$, so the log-odds become $2.56$. Odds 12.9:1 give $p(S)=0.93$. Query labelled correctly: $0.93(0.9)+0.07(0.5)=0.87$. With all labels correct it is 0.90.

Sweep the number of wrong labels

The same two-task model with 4 examples. For 0 to 4 wrong labels, accuracy is 0.90, 0.90, 0.87, 0.74, 0.56. It holds up while the prior dominates. It falls toward 0.5 only once the wrong labels are numerous enough to beat the prior. Under R the query label is a coin flip, so it is not inverted.
import numpy as np
prior = np.array([0.99, 0.01])   # S: real sentiment, R: arbitrary labels
p_ok = np.array([0.9, 0.5])      # P(label matches true sentiment | task)
for wrong in range(5):           # out of 4 examples
    right = 4 - wrong
    like = p_ok**right * (1 - p_ok)**wrong
    post = prior * like / (prior * like).sum()
    acc = post @ p_ok            # P(query gets true-sentiment label)
    print(wrong, post.round(3), acc.round(3))

Remember

ICL means inferring which latent task the prompt comes from, then predicting as that task would. The examples mainly locate a task the model already knows. Their format, label set and input type carry most of the evidence. Label correctness matters a lot only when enough consistent wrong labels outweigh a strong prior.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.