Context. Garg et al. showed that a transformer trained on random regression prompts predicts like least squares, and von Oswald et al. and Akyürek et al. built layers that take gradient steps on the prompt's squared loss. Missing was an end-to-end account: what accuracy a given depth and head count achieve, what risk follows, how many pretraining prompts find such weights, and how one network can run different procedures on different prompts. One attention layer is one gradient step on the prompt's empirical risk for any smooth convex loss, with the heads together approximating the loss derivative by a sum of ReLUs, and because convex gradient descent is non-expansive the per-layer errors only add. A few more layers can hold out part of the prompt, score the candidates and keep the best, so adaptivity, up to nearly Bayes-optimal prediction on a mixture of tasks, requires only a validation split and no new architecture.
Setting. Each token carries , (0 for the query), scratch space and a flag (1 for training pairs, 0 for the query), and the prediction is read, clipped, from the query's output token. Attention uses a ReLU normalized by in place of softmax, and every layer is residual: One definition captures what heads can compute: is -approximable by sum of ReLUs if on with and . Mildly smooth functions qualify with modest , and piecewise-linear ones exactly. Data are bounded, , , unless Gaussian.
Results: in-context algorithms. Gradient descent on any convex loss whose derivative is a sum of ReLUs runs in context, one layer per step, with error linear in depth: attention-only layers with heads store iterates with on every prompt whose empirical risk is -smooth on the ball of radius and has a minimizer of norm at most (Theorem 13). On a prompt with , layers with at most 3 heads compute ridge to accuracy , , and so inherit its risk: for , layers come within of the best linear predictor on every sub-Gaussian well-posed distribution, linear or not, and layers within of the Bayes risk under , where the Bayes predictor is ridge with . GLMs, logistic regression included, need many heads ( per layer; layers and heads give excess risk ), and Lasso needs many layers ( layers, 2 heads; on an -sparse Gaussian model with , with probability , risk ).
Results: selection and pretraining. Given predictors fitted on a training split, three more layers output a convex combination whose validation loss is within of the best, so for a convex risk (Proposition 15). For noisy linear models whose noise level is drawn from , with and , a transformer with layers and heads is within of the Bayes risk of every component, hence of the mixture (Theorem 17). For pretraining, the empirical risk minimizer over prompts within the norm ball (depth , heads, MLP width , token dimension , norm at most ) is, with probability , within of the best in the ball, logarithmic in the norms (Theorem 20); the constructions as comparators give polynomial prompt counts for ridge and the noise mixture.
One attention layer takes one gradient step, one head per ReLU piece of the loss derivative. The query carries the current iterate (stored in every token), the key carries , and the value the direction : head scores , with a key term that pushes the score below zero on the query token, and writes into the -slot. Summed over heads the ReLUs rebuild the derivative, so the residual update is up to an error of norm . This determines the head count. The squared loss needs two heads, since , an penalty one more, and a smooth loss as many as its sum-of-ReLUs approximation. A final 2-head layer reads out via .
Errors add linearly across layers because convex gradient descent is non-expansive. Stacking gives a perturbed iteration with , and co-coercivity of a convex -smooth shows that never moves two points apart: Each layer passes the earlier error on without amplifying it and adds its own, so ; the same argument around the minimizer keeps the iterates in the ball where the ReLU approximation is valid. Without convexity this fails: for GD on a two-layer network the construction needs two layers per step and the error compounds exponentially.
Ridge, least squares and GLMs are gradient descent run for enough steps, and their risks follow by conditioning on a well-conditioned prompt. On a well-conditioned prompt the ridge objective is -strongly convex and -smooth, so with gradient descent contracts like and steps reach accuracy ; the squared loss is exactly a sum of ReLUs, so the transformer adds no error. GLMs are the same argument with approximated by ReLUs, which is where the heads come from. Designs with are well conditioned except with polynomially small probability, where the clipped output adds little risk, so the classical risks transfer.
Lasso uses proximal gradient, with the MLP doing the soft threshold. Proximal gradient alternates a gradient step on the squared loss (two heads) with the prox, and soft thresholding is piecewise linear, so a token-wise MLP of width implements it exactly. That gives the generic steps without strong convexity, which fails when . For a depth that does not grow with , restricted strong convexity and smoothness of the Gaussian design on sparse directions make proximal gradient converge linearly to within the statistical error (as in Agarwal et al.), and the Lasso risk transfers.
Post-ICL validation takes three layers, which score, rank and mix the candidates. Tokens carry for training, for validation and 0 for the query, and the base algorithms run in parallel on the training split. An attention layer scores all on the validation tokens, with heads approximating the loss as a sum of ReLUs. An MLP ranks them: is at most only when , and flags near-minimizers. A last attention layer mixes, so is the part of a total weight of one left after the earlier flagged predictors have received theirs. The weights are nonnegative and sum to one, a soft version of the rule "take the first near-minimizer", and convexity of the risk turns this into the oracle inequality.
Validation over ridge penalties is nearly Bayes-optimal for mixed noise levels, with excess risk of order . Since , it suffices to compete with the oracle that knows : fit all ridges, , on points and pick one. The Bayes predictor averages the same ridges, but its weights involve and a data-dependent exponent, which attention does not compute easily. The excess comes from the lost training points, , and from the uniform deviation of validation losses, , and balancing them sets the split: The transformer commits to the noise level that validates best instead of carrying a posterior over it.
Pre-ICL testing computes a statistic of the prompt first and branches on it. To tell classification from regression, average a label test over the prompt, with a pair of tents of width at 0 and 1. Since is a combination of six ReLUs of , one 6-head attention layer computes exactly at every token. Another layer thresholds it into a gate in , and the network outputs the gated combination of logistic regression and least squares: for binary labels the gate is exactly 1, and for labels not concentrated near it is 0 with high probability. This is Proposition 19.
Pretraining needs polynomially many prompts because transformers are Lipschitz in their weights. With clipping after each layer, the output on bounded inputs is Lipschitz in with constant , where are polynomial in and , and the norm ball has log covering number . Chaining for the bounded loss process then gives the Lipschitz constant is exponential in but enters only through the log, giving the extra factor in Theorem 20. The construction only certifies that the comparator is small, and the result says nothing about what SGD finds.
Trained transformers behave as if they select an algorithm for each prompt. A 12-layer ReLU-attention encoder matches the best baseline on noiseless, noisy and sparse regression and on linear classification. Trained on a mixture of two noise levels, or of regression and classification, one model approaches the best baseline on each component, while single-task models fall short on the other task; a GPT-2 decoder does the same. This is behavioral evidence only; the trained weights are not probed for validation or testing.
Consequences and limits. "The transformer runs gradient descent" becomes quantitative: on well-conditioned convex problems the depth needed grows like the logarithm of the inverse accuracy, the number of heads sets how accurately the loss derivative is represented, polynomially many prompts suffice to find the weights, and selection by validation is nearly Bayes-optimal on a noise mixture without representing the posterior. The limits are that the constructions are existence results for ReLU attention, and the paper does not show that softmax transformers trained by SGD find them; the in-context guarantees need a well-conditioned prompt ( for least squares), so the interpolation regime of the benign-overfitting papers is handled only by Lasso and ridge; the validation route's excess decays only like ; and the pretraining bound is for constrained ERM and grows with .
Terms
- ReLU attention
- in place of softmax, residual
- Sum of ReLUs
- , ; the heads approximate in this form
- ICGD layer
- query carries , key carries , value carries : one step of
- Non-expansive GD
- for convex smooth , ; errors add linearly
- condition number of the prompt; depth
- Ridge as Bayes
- , noise : posterior mean is ridge with
- Post-ICL validation
- fit algorithms on a train split, score on a validation split, mix near-minimizers
- ; ; ReLU weights selecting a near-minimizer
- Pre-ICL testing
- compute a prompt statistic (e.g. are labels binary) in one layer, then gate between algorithms
- balances lost training data against validation error; rate
Quiz
- In the one-layer gradient step, where does the derivative get evaluated?The query carries and the key carries , so the ReLU of their inner product is one term of the sum-of-ReLUs approximation of . The value supplies the direction, and summing heads and tokens gives the gradient.
- Ridge needs at most 3 heads per layer, while a general GLM needs . What causes the difference?Heads are spent one per ReLU piece of . The squared loss has a piecewise-linear derivative, represented exactly by two ReLUs plus one head for the penalty, while a smooth nonlinear link needs a sum-of-ReLUs approximation whose size grows as the accuracy shrinks.
- Why does the paper not have the transformer compute the exact Bayes predictor for the noise mixture?The posterior mean is a weighted sum of the ridge estimators, but the weights contain determinants and data-dependent exponents. Validation sidesteps them and competes with the oracle that knows , which is enough because .
- What does the pretraining guarantee (Theorem 20) use the in-context constructions for?The generalization bound holds for constrained ERM over any norm ball by chaining and Lipschitzness in . The constructions only certify that some transformer in the ball has small ICL risk, so the ERM's risk is small too; nothing is said about SGD.
Sources
- Yu Bai, Fan Chen, Huan Wang, Caiming Xiong, and Song Mei. Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection. NeurIPS, 2023.
- Shivam Garg, Dimitris Tsipras, Percy Liang, and Gregory Valiant. What Can Transformers Learn In-Context? A Case Study of Simple Function Classes. NeurIPS, 2022.
- Johannes von Oswald et al. Transformers learn in-context by gradient descent. ICML, 2023.
- Ekin Akyürek, Dale Schuurmans, Jacob Andreas, Tengyu Ma, and Denny Zhou. What learning algorithm is in-context learning? Investigations with linear models. ICLR, 2023.