Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection

Yu Bai, Fan Chen, Huan Wang, Caiming Xiong, Song Mei

Introduction

Large neural sequence models have demonstrated remarkable in-context learning (ICL) capabilities , where models can make accurate predictions on new tasks when prompted with training examples from the same task, in a zero-shot fashion without any parameter update to the model. A prevalent example is large language models based on the transformer architecture , which can perform a diverse range of tasks in context when trained on enormous text . Recent models in this paradigm such as GPT-4 achieve surprisingly impressive ICL performance that makes them akin to a general-purpose agent in many aspects . Such strong capabilities call for better understandings, which a recent line of work tackles from various aspects .

Recent pioneering work of Garg et al. proposes an interpretable and theoretically amenable setting for understanding ICL in transformers. They perform ICL experiments where input tokens are real-valued (input, label) pairs generated from standard statistical models such as linear models (and the sparse version), neural networks, and decision trees. Garg et al. find that transformers can learn to perform ICL with prediction power (and fitted functions) matching standard machine learning algorithms for these settings, such as least squares for linear models, and Lasso for sparse linear models. Subsequent work further studies the internal mechanisms , expressive power , and generalization of transformers in this setting. However, these works only showcase simple mechanisms such as regularized regression or gradient descent , which are arguably only a small subset of what transformers are capable of in practice; or expressing universal function classes not specific to ICL . This motivates the following question:

How do transformers learn in context beyond implementing simple algorithms?

This paper makes steps on this question by making two main contributions: (1) We unveil a general mechanism—in-context algorithm selection—by which a single transformer can adaptively select different “base” ICL algorithms to use on different ICL instances, without any explicit prompting of the right algorithm to use in the input sequence. For example, a transformer may choose to perform ridge regression with regularization λ1\lambda_{1} on ICL instance 1, and λ2\lambda_{2} on ICL instance 2 (Fig. 2); or perform regression on ICL instance 1 and classification on ICL instance 2 (Fig. 5). This adaptivity allows transformers to achieve much stronger ICL performance than the base ICL algorithms. We both prove this in theory, and demonstrate this phenomenon empirically on standard transformer architectures. (2) Along the way, equally importantly, we present a first comprehensive theory for ICL in transformers by establishing end-to-end quantitative guarantees for the expressive power, in-context prediction performance, and sample complexity of pretraining. These results add upon the recent line of work on the statistical learning theory of transformers , and lay out a foundation for the intriguing special case where the learning targets are themselves ICL algorithms.

We prove that transformers can implement a broad class of standard machine learning algorithms in context, such as least squares and ridge regression (Section 3.1), convex risk minimization for learning generalized linear models (such as logistic regression; Section 3.2), Lasso (Section 3.3), and gradient descent for two-layer neural networks (Section 3.4 & Appendix G). Our constructions admit mild bounds on the number of layers, heads, and weight norms, and achieve near-optimal prediction power on many in-context data distributions.

Technically, the above transformer constructions build on a new efficient implementation of in-context gradient descent (Section 3.5), which could be broaderly applicable. For a broad class of smooth convex empirical risks over the in-context training data, we construct an (L+1)(L+1)-layer transformer that approximates LL steps of gradient descent. Notably, the approximation error accumulates only linearly in LL, utilizing a stability-like property of smooth convex optimization.

We prove that transformers can perform in-context algorithm selection (Section 4). We construct two algorithm selection mechanisms: Post-ICL validation (Section 4.1), and Pre-ICL testing (Section 4.2). For both mechanisms, we provide general constructions as well as concrete examples. Fig. 1 provides a pictorial illustration of the two mechanisms.

As a concrete application, using the post-ICL validation mechanism, we construct a transformer that can perform nearly Bayes-optimal ICL on noisy linear models with mixed noise levels (Section 4.1.1), a more complex task than those considered in existing work.

We provide the first line of results for pretraining transformers to perform the various ICL tasks above, from polynomially many training sequences (Section 5).

Experimentally, we find that learned transformers indeed exhibit strong in-context algorithm selection capabilities in the settings considered in our theory (Section 6). For example, Fig. 2 shows that a single transformer can approach the individual Bayes risks (the optimal risk among all possible algorithms) simultaneously on two noisy linear models with different noise levels.

We humbly remark that the typical toolkit of a statistician contains much more beyond those covered in this work, including and not limited to inference, uncertainty quantification, and theoretical analysis. This work merely aims to show the algorithm selection capability of transformers, akin to what a statistician can do.

1 Related work

The in-context learning (ICL) capability of large language models (LLMs) has gained significant attention since demonstrated on GPT-3 Brown et al. . A number of subsequent empirical studies have contributed to a better understanding of the capabilities and limitations of ICL in LLM systems, which include but are not limited to . For an overview of ICL, see the survey by Dong et al. which highlights some key findings and advancements in this direction.

A line of recent work investigates why and how LLMs perform ICL . In particular, Xie et al. propose a Bayesian inference framework explaining how ICL works despite formatting differences between training and inference distributions. Garg et al. show empirically that transformers could be trained from scratch to perform ICL of linear models, sparse linear models, two-layer neural networks, and decision trees. Li et al. analyze the generalization error of trained ICL transformers from a stability viewpoint. They also experimentally show that transformers could perform “in-context model selection” (conceptually similar to in-context algorithm selection considered in this work) in specific tasks and presented related theoretical hypotheses. However, they do not provide concrete mechanisms or constructions for in-context model selection. A recent work shows that pretrained transformers can perform Bayesian inference in latent variable models, which may also be interpreted as a mechanism for ICL. Our experimental findings extend these results by unveiling and demonstrating the in-context algorithm selection capabilities of transformers.

Closely related to our theoretical results are , which show (among many things) that transformers can perform ICL by simulating gradient descent. However, these results do not provide quantitative error bounds for simulating multi-step gradient descent, and only handle linear regression models or their simple variants. Among these works, Akyürek et al. showed that transformers can implement learning algorithms for linear models based on gradient descent and closed-form ridge regression; it also presented preliminary evidence that learned transformers perform ICL similar to Bayes-optimal ridge regression. Our work builds upon and substantially extends this line of work by (1) providing a more efficient construction for in-context gradient descent; (2) providing an end-to-end theory with additional results for pretraining and statistical power; (3) analyzing a broader spectrum of ICL algorithms, including least squares, ridge regression, Lasso, convex risk minimization for generalized linear models, and gradient descent on two-layer neural networks; and (4) constructing more complex ICL procedures using in-context algorithm selection.

When in-context data are generated from a prior, the Bayes risk is a theoretical lower bound for the risk of any possible ICL algorithm, including transformers. Xie et al. , Akyürek et al. observe that learned transformers behave closely to the Bayes predictor on a variety of tasks such as hidden Markov models and noisy linear regression with a fixed noise level . Using the in-context algorithm selection mechanism (more precisely the post-ICL validation mechanism), we show that transformers can perform nearly-Bayes optimal ICL in noisy linear models with mixed noise levels (a strictly more challenging task than considered in ), with both concrete theoretical guarantees (Section 4.1.1) and empirical evidence (Fig. 2 & 4).

The transformer architecture, introduced by , has revolutionized natural language processing and been adopted in most of the recently developed large language models such as BERT and GPT . Broaderly, transformers have demonstrated remarkable performance in many other fields of artificial intelligence such as computer vision, speech, graph processing, reinforcement learning, and biological applications . Towards a better theoretical understanding, recent work has studied the capabilities , limitations , and internal workings of transformers.

We remark that the transformer architecture used in our theoretical constructions differs from the standard one by replacing the softmax activation (in the attention layers) with a (normalized) ReLU function. Transformers with ReLU activations is experimentally studied in the recent work of Shen et al. , who find that they perform as well as the standard softmax activation in many NLP tasks.

Training models (such as transformers) to perform ICL can be viewed as an approach for the broader problem of learning-to-learn or meta-learning . A number of other approaches has been studied extensively for this problem, including (and not limited to) training a meta-learner on how to update the parameters of a downstream learner , learning parameter initializations that quickly adapt to downstream tasks , learning latent embeddings that allow for effective similarity search . Most relevant to the ICL setting are approaches that directly take as input examples from a downstream task and a query input and produce the corresponding output . For a comprehensive overview, see the survey .

Theoretical aspects of meta-learning have received significant recent interest . In particular, analyzed the benefit of multi-task learning through a representation learning perspective, and studied the statistical properties of learning the parameter initialization for downstream tasks.

We build on various existing techniques from the statistics and learning theory literature to establish our approximation and generalization guarantees for transformers. For the approximation component, we rely on a technical result of Bach on the approximation power of ReLU networks. We use this result to show that transformers can approximate gradient descent (GD) on a broad range of loss functions, substantially extending the results of who primarily consider the square loss. The recent work of Giannou et al. also approximates GD with general loss functions by transformers, though using a different technique of forcing the softmax activations to act as sigmoids. Our analyses of Lasso and generalized linear models build on . Our generalization bound for transformers (used in our pretraining results) build on a chaining argument .

Preliminaries

Above, Eq. 1 uses a normalized ReLU activation t↦σ(t)/Nt\mapsto\sigma(t)/N in place of the standard softmax activation, which is for technical convenience and does not affect the essence of our studyFor each query index ii, the attention weights {σ(⟨Qmhi,Kmhj⟩)/N}j∈[N]{\{\sigma(\left\langle{\mathbf{Q}}_{m}{\mathbf{h}}_{i},{\mathbf{K}}_{m}{\mathbf{h}}_{j}\right\rangle)/N\}}_{j\in[N]} is also a set of non-negative weights that sum to O(1)O(1) (similar as a softmax probability distribution) in typical scenarios..

We consider a transformer architecture with L≥1L\geq 1 transformer layers, each consisting of a self-attention layer followed by an MLP layer.

We additionally define the following norm of a transformer TFθ{\rm TF}_{\bm{\theta}}:

In (2), the choices of the operator norm and max/sums are for convenience only and not essential, as our results (e.g. for pretraining) depend only logarithmically on ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}.

2 In-context learning

We emphasize that the setting above considers predicting only at the last token xN+1{\mathbf{x}}_{N+1}, which is without much loss of generality. Our constructions may be generalized to predicting at every token, by using a decoder architecture and a corresponding input format (cf. Appendix B). Our theory focuses on predicting at the last token only, which simplifies the setting. Our experiments test both settings.

Basic in-context learning algorithms

We begin by constructing transformers that approximately implement a variety of standard machine learning algorithms in context, with mild size bounds and near-optimal prediction power on many standard in-context data distributions.

Consider the standard ridge regression estimator over the in-context training examples D\mathcal{D} with regularization λ≥0\lambda\geq 0 (reducing to least squares at λ=0\lambda=0 and N≥dN\geq d):

We show that transformers can approximately implement Eq. ICRidge (proof in Section D.1).

For any λ≥0\lambda\geq 0, 0≤α≤β0\leq\alpha\leq\beta with κ:=β+λα+λ\kappa\mathrel{\mathop{:}}=\frac{\beta+\lambda}{\alpha+\lambda}, Bw>0B_{w}>0, and ε<BxBw/2\varepsilon<B_{x}B_{w}/2, there exists an LL-layer attention-only transformer TFθ0{\rm TF}^{0}_{\bm{\theta}} with

(with R:=max⁡{BxBw,By,1}R\mathrel{\mathop{:}}=\max{\{B_{x}B_{w},B_{y},1\}}) such that the following holds. On any input data (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) such that the problem Eq. ICRidge is well-conditioned and has a bounded solution:

TFθ0{\rm TF}^{0}_{\bm{\theta}} approximately implements Eq. ICRidge: The prediction y^N+1=ready(TFθ0(H))\widehat{y}_{N+1}={\sf read}_{\sf y}({\rm TF}^{0}_{\bm{\theta}}({\mathbf{H}})) satisfies

Further, the second-to-last layer approximates wridgeλ{\mathbf{w}}_{{\rm ridge}}^{\lambda}: we have ∥readw(hi(L−1))−wridgeλ∥2≤ε/Bx\|{{\sf read}_{\sf w}({\mathbf{h}}^{(L-1)}_{i})-{\mathbf{w}}_{{\rm ridge}}^{\lambda}}\|_{2}\leq\varepsilon/B_{x} for all i∈[N+1]i\in[N+1] (see Section D.1 for the definition of readw{\sf read}_{\sf w}).

Theorem 4 presents the first quantitative construction for end-to-end in-context ridge regression up to arbitrary precision, and improves upon Akyürek et al. whose construction does not give (or directly imply) an explicit error bound like Eq. 6. Further, the bounds on the number of layers and heads in Eq. 4 are mild (constant heads and logarithmically many layers).

Combining Theorem 4 with standard analyses of linear regression yields the following corollaries (proofs in Section D.3 & D.4).

For any N≥O~(d)N\geq\widetilde{\mathcal{O}}(d), there exists an O(κlog⁡(κN/σ))\mathcal{O}(\kappa\log(\kappa N/\sigma))-layer transformer θ{\bm{\theta}}, such that on any P\mathsf{P} satisfying standard statistical assumptions for least squares (Assumption), its ICL prediction y^N+1\widehat{y}_{N+1} achieves

Assumption requires only generic tail properties such as sub-Gaussianity, and not realizability (i.e., P\mathsf{P} follows a true linear model); κ,σ\kappa,\sigma above denote the covariance condition number and the noise level therein. The O~(dσ2/N)\widetilde{\mathcal{O}}(d\sigma^{2}/N) excess risk is known to be rate-optimal for linear regression , and Corollary 5 achieves this in context with a transformer with only logarithmically many layers.

Next, consider Bayesian linear models where each in-context data distribution P=Pw⋆lin\mathsf{P}=\mathsf{P}^{\sf lin}_{{\mathbf{w}}_{\star}} is drawn from a Gaussian prior π:w⋆∼N(0,Id/d)\pi:{\mathbf{w}}_{\star}\sim\mathsf{N}(0,{\mathbf{I}}_{d}/d), and (x,y)∼Pw⋆lin({\mathbf{x}},y)\sim\mathsf{P}^{\sf lin}_{{\mathbf{w}}_{\star}} is sampled as x∼N(0,Id){\mathbf{x}}\sim\mathsf{N}({\mathbf{0}},{\mathbf{I}}_{d}), y=⟨w⋆,x⟩+N(0,σ2)y=\left\langle{\mathbf{w}}_{\star},{\mathbf{x}}\right\rangle+\mathsf{N}(0,\sigma^{2}). It is a standard result that the Bayes estimator of yN+1y_{N+1} given (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) is given by ridge regression Eq. ICRidge: y^N+1Bayes:=⟨wridgeλ,xN+1⟩\widehat{y}^{\sf Bayes}_{N+1}\mathrel{\mathop{:}}=\langle{\mathbf{w}}_{\rm ridge}^{\lambda},{\mathbf{x}}_{N+1}\rangle with λ=dσ2/N\lambda=d\sigma^{2}/N. We show that transformers achieve nearly-Bayes risk for this problem, and we use

to denote the Bayes risk of this problem under prior π\pi.

2 In-context learning of generalized linear models

As a natural generalization of linear regression, we now show that transformers can recover learn generalized linear models (GLMs) (which includes logistic regression for linear classification as an important special case), by implementing the corresponding convex risk minimization algorithm in context, and achieve near-optimal excess risk under standard statistical assumptions.

The following result (proof in Section E.1) shows that, as long as the empirical risk L^N\widehat{L}_{N} satisfies strong convexity and bounded solution conditions (similar as in Theorem 4), transformers can approximately implement the ERM predictor g(⟨xN+1,wGLM⟩)g(\left\langle{\mathbf{x}}_{N+1},{\mathbf{w}}_{\rm GLM}\right\rangle), with wGLM{\mathbf{w}}_{\rm GLM} given by Eq. ICGLM.

For any 0<α<β0<\alpha<\beta with κ:=βα\kappa\mathrel{\mathop{:}}=\frac{\beta}{\alpha}, Bw>0,Bx>0B_{w}>0,B_{x}>0, κw:=LgBx2/α+1\kappa_{w}:=L_{g}B_{x}^{2}/\alpha+1 and ε<Bw/2\varepsilon<B_{w}/2, there exists an attention-only transformer TFθ0{\rm TF}^{0}_{\bm{\theta}} with

(where Lg:=sup⁡t∣g′(t)∣L_{g}\mathrel{\mathop{:}}=\sup_{t}\left|g^{\prime}(t)\right|, R:=max⁡{BxBw,By,1}R\mathrel{\mathop{:}}=\max{\{B_{x}B_{w},B_{y},1\}}, and Cg>0C_{g}>0 is a constant that depends only on RR and the C2C^{2}-smoothness of gg within [−R,R][-R,R]), such that the following holds. On any input data (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) such that

TFθ0(H(0)){\rm TF}^{0}_{\bm{\theta}}({\mathbf{H}}^{(0)}) approximately implements Eq. ICGLM: We have hN+1(L+1):=[xN+1;y^N+1;w^;1;1]{\mathbf{h}}^{(L+1)}_{N+1}\mathrel{\mathop{:}}=[{\mathbf{x}}_{N+1};\widehat{y}_{N+1};\widehat{{\mathbf{w}}};1;1], where

In Theorem 7, the number of heads scales as O~(1/ε2)\widetilde{\mathcal{O}}(1/\varepsilon^{2}) as opposed to Θ(1)\Theta(1) as in ridge regression (Theorem 4), due to the fact that the gradient of the loss is in general a smooth function that can be only approximately expressed as a sum-of-relus (cf. Definition 12 & Lemma A.0) rather than exactly expressed as in the case for the square loss.

w^\widehat{{\mathbf{w}}} achieves small excess risk under the population loss, i.e. for the linear prediction y^N+1lin:=⟨xN+1,w^⟩\widehat{y}^{\sf lin}_{N+1}\mathrel{\mathop{:}}=\left\langle{\mathbf{x}}_{N+1},\widehat{{\mathbf{w}}}\right\rangle,

Above, O(⋅)\mathcal{O}\left(\cdot\right) hides constants that depend polynomially on the parameters in Assumption. Similar as in Corollary 5, the O(d/N)\mathcal{O}(d/N) excess risk obtained here matches the optimal (fast) rate for typical learning problems with dd parameters and NN samples .

Applying Theorem 8 to logistic regression, we have the following result as a direct corollary. Below, the Gaussian input assumption is for convenience only and can be genearalized to e.g. sub-Gaussian input.

Consider any in-context data distribution P\mathsf{P} satisfying

For the link function g=σlogg=\sigma_{{\rm log}} and Bw⋆=O(1)B_{w}^{\star}=\mathcal{O}\left(1\right), we can choose Bw,Bμ,μg,Lg,μx,Kx,Ky=Θ(1)B_{w},B_{\mu},\mu_{g},L_{g},\mu_{x},K_{x},K_{y}=\Theta\left(1\right) so that Assumption holds. In that case, when N≥O(d)N\geq\mathcal{O}\left(d\right), there exists a transformer θ{\bm{\theta}} with L=O(log⁡(N))L=\mathcal{O}\left(\log(N)\right) layers, such that for any P\mathsf{P} considered above,

The estimation w^=read~w(TFθ(H))\widehat{{\mathbf{w}}}=\widetilde{\sf read}_{\sf w}({\rm TF}_{\bm{\theta}}({\mathbf{H}})) outputted by θ{\bm{\theta}} achieves excess risk bound Eq. 8.

(Realizable setting) Consider the logistic in-context data distribution

Then, for any distribution P=Pβlog\mathsf{P}=\mathsf{P}^{\sf log}_{{\bm{\beta}}} with ∥β∥2≤Bw⋆\left\|{{\bm{\beta}}}\right\|_{2}\leq B_{w}^{\star}, the prediction y^N+1=read~y(TFθ(H))\widehat{y}_{N+1}=\widetilde{\sf read}_{\sf y}({\rm TF}_{{\bm{\theta}}}({\mathbf{H}})) of θ{\bm{\theta}} additionally achieves the square loss excess risk Eq. 9.

3 In-context Lasso

We show that transformers can also approximate in-context Lasso with a mild number of layers, and can perform sparse linear regression in standard sparse linear models (proofs in Appendix F).

For any λN≥0\lambda_{N}\geq 0, β>0\beta>0, Bw>0B_{w}>0, and ε>0\varepsilon>0, there exists a LL-layer transformer TFθ{\rm TF}_{\bm{\theta}} with

For any d,N≥1,δ>0,Bw⋆,σ>0d,N\geq 1,\delta>0,B_{w}^{\star},\sigma>0, there exists a O~((Bw⋆)2/σ2×(1+(d/N)))\widetilde{\mathcal{O}}((B_{w}^{\star})^{2}/\sigma^{2}\times(1+(d/N)))-layer transformer θ{\bm{\theta}} such that the following holds: For any ss and N≥O(slog⁡(d/δ))N\geq\mathcal{O}\left(s\log(d/\delta)\right), suppose that P\mathsf{P} is an ss-sparse linear model: xi∼N(0,Id){\mathbf{x}}_{i}\sim\mathsf{N}(0,{\mathbf{I}}_{d}), yi=⟨w⋆,xi⟩+N(0,σ2)y_{i}=\left\langle{\mathbf{w}}_{\star},{\mathbf{x}}_{i}\right\rangle+\mathsf{N}(0,\sigma^{2}) for any ∥w⋆∥2≤Bw⋆\left\|{{\mathbf{w}}_{\star}}\right\|_{2}\leq B_{w}^{\star} and ∥w⋆∥0≤s\left\|{{\mathbf{w}}_{\star}}\right\|_{0}\leq s, then with probability at least 1−δ1-\delta (over the randomness of D\mathcal{D}), the transformer output y^N+1\widehat{y}_{N+1} achieves

The O~(slog⁡d/N)\widetilde{\mathcal{O}}(s\log d/N) excess risk obtained in Theorem 11 is optimal up to log factors . We remark that Theorem 11 is not a direct corollary of Theorem 10; Rather, the bound on the number of layers in Theorem 11 requires a sharper convergence analysis of the Eq. ICLasso problem under sparse linear models (Section F.2), similar to .

4 Gradient descent on two-layer neural networks

Thus far, we have focused on convex risks with (generalized) linear predictors of the form x↦⟨w,x⟩{\mathbf{x}}\mapsto\left\langle{\mathbf{w}},{\mathbf{x}}\right\rangle. To move beyond both restrictions, as a primary example, we show that transformers can approximate in-context gradient descent on two-layer neural networks (NNs).

5 Mechanism: In-context gradient descent

Technically, the constructions in Section 3.1-3.3 rely on a new efficient construction for transformers to implement in-context gradient descent and its variants, which we present as follows. We begin by presenting the result for implementing (vanilla) gradient descent on convex empirical risks.

such that sup⁡z∈[−R,R]k∣g(z)−fM,C(z)∣≤εapprox\sup_{{\mathbf{z}}\in[-R,R]^{k}}|g({\mathbf{z}})-f_{M,C}({\mathbf{z}})|\leq\varepsilon_{\rm approx}.

Definition 12 is known to contain broad class of functions. For example, any mildly smooth kk-variate function is approximable by a sum of relus for any (εapprox,R)(\varepsilon_{\rm approx},R), with mild bounds on (M,C)(M,C) (Proposition A.0, building on results of Bach ). Also, any function that is a (M,C)(M,C)-sum of relus itself (which includes all piecewise linear functions) is by definition (0,∞,M,C)(0,\infty,M,C)-approximable by sum of relus.

We show that LL steps of Eq. ICGD can be approximately implemented by an (L+1)(L+1)-layer transformer.

