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
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.
The examples point at a task
ICL as Bayesian task inference
Two wrong labels out of four
Sweep the number of wrong labels
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
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.
Bytewise
All courses