Fix any Bw>0B_{w}>0, L>1L>1, η>0\eta>0, and ε≤Bw/(2L)\varepsilon\leq B_{w}/(2L). Suppose that

TFθ0(H(0)){\rm TF}^{0}_{\bm{\theta}}({\mathbf{H}}^{(0)}) approximately implements Eq. ICGD with initialization wGD0=0{\mathbf{w}}^{0}_{{\rm GD}}={\mathbf{0}}:

(Prediction space) The final output H(L+1)=TFθ0(H(0)){\mathbf{H}}^{(L+1)}={\rm TF}^{0}_{\bm{\theta}}({\mathbf{H}}^{(0)}) approximates the prediction of LL steps of Eq. ICGD: We have hN+1(L+1)=[xN+1;y^N+1;w^L;0D−2d−3;1;ti]{\mathbf{h}}^{(L+1)}_{N+1}=[{\mathbf{x}}_{N+1};\widehat{y}_{N+1};\widehat{{\mathbf{w}}}^{L};\mathbf{0}_{D-2d-3};1;t_{i}], where y^N+1=⟨w^L,xN+1⟩\widehat{y}_{N+1}=\left\langle\widehat{{\mathbf{w}}}^{L},{\mathbf{x}}_{N+1}\right\rangle so that

Further, the transformer admits norm bound ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤2+R+2ηC{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 2+R+2\eta C.

The proof can be found in Section C.3. Theorem 13 substantially generalizes that of von Oswald et al. (which only does GD on square losses with a linear self-attention), and is simpler than the ones in Akyürek et al. and Giannou et al. . See Fig. 3 for a pictorial illustration of the basic component of the construction, which implements a single step of gradient descent using a single attention layer (Proposition C.0).

Technically, we utilize the stability of convex gradient descent as in the following lemma (proof in Section C.4) to obtain the linear error accumulation in Theorem 13; the error accumulation will become exponential in LL in the non-convex case in general; see Lemma G.0(b).

In-context algorithm selection

We now show that transformers can perform various kinds of in-context algorithm selection, which allows them to implement more complex ICL procedures by adaptively selecting different “base” algorithms on different input sequences. We construct two general mechanisms: Post-ICL validation, and Pre-ICL testing; See Fig. 1 for a pictorial illustration.

denote the validation loss of any predictor ff.

We show that (proof in Section H.1) a 3-layer transformer can output a predictor f^\widehat{f} that achieves nearly the smallest validation loss, and thus nearly optimal expected loss if L^val\widehat{L}_{{\sf val}} concentrates around the expected loss LL. Below, the input sequence H{\mathbf{H}} uses a generalized positional encoding pi:=[0D−(d+3);1;ti]{\mathbf{p}}_{i}\mathrel{\mathop{:}}=[{\mathbf{0}}_{D-(d+3)};1;t_{i}] in Eq. 3, where ti:=1t_{i}\mathrel{\mathop{:}}=1 for i∈Dtraini\in\mathcal{D}_{{\sf train}}, ti:=−1t_{i}\mathrel{\mathop{:}}=-1 for i∈Dvali\in\mathcal{D}_{{\sf val}}, and tN+1:=0t_{N+1}\mathrel{\mathop{:}}=0.

As an example, we use Proposition 15 to construct a transformer to perform in-context ridge regression with regularization selection according to the unregularized validation loss L^val(w):=12∣Dval∣∑(xi,yi)∈Dval(⟨w,xi⟩−yi)2\widehat{L}_{{\sf val}}({\mathbf{w}})\mathrel{\mathop{:}}=\frac{1}{2|\mathcal{D}_{{\sf val}}|}\sum_{(x_{i},y_{i})\in\mathcal{D}_{\sf val}}{\left(\left\langle{\mathbf{w}},{\mathbf{x}}_{i}\right\rangle-y_{i}\right)}^{2} (proof in Section H.2). Let λ1,…,λK≥0\lambda_{1},\dots,\lambda_{K}\geq 0 be KK fixed regularization strengths.

There exists a transformer with O(log⁡(1/ε))\mathcal{O}(\log(1/\varepsilon)) layers and O(K)\mathcal{O}(K) heads such that the following holds: On any (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) well-conditioned (cf. Eq. 5) for all {λk}k∈[K]{\{\lambda_{k}\}}_{k\in[K]}, it outputs y^N+1=⟨w^,xN+1⟩\widehat{y}_{N+1}=\left\langle\widehat{{\mathbf{w}}},{\mathbf{x}}_{N+1}\right\rangle, where

Above, w^ridge,trainλ\widehat{{\mathbf{w}}}_{{\rm ridge},{\sf train}}^{\lambda} denotes the solution to Eq. ICRidge on the training split Dtrain\mathcal{D}_{{\sf train}}, and γ′:=2(BxBw+By)Bxε+γ\gamma^{\prime}\mathrel{\mathop{:}}=2(B_{x}B_{w}+B_{y})B_{x}\varepsilon+\gamma, where Bx,Bw,ByB_{x},B_{w},B_{y} are the bounds in the well-conditioned assumption Eq. 5.

1.1 Nearly Bayes-optimal ICL on noisy linear models with mixed noise levels

We build on Theorem 16 to show that transformers can perform nearly Bayes-optimal ICL when data come from noisy linear models with a mixture of KK different noise levels σ1,…,σK>0\sigma_{1},\dots,\sigma_{K}>0.

Concretely, consider the following data generating model, where we first sample P=Pw⋆,σk∼π\mathsf{P}=\mathsf{P}_{{\mathbf{w}}_{\star},\sigma_{k}}\sim\pi from k∼Λ∈Δ([K])k\sim\Lambda\in\Delta([K]), w⋆∼N(0,Id/d){\mathbf{w}}_{\star}\sim\mathsf{N}({\mathbf{0}},{\mathbf{I}}_{d}/d), and then sample data {(xi,yi)}i∈[N+1]∼iidPw⋆,σk{\{({\mathbf{x}}_{i},y_{i})\}}_{i\in[N+1]}\stackrel{{\scriptstyle\rm iid}}{{\sim}}\mathsf{P}_{{\mathbf{w}}_{\star},\sigma_{k}} as

For any fixed (N,d)(N,d), consider the Bayes risk for predicting yN+1y_{N+1} under this model:

By standard Bayesian calculations, the above Bayes risk is attained when A\mathcal{A} is a certain mixture of KK ridge regressions with regularization λk=dσk2/N\lambda_{k}=d\sigma_{k}^{2}/N; however, the mixing weights depend on D\mathcal{D} in a highly non-trivial fashion (see Section I.2 for a derivation). By using the post-ICL validation mechanism in Theorem 16, we construct a transformer that achieves nearly the Bayes risk.

For sufficiently large N,dN,d, there exists a transformer with O(log⁡N)\mathcal{O}(\log N) layers and O(K)\mathcal{O}(K) heads such that on the above model, it outputs a prediction y^N+1\widehat{y}_{N+1} that is nearly Bayes-optimal:

In particular, Theorem 17 applies in the proportional setting where N,dN,d are large and N/d=Θ(1)N/d=\Theta(1) , in which case BayesRiskπ=Θ(1){\sf BayesRisk}_{\pi}=\Theta(1), and thus the transformer achieves vanishing excess risk relative to the Bayes risk at large NN. This substantially strengthens the results of Akyürek et al. , who empirically find that transformers can achieve nearly Bayes risk under any fixed noise level. By contrast, Theorem 17 shows that a single transformer can achieve nearly Bayes risk even under a mixture of KK noise levels, with quantitative guarantees. Also, our proof in fact gives a stronger guarantee: The transformer approaches the individual Bayes risks on all KK noise levels simultaneously (in addition to the overall Bayes risk for k∼Λk\sim\Lambda as in Theorem 17). We demonstrate this empirically in Section 6 (cf. Fig. 4 & 2).

As another example of the post-ICL validation mechanism, we construct a transformer that can learn a generalized linear model with adaptively chosen link function for the particular ICL instance; see Theorem I.0.

2 Pre-ICL testing mechanism

In our second mechanism, pre-ICL testing, the transformer runs a distribution testing procedure on the input sequence to determine the right ICL algorithm to use. While the test (and thus the mechanism itself) could in principle be general, we focus on cases where the test amounts to computing some simple summary statistics of the input sequence.

To showcase pre-ICL testing, we consider the toy problem of selecting between in-context regression and in-context classification, by running the following binary type check on the input labels {yi}i∈[N]{\{y_{i}\}}_{i\in[N]}.

There exists a single attention layer with 6 heads that implements Ψbinary\Psi^{\sf binary} exactly.

Using this test, we construct a transformer that performs logistic regression when labels are binary, and linear regression with high probability if the label admits a continuous distribution.

There exists a transformer with O(log⁡(1/ε))\mathcal{O}(\log(1/\varepsilon)) layers such that the following holds: On any D\mathcal{D} such that yi∈{0,1}y_{i}\in{\{0,1\}}, it outputs y^N+1\widehat{y}_{N+1} that ε\varepsilon-approximates the prediction of in-context logistic regression.

By contrast, for any distribution P\mathsf{P} whose marginal distribution of yy is not concentrated around {0,1}{\{0,1\}}, with high probability (over D\mathcal{D}), y^N+1\widehat{y}_{N+1} ε\varepsilon-approximates the prediction of in-context least squares.

The proofs can be found in Section H.3. We additionally show that transformers can implement more complex tests such as a linear correlation test, which can be useful in certain scenarios such as “confident linear regression” (predict only when the signal-to-noise ratio is high); see Section H.4.

Analysis of pretraining

Thus far, we have established the existence of transformers for performing various ICL tasks with good in-context statistical performance. We now analyze the sample complexity of pretraining these transformers from a finite number of training ICL instances.

Above, clipBy(t):=max⁡{min⁡{t,By},−By}\mathsf{clip}_{B_{y}}(t)\mathrel{\mathop{:}}=\max{\{\min{\{t,B_{y}\}},-B_{y}\}} is the standard clipping operator onto [−By,By][-B_{y},B_{y}], and TFθR{\rm TF}^{R}_{{\bm{\theta}}} the transformer architecture as in Definition 3 with clipping operators after each layer: let H(0)=clipR(H){\mathbf{H}}^{(0)}=\mathsf{clip}_{\sf R}({\mathbf{H}}),

The clipping operator is used to control the Lipschitz constant of TFθ{\rm TF}_{\bm{\theta}} with respect to θ{\bm{\theta}}, and we typically choose a sufficiently large clipping radius R{\sf R} so that it does not modify the behavior of the transformer on any input sequence of our concern.

We draw ICL instances Z:=(H,yN+1)=(D,(xN+1,yN+1)){\mathbf{Z}}\mathrel{\mathop{:}}=({\mathbf{H}},y_{N+1})=(\mathcal{D},({\mathbf{x}}_{N+1},y_{N+1})) from a (meta-)distribution denoted as π\pi, which first sample an in-context data distribution P∼π\mathsf{P}\sim\pi, then sample iid examples (xi,yi)i=1N+1∼iidP⊗(N+1)({\mathbf{x}}_{i},y_{i})_{i=1}^{N+1}\stackrel{{\scriptstyle\rm iid}}{{\sim}}\mathsf{P}^{\otimes(N+1)} and form D={(xi,yi)}i∈[N]\mathcal{D}={\{({\mathbf{x}}_{i},y_{i})\}}_{i\in[N]}. Our pretraining loss is the average ICL loss on nn pretraining instances Z(1:n)∼iidπ{\mathbf{Z}}^{(1:n)}\stackrel{{\scriptstyle\rm iid}}{{\sim}}\pi, and we consider the corresponding test ICL loss on a new test instance:

Our pretraining algorithm is to solve a standard constrained empirical risk minimization (ERM) problem over transformers with LL layers, MM heads, and norm bound BB (recall the definition of the ∣ ⁣∣ ⁣∣⋅∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{\cdot}\right|\!\right|\!\right|} norm in Eq. 2):

By standard uniform concentration analysis via chaining arguments (Proposition A.0; see also [84, Chapter 5] for similar arguments), we have the following excess loss guarantee for Eq. TF-ERM. The proof can be found in Section J.2.

With probability at least 1−ξ1-\xi (over the pretraining instances {Zj}j∈[n]{\{{\mathbf{Z}}^{j}\}}_{j\in[n]}), the solution θ^\widehat{{\bm{\theta}}} to Eq. TF-ERM satisfies

where ι=log⁡(2+max⁡{B,R,By})\iota=\log(2+\max{\left\{B,{\sf R},B_{y}\right\}}) is a log factor.

2 Examples of pretraining for in-context regression problems

In Theorem 20, the comparator inf⁡θ∈ΘL,M,D′,BLicl(θ)\inf_{{\bm{\theta}}\in\Theta_{L,M,D^{\prime},B}}L_{{\sf icl}}({\bm{\theta}}) is simply the smallest expected ICL loss for ICL instances drawn from π\pi, among all transformers within the norm ball ΘL,M,D′,B\Theta_{L,M,D^{\prime},B}. Using our constructions in Section 3 & 4, we show that this comparator loss is small on various (meta-)distribution π\pi’s, by which we obtain end-to-end guarantees for pretraining transformers with small ICL loss at test time. Here we showcase this argument on several representative regression problems.

Suppose P∼π\mathsf{P}\sim\pi is almost surely well-posed for in-context linear regression (Assumption) with the canonical parameters. Then, for N≥O~(d)N\geq\widetilde{\mathcal{O}}{\left(d\right)}, with probability at least 1−ξ1-\xi (over the training instances Z(1:n){\mathbf{Z}}^{(1:n)}), the solution θ^\widehat{{\bm{\theta}}} of Eq. TF-ERM with L=O(κlog⁡(κN/σ))L=\mathcal{O}(\kappa\log(\kappa N/\sigma)) layers, M=3M=3 heads, D′=0D^{\prime}=0 (attention-only), and B=O(κd)B=\mathcal{O}(\sqrt{\kappa d}) achieves small excess ICL risk over wP⋆{\mathbf{w}}^{\star}_{\mathsf{P}}:

where O~(⋅)\widetilde{\mathcal{O}}(\cdot) only hides polylogarithmic factors in κ,N,1/σ\kappa,N,1/\sigma.

To our best knowledge, Theorem 21 offers the first end-to-end result for pretraining a transformer to perform in-context linear regression with explicit excess loss bounds. The O~(κ2d2/n)\widetilde{\mathcal{O}}(\sqrt{\kappa^{2}d^{2}/n}) term originates from the generalization of pretraining (Theorem 20), where as the O~(dσ2/N)\widetilde{\mathcal{O}}(d\sigma^{2}/N) term agrees with the standard fast rate for the excess loss of linear regression . Further, as long as n≥O~(κ2N/σ2)n\geq\widetilde{\mathcal{O}}(\kappa^{2}N/\sigma^{2}), the excess risk achieves the optimal rate O~(dσ2/N)\widetilde{\mathcal{O}}(d\sigma^{2}/N) (up to log factors).

By similar arguments as in the proof of Theorem 21, we can directly turn most of our other expressivity results into results on the pretrained transformers. Here we present three such additional examples (proofs in Section J.4-J.6). The first example is for the sparse linear regression problem considered in Theorem 11.

Suppose each P∼π\mathsf{P}\sim\pi is almost surely an instance of the sparse linear model specified in Theorem 11 with parameters Bw⋆B_{w}^{\star} and σ\sigma. Suppose N≥O~(slog⁡((d∨N)/σ))N\geq\widetilde{\mathcal{O}}{\left(s\log((d\vee N)/\sigma)\right)} and let κ:=Bw⋆/σ\kappa\mathrel{\mathop{:}}=B_{w}^{\star}/\sigma.

Then with probability at least 1−ξ1-\xi (over the training instances Z(1:n){\mathbf{Z}}^{(1:n)}), the solution θ^\widehat{{\bm{\theta}}} of Eq. TF-ERM with L=O~(κ2(1+d/N))L=\widetilde{\mathcal{O}}(\kappa^{2}(1+d/N)) layers, M=2M=2 heads, D′=2dD^{\prime}=2d, and B=O~(poly(d,Bw⋆,σ))B=\widetilde{\mathcal{O}}{\left({\rm poly}(d,B_{w}^{\star},\sigma)\right)} achieves small excess ICL risk:

where O~(⋅)\widetilde{\mathcal{O}}(\cdot) only hides polylogarithmic factors in d,N,1/σd,N,1/\sigma.

Our next example is for the problem of noisy linear regression with mixed noise levels considered in Theorem 17 and Theorem I.0. There, the constructed transformer uses the post-ICL validation mechanism to perform ridge regression with an adaptive regulariation strength depending on the particular input sequence.

Suppose π\pi is the data generating model (noisy linear model with mixed noise levels) considered in Theorem I.0, with σmax⁡≤O(1)\sigma_{\max}\leq\mathcal{O}(1). Let N≥d/10N\geq d/10.

Then, with probability at least 1−ξ1-\xi (over the training instances Z(1:n){\mathbf{Z}}^{(1:n)}), the solution θ^\widehat{{\bm{\theta}}} of Eq. TF-ERM with input dimension D=Θ(dK)D=\Theta(dK), L=O(σmin⁡−2log⁡(N/σmin⁡))L=\mathcal{O}(\sigma_{\min}^{-2}\log(N/\sigma_{\min})) layers, M=O(K)M=\mathcal{O}(K) heads, D′=O(K2)D^{\prime}=\mathcal{O}(K^{2}), and B=O(poly(K,σmin⁡−1,d,N))B=\mathcal{O}({\rm poly}(K,\sigma_{\min}^{-1},d,N)) achieves small excess ICL risk:

where O~(⋅)\widetilde{\mathcal{O}}(\cdot) only hides polylogarithmic factors in d,N,K,1/σmin⁡d,N,K,1/\sigma_{\min}.

Our final example is for in-context logistic regression. For simplicity we consider the realizable case.

Suppose for P∼π\mathsf{P}\sim\pi, P\mathsf{P} is almost surely a realizable logistic model (i.e. P=Pβlog\mathsf{P}=\mathsf{P}^{\sf log}_{{\bm{\beta}}} with ∥β∥2≤Bw⋆\left\|{{\bm{\beta}}}\right\|_{2}\leq B_{w}^{\star} as in Corollary 9). Suppose that Bw⋆=O(1)B_{w}^{\star}=\mathcal{O}\left(1\right) and N≥O(d)N\geq\mathcal{O}\left(d\right).

Then, with probability at least 1−ξ1-\xi (over the training instances Z(1:n){\mathbf{Z}}^{(1:n)}), the solution θ^\widehat{{\bm{\theta}}} of Eq. TF-ERM with L=O(log⁡(N))L=\mathcal{O}(\log(N)) layers, M=O~(d3N)M=\widetilde{\mathcal{O}}\left(d^{3}N\right) heads, D′=0D^{\prime}=0, and B=O(poly(d,N))B=\mathcal{O}({\rm poly}(d,N)) achieves small excess ICL risk:

where O~(⋅)\widetilde{\mathcal{O}}(\cdot) only hides polylogarithmic factors in d,Nd,N.

All results above are established by the expressivity results in Section 3 & 4 for transformers to implement various ICL procedures (such as least squares, Lasso, GLM, and ridge regression with in-context algorithm selection), combined with the generalization bound (Theorem 20). However, the transformer itself was not specified to encode any actual structure about the problem at hand in any result above, other than having sufficiently large number of layers, number of heads, and weight norms, which illustrates the flexibility of the transformer architecture.

Experiments

We test our theory by studying the ICL and in-context algorithm selection capabilities of transformers, using the encoder-based architecture in our theoretical constructions (Definition 3). Additional experimental details can be found in Section K.1.

We train a 12-layer transformer, with two modes for the training sequence (instance) distribution π\pi. In the “base” mode, similar to , we sample the training instances from one of the following base distributions (tasks), where we first sample P=Pw⋆∼π\mathsf{P}=\mathsf{P}_{{\mathbf{w}}_{\star}}\sim\pi by sampling w⋆∼N(0,Id/d){\mathbf{w}}_{\star}\sim\mathsf{N}({\mathbf{0}},{\mathbf{I}}_{d}/d), and then sample {(xi,yi)}i∈[N+1]∼iidPw⋆{\{({\mathbf{x}}_{i},y_{i})\}}_{i\in[N+1]}\stackrel{{\scriptstyle\rm iid}}{{\sim}}\mathsf{P}_{{\mathbf{w}}_{\star}} as xi∼iidN(0,Id){\mathbf{x}}_{i}\stackrel{{\scriptstyle\rm iid}}{{\sim}}\mathsf{N}({\mathbf{0}},{\mathbf{I}}_{d}), and yiy_{i} from one of the following models studied in Section 3:

Linear model: yi=⟨w⋆,xi⟩y_{i}=\left\langle{\mathbf{w}}_{\star},{\mathbf{x}}_{i}\right\rangle;

Noisy linear model: yi=⟨w⋆,xi⟩+σziy_{i}=\left\langle{\mathbf{w}}_{\star},{\mathbf{x}}_{i}\right\rangle+\sigma z_{i}, where σ>0\sigma>0 is a fixed noise level, and zi∼N(0,1)z_{i}\sim\mathsf{N}(0,1).

Sparse linear model: yi=⟨w⋆,xi⟩y_{i}=\left\langle{\mathbf{w}}_{\star},{\mathbf{x}}_{i}\right\rangle with ∥w⋆∥0≤s\left\|{{\mathbf{w}}_{\star}}\right\|_{0}\leq s, where s<ds<d is a fixed sparsity level, and in this case we sample w⋆{\mathbf{w}}_{\star} from a special prior supported on ss-sparse vectors;

Linear classification model: yi=sign⁡(⟨w⋆,xi⟩)y_{i}=\operatorname{sign}(\left\langle{\mathbf{w}}_{\star},{\mathbf{x}}_{i}\right\rangle).

These base tasks have been empirically investigated by Garg et al. , though we remark that our architecture (used in our theory) differs from theirs in several aspects, such as encoder-based architecture instead of decoder-based, and ReLU activation instead of softmax. All experiments use d=20d=20. We choose σ∈{σ1,σ2}={0.1,0.5}\sigma\in{\{\sigma_{1},\sigma_{2}\}}={\{0.1,0.5\}} and N=20N=20 for noisy linear regression, s=3s=3 and N=10N=10 for sparse linear regression, and N=40N=40 for linear regression and linear classification.

In the “mixture” mode, π\pi is the uniform mixture of two or more base distributions. We consider two representative mixture modes studied in Section 4:

Linear model + linear classification model;

Noisy linear model with four noise levels σ∈{0.1,0.25,0.5,1}\sigma\in{\{0.1,0.25,0.5,1\}}.

Transformers trained with the mixture mode will be evaluated on multiple base distributions simultaneously. When the base distributions are sufficiently diverse, a transformer performing well on all of them will likely be performing some level of in-context algorithm selection. We evaluate transformers against standard machine learning algorithms in context (for each task respectively) as baselines.

Fig. 4 shows the ICL performance of transformers on five base tasks, within each the transformer is trained on the same task. Transformers match the best baseline algorithm in four out of the five cases, except for the sparse regression task where the Transformer still outperforms least squares and matches Lasso with some choices of λ\lambda (thus utilizing sparsity to some extent). This demonstrates the strong ICL capability of the transformer architecture considered in our theory.

Fig. 4 & 4 examine the in-context algorithm selection capability of transformers, on noisy linear regression with two different noise levels (Fig. 4), and regression + classification (Fig. 4). In both figures, the transformer trained in the mixture mode (TF_alg_select) approaches the best baseline algorithm on both tasks simultaneously. By contrast, transformers trained in the base mode for one of the tasks perform well on that task but behave suboptimally on the other task as expected. The existence of TF_alg_select showcases a single transformer that performs well on multiple tasks simultaneously (and thus has to perform in-context algorithm selection to some extent), supporting our theoretical results in Section 4.

2 Decoder-based architecture & details for Figure 2

ICL capabilities have also been demonstrated in the literature for decoder-based architectures . There, the transformer can do in-context predictions at every token xi{\mathbf{x}}_{i} using past tokens {(xj,yj)}j≤i−1{\{({\mathbf{x}}_{j},{\mathbf{y}}_{j})\}}_{j\leq i-1} as training examples. Here we show that such architectures is also able to perform in-context algorithm selection at every token; For results for this architecture on “base” ICL tasks (such as those considered in Fig. 4), we refer the readers to Garg et al. .

Our setup is the same as the two “mixture” modes (linear model + linear classification model, and noisy linear models with two different noise levels) as in Section 6.1, except that the architecture is GPT-2 following Garg et al. , and the input format is changed to Eq. 15 (so that the input sequence has 2N+12N+1 tokens) without positional encodings. For every i∈[N+1]i\in[N+1], we extract the prediction y^i\widehat{y}_{i} using a linear read-out function applied on output token 2i−12i-1, and the (learnable) linear read-out function is the same across all tokens, similar as in Section 6.1. The rest of the setup (optimization, training, and evaluation) is the same as in Section 6.1 & K.1. Note that we also train on the objective Eq. 48 for all tokens averaged, instead of for the last test token as in Section 6.1.

Fig. 2 shows the results for noisy linear models with two different noise levels, and Fig. 5 shows the results for linear model + linear classification model. We observe that at every token, In both cases, TF_alg_select nearly matches the strongest baseline for both tasks simultaneously, whereas transformers trained on a single task perform suboptimally on the other task. Further, this phenomenon consistently shows up at every token. For example, in Fig. 2 & 2, TF_alg_select matches ridge regression with the optimal λ\lambda on all tokens i∈{1,…,N}i\in{\{1,\dots,N\}} (N=40N=40). In Fig. 5 & 5, TF_alg_select matches least squares on the regression task and logistic regression on the classification task on all tokens i∈[N]i\in[N]. This demonstrates the in-context algorithm selection capabilities of standard decoder-based transformer architectures.

Conclusion

This work shows that transformers can perform complex in-context learning procedures with strong in-context algorithm selection capabilties, by both explicit theoretical constructions and experiments. We believe our work opens up many exciting directions, such as (1) more mechanisms for in-context algorithm selection; (2) Bayes-optimal ICL on other problems by either the post-ICL validation mechanism or new approaches; (3) understanding the internal workings of transformers performing in-context algorithm selection; (4) other mechanisms for implementing complex ICL procedures beyond in-context algorithm selection; (5) further statistical analyses, e.g. of pretraining.

Acknowledgment

The authors would like to thank Tengyu Ma and Jason D. Lee for the many insightful discussions. S. Mei is supported in part by NSF DMS-2210827 and NSF CCF-2315725.

References

Appendix A Technical tools

A.1 Concentration inequalities

Let β∼N(0,Id/d){\bm{\beta}}\sim\mathsf{N}({\mathbf{0}},{\mathbf{I}}_{d}/d). Then we have

The following lemma is a standard result of covariance concentration, see e.g. [82, Theorem 4.6.1].

Suppose that x1,⋯ ,xN{\mathbf{x}}_{1},\cdots,{\mathbf{x}}_{N} are independent dd-dimensional KK-sub-Gaussian random vectors. Then as long as N≥C0dN\geq C_{0}d, with probability at least 1−exp⁡(−N/C0)1-\exp(-N/C_{0}) we have

A.2 Approximation theory

The following result for expressing smooth functions as a random feature model with relu activation is adapted from Bach [4, Proposition 5].

where ϕ(⋅;⋅):X×W→[−B,B]\phi(\cdot;\cdot):\mathcal{X}\times\mathcal{W}\to[-B,B] is LL-Lipschitz (in dXd_{\mathcal{X}}) in the first argument, and μ\mu is a signed measure over W\mathcal{W} with finite total measure A=TV⁡(μ)<∞A=\operatorname{TV}(\mu)<\infty. Then for any ε>0\varepsilon>0, there exists α1,⋯ ,αK∈{±1}\alpha_{1},\cdots,\alpha_{K}\in{\{\pm 1\}}, w1,⋯ ,wK∈W{\mathbf{w}}_{1},\cdots,{\mathbf{w}}_{K}\in\mathcal{W} with K=O(A2B2log⁡N(X,dX,ε3AL)/ε2)K=\mathcal{O}(A^{2}B^{2}\log\mathcal{N}(\mathcal{X},d_{\mathcal{X}},\frac{\varepsilon}{3AL})/\varepsilon^{2}), such that

where N(X,dX,ε3AL)\mathcal{N}(\mathcal{X},d_{\mathcal{X}},\frac{\varepsilon}{3AL}) denotes the (ε3AL)(\frac{\varepsilon}{3AL})-covering number of X\mathcal{X} in dXd_{\mathcal{X}}.

Let α(w):=sign⁡(dμ(w))∈{±1}\alpha({\mathbf{w}})\mathrel{\mathop{:}}=\operatorname{sign}(d\mu({\mathbf{w}}))\in{\{\pm 1\}} denote the sign of the density dμ(w)d\mu({\mathbf{w}}). We have

Note that ∣dμ(w)∣/A|d\mu({\mathbf{w}})|/A is the density of a probability distribution over W\mathcal{W}. Thus for any x∈X{\mathbf{x}}\in\mathcal{X}, as long as K≥O(A2B2log⁡(1/δ)/ε2)K\geq\mathcal{O}(A^{2}B^{2}\log(1/\delta)/\varepsilon^{2}), we can sample w1,…,wK∼iid∣dμ(⋅)∣/A{\mathbf{w}}_{1},\dots,{\mathbf{w}}_{K}\stackrel{{\scriptstyle\rm iid}}{{\sim}}|d\mu(\cdot)|/A, and obtain by Hoeffding’s inequality that with probability at least 1−δ1-\delta,

Let N(ε3AL):=N(X,dX,ε3AL)\mathcal{N}(\frac{\varepsilon}{3AL})\mathrel{\mathop{:}}=\mathcal{N}(\mathcal{X},d_{\mathcal{X}},\frac{\varepsilon}{3AL}) for shorthand. By union bound, as long as K≥O(A2B2log⁡(N(ε3AL)/δ)/ε2)K\geq\mathcal{O}(A^{2}B^{2}\log(\mathcal{N}(\frac{\varepsilon}{3AL})/\delta)/\varepsilon^{2}), we have with probability at least 1−δ1-\delta that for every x^\widehat{{\mathbf{x}}} in the covering set corresponding to N(ε3AL)\mathcal{N}(\frac{\varepsilon}{3AL}),

Taking δ=1/2\delta=1/2 (for which K=O(A2B2log⁡N(ε3AL)/ε2)K=\mathcal{O}(A^{2}B^{2}\log\mathcal{N}(\frac{\varepsilon}{3AL})/\varepsilon^{2})), by the probabilistic method, there exists a deterministic set {wi}i∈[K]⊂W{\{{\mathbf{w}}_{i}\}}_{i\in[K]}\subset\mathcal{W} and {αi:=α(wi)}i∈[K]∈{±1}{\{\alpha_{i}\mathrel{\mathop{:}}=\alpha({\mathbf{w}}_{i})\}}_{i\in[K]}\in{\{\pm 1\}} such that the above holds.

Next, note that both gg (by Eq. 13) and the function x↦AK∑i=1Kα(wi)ϕ(x;wi){\mathbf{x}}\mapsto\frac{A}{K}\sum_{i=1}^{K}\alpha({\mathbf{w}}_{i})\phi({\mathbf{x}};{\mathbf{w}}_{i}) are (AL)(AL)-Lipschitz. Therefore, for any x∈X{\mathbf{x}}\in\mathcal{X}, taking x^\widehat{{\mathbf{x}}} to be the point in the covereing set with dX(x,x^)≤ε3ALd_{\mathcal{X}}({\mathbf{x}},\widehat{{\mathbf{x}}})\leq\frac{\varepsilon}{3AL}, we have

such that sup⁡z∈[−R,R]k∣f(z)−g(z)∣≤εapprox\sup_{{\mathbf{z}}\in[-R,R]^{k}}\left|f({\mathbf{z}})-g({\mathbf{z}})\right|\leq\varepsilon_{\rm approx}.

A.3 Optimization

The following convergence result for minimizing a smooth and strongly convex function is standard from the convex optimization literature, see e.g. Bubeck [13, Theorem 3.10].

The following convergence result of proximal gradient descent (PGD) on convex composite minimization problem is also standard, see e.g. .

A.4 Uniform convergence

The following result is shown in [84, Section 5.6].

Suppose that {Xθ}θ\{X_{\theta}\}_{\theta} is a zero-mean random process indexed by θ∈Θ\theta\in\Theta such that ∥Xθ−Xθ′∥ψ≤ρ(θ,θ′)\left\|X_{\theta}-X_{\theta^{\prime}}\right\|_{\psi}\leq\rho(\theta,\theta^{\prime}) for some metric ρ\rho on the space Θ\Theta. Then it holds that

where DD is the diameter of the metric space (Θ,ρ)(\Theta,\rho), and the generalized Dudley entropy integral JJ is given by

where N(δ;Θ,ρ)N(\delta;\Theta,\rho) is the δ\delta-covering number of (Θ,ρ)(\Theta,\rho).

As a corollary of Theorem A.0, we have the following result.

Suppose that {Xθ}θ∈Θ\{X_{\theta}\}_{\theta\in\Theta} is a zero-mean random process given by

The index set Θ\Theta is equipped with a distance ρ\rho and diameter DD. Further, assume that for some constant AA, for any ball Θ′\Theta^{\prime} of radius rr in Θ\Theta, the covering number admits upper bound log⁡N(δ;Θ′,ρ)≤dlog⁡(2Ar/δ)\log N(\delta;\Theta^{\prime},\rho)\leq d\log(2Ar/\delta) for all 0<δ≤2r0<\delta\leq 2r.

Then with probability at least 1−δ1-\delta, it holds that

where CC is a universal constant, and we denote κ=1+B1D/B0\kappa=1+B^{1}D/B^{0}.

Furthermore, if we replace the SG{\rm SG} in assumption (b) and (c) by SE{\rm SE}, then with probability at least 1−δ1-\delta, it holds that

Fix a D0∈(0,D]D_{0}\in(0,D] to be specified later. We pick a (D0/2)(D_{0}/2)-covering Θ0\Theta_{0} of Θ\Theta so that log⁡∣Θ0∣≤dlog⁡(2AD/D0)\log\left|\Theta_{0}\right|\leq d\log(2AD/D_{0}). Then, by the standard uniform covering of independent sub-Gaussian random variables, we have with probability at least 1−δ/21-\delta/2,

Assume that Θ0={θ1,⋯ ,θn}\Theta_{0}=\{\theta_{1},\cdots,\theta_{n}\}. For each j∈[n]j\in[n], we consider Θj\Theta_{j} is the ball centered at θj\theta_{j} of radius D0D_{0} in (Θ,ρ)(\Theta,\rho). Then θ∈Θj\theta\in\Theta_{j} has diameter D0D_{0} and admits covering number bound log⁡N(Θj,δ)≤dlog⁡(AD0/δ)\log\mathcal{N}(\Theta_{j},\delta)\leq d\log(AD_{0}/\delta). Hence, we can apply Theorem A.0 with the process {Xθ}θ∈Θj\{X_{\theta}\}_{\theta\in\Theta_{j}}, then

Therefore, we can let t≤log⁡(2n/δ)/Nt\leq\sqrt{\log(2n/\delta)/N} in the above inequality and taking the union bound over j∈[n]j\in[n], and hence with probability at least 1−δ/21-\delta/2, it holds that for all j∈[n]j\in[n],

Notice that for each θ∈Θ,\theta\in\Theta, there exists j∈[n]j\in[n] such that θ∈Θj\theta\in\Theta_{j}, and hence

Thus, with probability at least 1−δ1-\delta, it holds

Taking D0=D/κD_{0}=D/\kappa completes the proof of SG{\rm SG} case.

We next consider the SE{\rm SE} case. The idea is the same as the SG{\rm SG} case, but in this case we need to consider the following Orlicz-norm:

Then Bernstein’s inequality of SE{\rm SE} random variables yields

for some universal constant C0C_{0}. Therefore, we can repeat the argument above to deduce that with probability at least 1−δ1-\delta, it holds

Taking D0=D/κD_{0}=D/\kappa completes the proof. ∎

A.5 Useful properties of transformers

The following result can be obtained immediately by “joining” the attention heads and MLP layers of two single-layer transformers.

Further, θ{\bm{\theta}} has at most M≤M1+M2M\leq M_{1}+M_{2} heads, D′≤D1′+D2′D^{\prime}\leq D^{\prime}_{1}+D^{\prime}_{2} hidden dimension in its MLP layer, and norm bound ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤∣ ⁣∣ ⁣∣θ1∣ ⁣∣ ⁣∣+∣ ⁣∣ ⁣∣θ2∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq{\left|\!\left|\!\left|{{\bm{\theta}}_{1}}\right|\!\right|\!\right|}+{\left|\!\left|\!\left|{{\bm{\theta}}_{2}}\right|\!\right|\!\right|}.

When L1=L2L_{1}=L_{2} (θ1{\bm{\theta}}_{1} and θ2{\bm{\theta}}_{2} have the same number of layers), the result follows directly by applying Proposition A.0 repeatedly for all L1L_{1} layers and the definition of the norm Eq. 2.

If (without loss of generality) L1<L2L_{1}<L_{2}, we can augment θ1{\bm{\theta}}_{1} to L2L_{2} layers by adding (L2−L1)(L_{2}-L_{1}) layers with zero attention heads, and zero MLP hidden dimension (note that this does not change M1M_{1}, D1′D_{1}^{\prime}, and ∣ ⁣∣ ⁣∣θ1∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}_{1}}\right|\!\right|\!\right|}). Due to the residual structure, the transformer maintains the output P1(H1)P_{1}({\mathbf{H}}_{1}) throughout layer L1+1,…,L2L_{1}+1,\dots,L_{2}, and it reduces to the case L1=L2L_{1}=L_{2}. ∎

Appendix B Extension to decoder-based architecture

Decoder TFs are the same as encoder TFs, except that the attention layers are replaced by masked attention layers with a specific decoder-based (causal) attention mask.

Notice that standard masked attention definitions use the pre-activation additive masks (with mask value −∞-\infty) . The post-activation multiplicative masks we use is equivalent to the pre-activation additive masks, and the modified presentation is for notational convenience. We also use a normalized ReLU activation t↦σ(t)/it\mapsto\sigma(t)/i in place of the standard softmax activation to be consistent with Definition 1. Note that the normalization 1/i1/i is to ensure that the attention weights {σ(⟨Qmhi,Kmhj⟩)/i}j∈[i]{\{\sigma(\left\langle{\mathbf{Q}}_{m}{\mathbf{h}}_{i},{\mathbf{K}}_{m}{\mathbf{h}}_{j}\right\rangle)/i\}}_{j\in[i]} is a set of non-negative weights that sum to O(1)O(1). The motivation of masked attention layer is to ensure that, when processing a sequence of tokens, the computations at any token do not see any later token.

We next define the decoder-based transformers with L≥1L\geq 1 transformer layers, each consisting of a masked attention layer (c.f. Definition B.0) followed by an MLP layer (c.f. Definition 2). This definition is similar to the definition of encoder-based transformers (c.f., Definition 3), except that we replace the attention layers by masked attention layers.

We also use Eq. 2 to define the norm of DTFθ{\rm DTF}_{\bm{\theta}}.

B.2 In-context learning with decoder-based transformers

Eq. 15 is different from out input format Eq. 3 for encoder-based TFs. The main difference is that (xi,yi)({\mathbf{x}}_{i},y_{i}) are in different tokens in (15), whereas (xi,yi)({\mathbf{x}}_{i},y_{i}) are in the same token in (3). The reason for the former (i.e., different tokens in decoder) is that we want to avoid every [xi;0][{\mathbf{x}}_{i};0] token seeing the information of yiy_{i}, since we will evaluate the loss at every token. The reason for the latter (i.e., the same token in encoder) is for presentation convenience: since we only evaluate the loss at the last token, it is not necessary to alternate between [xi;0][{\mathbf{x}}_{i};0] and [0;yi][{\mathbf{0}};y_{i}] to avoid information leakage.

B.3 Results

We discuss how our theoretical results upon encoder TFs can be converted to those of the decoder TFs. Taking the implementation of (ICGD) (a key mechanism that enables most basic ICL algorithms such as ridge regression; cf. Section 3.5) as an example, this conversion is enabled by the following facts: (a) the input format (15) of decoders can be converted to the input format (3) of encoders by a 2-layer decoder TF; (b) the encoder TF that implements (ICGD) with input format (3), by a slight parameter modification, can be converted to a decoder TF that implements the (ICGD) algorithm with a converted input format.

Despite the difference between the input format (15) and (3), we show that there exists a 2-layer decoder TF that can convert the input format (15) to format (3). The proof can be found in Section B.4.

There exists a 2-layer decoder TF DTF{\rm DTF} with 33 heads per layer, hidden dimension 22 and ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤12{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 12 such that upon taking input H{\mathbf{H}} of format (15), it outputs H~=DTF(H)\widetilde{{\mathbf{H}}}={\rm DTF}({\mathbf{H}}) with

In particular, format Eq. 16 contains format Eq. 3 as a submatrix, by restricting to the {1,2,…,D−1,D−2,D}\{1,2,\ldots,D-1,D-2,D\} rows and {2,4,…,2N−2,2N,2N+1}\{2,4,\ldots,2N-2,2N,2N+1\} columns.

The construction in Theorem 13 can be generalized to using the input format Eq. 16 along with a decoder TF, by using the scratch pad within the last token to record the gradient descent iterates. Further, if we slightly change the normalization in MSK{\rm MSK} from 1/i1/i to 1/((i−1)∨1)1/((i-1)\vee 1), then the same construction performs Eq. ICGD (with training examples {1,…,j}{\{1,\dots,j\}}) at every token i=2j+1i=2j+1 (corresponding to predicting at xj+1{\mathbf{x}}_{j+1}). Building on this extension, all our constructions in Section 3 and Section 4.2 can be generalized to decoder TFs.

B.4 Proof of Proposition B.0

In the following, we construct the desired θ=(θ(1),θ(2)){\bm{\theta}}=({\bm{\theta}}^{(1)},{\bm{\theta}}^{(2)}) as follows.

for all i,ji,j. By the structure of hi{\mathbf{h}}_{i}, these matrices indeed exist, and further it is straightforward to check that they have norm bounds

Notice that ti≠0t_{i}\neq 0 only when 2∣i2\mid i, we then compute for i=2ki=2k that

for all i,ji,j. By the structure of hi(1){\mathbf{h}}_{i}^{(1)}, these matrices indeed exist, and further it is straightforward to check that they have norm bounds

where the last equality follows from the fact that

Finally, we only need to take a MLP layer θmlp(2)=(W1(2),W2(2)){\bm{\theta}}_{\tt mlp}^{(2)}=({\mathbf{W}}_{1}^{(2)},{\mathbf{W}}_{2}^{(2)}) with hidden dimension 2 that maps

Combining the two steps above, we complete the proof of Proposition B.0. ∎

Appendix C Proofs for Section 3.5 and additional results

The definition above captures the proximal operator proxηR\mathbf{prox}_{\eta\mathcal{R}} for a broad class of regularizers, such as the (commonly-used) L1L_{1} and L2L_{2} regularizer listed in the following proposition, for all of which one can directly check that they can be exactly implemented by an MLP as stated below.

For R=λ∥⋅∥1\mathcal{R}=\lambda\left\|{\cdot}\right\|_{1}, proxηR\mathbf{prox}_{\eta\mathcal{R}} is (0,+∞,4d,4+2ηλ)(0,+\infty,4d,4+2\eta\lambda)-approximable by MLP.

For R=λ2∥⋅∥22\mathcal{R}=\frac{\lambda}{2}\left\|{\cdot}\right\|_{2}^{2}, proxηR\mathbf{prox}_{\eta\mathcal{R}} is (0,+∞,2d,2+2ηλ)(0,+\infty,2d,2+2\eta\lambda)-approximable by MLP.

Fix any Bw>0B_{w}>0, L>1L>1, η>0\eta>0, and ε+ε′≤Bw/(2L)\varepsilon+\varepsilon^{\prime}\leq B_{w}/(2L). Suppose that

R\mathcal{R} convex, and the proximal operator proxηR(w)\mathbf{prox}_{\eta\mathcal{R}}({\mathbf{w}}) is (ηε′,R′,D′,C′)(\eta\varepsilon^{\prime},R^{\prime},D^{\prime},C^{\prime})-approximable by MLP with R′=sup⁡∥w∥2≤Bw∥wη+∥2+ηεR^{\prime}=\sup_{\left\|{{\mathbf{w}}}\right\|_{2}\leq B_{w}}\left\|{{\mathbf{w}}_{\eta}^{+}}\right\|_{2}+\eta\varepsilon.

TFθ(H(0)){\rm TF}_{\bm{\theta}}({\mathbf{H}}^{(0)}) approximately implements Eq. ICGD:

(Prediction space) The final output H(L+1)=TFθ(H(0)){\mathbf{H}}^{(L+1)}={\rm TF}_{\bm{\theta}}({\mathbf{H}}^{(0)}) approximates the prediction of LL steps of Eq. ICGD: We have hN+1(L+1)=[xN+1;y^N+1;w^L;0D−2d−3;1;ti]{\mathbf{h}}^{(L+1)}_{N+1}=[{\mathbf{x}}_{N+1};\widehat{y}_{N+1};\widehat{{\mathbf{w}}}^{L};\mathbf{0}_{D-2d-3};1;t_{i}], where y^N+1=⟨w^L,xN+1⟩\widehat{y}_{N+1}=\left\langle\widehat{{\mathbf{w}}}^{L},{\mathbf{x}}_{N+1}\right\rangle so that

Further, the weight matrices have norm bounds ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤3+R+2ηC+C′{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 3+R+2\eta C+C^{\prime}.

The proof of Theorem C.0 is essentially similar to the proof of Theorem 13, using the following generalized version of Lemma 14.

The proof of the above lemma is done by utilizing the non-expansiveness of the PGD operator w↦proxηR(w−η∇f(w)){\mathbf{w}}\mapsto\mathbf{prox}_{\eta\mathcal{R}}({\mathbf{w}}-\eta\nabla f({\mathbf{w}})) and otherwise following the same arguments as for Lemma 14.

C.2 Approximating a single GD step

Then, for any ε>0\varepsilon>0, there exists an attention layer θ={(Qm,Km,Vm)}m∈[M]{\bm{\theta}}={\{({\mathbf{Q}}_{m},{\mathbf{K}}_{m},{\mathbf{V}}_{m})\}}_{m\in[M]} with MM heads such that, for any input sequence that takes form hi=[xi;yi′;w;0D−2d−3;1;ti]{\mathbf{h}}_{i}=[{\mathbf{x}}_{i};y_{i}^{\prime};{\mathbf{w}};\mathbf{0}_{D-2d-3};1;t_{i}] with ∥w∥2≤Bw\left\|{{\mathbf{w}}}\right\|_{2}\leq B_{w}, it gives output h~i=[Attnθ(H)]i=[xi;yi′;w~;0D−2d−3;1;ti]\widetilde{{\mathbf{h}}}_{i}={\left[{\rm Attn}_{\bm{\theta}}({\mathbf{H}})\right]}_{i}=[{\mathbf{x}}_{i};y_{i}^{\prime};\widetilde{{\mathbf{w}}};\mathbf{0}_{D-2d-3};1;t_{i}] for all i∈[N+1]i\in[N+1], where

Further, ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤2+R+2ηC{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 2+R+2\eta C.

for all i,j∈[N+1]i,j\in[N+1]. As the input has structure hi=[xi;yi′;w;0D−2d−3;1;ti]{\mathbf{h}}_{i}=[{\mathbf{x}}_{i};y_{i}^{\prime};{\mathbf{w}};\mathbf{0}_{D-2d-3};1;t_{i}], these matrices indeed exist, and further it is straightforward to check that they have norm bounds

Consequently, ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤2+R+2ηC{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 2+R+2\eta C.

where the last equality follows from the bound

so that the above relu equals if tj≤0t_{j}\leq 0. Therefore,

Thus letting the attention layer θ={(Vm,Qm,Km)}m∈[M]{\bm{\theta}}={\{({\mathbf{V}}_{m},{\mathbf{Q}}_{m},{\mathbf{K}}_{m})\}}_{m\in[M]}, we have

C.3 Proof of Theorem 13

We first prove part (a), which requires constructing the first LL layers of θ{\bm{\theta}}. Note that by our precondition L≤Bw/(2ε)L\leq B_{w}/(2\varepsilon).

We now prove part (b), which requires constructing the last attention layer θ(L+1){\bm{\theta}}^{(L+1)}. Recall hi(L)=[xi;yi′;w^L;0D−2d−3;1;ti]{\mathbf{h}}^{(L)}_{i}=[{\mathbf{x}}_{i};y_{i}^{\prime};\widehat{{\mathbf{w}}}^{L};\mathbf{0}_{D-2d-3};1;t_{i}] for all i∈[N+1]i\in[N+1]. We construct a 2-head attention layer θ(L+1)={(Qm(L+1),Km(L+1),Vm(L+1))}m=1,2{\bm{\theta}}^{(L+1)}={\{({\mathbf{Q}}^{(L+1)}_{m},{\mathbf{K}}^{(L+1)}_{m},{\mathbf{V}}^{(L+1)}_{m})\}}_{m=1,2} such that for every i,j∈[N+1]i,j\in[N+1],

Note that the weight matrices have norm bound

Above, (i) uses the identity t=σ(t)−σ(−t)t=\sigma(t)-\sigma(-t). Further by part (a) we have

This proves part (b), and also finishes the proof Theorem 13 where the overall (L+1)(L+1)-layer attention-only transformer is given by TFθ0{\rm TF}^{0}_{\bm{\theta}} with

C.4 Proof of Lemma 14

As ff is a convex, LfL_{f} smooth function on B2d(R)\mathsf{B}_{2}^{d}(R), the mapping Tη:w↦w−η∇f(w)\mathcal{T}_{\eta}:{\mathbf{w}}\mapsto{\mathbf{w}}-\eta\nabla f({\mathbf{w}}) is non-expansive in ∥⋅∥2\|{\cdot}\|_{2}: Indeed, for any w,w′∈B2d(R){\mathbf{w}},{\mathbf{w}}^{\prime}\in\mathsf{B}_{2}^{d}(R) we have

Above, (i) uses the property ⟨w−w′,∇f(w)−∇f(w′)⟩≥1Lf∥∇f(w)−∇f(w′)∥22\left\langle{\mathbf{w}}-{\mathbf{w}}^{\prime},\nabla f({\mathbf{w}})-\nabla f({\mathbf{w}}^{\prime})\right\rangle\geq\frac{1}{L_{f}}\|{\nabla f({\mathbf{w}})-\nabla f({\mathbf{w}}^{\prime})}\|_{2}^{2} for smooth convex functions [60, Theorem 2.1.5]; (ii) uses the precondition that η≤2/Lf\eta\leq 2/L_{f}.

The lemma then follows directly by induction on LL. The base case of L=0L=0 follows directly by assumption that w^0=wGD0∈B2d(R/2)\widehat{{\mathbf{w}}}^{0}={\mathbf{w}}^{0}_{{\rm GD}}\in\mathsf{B}_{2}^{d}(R/2). Suppose the claim holds for iterate LL. For iterate L+1≤R/(2ε)L+1\leq R/(2\varepsilon), we have

Above, (i) uses the non-expansiveness, and (ii) uses the inductive hypothesis. Similarly, by our assumption w⋆=Tη(w⋆){\mathbf{w}}^{\star}=\mathcal{T}_{\eta}({\mathbf{w}}^{\star}),

Further, the transformer admits norm bound ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤2+R+(2C+λ)η.{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 2+R+(2C+\lambda)\eta.

Appendix D Proofs for Section 3.1

Fix λ≥0\lambda\geq 0, 0≤α≤β0\leq\alpha\leq\beta with κ:=β+λα+λ\kappa\mathrel{\mathop{:}}=\frac{\beta+\lambda}{\alpha+\lambda}, and Bw>0B_{w}>0, and consider any in-context data D\mathcal{D} such that the precondition of Theorem 4 holds. Let

Consider the gradient descent algorithm on the ridge loss

with initialization, learning rate, and number of steps

By standard convergence results for strongly convex and smooth functions (Proposition A.0), we have for all t≥1t\geq 1 that

Combining Eq. 18 and Eq. 19, we obtain that

Further, we have readw(hiT)=wGDT{\sf read}_{\sf w}({\mathbf{h}}_{i}^{T})={\mathbf{w}}_{{\rm GD}}^{T} for all i∈[N+1]i\in[N+1], where readw(h):=h(d+2):(2d+1){\sf read}_{\sf w}({\mathbf{h}})\mathrel{\mathop{:}}={\mathbf{h}}_{(d+2):(2d+1)} (cf. Corollary C.0), so that ∥readw(hiT)−wridgeλ∥2≤ε/Bx\|{{\sf read}_{\sf w}({\mathbf{h}}_{i}^{T})-{\mathbf{w}}^{\lambda}_{{\rm ridge}}}\|_{2}\leq\varepsilon/B_{x} as shown above. This finishes the proof. ∎

D.2 Statistical analysis of in-context least squares

denote the expected risk of w{\mathbf{w}} over a new test example (x′,y′)∼P({\mathbf{x}}^{\prime},y^{\prime})\sim\mathsf{P}.

∥x∥2≤Bx\|{{\mathbf{x}}}\|_{2}\leq B_{x} and ∣y∣≤By\left|y\right|\leq B_{y} almost surely;

The whitened vector ΣP−1/2x{\mathbf{\Sigma}}_{\mathsf{P}}^{-1/2}{\mathbf{x}} is K2K^{2}-sub-Gaussian for some K≥1K\geq 1.

Further, we say P\mathsf{P} is well-posed with canonical parameters if

where Θ(⋅)\Theta(\cdot) and O(⋅)\mathcal{O}(\cdot) only hides absolute constants.

The following result bounds the excess risk of least squares under Assumption with a clipping operation on the predictor; the clipping allows the result to only depend on the second moment of the noise (cf. Assumption(5)) instead of e.g. its sub-Gaussianity, and also makes the result convenient to be directly translated to a result for transformers.

Suppose distribution P\mathsf{P} satisfies Assumption. Then as long as N≥O(dK4log⁡(1/δ))N\geq\mathcal{O}(dK^{4}\log(1/\delta)), we have the following:

The (clipped) least squares predictor achieves small expected excess risk (fast rate) over the best linear predictor: For any clipping radius R≥ByR\geq B_{y},

We have P(Ecov∩Ew)≥1−δ/10\mathsf{P}(E_{\rm cov}\cap E_{w})\geq 1-\delta/10, where

We first show P(Ecov)≥1−δ/20\mathsf{P}(E_{\rm cov})\geq 1-\delta/20. Let Σ^:=1N∑i=1Nxixi⊤\widehat{{\mathbf{\Sigma}}}\mathrel{\mathop{:}}=\frac{1}{N}\sum_{i=1}^{N}{\mathbf{x}}_{i}{\mathbf{x}}_{i}^{\top}, and let the whitened covariance and noise variables be denoted as

Also let zi:=yi−⟨xi,wP⋆⟩z_{i}\mathrel{\mathop{:}}=y_{i}-\left\langle{\mathbf{x}}_{i},{\mathbf{w}}^{\star}_{\mathsf{P}}\right\rangle denote the “noise” variables. Note that

Setting N≥O(K4(d+log⁡(1/δ)))N\geq\mathcal{O}(K^{4}(d+\log(1/\delta))) ensures that the right-hand side above is at most 1/21/2, on which event we have

i.e. EcovE_{\rm cov} holds. This shows that P(Ecovc)≤δ/10\mathsf{P}(E_{\rm cov}^{c})\leq\delta/10.

Next, we show Eq. 21. Using EcovE_{\rm cov}, we decompose the risk as

Above, (i) follows by conditioning on x1{\mathbf{x}}_{1} and using Assumption(5). Combining with Eq. 25, we obtain

Therefore, using an argument similar to Chebyshev’s inequality,

D.3 Proof of Corollary 5

The proof follows by first checking the well-conditionedness of the data D\mathcal{D} (cf. Eq. 5) with high probability, then invoking Theorem 4 (for approximation least squares) and Proposition D.0 (for the statistical power of least squares).

First, as P\mathsf{P} satisfies Assumption, by Proposition D.0, as long as N≥O(K4(d+log⁡(1/δ)))N\geq\mathcal{O}(K^{4}(d+\log(1/\delta))), we have with probability at least 1−δ/101-\delta/10 that event Ecov∩EwE_{\rm cov}\cap E_{w} holds. On this event, we have

and thus the dataset D\mathcal{D} is well-conditioned (in the sense of Eq. 5) with parameters α=λmin⁡/2\alpha=\lambda_{\min}/2, β=2λmax⁡\beta=2\lambda_{\max}, and BwB_{w} defined as above. Note that the condition number of Σ^\widehat{{\mathbf{\Sigma}}} is upper bounded by β/α=4λmax⁡/λmin⁡≤4κ\beta/\alpha=4\lambda_{\max}/\lambda_{\min}\leq 4\kappa, where κ\kappa is the upper bound on the condition number of ΣP{\mathbf{\Sigma}}_{\mathsf{P}} as in Assumption(c).

Note that Bw≤O(Bw⋆+By2/λmin⁡)B_{w}\leq\mathcal{O}(B_{w}^{\star}+\sqrt{B_{y}^{2}/\lambda_{\min}}) by the above choice of δ\delta.

such that on Ecov∩EwE_{\rm cov}\cap E_{w} (so that D\mathcal{D} is well-conditioned), we have (choosing the clipping radius in read~y(⋅)=clipBy(ready(⋅))\widetilde{\sf read}_{\sf y}(\cdot)=\mathsf{clip}_{B_{y}}({\sf read}_{\sf y}(\cdot)) to be ByB_{y}):

We now bound the excess risk of the above transformer. Combining Proposition D.0 and Eq. 29, we have

Above, (i) uses the approximation guarantee Eq. 29 as well as Proposition D.0(a) (with clipping radius ByB_{y}). This proves the desired excess risk guarantee.

Finally, under the canonical choice of parameters Eq. 20, the bounds for L,M,∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣L,M,{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|} simplify to

and the requirement for NN simplifies to N≥O(d+log⁡(1/δ))=O~(d)N\geq\mathcal{O}(d+\log(1/\delta))=\widetilde{\mathcal{O}}(d) (as K=Θ(1)K=\Theta(1)). This proves the claim about the required NN and LL. ∎

D.4 Proof of Corollary 6

Fix parameters δ,ε‾>0\delta,\underline{\varepsilon}>0 to be specified later and a large universal constant C0C_{0}. Let us set

and we define E:=Eπ∩Ew∩Eb∩Eb,N+1\mathcal{E}\mathrel{\mathop{:}}=\mathcal{E}_{\pi}\cap\mathcal{E}_{w}\cap\mathcal{E}_{b}\cap\mathcal{E}_{b,N+1}. Under the event E\mathcal{E}, the problem (ICRidge) is well-conditioned and ∥wridgeλ∥≤Bw/2\|{\mathbf{w}}_{\rm ridge}^{\lambda}\|\leq B_{w}/2 (by Lemma D.0).

Therefore, Theorem 4 implies that for κ=α+λβ+λ\kappa=\frac{\alpha+\lambda}{\beta+\lambda}, there exists a L=⌈2κlog⁡(Bw/ε‾)⌉+1L=\left\lceil 2\kappa\log(B_{w}/\underline{\varepsilon})\right\rceil+1-layer transformer θ{\bm{\theta}} with prediction y^N+1:=read~y(TFθ0(H))\widehat{y}_{N+1}:=\widetilde{\sf read}_{\sf y}({\rm TF}^{0}_{\bm{\theta}}({\mathbf{H}})) (clipped by ByB_{y}), such that under the good event E\mathcal{E}, we have y^N+1=clipBy(⟨xN+1,w^⟩)\widehat{y}_{N+1}=\mathsf{clip}_{B_{y}}(\left\langle{\mathbf{x}}_{N+1},\widehat{{\mathbf{w}}}\right\rangle) and ∥w^−wridgeλ∥≤ε‾\|\widehat{{\mathbf{w}}}-{\mathbf{w}}_{\rm ridge}^{\lambda}\|\leq\underline{\varepsilon}.

In the following, we show that θ{\bm{\theta}} is indeed the desired transformer (when ε‾\underline{\varepsilon} and δ\delta is suitably chosen). Notice that we have

and we analyze these two parts separately.

where the inequality is because yN+1∈[−By,By]y_{N+1}\in[-B_{y},B_{y}] under the good event E\mathcal{E}. Notice that by our construction, under the good event E\mathcal{E}, w^=w^(D)\widehat{{\mathbf{w}}}=\widehat{{\mathbf{w}}}(\mathcal{D}) depends only on the dataset D\mathcal{D}We need this, as on Ec\mathcal{E}^{c}, the transformer output at this location could in principle depend additionally on xN+1{\mathbf{x}}_{N+1}, as Eq. 17 may not hold due to the potential unbounededness of its input. A similar fact will also appear in later proofs (for generalized linear models and Lasso).. Therefore, we have ∥w^(D)−wridgeλ(D)∥≤ε‾\|\widehat{{\mathbf{w}}}(\mathcal{D})-{\mathbf{w}}_{\rm ridge}^{\lambda}(\mathcal{D})\|\leq\underline{\varepsilon} as long as the event E0:=Eπ∩Ew∩Eb\mathcal{E}_{0}\mathrel{\mathop{:}}=\mathcal{E}_{\pi}\cap\mathcal{E}_{w}\cap\mathcal{E}_{b} holds for (w⋆,D)({\mathbf{w}}_{\star},\mathcal{D}). Thus, under E0\mathcal{E}_{0},

Combining the inequalities above, we have

Therefore, it suffices to take δ=c0log⁡2(N)(ε21+σ2)2\delta=\frac{c_{0}}{\log^{2}(N)}{\left(\frac{\varepsilon^{2}}{1+\sigma^{2}}\right)}^{2} for some small constant c0c_{0}, then as long as

our choice of ε‾\underline{\varepsilon} and δ\delta is feasible. Note that κ≤O(1+σ−2)\kappa\leq\mathcal{O}\left(1+\sigma^{-2}\right), and hence under such choice of (ε‾,δ)(\underline{\varepsilon},\delta), we have L=O(log⁡(1/ε))L=O{\left(\log(1/\varepsilon)\right)} and ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣=O~(d){\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}=\widetilde{O}{\left(\sqrt{d}\right)}. This is the desired result. ∎

Under the event Eπ∩Ew\mathcal{E}_{\pi}\cap\mathcal{E}_{w}, we have \big{\|}{\mathbf{w}}_{\rm ridge}^{\lambda}\big{\|}_{2}\leq\mathcal{O}\left(B_{w}^{\star}+\sigma\right).

By the definition of wridgeλ{\mathbf{w}}_{\rm ridge}^{\lambda} and recall that λ=dσ2/N\lambda=d\sigma^{2}/N, we have wridgeλ=(X⊤X+dσ2Id)−1X⊤y{\mathbf{w}}_{\rm ridge}^{\lambda}=({\mathbf{X}}^{\top}{\mathbf{X}}+d\sigma^{2}{\mathbf{I}}_{d})^{-1}{\mathbf{X}}^{\top}{\mathbf{y}}.

Therefore, we only need to prove the following fact: for any γ>0\gamma>0 and β^=(X⊤X+dγId)−1X⊤y\widehat{\bm{\beta}}=({\mathbf{X}}^{\top}{\mathbf{X}}+d\gamma{\mathbf{I}}_{d})^{-1}{\mathbf{X}}^{\top}{\mathbf{y}}, we have

Appendix E Proofs for Section 3.2

We begin by stating our assumptions on the well-posedness of the generalized linear models.

We assume that there is some Bμ>0B_{\mu}>0 such that for any t∈[−Bμ,Bμ]t\in[-B_{\mu},B_{\mu}], g′(t)≥μg>0g^{\prime}(t)\geq\mu_{g}>0.

We also assume that for each i∈[N+1]i\in[N+1], (xi,yi)({\mathbf{x}}_{i},y_{i}) is independently sampled from P\mathsf{P} such that the following holds.

Under the law (x,y)∼P({\mathbf{x}},y)\sim\mathsf{P}, We have x∼SG(Kx){\mathbf{x}}\sim{\rm SG}(K_{x}), y∼SG(Ky)y\sim{\rm SG}(K_{y}) and g(⟨w,x⟩)∼SG(Ky) ∀w∈B2(Bw)g(\left\langle{\mathbf{w}},{\mathbf{x}}\right\rangle)\sim{\rm SG}(K_{y})~{}\forall{\mathbf{w}}\in\mathsf{B}_{2}(B_{w}).

For β⋆=arg min⁡LP{\bm{\beta}}^{\star}=\operatorname*{arg\,min}L_{\mathsf{P}}, it holds ∥β⋆∥2≤Bw/4\left\|{{\bm{\beta}}^{\star}}\right\|_{2}\leq B_{w}/4.

Let us fix parameters εg>0\varepsilon_{g}>0 and T>0T>0 (that we specify later in proof).

Define R=max⁡{BxBw,By,1}R=\max\{B_{x}B_{w},B_{y},1\} and

By Proposition A.0, gg is (εg,M,R,C)(\varepsilon_{g},M,R,C) with

Therefore, we can invoke Theorem 13 to obtain that, as long as 2Tεg≤Bw2T\varepsilon_{g}\leq B_{w}, there exists a TT-layer attention-only transformer θ(1:T){\bm{\theta}}^{(1:T)} with MM heads per layer, such that for any input H{\mathbf{H}} of format (3) and satisfies (7), its last layer outputs hi(T)=[xi;yi′;w^T;0D−2d−3;1;ti]{\mathbf{h}}_{i}^{(T)}=[{\mathbf{x}}_{i};y_{i}^{\prime};\widehat{{\mathbf{w}}}^{T};{\mathbf{0}}_{D-2d-3};1;t_{i}], such that

Furthermore, we can show that (similar to the proof of Theorem 13 (b)), there exists a single attention layer θ(T+1){\bm{\theta}}^{(T+1)} with MM heads such that it outputs hN+1(T+1)=[xN+1;y^N+1;w^T;0D−2d−3;1;0]{\mathbf{h}}_{N+1}^{(T+1)}=[{\mathbf{x}}_{N+1};\widehat{y}_{N+1};\widehat{{\mathbf{w}}}^{T};{\mathbf{0}}_{D-2d-3};1;0], where ∣y^N+1−g(⟨xN+1,w^T⟩)∣≤εg\left|\widehat{y}_{N+1}-g(\left\langle{\mathbf{x}}_{N+1},\widehat{{\mathbf{w}}}^{T}\right\rangle)\right|\leq\varepsilon_{g}.

In the following, we show that for suitably chosen (T,εg)(T,\varepsilon_{g}), θ=(θ(1:T),θ(T+1)){\bm{\theta}}=({\bm{\theta}}^{(1:T)},{\bm{\theta}}^{(T+1)}) is the desired transformer. First notice that its output hN+1(T+1)=[xN+1;y^N+1;w^T;0D−2d−3;1;0]{\mathbf{h}}_{N+1}^{(T+1)}=[{\mathbf{x}}_{N+1};\widehat{y}_{N+1};\widehat{{\mathbf{w}}}^{T};{\mathbf{0}}_{D-2d-3};1;0] satisfies

Therefore, for any fixed ε>0\varepsilon>0, we can take

so that the θ{\bm{\theta}} we construct above ensures ∣y^N+1−g(⟨xN+1,wGLM⟩)∣≤ε\left|\widehat{y}_{N+1}-g(\left\langle{\mathbf{x}}_{N+1},{\mathbf{w}}_{\rm GLM}\right\rangle)\right|\leq\varepsilon for any input H{\mathbf{H}} that satisfies (7). The upper bound on ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|} follows immediately from Theorem 13. ∎

E.2 Proof of Theorem 8

We summarize some basic and useful facts about GLM in the following theorem. Its proof is presented in Section E.3 - E.6.

Under Assumption Assumption, the following statements hold with universal constant C0C_{0} and constant C1,C2C_{1},C_{2} that depend only on the parameters (Kx,Ky,Bμ,Bw,μx,Lg,μg)(K_{x},K_{y},B_{\mu},B_{w},\mu_{x},L_{g},\mu_{g}).

(a) As long as N≥C1⋅dN\geq C_{1}\cdot d, the following event happens with probability at least 1−2e−N/C11-2e^{-N/C_{1}}:

(b) For any δ>0\delta>0, we have with probability at least 1−δ1-\delta that

where we denote ι=log⁡(2+LgKx2Bw/Ky)\iota=\log(2+L_{g}K_{x}^{2}B_{w}/K_{y}).

(c) Condition on (a) holds and N≥C2⋅dN\geq C_{2}\cdot d, the event Er:={∥wGLM∥2≤Bw/2}\mathcal{E}_{r}:=\{\left\|{{\mathbf{w}}_{\rm GLM}}\right\|_{2}\leq B_{w}/2\} happens with probability at least 1−eN/C21-e^{N/C_{2}}.

(d) For any w∈B2(Bw){\mathbf{w}}\in\mathsf{B}_{2}(B_{w}), it holds that

(e) (Realizable setting) As long as wGLM∈B2(Bw){\mathbf{w}}_{\rm GLM}\in\mathsf{B}_{2}(B_{w}), it holds that

Under the event E\mathcal{E} and our choice of α,β\alpha,\beta, the problem (ICGLM) is well-conditioned (i.e. (7) holds).

Theorem 7 implies that there exists a transformer θ{\bm{\theta}} such that for any input H{\mathbf{H}} of the form (3), TFθ{\rm TF}_{\bm{\theta}} outputs hN+1′=[xN+1;y~N+1;w~;0D−2d−3;1;0]{\mathbf{h}}_{N+1}^{\prime}=[{\mathbf{x}}_{N+1};\widetilde{y}_{N+1};\widetilde{{\mathbf{w}}};{\mathbf{0}}_{D-2d-3};1;0], such that the output is given by y^N+1=read~y(TFθ(H))=clipBy(y~N+1)\widehat{y}_{N+1}=\widetilde{\sf read}_{\sf y}({\rm TF}_{\bm{\theta}}({\mathbf{H}}))=\mathsf{clip}_{B_{y}}(\widetilde{y}_{N+1}) and w^=read~w(TFθ(H)):=Proj⁡B2(Bw)(w~)\widehat{{\mathbf{w}}}=\widetilde{\sf read}_{\sf w}({\rm TF}_{\bm{\theta}}({\mathbf{H}}))\mathrel{\mathop{:}}=\operatorname{Proj}_{\mathsf{B}_{2}(B_{w})}(\widetilde{{\mathbf{w}}}), and the following holds on the good event E\mathcal{E}:

y~N+1=fD(xN+1)\widetilde{y}_{N+1}=f_{\mathcal{D}}({\mathbf{x}}_{N+1}), where fD=A(D)f_{\mathcal{D}}=\mathcal{A}(\mathcal{D}) is a predictor such that ∣fD(x)−g(⟨x,wGLM⟩)∣≤ε\left|f_{\mathcal{D}}({\mathbf{x}})-g(\left\langle{\mathbf{x}},{\mathbf{w}}_{\rm GLM}\right\rangle)\right|\leq\varepsilon for all x∈B2(Bx){\mathbf{x}}\in\mathsf{B}_{2}(B_{x}).

w~=w~(D)∈B2(Bw)\widetilde{{\mathbf{w}}}=\widetilde{{\mathbf{w}}}(\mathcal{D})\in\mathsf{B}_{2}(B_{w}) depends only on D\mathcal{D} (by the proof of Theorem 7 and Theorem 13), such that \big{\|}\nabla\widehat{L}_{N}(\widetilde{{\mathbf{w}}})\big{\|}_{2}\leq\frac{\beta\varepsilon}{L_{g}B_{w}}.

In the following, we show that θ{\bm{\theta}} constructed above fulfills both (a) & (b) of Theorem 8. The bounds on number of layers and heads and ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|} follows from plugging our choice of Bx,ByB_{x},B_{y} in our proof of Theorem 7.

Proof of Theorem 8 (a). Notice that under the good event E\mathcal{E}, we have w^=w~=w~(D)\widehat{{\mathbf{w}}}=\widetilde{{\mathbf{w}}}=\widetilde{{\mathbf{w}}}(\mathcal{D}) depends only on D\mathcal{D}. Then we have

Thus, we can consider E0=Er∩Ew∩Eb\mathcal{E}_{0}=\mathcal{E}_{r}\cap\mathcal{E}_{w}\cap\mathcal{E}_{b}, and then

and by Theorem E.0 (b) and taking integration over δ>0\delta>0, we have

Taking ε2≤Ky2ιBwKx2dN\varepsilon^{2}\leq\frac{K_{y}^{2}\iota}{B_{w}K_{x}^{2}}\frac{d}{N} completes the proof. ∎

Proof of Theorem 8 (b). Similar to the proof of Corollary 6, we have

where the inequality follows from yN+1∈[−By,By]y_{N+1}\in[-B_{y},B_{y}] on event E\mathcal{E}. For the first part, we have

where we use the fact that the conditional distribution of (xN+1,yN+1)∣D({\mathbf{x}}_{N+1},y_{N+1})|\mathcal{D} agrees with P\mathsf{P}. Thus,

Taking ε2≤LgKx2Ky2ιμxμgdN\varepsilon^{2}\leq\frac{L_{g}K_{x}^{2}K_{y}^{2}\iota}{\mu_{x}\mu_{g}}\frac{d}{N} completes the proof. ∎

Suppose that x∼SG(Kx){\mathbf{x}}\sim{\rm SG}(K_{x}), y∼SG(Ky)y\sim{\rm SG}(K_{y}), and w{\mathbf{w}} is a (possibly random) vector such that ∥w∥2≤Bw\left\|{{\mathbf{w}}}\right\|_{2}\leq B_{w}. Then

E.3 Proof of Theorem E.0 (a)

We begin with the upper bound on λmax⁡(∇2L^N(w))\lambda_{\max}(\nabla^{2}\widehat{L}_{N}({\mathbf{w}})). By Lemma A.0, as long as N≥C0⋅dN\geq C_{0}\cdot d, the following event

In the following, we analyze the quantity λmax⁡(∇2L^N(w))\lambda_{\max}(\nabla^{2}\widehat{L}_{N}({\mathbf{w}})). We have to invoke the following covering argument (see e.g. [82, Section 4.1.1]).

Therefore, we can define h(t):=(Bμ−∣t∣)+h(t)\mathrel{\mathop{:}}=(B_{\mu}-\left|t\right|)_{+} (which is a 11-Lipschitz function), and we have

By our definition of A(w)A({\mathbf{w}}), we have (for any fixed BxvB_{xv})

(a) With norm ρ(w,w′)=∥w−w′∥2\rho({\mathbf{w}},{\mathbf{w}}^{\prime})=\left\|{{\mathbf{w}}-{\mathbf{w}}^{\prime}}\right\|_{2}, log⁡N(Bρ(w,r),δ)≤dlog⁡(2Ar/δ)\log\mathcal{N}(\mathsf{B}_{\rho}({\mathbf{w}},r),\delta)\leq d\log(2Ar/\delta) with constant A=2A=2;

(b) Let f(x;w):=h(⟨w,xi⟩)min⁡{⟨v,xi⟩2,Bxv2}f({\mathbf{x}};{\mathbf{w}})\mathrel{\mathop{:}}=h(\left\langle{\mathbf{w}},{\mathbf{x}}_{i}\right\rangle)\min{\left\{\left\langle{\mathbf{v}},{\mathbf{x}}_{i}\right\rangle^{2},B_{xv}^{2}\right\}}, then ∣f(x;w)∣≤BμBxv2\left|f({\mathbf{x}};{\mathbf{w}})\right|\leq B_{\mu}B_{xv}^{2} and hence in SG(CBμBxv2){\rm SG}(CB_{\mu}B_{xv}^{2}) for any random x{\mathbf{x}};

(c) For w,w′∈W{\mathbf{w}},{\mathbf{w}}^{\prime}\in\mathcal{W}, we have ∣h(⟨w,xi⟩)−h(⟨w′,xi⟩)∣≤∣⟨w−w′,xi⟩∣\left|h(\left\langle{\mathbf{w}},{\mathbf{x}}_{i}\right\rangle)-h(\left\langle{\mathbf{w}}^{\prime},{\mathbf{x}}_{i}\right\rangle)\right|\leq\left|\left\langle{\mathbf{w}}-{\mathbf{w}}^{\prime},{\mathbf{x}}_{i}\right\rangle\right|. Hence, because x∼SG(Kx){\mathbf{x}}\sim{\rm SG}(K_{x}), the random variable h(⟨w,x⟩)−h(⟨w′,x⟩)h(\left\langle{\mathbf{w}},{\mathbf{x}}\right\rangle)-h(\left\langle{\mathbf{w}}^{\prime},{\mathbf{x}}\right\rangle) is SG(CKx∥w−w′∥2){\rm SG}(CK_{x}\|{\mathbf{w}}-{\mathbf{w}}^{\prime}\|_{2}), and the random variable f(x;w)−f(x;w′)f({\mathbf{x}};{\mathbf{w}})-f({\mathbf{x}};{\mathbf{w}}^{\prime}) is SG(CKxBxv2∥w−w′∥2){\rm SG}(CK_{x}B_{xv}^{2}\|{\mathbf{w}}-{\mathbf{w}}^{\prime}\|_{2}).

Therefore, we can apply Proposition A.0 to obtain that with probability 1−δ01-\delta_{0}, it holds

where we denote κg=1+KxBw/Bμ\kappa_{g}=1+K_{x}B_{w}/B_{\mu}. Setting δ0=δ/∣V∣\delta_{0}=\delta/\left|\mathcal{V}\right| and taking the union bound over v∈V{\mathbf{v}}\in\mathcal{V}, we obtain that with probability at least 1−δ1-\delta,

where we use log⁡∣V∣≤dlog⁡(4/εv)\log|\mathcal{V}|\leq d\log(4/\varepsilon_{{\mathbf{v}}}). Therefore, we plug in the definition of εv\varepsilon_{\mathbf{v}} and BxvB_{xv} to deduce that, if we set

Under Assumption, for Bxv=Kx(15+log⁡(Kx2/μx))B_{xv}=K_{x}(15+\log(K_{x}^{2}/\mu_{x})), it holds

Because x∼SG(Kx){\mathbf{x}}\sim{\rm SG}(K_{x}), x⊤v∼SG(Kx){\mathbf{x}}^{\top}{\mathbf{v}}\sim{\rm SG}(K_{x}), and a simple calculation yields

E.4 Proof of Theorem E.0 (b)

(a) With norm ρ(w,w′)=∥w−w′∥2\rho({\mathbf{w}},{\mathbf{w}}^{\prime})=\left\|{{\mathbf{w}}-{\mathbf{w}}^{\prime}}\right\|_{2}, log⁡N(δ;Bρ(r),ρ)≤dlog⁡(2Ar/δ)\log N(\delta;\mathsf{B}_{\rho}(r),\rho)\leq d\log(2Ar/\delta) with constant A=2A=2;

(b) For z=[x;y]{\mathbf{z}}=[{\mathbf{x}};y], we let f(z;w):=(g(⟨w,x⟩)−y)⟨x,v⟩f({\mathbf{z}};{\mathbf{w}})\mathrel{\mathop{:}}={\left(g(\left\langle{\mathbf{w}},{\mathbf{x}}\right\rangle)-y\right)}\left\langle{\mathbf{x}},{\mathbf{v}}\right\rangle, then f(z;w)∼SE(CKxKy)f({\mathbf{z}};{\mathbf{w}})\sim{\rm SE}(CK_{x}K_{y}) for any w{\mathbf{w}} by our assumption on (x,y)({\mathbf{x}},y);

(c) For w,w′∈W{\mathbf{w}},{\mathbf{w}}^{\prime}\in\mathcal{W}, we have ∣g(⟨w,x⟩)−g(⟨w′,x⟩)∣≤Lg∣⟨w−w′,x⟩∣\left|g(\left\langle{\mathbf{w}},{\mathbf{x}}\right\rangle)-g(\left\langle{\mathbf{w}}^{\prime},{\mathbf{x}}\right\rangle)\right|\leq L_{g}\left|\left\langle{\mathbf{w}}-{\mathbf{w}}^{\prime},{\mathbf{x}}\right\rangle\right|. Hence, because x∼SG(Kx){\mathbf{x}}\sim{\rm SG}(K_{x}), the random variable g(⟨w,xi⟩)−g(⟨w′,xi⟩)g(\left\langle{\mathbf{w}},{\mathbf{x}}_{i}\right\rangle)-g(\left\langle{\mathbf{w}}^{\prime},{\mathbf{x}}_{i}\right\rangle) is sub-Gaussian in SG(KxLg∥w−w′∥2){\rm SG}(K_{x}L_{g}\|{\mathbf{w}}-{\mathbf{w}}^{\prime}\|_{2}). Thus, f(z;w)−f(z;w′)f({\mathbf{z}};{\mathbf{w}})-f({\mathbf{z}};{\mathbf{w}}^{\prime}) is sub-exponential in SE(CKx2Lg∥w−w′∥2){\rm SE}(CK_{x}^{2}L_{g}\|{\mathbf{w}}-{\mathbf{w}}^{\prime}\|_{2}).

Therefore, we can apply Proposition A.0 to obtain that with probability 1−δ01-\delta_{0}, it holds

where we denote κy=1+LgKx2Bw/Ky\kappa_{y}=1+L_{g}K_{x}^{2}B_{w}/K_{y}. Setting δ0=δ/∣V∣\delta_{0}=\delta/\left|\mathcal{V}\right| and taking the union bound over v∈V{\mathbf{v}}\in\mathcal{V}, we obtain that with probability at least 1−δ1-\delta,

E.5 Proof of Theorem E.0 (c)

In the following, we condition on (a) holds, i.e. L^N\widehat{L}_{N} is α\alpha-strongly-convex and β\beta smooth over B2(Bw)\mathsf{B}_{2}(B_{w}) with α=μxμg/8\alpha=\mu_{x}\mu_{g}/8 and β=8LgKx2\beta=8L_{g}K_{x}^{2}. We define

Then by standard convex analysis, we have

so that as long as N≥C2dN\geq C_{2}d, the event Es\mathcal{E}_{s} holds with probability at least 1−exp⁡(−N/C2)1-\exp(-N/C_{2}). This is the desired result. ∎

E.6 Proof of Theorem E.0 (d) & (e)

We first prove Theorem E.0 (d). Notice that

Therefore, LpL_{p} is (μgμx)(\mu_{g}\mu_{x})-strongly-convex over B2(Bw)\mathsf{B}_{2}(B_{w}). Therefore, because β⋆∈B2(Bw){\bm{\beta}}^{\star}\in\mathsf{B}_{2}(B_{w}) is the global minimum of LpL_{p}, it holds that for all w∈B2(Bw){\mathbf{w}}\in\mathsf{B}_{2}(B_{w}),

On the other hand, by the (μgμx)(\mu_{g}\mu_{x})-strong-convexity of LpL_{p} over B2(Bw)\mathsf{B}_{2}(B_{w}), it holds that

Appendix F Proofs for Section 3.3

Fix λN≥0\lambda_{N}\geq 0, β>0\beta>0 and Bw>0B_{w}>0, and consider any in-context data D\mathcal{D} such that the precondition of Theorem 10 holds. Recall that

Consider the proximal gradient descent algorithm on the ridge loss

By the standard convergence result for proximal gradient descent (Proposition A.0), we have for all t≥1t\geq 1 that

F.2 Sharper convergence analysis of proximal gradient descent for Lasso

Throughout the rest of this section, we consider fixed N≥1N\geq 1, λN=ρνlog⁡dN\lambda_{N}=\sqrt{\frac{\rho\nu\log d}{N}} for ρ≥0\rho\geq 0, ν≥0\nu\geq 0 fixed (and to be determined), fixed 0<α≤β0<\alpha\leq\beta, and fixed Bw⋆>0B_{w}^{\star}>0. We write κ:=β/α,κs:=β(Bw⋆)2/ν2\kappa:=\beta/\alpha,\kappa_{s}\mathrel{\mathop{:}}=\beta(B_{w}^{\star})^{2}/\nu^{2}, and ωN:=ραslog⁡dN\omega_{N}\mathrel{\mathop{:}}=\frac{\rho}{\alpha}\frac{s\log d}{N}.

We say the Eq. ICLasso problem is well-conditioned with sparsity ss if the following conditions hold:

Further, λmax⁡(X⊤X/N)≤β\lambda_{\max}({\mathbf{X}}^{\top}{\mathbf{X}}/N)\leq\beta.

It holds that N≥32ρα⋅slog⁡dN\geq 32\frac{\rho}{\alpha}\cdot s\log d (i.e. 32ωN≤132\omega_{N}\leq 1).

The proof can be found in Section F.4. Combining Theorem F.0 with the construction in Theorem 10, we directly obtain the following result as a corollary.

For any N,d,s≥1N,d,s\geq 1, 0<α≤β0<\alpha\leq\beta, ν≥0\nu\geq 0, ρ≥0\rho\geq 0, there exists a LL-layer transformer TFθ{\rm TF}_{\bm{\theta}} with

F.3 Basic properties for Lasso

Suppose that Assumption2 holds. Then it holds that

where the second inequality follows from ε⊤XΔN≤∥X⊤ε∥∞N∥Δ∥1\frac{{\bm{\varepsilon}}^{\top}{\mathbf{X}}{\mathbf{\Delta}}}{N}\leq\frac{\left\|{{\mathbf{X}}^{\top}{\bm{\varepsilon}}}\right\|_{\infty}}{N}\left\|{{\mathbf{\Delta}}}\right\|_{1} and our assumption that 2∥X⊤ε∥∞N≤λN2\frac{\left\|{{\mathbf{X}}^{\top}{\bm{\varepsilon}}}\right\|_{\infty}}{N}\leq\lambda_{N}, and the last inequality is due to ∥Δ∥1=∥ΔS∥1+∥ΔSc∥1\left\|{{\mathbf{\Delta}}}\right\|_{1}=\left\|{{\mathbf{\Delta}}_{S}}\right\|_{1}+\left\|{{\mathbf{\Delta}}_{S^{c}}}\right\|_{1}. Therefore, we have

We follow the notation in the proof of Lemma F.0. By (33), we have

where the last inequality uses AM-GM inequality and Cauchy inequality. Notice that ρ20slog⁡dN≤23α\rho\frac{20s\log d}{N}\leq\frac{2}{3}\alpha, we now derive that

where we write R(w):=λN∥w∥1R({\mathbf{w}}):=\lambda_{N}\left\|{{\mathbf{w}}}\right\|_{1}. Then by the convexity of RR, we have

F.4 Proof of Theorem F.0

By Lemma F.0, we have ∥wt−w⋆∥1≤4s∥wt−w⋆∥2+2gaptλN\left\|{{\mathbf{w}}^{t}-{\mathbf{w}}_{\star}}\right\|_{1}\leq 4\sqrt{s}\left\|{{\mathbf{w}}^{t}-{\mathbf{w}}_{\star}}\right\|_{2}+\frac{2{\sf gap}^{t}}{\lambda_{N}}, which implies

We denote μN=ρ2log⁡dN\mu_{N}=\rho^{2}\frac{\log d}{N}. Using the assumption that X{\mathbf{X}} is (α,ρ)(\alpha,\rho)-RSC, we obtain that

Thus, as long as N≥30ρ2slog⁡dαN\geq\frac{30\rho^{2}s\log d}{\alpha}, we have

where the last inequality follows from Lemma F.0 and the definition of λN,μN\lambda_{N},\mu_{N}.

Then for all t≥T0−1t\geq T_{0}-1, we have (the second ≤\leq below uses Proposition A.0(2))

where the last inequality follows from Proposition A.0(2). Further, by Proposition A.0(3), we have

Hence, we can conclude that gapT≤ε{\sf gap}^{T}\leq\varepsilon for all TT such that

F.5 Proof of Theorem 11

In this section, we present the proof of Theorem 11 based on Theorem F.0. We begin by recalling the following RSC property of a Gaussian random matrix [84, Theorem 7.16], a classical result in the high-dimensional statistics literature.

where ρ(Σ)=max⁡i∈[d]Σii\rho({\mathbf{\Sigma}})=\max_{i\in[d]}\Sigma_{ii} is the maximum of diagonal entries of Σ{\mathbf{\Sigma}}.

Fix a parameter δ1≤δ\delta_{1}\leq\delta (which we will specify in proof) and a large universal constant C0C_{0}. Let us set

Similar to the proof of Corollary 6 (Section D.4), we consider the following good events (where ε=Xw⋆−y{\bm{\varepsilon}}={\mathbf{X}}{\mathbf{w}}_{\star}-{\mathbf{y}})

and we define E:=Ew∩Er∩Eb∩Eb,N+1\mathcal{E}\mathrel{\mathop{:}}=\mathcal{E}_{w}\cap\mathcal{E}_{r}\cap\mathcal{E}_{b}\cap\mathcal{E}_{b,N+1}.

Furthermore, we choose ν>0\nu>0 that correspond to the choice λN=8σlog⁡(4d/δ)N\lambda_{N}=8\sigma\sqrt{\frac{\log(4d/\delta)}{N}}, and we also assume N≥32c2c1⋅slog⁡dN\geq\frac{32c_{2}}{c_{1}}\cdot s\log d. Then, Assumption holds on the event E\mathcal{E}.

Therefore, we can apply Theorem F.0 with ε=νωN\varepsilon=\nu\omega_{N}, which implies that there exists a LL-layer transformer θ{\bm{\theta}} such that its prediction y^N+1:=read~y(TFθ0(H))\widehat{y}_{N+1}:=\widetilde{\sf read}_{\sf y}({\rm TF}^{0}_{\bm{\theta}}({\mathbf{H}})), so that under the good event E\mathcal{E} we have y^N+1=clipBy(⟨xN+1,w^⟩)\widehat{y}_{N+1}=\mathsf{clip}_{B_{y}}(\left\langle{\mathbf{x}}_{N+1},\widehat{{\mathbf{w}}}\right\rangle), where

In the following, we show that θ{\bm{\theta}} is indeed the desired transformer (similarly to the proof in Section D.4). Consider the conditional prediction error

and we analyze these two parts separately under the good event E0:=Ew∩Er∩Eb\mathcal{E}_{0}\mathrel{\mathop{:}}=\mathcal{E}_{w}\cap\mathcal{E}_{r}\cap\mathcal{E}_{b} of D\mathcal{D}.

where the inequality is because yN+1∈[−By,By]y_{N+1}\in[-B_{y},B_{y}] under the good event E\mathcal{E}. Notice that by our construction, under the good event E\mathcal{E}, w^=w^(D)\widehat{{\mathbf{w}}}=\widehat{{\mathbf{w}}}(\mathcal{D}) depends only on the dataset D\mathcal{D} (because it is the (L−1)(L-1)-th iterate of PGD on (ICLasso) problem). Applying Proposition F.0 to w^(D)\widehat{{\mathbf{w}}}(\mathcal{D}) and using the definition of ωN\omega_{N} and our choice of λN\lambda_{N}, we obtain that (under E0\mathcal{E}_{0})

Notice that under good event E0\mathcal{E}_{0}, the bad event Ec\mathcal{E}^{c} holds if and only if Eb,N+1c\mathcal{E}_{b,N+1}^{c} holds, and hence

where the last inequality is because the marginal distribution of yN+1y_{N+1} is simply N(0,σ2+∥w⋆∥22)\mathsf{N}(0,\sigma^{2}+\|{\mathbf{w}}_{\star}\|_{2}^{2}). Combining these yields

Therefore, choosing δ1=min⁡{δ,σBw⋆}\delta_{1}=\min\{\delta,\frac{\sigma}{B_{w}^{\star}}\} is enough for our purpose, and under such choice of δ1\delta_{1},

Combining the inequalities above, we can conclude that under E0\mathcal{E}_{0},

We also remark that in the construction above,

which would be useful for bounding ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}.

Appendix G Gradient descent on two-layer neural networks

We now move beyond the convex setting by showing that transformers can implement gradient descent on two-layer neural networks in context.

where W\mathcal{W} is a bounded domain. For the sake of simplicity, in the following discussion we assume that Proj⁡W\operatorname{Proj}_{\mathcal{W}} can be exactly implemented by a MLP layer (e.g. W=B∞(Rw)\mathcal{W}=\mathsf{B}_{\infty}(R_{w}) for some Rw>0R_{w}>0).

Fix any Bv,Bu>0B_{v},B_{u}>0, L≥1L\geq 1, η>0\eta>0, and ε>0\varepsilon>0. Suppose that

Then there exists a (2L)(2L)-layer transformer TFθ{\rm TF}_{\bm{\theta}} with

where Lf=sup⁡w∈W∥∇2L^N(w)∥opL_{f}=\sup_{{\mathbf{w}}\in\mathcal{W}}\left\|{\nabla^{2}\widehat{L}_{N}({\mathbf{w}})}\right\|_{\rm op} denotes the smoothness of L^N\widehat{L}_{N} within W\mathcal{W}.

denote the gradient mapping at w{\mathbf{w}} with step size η\eta, a standard measure of stationarity in constrained optimization . Note that GW,ηf(w)=∇f(w){\sf G}^{f}_{\mathcal{W},\eta}({\mathbf{w}})=\nabla f({\mathbf{w}}) when w−η∇f(w)∈W{\mathbf{w}}-\eta\nabla f({\mathbf{w}})\in\mathcal{W} (so that the projection does not take effect).

As long as η≤1/Lf\eta\leq 1/L_{f}, for all L≥1L\geq 1,

G.1 Proof of Theorem G.0

We only need to prove the following single-step version of Theorem G.0.

Before we present the formal (and technical) proof of Proposition G.0, we first provide some intuitions. To begin with, we first note that

Based on the observations above, we now present the proof of Proposition G.0.

We write D0=d+1+K(d+1)D_{0}=d+1+K(d+1) be the length of the vector [xi;yi;w][{\mathbf{x}}_{i};y_{i};{\mathbf{w}}]. We also define

The function r(t)r(t) is (εr,R1,M1,C1)(\varepsilon_{r},R_{1},M_{1},C_{1}) for R1=max⁡{BxBu,1}R_{1}=\max{\left\{B_{x}B_{u},1\right\}}, M1≤O~(C12εr−2)M_{1}\leq\widetilde{\mathcal{O}}\left(C_{1}^{2}\varepsilon_{r}^{-2}\right), where C1C_{1} depends only on R1R_{1} and the C2C^{2}-smoothness of rr. Therefore, there exists

such that sup⁡t∈[−R1,R1]∣r(t)−r‾(t)∣≤εr\sup_{t\in[-R_{1},R_{1}]}\left|r(t)-\overline{r}(t)\right|\leq\varepsilon_{r}.

The function (s,t)↦s⋅r′(t)(s,t)\mapsto s\cdot r^{\prime}(t) is (εp,R3,M3,C3)(\varepsilon_{p},R_{3},M_{3},C_{3}) for R3=max⁡{BxBu,BgBu,1}R_{3}=\max{\left\{B_{x}B_{u},B_{g}B_{u},1\right\}}, M3≤O~(C32εp−2)M_{3}\leq\widetilde{\mathcal{O}}\left(C_{3}^{2}\varepsilon_{p}^{-2}\right), where C3C_{3} depends only on R3R_{3} and the C3C^{3}-smoothness of r′r^{\prime}. Therefore, there exists

such that sup⁡(s,t)∈[−R3,R3]2∣P(s,t)−s⋅r′(t)∣≤εp\sup_{(s,t)\in[-R_{3},R_{3}]^{2}}\left|P(s,t)-s\cdot r^{\prime}(t)\right|\leq\varepsilon_{p}.

In the following, we proceed to construct the desired transformer step by step.

Step 1: construction of θattn(1){\bm{\theta}}_{\tt attn}^{(1)}. We consider the matrices {Qk,m(1),Kk,m(1),Vk,m(1)}k∈[K],m∈[M1]\{{\mathbf{Q}}^{(1)}_{k,m},{\mathbf{K}}^{(1)}_{k,m},{\mathbf{V}}^{(1)}_{k,m}\}_{k\in[K],m\in[M_{1}]} so that for all i,j∈[N+1]i,j\in[N+1], we have

As the input has structure hi=[xi;yi′;w;0;1;ti]{\mathbf{h}}_{i}=[{\mathbf{x}}_{i};y_{i}^{\prime};{\mathbf{w}};{\mathbf{0}};1;t_{i}], these matrices indeed exist, and further it is straightforward to check that they have norm bounds

Step 2: construction of θmlp(1){\bm{\theta}}_{\tt mlp}^{(1)}. We pick matrices W1,W2{\mathbf{W}}_{1},{\mathbf{W}}_{2} so that W1{\mathbf{W}}_{1} maps

By the definition of the function gg, for each i∈[N]i\in[N],

Step 3: construction of θattn(2){\bm{\theta}}_{\tt attn}^{(2)}. We consider the matrices {Qk,1,m(2),Kk,1,m(2),Vk,1,m(2)}k∈[K],m∈[M3]\{{\mathbf{Q}}^{(2)}_{k,1,m},{\mathbf{K}}^{(2)}_{k,1,m},{\mathbf{V}}^{(2)}_{k,1,m}\}_{k\in[K],m\in[M_{3}]} so that for all i,j∈[N+1]i,j\in[N+1], we have

We further consider the matrices {Qk,2,m(2),Kk,2,m(2),Vk,2,m(2)}k∈[K],m∈[M1]\{{\mathbf{Q}}^{(2)}_{k,2,m},{\mathbf{K}}^{(2)}_{k,2,m},{\mathbf{V}}^{(2)}_{k,2,m}\}_{k\in[K],m\in[M_{1}]} so that for all i,j∈[N+1]i,j\in[N+1], we have

By the structure of the input hi(1){\mathbf{h}}_{i}^{(1)}, these matrices indeed exist, and further it is straightforward to check that they have norm bounds

Furthermore, a simple calculation shows that

where the summation is taken over all possibilities of the tuple (k,w,m)(k,w,m), i.e. over the union of [K]×{1}×[M3][K]\times{\left\{1\right\}}\times[M_{3}] and [K]×{2}×[M1][K]\times{\left\{2\right\}}\times[M_{1}].

By our definition, we have ∣P(s,t)−sr′(t)∣≤εp\left|P(s,t)-sr^{\prime}(t)\right|\leq\varepsilon_{p} for all s,t∈[−R3,R3]s,t\in[-R_{3},R_{3}]. Therefore, for each i∈[N]i\in[N], k∈[K]k\in[K],

where Lr:=max⁡∣t∣≤BxBu∣r′(t)∣L_{r}\mathrel{\mathop{:}}=\max_{\left|t\right|\leq B_{x}B_{u}}\left|r^{\prime}(t)\right| is the upper bound of r′r^{\prime}. Similarly, for each i∈[N]i\in[N], k∈[K]k\in[K], we have

As for the case i=N+1i=N+1, we have gN+1=0g_{N+1}=0 and ∣P(ukgN+1,⟨vk,xN+1⟩)∣≤εp\left|P(u_{k}g_{N+1},\left\langle{\mathbf{v}}_{k},{\mathbf{x}}_{N+1}\right\rangle)\right|\leq\varepsilon_{p} for each k∈[K]k\in[K] by defintion. Combining these estimations and using (37) and (38), we can conclude that

Thus, letting the attention layer θattn(2)={(Vk,w,m(2),Qk,w,m(2),Kk,w,m(2))}(k,w,m){\bm{\theta}}_{\tt attn}^{(2)}={\{({\mathbf{V}}^{(2)}_{k,w,m},{\mathbf{Q}}^{(2)}_{k,w,m},{\mathbf{K}}^{(2)}_{k,w,m})\}}_{(k,w,m)}, we have

Step 4: construction of θmlp(2){\bm{\theta}}_{\tt mlp}^{(2)}. We only need to pick θmlp(2){\bm{\theta}}_{\tt mlp}^{(2)} so that it maps

By our assumption on the map Proj⁡W\operatorname{Proj}_{\mathcal{W}}, this is easy.

Combining the four steps above and taking θ=(θattn(1),θmlp(1),θattn(2),θmlp(2)){\bm{\theta}}=({\bm{\theta}}_{\tt attn}^{(1)},{\bm{\theta}}_{\tt mlp}^{(1)},{\bm{\theta}}_{\tt attn}^{(2)},{\bm{\theta}}_{\tt mlp}^{(2)}) completes the proof. ∎

G.2 Proof of Lemma G.0

Above, (i) uses the inequality ∥a−b∥22≥12∥a∥22−∥b∥22\left\|{a-b}\right\|_{2}^{2}\geq\frac{1}{2}\left\|{a}\right\|_{2}^{2}-\left\|{b}\right\|_{2}^{2}; (ii) uses the fact that projection to a convex set is a non-expansion; (iii) uses the definition of the gradient mapping.

By the LfL_{f}-smoothness of ff within W\mathcal{W}, we have

Dividing both sides by ηL/8\eta L/8 yields part (a).

Next, we prove part (b). Let C:=1+ηLfC\mathrel{\mathop{:}}=1+\eta L_{f}. We prove by induction that

Appendix H Proofs for Section 4

We begin by restating Proposition 15 into the following version, which contains additional size bounds on θ{\bm{\theta}}.

To prove Theorem H.0, we first state and prove the following two propositions.

There exists a 1-layer transformer TFθ{\rm TF}_{\bm{\theta}} with MKMK heads and ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤3R+2NKC/∣Dval∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 3R+2NKC/\left|\mathcal{D}_{\sf val}\right| such that for all H{\mathbf{H}} such that max⁡i{∣yi′∣}≤R,max⁡i,k{∣fk(xi)∣}≤R\max_{i}\{\left|y_{i}^{\prime}\right|\}\leq R,\max_{i,k}\{\left|f_{k}({\mathbf{x}}_{i})\right|\}\leq R, TFθ{\rm TF}_{\bm{\theta}} maps

where L~val(⋅)\widetilde{L}_{{\sf val}}(\cdot) is a functional such that max⁡k∣L~val(fk)−L^val(fk)∣≤ε\max_{k}\left|\widetilde{L}_{{\sf val}}(f_{k})-\widehat{L}_{{\sf val}}(f_{k})\right|\leq\varepsilon.

where the last equality follows from the bound ∣amfk(xj)+bmyj+dm∣≤R(∣am∣+∣bm∣)+dm≤2R\left|a_{m}f_{k}({\mathbf{x}}_{j})+b_{m}y_{j}+d_{m}\right|\leq R(|a_{m}|+|b_{m}|)+d_{m}\leq 2R, so that the above relu equals if tj≤0t_{j}\leq 0. Therefore, for each i∈[N+1]i\in[N+1] and k∈[K]k\in[K],

Thus letting the attention layer θ={(Vm,k,Qm,k,Km,k)}(m,k)∈[M]×[K]{\bm{\theta}}={\{({\mathbf{V}}_{m,k},{\mathbf{Q}}_{m,k},{\mathbf{K}}_{m,k})\}}_{(m,k)\in[M]\times[K]}, we have

There exists a 3-layer transformer TFθ{\rm TF}_{\bm{\theta}} with

We construct a θ{\bm{\theta}} which is a composition of 2 MLP layers followed by an attention layer (θmlp(1),θmlp(2),θattn(3))({\bm{\theta}}_{\tt mlp}^{(1)},{\bm{\theta}}_{\tt mlp}^{(2)},{\bm{\theta}}_{\tt attn}^{(3)}).

Step 1: construction of θmlp(1){\bm{\theta}}_{\tt mlp}^{(1)}. We consider matrix W1(1){\mathbf{W}}_{1}^{(1)} that maps

Step 2: construction of θmlp(2){\bm{\theta}}_{\tt mlp}^{(2)}. We consider matrix W1(2){\mathbf{W}}_{1}^{(2)} that maps

where uk=σ(1−γ−1ck)∀k∈[K]u_{k}=\sigma(1-\gamma^{-1}c_{k})\forall k\in[K]. Clearly, uk∈u_{k}\in, and uk>0u_{k}>0 if and only if ck≤γc_{k}\leq\gamma.

Step 3: construction of θattn(3){\bm{\theta}}_{\tt attn}^{(3)}. We define

Clearly, λk≥0\lambda_{k}\geq 0, and ∑kλk=1\sum_{k}\lambda_{k}=1. Further,

Therefore, it remains to construct θattn(3){\bm{\theta}}_{\tt attn}^{(3)} that implements f^=∑k=1Kλkfk\widehat{f}=\sum_{k=1}^{K}\lambda_{k}f_{k} based on [hi(2)]i[{\mathbf{h}}_{i}^{(2)}]_{i}. Notice that

for all i,j∈[N+1]i,j\in[N+1], where we understand f0=fK+1=0f_{0}=f_{K+1}=0 and 1k{\mathbf{1}}_{k} is the kk-dimensional vector with all entries being 1. By the structure of hi(2){\mathbf{h}}_{i}^{(2)}, these matrices indeed exist, and further it is straightforward to check that they have norm bounds

Now, for every i,j∈[N+1]i,j\in[N+1], k∈[K+1],w∈{0,1}k\in[K+1],w\in\{0,1\}, we have

where the last equality follows from fk(xi)+R≥0∀k∈[K]f_{k}({\mathbf{x}}_{i})+R\geq 0\forall k\in[K]. Therefore,

where the last equality is due to (42). Thus letting the attention layer θattn(3)={(Vk,w,Qk,w,Kk,w)}(k,w)∈[K+1]×{0,1}{\bm{\theta}}_{\tt attn}^{(3)}={\{({\mathbf{V}}_{k,w},{\mathbf{Q}}_{k,w},{\mathbf{K}}_{k,w})\}}_{(k,w)\in[K+1]\times\{0,1\}}, we have

for any input H=[hi]i{\mathbf{H}}=[{\mathbf{h}}_{i}]_{i} of the form described in Theorem H.0, and L~val(⋅)\widetilde{L}_{{\sf val}}(\cdot) is a functional such that max⁡k∣L~val(fk)−L^val(fk)∣≤γ/3\max_{k}\left|\widetilde{L}_{{\sf val}}(f_{k})-\widehat{L}_{{\sf val}}(f_{k})\right|\leq\gamma/3.

Next, by the proof of Proposition H.0, there exists (θmlp(1),θmlp(2),θattn(3))({\bm{\theta}}_{\tt mlp}^{(1)},{\bm{\theta}}_{\tt mlp}^{(2)},{\bm{\theta}}_{\tt attn}^{(3)}) that maps

where λ=(λ1,⋯ ,λK)∈Δ([K])\lambda=(\lambda_{1},\cdots,\lambda_{K})\in\Delta([K]) and λk>0\lambda_{k}>0 only when L~val(fk)≤min⁡k⋆L~val(fk⋆)+γ/3\widetilde{L}_{{\sf val}}(f_{k})\leq\min_{k^{\star}}\widetilde{L}_{{\sf val}}(f_{k^{\star}})+\gamma/3. Using the fact that max⁡k∣L~val(fk)−L^val(fk)∣≤γ/3\max_{k}|\widetilde{L}_{{\sf val}}(f_{k})-\widehat{L}_{{\sf val}}(f_{k})|\leq\gamma/3, we deduce that λ\lambda is supported on {k:L^val(fk)≤min⁡k⋆∈[K]L^val(fk⋆)+γ}\{k:\widehat{L}_{{\sf val}}(f_{k})\leq\min_{k_{\star}\in[K]}\widehat{L}_{{\sf val}}(f_{k_{\star}})+\gamma\}.

Therefore, θ=(θattn(1),θmlp(1),θmlp(2),θattn(3)){\bm{\theta}}=({\bm{\theta}}_{\tt attn}^{(1)},{\bm{\theta}}_{\tt mlp}^{(1)},{\bm{\theta}}_{\tt mlp}^{(2)},{\bm{\theta}}_{\tt attn}^{(3)}) is the desired transformer, with

H.2 Proof of Theorem 16

We first restate Theorem 16 into the following version which provides additional size bounds for θ{\bm{\theta}}. For the simplicity of presentation, throughout this subsection and Appendix I, we denote It={i:(xi,yi)∈Dtrain}\mathcal{I}_{t}=\{i:({\mathbf{x}}_{i},y_{i})\in\mathcal{D}_{{\sf train}}\}, Iv={i:(xi,yi)∈Dval}\mathcal{I}_{v}=\{i:({\mathbf{x}}_{i},y_{i})\in\mathcal{D}_{{\sf val}}\}, Xtrain=[xi]i∈It{\mathbf{X}}_{\sf train}=[{\mathbf{x}}_{i}]_{i\in\mathcal{I}_{t}} to be the input matrix corresponding to the training split only, and Ntrain=∣Dtrain∣N_{\sf train}=\left|\mathcal{D}_{{\sf train}}\right|, Nval=∣Dval∣N_{\sf val}=\left|\mathcal{D}_{{\sf val}}\right|.

For any sequence of regularizations {λk}k∈[K]\{\lambda_{k}\}_{k\in[K]}, 0≤α≤β0\leq\alpha\leq\beta with κ:=max⁡kβ+λkα+λk\kappa\mathrel{\mathop{:}}=\max_{k}\frac{\beta+\lambda_{k}}{\alpha+\lambda_{k}}, Bw>0B_{w}>0, γ>0\gamma>0, and ε<Bw/2\varepsilon<B_{w}/2, suppose in input format Eq. 3 we have D≥Θ(Kd)D\geq\Theta(Kd). Then there exists an LL-layer transformer TFθ{\rm TF}_{\bm{\theta}} with

such that the following holds. On any input data (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) such that the problem Eq. ICRidge is well-conditioned and has a bounded solution:

TFθ0{\rm TF}^{0}_{\bm{\theta}} approximately implements ridge selection: its prediction

For each k∈[K]k\in[K], w^k=w^k(Dtrain)\widehat{{\mathbf{w}}}_{k}=\widehat{{\mathbf{w}}}_{k}(\mathcal{D}_{{\sf train}}) approximates the ridge estimator wridgeλk(Dtrain){\mathbf{w}}_{\rm ridge}^{\lambda_{k}}(\mathcal{D}_{{\sf train}}), i.e. \big{\|}\widehat{{\mathbf{w}}}_{k}-{\mathbf{w}}_{\rm ridge}^{\lambda_{k}}(\mathcal{D}_{{\sf train}})\big{\|}_{2}\leq\varepsilon.

λ=(λ1,⋯ ,λK)∈Δ([K])\lambda=(\lambda_{1},\cdots,\lambda_{K})\in\Delta([K]) so that

In particular, if we set γ′=2(BxBw+By)Bxε+γ\gamma^{\prime}=2(B_{x}B_{w}+B_{y})B_{x}\varepsilon+\gamma, then it holds thatThis is because L^val(w)\widehat{L}_{{\sf val}}({\mathbf{w}}) is (BxBw+By)Bx(B_{x}B_{w}+B_{y})B_{x}-Lipschitz w.r.t. w∈B2(Bw){\mathbf{w}}\in\mathsf{B}_{2}(B_{w}).

where we denote w^ridge,trainλk:=wridgeλk(Dtrain)\widehat{{\mathbf{w}}}_{{\rm ridge},{\sf train}}^{\lambda_{k}}:={\mathbf{w}}_{\rm ridge}^{\lambda_{k}}(\mathcal{D}_{{\sf train}}).

To prove Theorem H.0, we first show that, for the squared validation loss, there exists a 3-layer transformer that performs predictor selection based on the exactly evaluated L^val(fk)\widehat{L}_{{\sf val}}(f_{k}) for each k∈[K]k\in[K]. (Proof in Section H.2.1.)

Then there exists a 3-layer transformer TFθ{\rm TF}_{\bm{\theta}} with

such that for any input H{\mathbf{H}} that takes form

Proof of Theorem H.0 First, by the proofTechnically, an adapted version where the underlying ICGD mechanism operates on the training split (with ti=1t_{i}=1) with size NtrainN_{\sf train} instead of on all NN training examples, which only changes ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|} by at most a constant factor, and does not change the number of layers and heads. of Theorem 4 and Proposition A.0, for each k∈[K]k\in[K], there exists a T=L−3T=L-3 layer transformer θ(1:T){\bm{\theta}}^{(1:T)} such that TFθ(1:T){\rm TF}_{{\bm{\theta}}^{(1:T)}} maps

so that if (43) holds, we have \big{\|}\widehat{{\mathbf{w}}}_{k}-{\mathbf{w}}_{\rm ridge}^{\lambda_{k}}\big{\|}_{2}\leq\varepsilon and w^k∈B2(Bw)\widehat{{\mathbf{w}}}_{k}\in\mathsf{B}_{2}(B_{w}).

Next, by Theorem H.0, there exists a 3-layer transformer θ(T+1:T+3){\bm{\theta}}^{(T+1:T+3)} that outputs

where w^=∑k=1Kλkw^k\widehat{{\mathbf{w}}}=\sum_{k=1}^{K}\lambda_{k}\widehat{{\mathbf{w}}}_{k}, λ=(λ1,⋯ ,λK)∈Δ([K])\lambda=(\lambda_{1},\cdots,\lambda_{K})\in\Delta([K]) so that

Similar to the proof of Proposition 15, Theorem H.0 is a direct corollary by combining Proposition H.0 with Proposition H.0.

There exists an attention layer TFθ{\rm TF}_{\bm{\theta}} with 2K2K heads and ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤3R+2NK/∣Dval∣{\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 3R+2NK/\left|\mathcal{D}_{\sf val}\right| such that TFθ{\rm TF}_{\bm{\theta}} maps

As the input has structure hi=[xi;yi′;∗;f1(xi);⋯ ;fK(xi);0K+1;1;ti]{\mathbf{h}}_{i}=[{\mathbf{x}}_{i};y_{i}^{\prime};*;f_{1}({\mathbf{x}}_{i});\cdots;f_{K}({\mathbf{x}}_{i});\mathbf{0}_{K+1};1;t_{i}], these matrices indeed exist, and further it is straightforward to check that they have norm bounds

where the second equality follows from the bound ∣fk(xj)−yj∣≤2R\left|f_{k}({\mathbf{x}}_{j})-y_{j}\right|\leq 2R, so that the relus equals if tj≤0t_{j}\leq 0. Thus letting the attention layer θ={(Vk,w,Qk,w,Kk,w)}(k,w)∈[K]×{0,1}{\bm{\theta}}={\{({\mathbf{V}}_{k,w},{\mathbf{Q}}_{k,w},{\mathbf{K}}_{k,w})\}}_{(k,w)\in[K]\times{\left\{0,1\right\}}}, we have

H.3 Proofs for Section 4.2

with ∑m∣am∣=8/ε\sum_{m}|a_{m}|=8/\varepsilon, max⁡mmax⁡{∣bm∣,∣cm∣}≤2\max_{m}\max{\{|b_{m}|,|c_{m}|\}}\leq 2. We can thus construct an attention layer θ={(Qm,Km,Vm)}m=16{\bm{\theta}}={\{({\mathbf{Q}}_{m},{\mathbf{K}}_{m},{\mathbf{V}}_{m})\}}_{m=1}^{6} with 66 heads such that

Further, we have ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤18/ε=O(1/ε){\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq 18/\varepsilon=\mathcal{O}(1/\varepsilon). This is the desired result. ∎

By composing the above attention layer with one additional layer (with 2 heads) that implement the following function

on the output Ψbinary(D)\Psi^{\sf binary}(\mathcal{D}), we directly obtain the following corollary.

at every token i∈[N+1]i\in[N+1], where we recall the definition of Ψbinary\Psi^{\sf binary} in Lemma 18.

H.3.2 Formal statement and proof of Proposition 19

for all ε∈(0,ε0]\varepsilon\in(0,\varepsilon_{0}]. A sufficient condition is that the density py\mathsf{p}_{y} is upper bounded by CC within [−ε0,ε0]∪[1−ε0,1+ε0][-\varepsilon_{0},\varepsilon_{0}]\cup[1-\varepsilon_{0},1+\varepsilon_{0}].

Throughout this section, let σlog(t):=(1+e−t)−1\sigma_{{\rm log}}(t)\mathrel{\mathop{:}}=(1+e^{-t})^{-1} denote the sigmoid activation, and let w^log\widehat{{\mathbf{w}}}_{{\rm log}} denote the solution to the in-context logistic regression problem, i.e. Eq. ICGLM with g(⋅)=σlog(⋅)g(\cdot)=\sigma_{{\rm log}}(\cdot).

For any Bw>0B_{w}>0, ε≤BxBw/10\varepsilon\leq B_{x}B_{w}/10, 0<α≤β0<\alpha\leq\beta with κ:=β/α\kappa\mathrel{\mathop{:}}=\beta/\alpha, and any (C,ε0)(C,\varepsilon_{0}), there exists a LL-layer attention-only transformer with

(with R:=max⁡{BxBw,By,1}R\mathrel{\mathop{:}}=\max{\{B_{x}B_{w},B_{y},1\}}, and ε\varepsilon depending only on (C,ε0)(C,\varepsilon_{0})) such that the following holds. Suppose the input format is Eq. 3 with dimension D≥3d+4D\geq 3d+4.

On any classification instance (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) (such that {yi}i∈[N]⊂{0,1}{\{y_{i}\}}_{i\in[N]}\subset{\{0,1\}}) that is well-conditioned for logistic regression in the sense of Eq. 7, it outputs y^N+1\widehat{y}_{N+1} that ε\varepsilon-approximates the prediction of in-context logistic regression:

On the contrary, for regression problems, i.e. any in-context distribution P\mathsf{P} whose marginal Py\mathsf{P}_{y} is (C,ε0)(C,\varepsilon_{0})-not-concentrated around {0,1}{\{0,1\}}, with probability at least 1−exp⁡(−cN)1-\exp(-cN) over D\mathcal{D} (where c>0c>0 depends only on (C,ε0)(C,\varepsilon_{0})), y^N+1\widehat{y}_{N+1} ε\varepsilon-approximates the prediction of in-context least squares if the data is well-conditioned:

where w^LS\widehat{{\mathbf{w}}}_{{\rm LS}} denotes the in-context least squares estimator, i.e. Eq. ICRidge with λ=0\lambda=0.

The result follows by combining the binary test in Corollary H.0 with Theorem 4 and Theorem 7. By those results, there exists three attention-only transformers θLS,θlog,θbin{\bm{\theta}}_{{\rm LS}},{\bm{\theta}}_{{\rm log}},{\bm{\theta}}_{{\rm bin}}, with (below Lg,Cg=Θ(1)L_{g},C_{g}=\Theta(1) for g=σlog(⋅)g=\sigma_{{\rm log}}(\cdot))

that outputs prediction y^N+1LS\widehat{y}^{{\rm LS}}_{N+1}, y^N+1log\widehat{y}^{{\rm log}}_{N+1} (at the (N+1)(N+1)-th token) and Ψthresbinary(D)\Psi^{\sf binary}_{\sf thres}(\mathcal{D}) (at every token) respectively, which satisfy

when the corresponding well-conditionednesses are satisfied. In particular, we can make w^log\widehat{{\mathbf{w}}}_{{\rm log}} well-defined on non-binary data, by multiplying Ψthresbinary(D)\Psi^{\sf binary}_{\sf thres}(\mathcal{D}) onto the xi{\mathbf{x}}_{i}’s (which can be implemented by slightly modifying θlog{\bm{\theta}}_{{\rm log}} without changing the order of the number of layers, heads, and norms) so that w^log=0\widehat{{\mathbf{w}}}_{{\rm log}}={\mathbf{0}} on any data where Ψthresbinary(D)=0\Psi^{\sf binary}_{\sf thres}(\mathcal{D})=0.

By joining θLS{\bm{\theta}}_{{\rm LS}} and θlog{\bm{\theta}}_{{\rm log}} using Proposition A.0, concatenating with θbin{\bm{\theta}}_{{\rm bin}} before, and concatenating with one additional attention layer with 2 heads after to implement

we obtain a single transformer θ{\bm{\theta}} with

which outputs Eq. 45 as its prediction (at the location for y^N+1\widehat{y}_{N+1}).

It remains to show that Eq. 45 reduces to either one of y^N+1log\widehat{y}^{{\rm log}}_{N+1} or y^N+1LS\widehat{y}^{{\rm LS}}_{N+1}. When the data are binary (yi∈{0,1}y_{i}\in{\{0,1\}}), we have Ψbinary(D)=1\Psi^{\sf binary}(\mathcal{D})=1 and Ψthresbinary(D)=1\Psi^{\sf binary}_{\sf thres}(\mathcal{D})=1, in which case Eq. 45 becomes exactly y^N+1log\widehat{y}_{N+1}^{{\rm log}}. By contrast, when data is sampled from a distribution that is (C,ε0)(C,\varepsilon_{0})-not-concentrated around {0,1}{\{0,1\}}, we have for any fixed ε≤ε0∧14C\varepsilon\leq\varepsilon_{0}\wedge\frac{1}{4C} that, letting Bε:=[−ε,ε]∪[1−ε,1+ε]B_{\varepsilon}\mathrel{\mathop{:}}=[-\varepsilon,\varepsilon]\cup[1-\varepsilon,1+\varepsilon] and pε:=Py(Bε)≤Cε≤14\mathsf{p}_{\varepsilon}\mathrel{\mathop{:}}=\mathsf{P}_{y}(B_{\varepsilon})\leq C\varepsilon\leq\frac{1}{4}, by Hoeffding’s inequality,

where c′>0c^{\prime}>0 is an absolute constant. On the event Ψthresbinary(D)=0\Psi^{\sf binary}_{\sf thres}(\mathcal{D})=0 (which happens with probability at least 1−exp⁡(−c′N)1-\exp(-c^{\prime}N)), Eq. 45 becomes exactly y^N+1LS\widehat{y}^{{\rm LS}}_{N+1}. This finishes the proof. ∎

H.4 Linear correlation test and application

For any fixed parameters λmin⁡,Bw⋆>0\lambda_{\min},B_{w}^{\star}>0, consider the linear correlation test over data D\mathcal{D} defined as

We show that Ψlin\Psi^{\sf lin} can be exactly implemented by a 3-layer transformer.

There exists a 3-layer attention-only transformer TFθ{\rm TF}_{\bm{\theta}} with at most 22 heads per layer and ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤O(1+λmin⁡2(Bw⋆)2){\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq\mathcal{O}(1+\lambda_{\min}^{2}(B_{w}^{\star})^{2}) such that on input sequence H{\mathbf{H}} of the form Eq. 3 with D≥2d+4D\geq 2d+4, the transformer exactly implements Ψlin\Psi^{\sf lin}: it outputs H~\widetilde{{\mathbf{H}}} such that h~i=[xi;yiti;∗;Ψlin(D);1]\widetilde{{\mathbf{h}}}_{i}=[{\mathbf{x}}_{i};y_{i}t_{i};*;\Psi^{\sf lin}(\mathcal{D});1] for all i∈[N+1]i\in[N+1].

We begin by noting the following basic facts:

Identity function can be implemented exactly by two ReLUs: t=σ(t)−σ(−t)t=\sigma(t)-\sigma(-t).

We construct the transformer θ{\bm{\theta}} as follows.

Layer 1: Use 2 heads to implement t^=1N∑i=1Nxiyi\widehat{{\mathbf{t}}}=\frac{1}{N}\sum_{i=1}^{N}{\mathbf{x}}_{i}y_{i}, where V{1,2}(1)hj=[±xj;0D−d]{\mathbf{V}}^{(1)}_{{\{1,2\}}}{\mathbf{h}}_{j}=[\pm{\mathbf{x}}_{j};{\mathbf{0}}_{D-d}], Q{1,2}(1)hi=[N+1N;0D−1]{\mathbf{Q}}^{(1)}_{{\{1,2\}}}{\mathbf{h}}_{i}=[\frac{N+1}{N};{\mathbf{0}}_{D-1}], and K{1,2}(1)hj=[±yjtj;0D−1]=[±yj1{j<N+1};0D−1]{\mathbf{K}}^{(1)}_{{\{1,2\}}}{\mathbf{h}}_{j}=[\pm y_{j}t_{j};{\mathbf{0}}_{D-1}]=[\pm y_{j}1\{j<N+1\};{\mathbf{0}}_{D-1}] (where we recall tj=1{j<N+1}t_{j}=1\{j<N+1\} and note that yjtjy_{j}t_{j} corresponds exactly to the location for yjy_{j} in H{\mathbf{H}}, cf. Eq. 3). By manipulating the output dimension in V(1){\mathbf{V}}^{(1)}, write the result t^\widehat{{\mathbf{t}}} into blank memory space with dimension dd at every token i∈[N+1]i\in[N+1].

Layer 2: Use a single head to compute ∥t^∥22\|{\widehat{{\mathbf{t}}}}\|_{2}^{2}: Q1(2)hi(1)=[t^;0D−d]{\mathbf{Q}}^{(2)}_{1}{\mathbf{h}}_{i}^{(1)}=[\widehat{{\mathbf{t}}};{\mathbf{0}}_{D-d}], K1(2)hj(1)=[t^;0D−d]{\mathbf{K}}^{(2)}_{1}{\mathbf{h}}_{j}^{(1)}=[\widehat{{\mathbf{t}}};{\mathbf{0}}_{D-d}], and V1(2)hj(1)=[1;0D−1]{\mathbf{V}}^{(2)}_{1}{\mathbf{h}}_{j}^{(1)}=[1;{\mathbf{0}}_{D-1}]. By manipulating the output dimension in V(2){\mathbf{V}}^{(2)}, write the result ∥t^∥22\|{\widehat{{\mathbf{t}}}}\|_{2}^{2} into blank memory space with dimension 1 at every token i∈[N+1]i\in[N+1]. After layer 2, we have hi(3)=[xi;yiti;∗;∥t^∥22;∗;1]{\mathbf{h}}_{i}^{(3)}=[{\mathbf{x}}_{i};y_{i}t_{i};*;\|{\widehat{{\mathbf{t}}}}\|_{2}^{2};*;1].

Layer 3: Use 2 heads to implement two ReLU functions with bias: ∥t^∥22↦1B−A(σ(∥t^∥22−A)−σ(∥t^∥22−B))\|{\widehat{{\mathbf{t}}}}\|_{2}^{2}\mapsto\frac{1}{B-A}(\sigma(\|{\widehat{{\mathbf{t}}}}\|_{2}^{2}-A)-\sigma(\|{\widehat{{\mathbf{t}}}}\|_{2}^{2}-B)). The two query (or key) matrices contain values AA and BB. In our problem we take

so that the above ReLU function implements Ψlin(D)\Psi^{\sf lin}(\mathcal{D}) exactly. Write the result into a blank memory space with dimension 1. We finish the proof by noting that ∣ ⁣∣ ⁣∣θ∣ ⁣∣ ⁣∣≤O(1+λmin⁡2(Bw⋆)2){\left|\!\left|\!\left|{{\bm{\theta}}}\right|\!\right|\!\right|}\leq\mathcal{O}(1+\lambda_{\min}^{2}(B_{w}^{\star})^{2}). ∎

We consider the following well-posedness assumption for the linear correlation test Ψlin\Psi^{\sf lin}. Note that, similar as Assumption, the assumption does not require the data to be generated from any true linear model, but rather only requires some properties about the best linear fit wP⋆{\mathbf{w}}^{\star}_{\mathsf{P}}, as well as sub-Gaussianity conditions.

∥x∥2≤Bx\|{{\mathbf{x}}}\|_{2}\leq B_{x} and ∣y∣≤By\left|y\right|\leq B_{y} almost surely;

The whitened vector ΣP−1/2x{\mathbf{\Sigma}}_{\mathsf{P}}^{-1/2}{\mathbf{x}} is K2K^{2}-sub-Gaussian for some K≥1K\geq 1.

The label yy is σ2\sigma^{2}-sub-Gaussian.

The residual z:=y−⟨x,wP⋆⟩z\mathrel{\mathop{:}}=y-\left\langle{\mathbf{x}},{\mathbf{w}}^{\star}_{\mathsf{P}}\right\rangle is σ2\sigma^{2}-sub-Gaussian with probability one (over x{\mathbf{x}}).

The following results states that Ψlin\Psi^{\sf lin} achieves high power as long as the sample size is high enough, and the signal ∥wP⋆∥2\|{{\mathbf{w}}^{\star}_{\mathsf{P}}}\|_{2} is either sufficiently high or sufficiently low.

Suppose distribution P\mathsf{P} satisfies Assumption with parameters λmin⁡,λmax⁡,Bw⋆‾\lambda_{\min},\lambda_{\max},\overline{B_{w}^{\star}}. Then, for the linear correlation test Ψlin\Psi^{\sf lin} with parameters (λmin⁡,Bw⋆)(\lambda_{\min},B_{w}^{\star}) with Bw⋆≤Bw⋆‾B_{w}^{\star}\leq\overline{B_{w}^{\star}} and any N≥O~(max⁡{K4,λmax⁡K2σ2(Bw⋆)2λmin⁡2}⋅d)N\geq\widetilde{\mathcal{O}}{\left(\max{\{K^{4},\frac{\lambda_{\max}K^{2}\sigma^{2}}{(B_{w}^{\star})^{2}\lambda_{\min}^{2}}\}}\cdot d\right)}, we have

If ∥wP⋆∥2≥Bw⋆\|{{\mathbf{w}}^{\star}_{\mathsf{P}}}\|_{2}\geq B_{w}^{\star}, then with probability at least 1−δ1-\delta over D\mathcal{D}, we have Ψlin(D)=1\Psi^{\sf lin}(\mathcal{D})=1.

If ∥wP⋆∥2≤λmin⁡10λmax⁡Bw⋆\|{{\mathbf{w}}^{\star}_{\mathsf{P}}}\|_{2}\leq\frac{\lambda_{\min}}{10\lambda_{\max}}B_{w}^{\star}, then with probability at least 1−δ1-\delta over D\mathcal{D}, we have Ψlin(D)=0\Psi^{\sf lin}(\mathcal{D})=0.

The proof is finished by recalling the definition of Ψlin\Psi^{\sf lin} in Eq. 46, so that Ψlin(D)=1\Psi^{\sf lin}(\mathcal{D})=1 if ∥t^∥2≥3λmin⁡Bw⋆/4\|{\widehat{{\mathbf{t}}}}\|_{2}\geq 3\lambda_{\min}B_{w}^{\star}/4, and Ψlin(D)=0\Psi^{\sf lin}(\mathcal{D})=0 if ∥t^∥2≤λmin⁡Bw⋆/4\|{\widehat{{\mathbf{t}}}}\|_{2}\leq\lambda_{\min}B_{w}^{\star}/4. ∎

By directly composing the linear correlation test in Lemma H.0 with the transformer construction in Corollary 5 (using an argument similar as the proof of Proposition H.0), and using the power of the linear correlation test Proposition H.0, we immediately obtain the following result, which outputs a prediction from (approximately) least squares if ψ^:=Ψlin(D)=1\widehat{\psi}\mathrel{\mathop{:}}=\Psi^{\sf lin}(\mathcal{D})=1, and abstains from predicting if ψ^=0\widehat{\psi}=0. This can be viewed as a form of “confident linear regression”, where the model predicts only if it thinks the linear signal is strong enough.

For any Bw>0B_{w}>0, 0<Bw⋆≤Bw⋆‾0<B_{w}^{\star}\leq\overline{B_{w}^{\star}}, 0≤λmin⁡≤λmax⁡0\leq\lambda_{\min}\leq\lambda_{\max}, ε≤BxBw/10\varepsilon\leq B_{x}B_{w}/10, 0<α≤β0<\alpha\leq\beta with κ:=β/α\kappa\mathrel{\mathop{:}}=\beta/\alpha, there exists a LL-layer attention-only transformer with

(with R:=max⁡{BxBw,By,1}R\mathrel{\mathop{:}}=\max{\{B_{x}B_{w},B_{y},1\}}) such that the following holds. Let N≥O~(max⁡{K4,λmax⁡K2σ2(Bw⋆)2λmin⁡2}⋅d)N\geq\widetilde{\mathcal{O}}{\left(\max{\{K^{4},\frac{\lambda_{\max}K^{2}\sigma^{2}}{(B_{w}^{\star})^{2}\lambda_{\min}^{2}}\}}\cdot d\right)}. Suppose the input format is Eq. 3 with dimension D≥2d+4D\geq 2d+4. Let ICL instance (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}) be drawn from any distribution P\mathsf{P} satisfying Assumption. Then the transformer outputs a 2-dimensional prediction (within the test token h~N+1\widetilde{{\mathbf{h}}}_{N+1})

If ∥wP⋆∥2≥Bw⋆\|{{\mathbf{w}}^{\star}_{\mathsf{P}}}\|_{2}\geq B_{w}^{\star}, then with probability at least 1−δ1-\delta over D\mathcal{D}, we have ∣y^N+1−⟨w^LS,xN+1⟩∣≤ε|\widehat{y}_{N+1}-\left\langle\widehat{{\mathbf{w}}}_{{\rm LS}},{\mathbf{x}}_{N+1}\right\rangle|\leq\varepsilon, and ψ^=1\widehat{\psi}=1 if D\mathcal{D} is in addition well-conditioned for least squares (in the sense of Eq. 5 with λ=0\lambda=0).

If ∥wP⋆∥2≤λmin⁡10λmax⁡Bw⋆\|{{\mathbf{w}}^{\star}_{\mathsf{P}}}\|_{2}\leq\frac{\lambda_{\min}}{10\lambda_{\max}}B_{w}^{\star}, then with probability at least 1−δ1-\delta over D\mathcal{D}, we have y^N+1=0\widehat{y}_{N+1}=0 and ψ^=0\widehat{\psi}=0.

Appendix I Proof of Theorem 17: Noisy linear model with mixed noise levels

Suppose that N≥0.1dN\geq 0.1d and we write σmax⁡=max⁡k{σk,1},σmin⁡=min⁡k{σk,1}\sigma_{\max}=\max_{k}\{\sigma_{k},1\},\sigma_{\min}=\min_{k}\{\sigma_{k},1\}. Suppose in input format Eq. 3 we have D≥Θ(Kd)D\geq\Theta(Kd). Then there exists a transformer θ{\bm{\theta}} with

such that for any k∈[K]k\in[K], it holds that

if we choose Nval:=∣Dval∣≍N2/3[log⁡K]1/3N_{\sf val}\mathrel{\mathop{:}}=\left|\mathcal{D}_{{\sf val}}\right|\asymp N^{2/3}[\log K]^{1/3}.

The core of the proof of Theorem I.0 is to show that any estimator w^\widehat{{\mathbf{w}}} that achieves small validation loss L^val\widehat{L}_{{\sf val}} must achieve small population loss.

Throughout the rest of this section, recall that we define Ntrain=∣Dtrain∣,Nval=∣Dval∣N_{\sf train}=\left|\mathcal{D}_{\sf train}\right|,N_{\sf val}=\left|\mathcal{D}_{\sf val}\right|, Itrain={i:(xi,yi)∈Dtrain}\mathcal{I}_{\sf train}=\{i:({\mathbf{x}}_{i},y_{i})\in\mathcal{D}_{\sf train}\}, Ival={i:(xi,yi)∈Dval}\mathcal{I}_{\sf val}=\{i:({\mathbf{x}}_{i},y_{i})\in\mathcal{D}_{\sf val}\}, and Xtrain=[xi]i∈Itrain{\mathbf{X}}_{\sf train}=[{\mathbf{x}}_{i}]_{i\in\mathcal{I}_{\sf train}}.

Fix parameters δ,ε,γ>0\delta,\varepsilon,\gamma>0 and a large universal constant C0C_{0}. Let us set

Then, we define good events similarly to the proof of Corollary 6 (Section D.4):

For the ridge λk=dσk2Ntrain\lambda_{k}=\frac{d\sigma_{k}^{2}}{N_{\sf train}} and parameters (α,β,γ,ε)(\alpha,\beta,\gamma,\varepsilon), we consider the transformer θ{\bm{\theta}} constructed in Theorem H.0, with a clipped prediction y^N+1=read~y(TFθ(H))\widehat{y}_{N+1}=\widetilde{\sf read}_{\sf y}({\rm TF}_{{\bm{\theta}}}({\mathbf{H}})).

and we analyze these two parts separately.

Recall that by our construction, when E\mathcal{E} holds, we have y^N+1=clipBy(⟨w^,xN+1⟩)\widehat{y}_{N+1}=\mathsf{clip}_{B_{y}}(\left\langle\widehat{{\mathbf{w}}},{\mathbf{x}}_{N+1}\right\rangle) and the statements of Theorem H.0 hold for w^\widehat{{\mathbf{w}}}. Thus, we have

Let us consider the following risk functional

Then, under the good event E0:=Eπ∩Ew∩Eb,train∩Eb,test\mathcal{E}_{0}\mathrel{\mathop{:}}=\mathcal{E}_{\pi}\cap\mathcal{E}_{w}\cap\mathcal{E}_{b,{\sf train}}\cap\mathcal{E}_{b,{\sf test}} of (w⋆,D)({\mathbf{w}}_{\star},\mathcal{D}),

By our construction, under the good event E0\mathcal{E}_{0}, we have

where \big{\|}\widehat{{\mathbf{w}}}_{l}(\mathcal{D}_{{\sf train}}))-{\mathbf{w}}^{\lambda_{l}}_{\rm ridge}(\mathcal{D}_{{\sf train}})\big{\|}_{2}\leq\varepsilon for each l∈[K]l\in[K]. Clearly,

We next deal with the term εval:=max⁡l∈[K]∣L^val(w^l(Dtrain))−Lval,w⋆(w^l(Dtrain))∣\varepsilon_{{\sf val}}\mathrel{\mathop{:}}=\max_{l\in[K]}\left|\widehat{L}_{{\sf val}}(\widehat{{\mathbf{w}}}_{l}(\mathcal{D}_{{\sf train}}))-L_{{\sf val},{\mathbf{w}}_{\star}}(\widehat{{\mathbf{w}}}_{l}(\mathcal{D}_{{\sf train}}))\right|. Note that for the good event Etrain:=Eπ∩Ew∩Eb,train\mathcal{E}_{{\sf train}}\mathrel{\mathop{:}}=\mathcal{E}_{\pi}\cap\mathcal{E}_{w}\cap\mathcal{E}_{b,{\sf train}} of (w⋆,Dtrain)({\mathbf{w}}_{\star},\mathcal{D}_{{\sf train}}), we have

Therefore, we can choose (ε,Nval)(\varepsilon,N_{\sf val}) so that Nval≤N/2N_{\sf val}\leq N/2 as

It is worth noting that such choice of NvalN_{\sf val} is feasible as long as N≳Bw4σmax⁡4log⁡KN\gtrsim\frac{B_{w}^{4}}{\sigma_{\max}^{4}}\log K. Under such choice, we obtain

Similar to the proof of Corollary 6, we have

I.2 Derivation of the exact Bayes predictor

Let (D,xN+1,yN+1)(\mathcal{D},{\mathbf{x}}_{N+1},y_{N+1}) be (N+1)(N+1) observations from the data generating model π\pi considered in Section 4.1.1. On observing (D,xN+1)(\mathcal{D},{\mathbf{x}}_{N+1}), the Bayes predictor of yN+1y_{N+1} is given by its posterior mean:

(Note that the sample covariance within Σ^k′\widehat{{\mathbf{\Sigma}}}_{k^{\prime}} is not normalized by NN, which is not to be confused with remaining parts within the paper.) Therefore, the posterior mean Eq. 47 is exactly a weighted combination of KK ridge regression estimators, each with regularization dσk2/Nd\sigma_{k}^{2}/N.

Note that such mixing weights involve the determinant of the matrix Σ^k′=X⊤X+dσk′2Id\widehat{{\mathbf{\Sigma}}}_{k^{\prime}}={\mathbf{X}}^{\top}{\mathbf{X}}+d\sigma_{k^{\prime}}^{2}{\mathbf{I}}_{d}, which depends on the data X{\mathbf{X}} in a non-trivial fashion; Any transformer has to approximate these weights if their mechanism is to directly approximate the exact Bayesian predictor Eq. 47.

I.3 Useful lemmas

We denote yt=[yi]i∈Itrain{\mathbf{y}}_{t}=[y_{i}]_{i\in\mathcal{I}_{\sf train}}, then by definition wridgeλk(Dtrain)=(Xtrain⊤Xtrain+dσk2)−1Xtrainyt{\mathbf{w}}_{\rm ridge}^{\lambda_{k}}(\mathcal{D}_{\sf train})=({\mathbf{X}}_{\sf train}^{\top}{\mathbf{X}}_{\sf train}+d\sigma_{k}^{2})^{-1}{\mathbf{X}}_{\sf train}{\mathbf{y}}_{t} (with λk=dσk2/Ntrain\lambda_{k}=d\sigma_{k}^{2}/N_{\sf train}). Thus, a simple calculation yields

where in the above inequality we denote Σ:=Xtrain⊤Xtrain+dσk2Id{\mathbf{\Sigma}}\mathrel{\mathop{:}}={\mathbf{X}}_{\sf train}^{\top}{\mathbf{X}}_{\sf train}+d\sigma_{k}^{2}{\mathbf{I}}_{d} and use the following fact:

Case 1. We first suppose that Ntrain≤16dN_{\sf train}\leq 16d. Then by definition Σ⪰dσk2Id{\mathbf{\Sigma}}\succeq d\sigma_{k}^{2}{\mathbf{I}}_{d}, and hence

Combining these two cases finishes the proof. ∎

Condition on the event Etrain\mathcal{E}_{{\sf train}}, we have

where we denote w^l=w^l(Dtrain)\widehat{{\mathbf{w}}}_{l}=\widehat{{\mathbf{w}}}_{l}(\mathcal{D}_{{\sf train}}).

We only need to work with a fixed pair of (w⋆,Dtrain)({\mathbf{w}}_{\star},\mathcal{D}_{{\sf train}}) such that Etrain\mathcal{E}_{{\sf train}} holds. Hence, in the following we only consider the randomness of Dval\mathcal{D}_{{\sf val}} conditional on such a (w⋆,Dtrain)({\mathbf{w}}_{\star},\mathcal{D}_{{\sf train}}).

Note that under Etrain\mathcal{E}_{{\sf train}}, we have w^l∈B2(Bw)\widehat{{\mathbf{w}}}_{l}\in\mathsf{B}_{2}(B_{w}) for all l∈[K]l\in[K], and hence σk2+∥w⋆−w^l∥2≤5Bw2\sigma_{k}^{2}+\|{\mathbf{w}}_{\star}-\widehat{{\mathbf{w}}}_{l}\|^{2}\leq 5B_{w}^{2}. We then have (yi−⟨xi,w^l⟩)2(y_{i}-\langle{\mathbf{x}}_{i},\widehat{{\mathbf{w}}}_{l}\rangle)^{2}’s are (conditional) i.i.d random variables in SE(CBw4){\rm SE}(CB_{w}^{4}). Then, by Bernstein’s inequality, we have

where cc is a universal constant. Applying the union bound, we obtain

Taking integration completes the proof. ∎

I.4 Generalized linear models with adaptive link function selection

For any k⋆∈[K]k^{\star}\in[K] and any distribution P\mathsf{P} that is a generalized linear model of the link function gk⋆g_{k^{\star}} and some parameter β{\bm{\beta}}, if Assumption Assumption holds for each pair (P,gk)(\mathsf{P},g_{k}), then

For each k∈[K]k\in[K], we consider optimizing the following training loss:

Fix a large universal constant C0C_{0}. Let us set

Then, we define good events similarly to the proof of Corollary 6 (Section D.4):

Similar to the proof of Theorem 8 (Section E.2), we know the good event E:=Ew∩Er∩Eb,train∩Eb,test∩Eb,N+1\mathcal{E}\mathrel{\mathop{:}}=\mathcal{E}_{w}\cap\mathcal{E}_{r}\cap\mathcal{E}_{b,{\sf train}}\cap\mathcal{E}_{b,{\sf test}}\cap\mathcal{E}_{b,N+1} holds with high probability: P(Ec)≤O(N−10)\mathsf{P}(\mathcal{E}^{c})\leq\mathcal{O}\left(N^{-10}\right).

Similar to the proof of Theorem H.0, we can show that there exists a transformer θ{\bm{\theta}} with prediction y^N+1=read~y(TFθ(H))\widehat{y}_{N+1}=\widetilde{\sf read}_{\sf y}({\rm TF}_{{\bm{\theta}}}({\mathbf{H}})) (clipped by ByB_{y}), such that (for any P\mathsf{P}) the following holds under E\mathcal{E}:

For each k∈[K]k\in[K], fk=Ak(Dtrain)f_{k}=\mathcal{A}_{k}(\mathcal{D}_{\sf train}) is a predictor such that ∣fk(xi)−gk(⟨xi,wGLM(k)⟩)∣≤ε\left|f_{k}({\mathbf{x}}_{i})-g_{k}(\langle{\mathbf{x}}_{i},{\mathbf{w}}_{\rm GLM}^{(k)}\rangle)\right|\leq\varepsilon for all i∈[N+1]i\in[N+1] (where ε\varepsilon is chosen as in Section E.2).

y^N+1=clipBy(f^(xN+1))\widehat{y}_{N+1}=\mathsf{clip}_{B_{y}}(\widehat{f}({\mathbf{x}}_{N+1})), where f^=ATF(D)\widehat{f}=\mathcal{A}_{\sf TF}(\mathcal{D}) is an aggregated predictor given by f^=∑kλkfk\widehat{f}=\sum_{k}\lambda_{k}f_{k}, such that (λk)(\lambda_{k}) is a distribution supported on k∈[K]k\in[K] such that L^val(fk)≤min⁡k′∈[K]L^val(fk′)+γ.\widehat{L}_{{\sf val}}(f_{k})\leq\min_{k^{\prime}\in[K]}\widehat{L}_{{\sf val}}(f_{k^{\prime}})+\gamma.

Similar to the proof of Theorem 8, for E0:=Ew∩Er∩Eb,train∩Eb,test\mathcal{E}_{0}\mathrel{\mathop{:}}=\mathcal{E}_{w}\cap\mathcal{E}_{r}\cap\mathcal{E}_{b,{\sf train}}\cap\mathcal{E}_{b,{\sf test}}, we have

For the first term, repeating the argument in the proof of Theorem 8 directly yields that for Etrain:=Ew∩Er∩Eb,train\mathcal{E}_{\sf train}\mathrel{\mathop{:}}=\mathcal{E}_{w}\cap\mathcal{E}_{r}\cap\mathcal{E}_{b,{\sf train}},

For the second term, similar to Lemma I.0, we can show that conditional on Dtrain\mathcal{D}_{\sf train} such that Etrain\mathcal{E}_{\sf train} holds, it holds

Combining these inequalities and suitably choosing γ\gamma complete the proof. ∎

Appendix J Proofs for Section 5

For any p∈[1,∞]p\in[1,\infty], let ∥H∥2,p:=(∑i=1N∥hi∥2p)1/p\left\|{\mathbf{H}}\right\|_{2,p}\mathrel{\mathop{:}}=(\sum_{i=1}^{N}\left\|{{\mathbf{h}}_{i}}\right\|_{2}^{p})^{1/p} denote the column-wise (2,p)(2,p)-norm of H{\mathbf{H}}. For any radius R>0{\sf R}>0, we denote HR:={H:∥H∥2,∞≤R}\mathcal{H}_{\sf R}\mathrel{\mathop{:}}=\{{\mathbf{H}}:\left\|{\mathbf{H}}\right\|_{2,\infty}\leq{\sf R}\} be the ball of radius R{\sf R} under norm ∥⋅∥2,∞\left\|\cdot\right\|_{2,\infty}.

For a single MLP layer θmlp=(W1,W2){\bm{\theta}}_{\tt mlp}=({\mathbf{W}}_{1},{\mathbf{W}}_{2}), we introduce its norm (as in (2))

For any fixed hidden dimension D′D^{\prime}, we consider

Then for H∈HR{\mathbf{H}}\in\mathcal{H}_{\sf R}, θattn∈Θattn,B{\bm{\theta}}_{\tt attn}\in\Theta_{{\tt attn},B}, the function (θattn,H)↦Attnθattn(H)({\bm{\theta}}_{\tt attn},{\mathbf{H}})\mapsto{\rm Attn}_{{\bm{\theta}}_{\tt attn}}({\mathbf{H}}) is (B2R3)(B^{2}{\sf R}^{3})-Lipschitz w.r.t. θattn{\bm{\theta}}_{\tt attn} and (1+B3R2)(1+B^{3}{\sf R}^{2})-Lipschitz w.r.t. H{\mathbf{H}}.

Now, for θattn′={(Vm′,Qm′,Km′)}m∈[M]\theta_{{\tt attn}}^{\prime}={\{({\mathbf{V}}_{m}^{\prime},{\mathbf{Q}}_{m}^{\prime},{\mathbf{K}}_{m}^{\prime})\}}_{m\in[M]}, we consider

Clearly ∥Attnθattn(H)−Attnθattn′(H)∥2,∞=max⁡i∥h~i−h~i′∥2\left\|{\rm Attn}_{{\bm{\theta}}_{\tt attn}}({\mathbf{H}})-{\rm Attn}_{\theta_{{\tt attn}}^{\prime}}({\mathbf{H}})\right\|_{2,\infty}=\max_{i}\left\|{\widetilde{{\mathbf{h}}}_{i}-\widetilde{{\mathbf{h}}}_{i}^{\prime}}\right\|_{2}. For any i∈[N]i\in[N], we have

where the second inequality uses the definition of operator norm, the third inequality follows from the triangle inequality, the forth inequality is because ∥Qmhi∥2≤BR,∥Kmhj∥2≤BR\left\|{{\mathbf{Q}}_{m}{\mathbf{h}}_{i}}\right\|_{2}\leq B{\sf R},\left\|{{\mathbf{K}}_{m}{\mathbf{h}}_{j}}\right\|_{2}\leq B{\sf R}, and σ\sigma is 1-Lipschitz. This completes the proof the Lipschitzness w.r.t. θattn{\bm{\theta}}_{\tt attn}.

Similarly, we consider H′=[hi′]{\mathbf{H}}^{\prime}=[{\mathbf{h}}_{i}^{\prime}], and

where the last inequality uses ∣ ⁣∣ ⁣∣θattn∣ ⁣∣ ⁣∣≤B{\left|\!\left|\!\left|{{\bm{\theta}}_{\tt attn}}\right|\!\right|\!\right|}\leq B and the AM-GM inequality. This completes the proof the Lipschitzness w.r.t. H{\mathbf{H}}. ∎

For a fixed number of heads MM and hidden dimension D′D^{\prime}, we consider

Then for the function TFR{\rm TF}^{\sf R} given by

TFR{\rm TF}^{\sf R} is BΘB_{\Theta}-Lipschitz w.r.t θ{\bm{\theta}} and LHL_{H}-Lipschitz w.r.t. H{\mathbf{H}}, where BΘ:=BR(1+BR2+B3R2)B_{\Theta}:=B{\sf R}(1+B{\sf R}^{2}+B^{3}{\sf R}^{2}) and BH:=(1+B2)(1+B2R3)B_{H}:=(1+B^{2})(1+B^{2}{\sf R}^{3}).

For any θ=(θattn,θmlp){\bm{\theta}}=({\bm{\theta}}_{\tt attn},{\bm{\theta}}_{\tt mlp}), H∈HR{\mathbf{H}}\in\mathcal{H}_{\sf R}, and θ′=(θattn′,θmlp′)\theta^{\prime}=(\theta_{{\tt attn}}^{\prime},\theta_{{\tt mlp}}^{\prime}), we have

where the second inequality follows from Lemma J.0 and Lemma J.0 and the fact that ∥Attnθattn(H)∥2,∞≤R‾:=R+B3R3\left\|{\rm Attn}_{{\bm{\theta}}_{\tt attn}}({\mathbf{H}})\right\|_{2,\infty}\leq\overline{{\sf R}}\mathrel{\mathop{:}}={\sf R}+B^{3}{\sf R}^{3} for all H∈HR{\mathbf{H}}\in\mathcal{H}_{\sf R}.

Furthermore, for H′∈HR{\mathbf{H}}^{\prime}\in\mathcal{H}_{\sf R}, we have

which also follows from Lemma J.0 and Lemma J.0. ∎

For a fixed number of heads MM and hidden dimension D′D^{\prime}, we consider

Then the function TFR{\rm TF}^{\sf R} is (LBHL−1BΘ)(LB_{H}^{L-1}B_{\Theta})-Lipschitz w.r.t θ∈ΘTF,L,B{\bm{\theta}}\in\Theta_{{\rm TF},L,B} for any fixed H{\mathbf{H}}.

For θ=θ(1:L)∈ΘTF,L,B,θ~=θ~(1:L)∈ΘTF,L,B{\bm{\theta}}={\bm{\theta}}^{(1:L)}\in\Theta_{{\rm TF},L,B},\widetilde{{\bm{\theta}}}=\widetilde{{\bm{\theta}}}^{(1:L)}\in\Theta_{{\rm TF},L,B}, we have

where the second inequality follows from Corollary J.0, and the last inequality is because BH≥1B_{H}\geq 1. ∎

J.2 Proof of Theorem 20

In this section, we prove a slightly more general result by considering the general ICL loss

where Z(1:n){\mathbf{Z}}^{(1:n)} are i.i.d copies of Z∼P,P∼π{\mathbf{Z}}\sim\mathsf{P},\mathsf{P}\sim\pi. It remains to apply Proposition A.0 to the random process {Xθ}\{X_{{\bm{\theta}}}\}. We verify the preconditions:

(a) By [84, Example 5.8], it holds that log⁡N(δ;B∣ ⁣∣ ⁣∣⋅∣ ⁣∣ ⁣∣(r),∣ ⁣∣ ⁣∣⋅∣ ⁣∣ ⁣∣)≤L(3MD2+2DD′)log⁡(1+2r/δ)\log N(\delta;\mathsf{B}_{{\left|\!\left|\!\left|{\cdot}\right|\!\right|\!\right|}}(r),{\left|\!\left|\!\left|{\cdot}\right|\!\right|\!\right|})\leq L(3MD^{2}+2DD^{\prime})\log(1+2r/\delta), where B∣ ⁣∣ ⁣∣⋅∣ ⁣∣ ⁣∣(r)\mathsf{B}_{{\left|\!\left|\!\left|{\cdot}\right|\!\right|\!\right|}}(r) is any ball of radius rr under norm ∣ ⁣∣ ⁣∣⋅∣ ⁣∣ ⁣∣{\left|\!\left|\!\left|{\cdot}\right|\!\right|\!\right|}.

Therefore, we can apply the uniform concentration result in Proposition A.0 to obtain that, with probability at least 1−ξ1-\xi,

J.3 Proof of Theorem 21

By Corollary 5, there exists a transformer TFθ{\rm TF}_{\bm{\theta}} such that for every P\mathsf{P} satisfying Assumption with canonical parameters (and thus in expectation over P∼π\mathsf{P}\sim\pi) and every N≥O~(d)N\geq\widetilde{\mathcal{O}}(d), it outputs prediction y^N+1=read~y(TFθ(H))\widehat{y}_{N+1}=\widetilde{\sf read}_{\sf y}({\rm TF}_{\bm{\theta}}({\mathbf{H}})) such that

We can thus apply Theorem 20 to obtain that the solution θ^\widehat{{\bm{\theta}}} to Eq. TF-ERM with the above choice of (L,M,B)(L,M,B) and D′=0D^{\prime}=0 (attention-only) satisfies the following with probability at least 1−ξ1-\xi:

Above, ι=O(log⁡(1+max⁡{By,R,B}))=O~(1)\iota=\mathcal{O}(\log(1+\max{\{B_{y},{\sf R},B\}}))=\widetilde{\mathcal{O}}(1). This finishes the proof. ∎

J.4 Proof of Theorem 22

We invoke Theorem 11 (using the construction in Theorem 10 with a different choice of LL) with the following parameters:

where O~(⋅)\widetilde{\mathcal{O}}(\cdot) hides polylogarithmic factors in d,N,Bw⋆,κd,N,B_{w}^{\star},\kappa.

By inspecting the proof, the same result holds if we change TFθ{\rm TF}_{\bm{\theta}} to the clipped version TFθR{\rm TF}^{{\sf R}}_{\bm{\theta}} if we choose R2=O(Bx2+By2+(Bw⋆)2+1)=O(d+(Bw⋆)2+σ2){\sf R}^{2}=\mathcal{O}(B_{x}^{2}+B_{y}^{2}+(B_{w}^{\star})^{2}+1)=\mathcal{O}(d+(B_{w}^{\star})^{2}+\sigma^{2}), so that on the good event E0\mathcal{E}_{0} considered therein, all intermediate outputs within TFθ{\rm TF}_{\bm{\theta}} has ∥⋅∥2,∞≤R\left\|\cdot\right\|_{2,\infty}\leq{\sf R} and thus the clipping does not modify the transformer output on the good event. On the bad event E0c\mathcal{E}_{0}^{c}, using the same argument as in the proof of Theorem 11, we have

Combining the above two bounds and further taking expectation over P∼π\mathsf{P}\sim\pi gives

We can thus apply Theorem 20 to obtain that the solution θ^\widehat{{\bm{\theta}}} to Eq. TF-ERM with the above choice of (L,M,B,D′)(L,M,B,D^{\prime}) satisfies the following with probability at least 1−ξ1-\xi:

Above, ι=O(log⁡(1+max⁡{By,R,B}))=O~(1)\iota=\mathcal{O}(\log(1+\max{\{B_{y},{\sf R},B\}}))=\widetilde{\mathcal{O}}(1). This finishes the proof. ∎

J.5 Proof of Theorem 23

We invoke Theorem 17 and Theorem I.0, which shows that (recalling the input dimension D=Θ(Kd)D=\Theta(Kd)) there exists a transformer θ{\bm{\theta}} with the following size bounds:

such that it outputs y^N+1\widehat{y}_{N+1} that satisfies

By inspecting the proof, the same result holds if we change TFθ{\rm TF}_{\bm{\theta}} to the clipped version TFθR{\rm TF}^{{\sf R}}_{\bm{\theta}} if we choose R2=O(Bx2+By2+(Bw⋆)2+1)=O(d+σmax⁡2){\sf R}^{2}=\mathcal{O}(B_{x}^{2}+B_{y}^{2}+(B_{w}^{\star})^{2}+1)=\mathcal{O}(d+\sigma_{\max}^{2}), so that on the good event considered therein, all intermediate outputs within TFθ{\rm TF}_{\bm{\theta}} has ∥⋅∥2,∞≤R\left\|\cdot\right\|_{2,\infty}\leq{\sf R} and thus the clipping does not modify the transformer output on the good event. Using this clipping radius, we obtain

We can thus apply Theorem 20 to obtain that the solution θ^\widehat{{\bm{\theta}}} to Eq. TF-ERM with the above choice of (L,M,B,D′)(L,M,B,D^{\prime}) satisfies the following with probability at least 1−ξ1-\xi:

Above, ι=O(log⁡(1+max⁡{By,R,B}))=O~(1)\iota=\mathcal{O}(\log(1+\max{\{B_{y},{\sf R},B\}}))=\widetilde{\mathcal{O}}(1). This finishes the proof. ∎

J.6 Proof of Theorem 24

The proof follows from similar arguments as of Theorem 22 and Theorem 23, where we plug in the size bounds (number of layers, heads, and weight norms) from Theorem 8 and Corollary 9. ∎

Appendix K Experimental details

We train a 12-layer encoder-only transformer, where each layer consists of an attention layer as in Definition 1 with M=8M=8 heads, hidden dimension D=64D=64, and ReLU activation (normalized by the sequence length), as well as an MLP layer as in Definition 2 hidden dimension D′=64D^{\prime}=64. We add Layer Normalization after each attention and MLP layer to help optimization, as in standard implementations . We append linear read-in layer and linear read-out layer before and after the transformer respectively, both applying a same affine transform to all tokens in the sequence and are trainable. The read-in layer maps any input vector to a DD-dimensional hidden state, and the read-out layer maps a DD-dimensional hidden state to a 1-dimensional scalar.

For the sparse linear regression experiment, we find that minimizing the training objective Eq. 48 alone was not enough, e.g. for the learned transformer to achieve better loss than the least squares algorithm (which achieves much higher test loss than the Lasso; cf. Fig. 4). To help optimization, we augment Eq. 48 with another loss that encourages the second-to-last hidden states to recover the true (sparse) coefficient w⋆{\mathbf{w}}_{\star}:

Specifically, the above loss encourages the first N0≤NN_{0}\leq N tokens within the second-to-last layer to be close to w⋆{\mathbf{w}}^{\star}. We choose N0=5N_{0}=5 (recall that the total number of tokens is N=10N=10 and sequence length is N+1=11N+1=11 for this experiment). We minimize the loss L(θ)+λLfit\mathchar45w(θ)L({\bm{\theta}})+\lambda L_{{\sf fit}\mathchar 45\relax{\sf w}}({\bm{\theta}}) with λ=0.1\lambda=0.1 for 2M steps for this task.

All evaluations are done on the trained transformer with 6400 test instances. We use the square loss for regression tasks, and the classification error (1−1-accuracy) between the true label yN+1∈{0,1}y_{N+1}\in{\{0,1\}} and the predicted label 1{y^N+1≥1/2}1\{\widehat{y}_{N+1}\geq 1/2\}. We report the means in all experiments, as well as their standard deviations (using one-std error bars) in Fig. 2, 2, 5, 5. In Fig. 2, 4, 4 5, all standard deviations are sufficiently small (not significantly exceeding the width of the markers), thus we did not show error bars in those plots.

We implement various baseline machine learning algorithms to compare with the learned transformers. A superset of the algorithms is shown in Fig. 4:

Least squares, Logistic regression: Standard algorithms for linear regression and linear classification, respectively. Note that least squares is also a valid algorithm for classification.

Averaging: The simple algorithm which computes the linear predictor w^=1N∑i=1Nyixi\widehat{{\mathbf{w}}}=\frac{1}{N}\sum_{i=1}^{N}y_{i}{\mathbf{x}}_{i} and predicts y^N+1=⟨w^,xN+1⟩\widehat{y}_{N+1}=\left\langle\widehat{{\mathbf{w}}},{\mathbf{x}}_{N+1}\right\rangle;

Ridge: Standard ridge regression as in Eq. ICRidge. We specifically consider two λ\lambda’s (denoted as lam_1 and lam_2): λ1,λ2=(0.005,0.125)\lambda_{1},\lambda_{2}=(0.005,0.125). These are the Bayes-optimal regularization strengths for the noise levels (σ1,σ2)=(0.1,0.5)(\sigma_{1},\sigma_{2})=(0.1,0.5) respectively under the noisy linear model (cf. Corollary 6), using the formula λ⋆=dσ2/N\lambda^{\star}=d\sigma^{2}/N, with (d,N)=(20,40)(d,N)=(20,40).

Lasso: Standard Lasso as in Eq. ICLasso with λ∈{1,0.1,0.01,0.001}\lambda\in{\{1,0.1,0.01,0.001\}}.

In Fig. 2, the ridge_analytical curve plots the expected risk of ridge regression under the noisy linear model over 20 geometrically spaced values of λ\lambda’s in between (λ1,λ2)(\lambda_{1},\lambda_{2}), using analytical formulae (with Monte Carlo simulations). The Bayes_err_{1,2} indicate the expected risks of λ1\lambda_{1} on task 1 (with noise σ1\sigma_{1}) and λ2\lambda_{2} on task 2 (with noise σ2\sigma_{2}), respectively.

K.2 Computational resource

All our experiments are performed on 8 Nvidia Tesla A100 GPUs (40GB memory). The total GPU time is approximately 5 days (on 8 GPUs), with the largest individual training run taking about a single day on a single GPU.