In-Context Learning with Representations: Contextual Generalization of Trained Transformers

In-context learning (ICL) refers to a remarkable capability of pretrained large language models, which can learn a new task given a few examples during inference. However, theoretical understanding of ICL is largely under-explored, particularly whether transformers can be trained to generalize to unseen examples in a prompt, which will require the model to acquire contextual knowledge of the prompt for generalization. This paper investigates the training dynamics of transformers by gradient descent through the lens of non-linear regression tasks. The contextual generalization here can be attained via learning the template function for each task in-context, where all template functions lie in a linear space with $m$ basis functions. We analyze the training dynamics of one-layer multi-head transformers to in-contextly predict unlabeled inputs given partially labeled prompts, where the labels contain Gaussian noise and the number of examples in each prompt are not sufficient to determine the template. Under mild assumptions, we show that the training loss for a one-layer multi-head transformer converges linearly to a global minimum. Moreover, the transformer effectively learns to perform ridge regression over the basis functions. To our knowledge, this study is the first provable demonstration that transformers can learn contextual (i.e., template) information to generalize to both unseen examples and tasks when prompts contain only a small number of query-answer pairs.

Paper

References (39)

Scroll for more · 27 remaining

Similar papers

Peer review

Reviewer vG3D5/10 · confidence 4/52024-07-08

Summary

This paper studies the training dynamics of multi-head Transformers by gradient descent on non-linear regression tasks via ICL. This work shows the linear convergence to the global minimum of the training and the in-context inference. An impressive contribution is that transformers are proven to learn contextual information to generalize to unseen examples when prompts contain a small number of input-label pairs. I am willing to update my review if my concerns are addressed.

Strengths

1. The problem to be solved is significant, interesting, and challenging. 2. The paper is well-written. 3. The comparison with the related works makes the motivation clear.

Weaknesses

1. It seems that the assumption that $H\geq N$ in Proposition 1 is too strong. The single-head works in Table 1 do not need this condition for few-shot ICL (Obviously, for single-head, $1<N$). I know the problem to solve is different, but this means that Transformers cannot handle long contexts without a large number of heads. 2. No experiments are conducted to verify the theory. It is important to verify the theory by experiments, even with the most simplified Transformer model. For example, can you show if $H<N$, the learning will fail, while if $H\geq N$, the learning becomes successful by a one-layer Transformer? 3. Some statements related to the existing works are wrong. For example, in [Li et al., 2024], $N=\mathcal{O}(\epsilon^{-2}T)$ is the number of data for the pretraining to enable ICL for the Transformer. It is not the number of examples in each prompt as stated in line 129 of the submitted manuscript. The number of examples in each prompt in [Li et al., 2024] is $l\_{tr}$ and $l\_{ts}$ in Eqn 9 of Theorem 3.3. 4. I am unsure whether the theoretical proof is correct. For example, I don't know how you derive linear convergence of the loss via only PL condition and the smoothness of the loss function. Please see Question 1.

Questions

1. Regarding Weakness 1, why do you have $\eta_Q$ in Eqn 63? I think by (41), (60), and Lemma 4.3 in [Nguyen and Mondelli, 2020], $\eta_Q$ should be replaced by $1$ in Eqn 63. 2. What is the requirement for the initialization of $w\_{h,k}$ defined in (7)? 3. It seems there is no requirement or assumption for the $f$ function to be learned. Is your bound derived for the worst case of all the possible $f$?

Rating

5

Confidence

4

Soundness

2

Presentation

3

Contribution

3

Limitations

There is no potential negative societal impact of this work.

Authorsrebuttal2024-08-09

Re: Reviewer vG3D

Thank you very much for your quick response! Below we change $N$ and find the smallest $H$ that achieves a training loss $\leq$ 0.5 within 100 iterations. We set $K=200, d=100, m=20, \tau=0.01$. Our result is as follows: | N | 1 | 5 | 10 | 20 | 30 | 40 | |-----|-----|-----|-----|-----|-----|-----| | H | 1 | 5 | 8 | 29 | 32 | 41 | This result verifies our theoretical finding that with a larger $N$, $H$ should be larger to ensure a small loss. If our response resolves your questions, we will highly appreciate your consideration in raising the score. Certainly, we are more than happy to answer your further questions.

Reviewer vG3D2024-08-09

Thank you for the response. I am wondering why you set the threshold of the training loss as 0.5. If you use a different threshold, will the bound still be tight? By the way, it seems that the result is not consistent with Figure 3 in the submitted PDF, where the case of $H=1$ cannot reach a loss of less than 0.5. Maybe the setting is different?

Authorsrebuttal2024-08-09

Re: Re: Reviewer vG3D

Thank you for your follow-up questions. >**regarding inconsistency with Figure 3** The configuration of the two settings are different. While both cases have $H=1$, in Figure 3, $N=30$ and clearly here $H=1$ cannot reach loss 0.5 (because our above experiment shows that $H$ should be at least 32 to reach loss 0.5 for $N=30$). But in the above experiment, $H=1$ is for $N=1$, where the loss can reach 0.5. We also mention that the parameters $K$ and $\tau$ are also set slightly different, but they are not the main reason to cause the difference. >**If you use a different threshold, will the bound still be tight?** We tried several configurations and found that the nature of the results are insensitive to the thresholds (and other hyperparameters): $N$ always increases along with $H$. In the following we present the results with loss threshold = 0.1 and 0.5 and iteration number = 200, and the other configurations remain the same as the above experiment: | N | 1 | 5 | 10 | 20 | 30 | 40 | |-----|-----|-----|-----|-----|-----|-----| | H (threshold=0.5) | 1 | 5 | 8 | 24 | 29 | 35 | | H (threshold=0.1) | 1 | 7 | 21 | 36 | 51 | 57 |

Reviewer vG3D2024-08-09

Thank you for your response. Although I am still concerned that $H\geq N$ is not tight, the overall trend makes sense. I also like the convergence analysis of this work. I have increased my rating to 5.

Authorsrebuttal2024-08-13

Many thanks for your feedback and increasing the score to support our paper. Regarding $H \ge N$, our numerical result somewhat indicates that $H$ cannot be much smaller than $N$ to guarantee a small loss. The main reason is because the one-layer transformer may not have substantial expressive power and hence could by nature requires a sufficiently large $H$. We suspect that such an assumption may not largely come from our technical tools, but may likely be due to the problem itself. It is great to hear you like the convergence analysis of this work, which we highly appreciate.

Reviewer MzSs6/10 · confidence 4/52024-07-11

Summary

This paper tackles the problem of task learning with representations using transformers. It presents a theoretical proof for convergence of training of a single-layer multi-head softmax attention transformer to the global minimum of the loss. Specifically, given $N$ example tokens and their corresponding task outputs, the transformer learns to predict the task output for unseen tokens $K-N$, where $K$ is the size of the dictionary. The paper shows that when trained for sufficiently long, the transformers can perform of ridge regression on the example tokens to label the unseen tokens.

Strengths

(1) The paper shows that the parameters of a single-layer multi-head softmax attention transformers can be trained at linear convergence rate (Theorem 1) to the global minimizer of the training loss. This result elucidates that the remarkably efficient training that a transformer architecture offers. (2) The paper proves that once trained for sufficiently long, the transformer performs ridge regression (Theorem 2) on the example tokens to label the query tokens during inference. This is a very promising step towards understanding the generalization capabilities of transformers, which is quite different from earlier works that require the number of example tokens $N$ to be large. (3) The paper is written crisp and clean, making it very easy to follow.

Weaknesses

(1) The optimal transformer configuration $(\boldsymbol{\theta})$ is not explicitly stated; only the functional form of the output is given, which makes it difficult to the interpret the result in the parameter $(\boldsymbol{\theta})$ space. (2) The result in this paper is about the wide single-layer transformers (more heads), rather than the deep ones (more layers), and relies on Assumption 3, which holds when the number of heads, $H\ge N$ (Proposition 2). This is restrictive and there might be other ways in which the assumption can hold even if $N > H$. Some empirical demonstrations with shallow transformers on simple experiments might support this better.

Questions

(1) Proposition 1 provides a way to make (almost) sure that Assumption 3 holds, but requiring $H \ge N$ seems very demanding in terms of compute. Can you comment on how to obtain similar results by simpler assumption? (2) I am curios about the motivation behind your problem formulation, i.e., task-based labelling of a finite dictionary? Is there a real-world problem where this might be of use?

Rating

6

Confidence

4

Soundness

3

Presentation

3

Contribution

3

Limitations

(1) One minor limitation is that the performance of the trained transformer depends on the relation between the number $N$ of example tokens and the dimension $m$ of the representational space (lines 295-299).

Authorsrebuttal2024-08-12

Dear Reviewer MzSs, Thank you very much for your response! In addition to what you mentioned as 'memorize' the basis vectors, our results contain two more points: (i) after 'memorizing/learning' basis vectors, the ICL implements **ridge regression** to use such basis information, which is a new characterization; (ii) such tranformers having those properties (memorizing basis vectors and implementing ridge regression) can be learned naturally by gradient descent. Many thanks, Authors

Reviewer TyrV4/10 · confidence 2/52024-07-12

Summary

The paper investigates the theoretical understanding of in-context learning, focusing on whether transformers can generalize to unseen examples within a prompt by acquiring contextual knowledge. The paper analyzes the training dynamics of transformers using non-linear regression tasks, demonstrating that multi-head transformers can predict unlabeled inputs given partially labeled prompts. The study shows that the training loss for a shallow multi-head transformer converges linearly to a global minimum, effectively learning to perform ridge regression. Main contributions include: (1) The paper establishes the convergence guarantee of a shallow transformer with multi-head softmax attention trained with gradient descent on general non-linear regression in-context learning tasks. (2) The paper analyzes the transformer’s behavior at inference time after training, demonstrating that the transformer decides its generating template by performing ridge regression. (3) The analysis framework allows overcoming several assumptions made in previous works, such as the requirement for large prompt lengths, orthogonality of data, restrictive initialization conditions, special structure of the transformer, and super wide models.

Strengths

1. The focus on analyzing training dynamics and convergence guarantees for transformers in ICL tasks is relatively novel. Prior works often concentrate on empirical performance without delving deeply into the theoretical underpinnings. The combination of gradient descent analysis with multi-head softmax attention in the context of non-linear regression is a creative fusion of ideas that opens up new avenues for understanding how transformers learn contextually. By overcoming assumptions such as the need for large prompt lengths, orthogonality of data, and super wide models, the paper advances the state of the art and provides a more realistic framework for understanding transformer performance. 2. The paper’s analysis of the convergence dynamics and ridge regression behavior of transformers is mathematically robust, with detailed proofs and clear logical progression. 3. The paper is well-organized, with a logical flow from the problem statement to the theoretical analysis and conclusions, making it easy to follow the argumentation.

Weaknesses

While the paper excels in theoretical analysis, it lacks empirical validation of the proposed concepts and results. It is suggested to include empirical experiments that demonstrate the practical applicability of the theoretical findings would significantly strengthen the paper. For instance, running experiments on standard benchmarks to show how the proposed theoretical insights translate to improved performance in real-world tasks would validate the results more strongly. The paper’s discussion on the practical implications of the theoretical findings is somewhat limited. This can make it challenging for practitioners to understand how to apply these insights to real-world problems.

Questions

1. Do you have any empirical results or planned experiments that demonstrate the practical applicability of your theoretical findings? 2. What are the potential impacts of the remaining assumptions and simplifications (e.g., specific initialization conditions, structure of the transformer) on the generalizability of your results?

Rating

4

Confidence

2

Soundness

2

Presentation

2

Contribution

3

Limitations

Please review the weaknesses.

Reviewer dubr7/10 · confidence 4/52024-07-29

Summary

This paper presents a rigorous theoretical analysis of the training dynamics and generalization capabilities of a one-layer transformer with multi-head softmax attention for in-context learning (ICL) of non-linear regression tasks. The authors consider a more challenging and realistic setting where prompts contain only a small number of noisy labeled examples, insufficient to fully determine the underlying template function. This addresses limitations in previous theoretical work that often required unrealistically large numbers of examples per prompt. A key theoretical contribution is proving convergence of the training loss to its global minimum at a linear rate when optimizing with gradient descent. This is the first such convergence result for transformers with multi-head softmax attention on ICL tasks. The analysis reveals that the transformer effectively learns to perform ridge regression during training. The paper provides precise bounds on the mean squared error between the transformer's predictions and those of the optimal ridge regression solution, as a function of the number of training iterations. The authors demonstrate that multi-head attention is crucial for succeeding at this ICL task. They provide a lower bound on the number of heads needed (H ≥ N), while also noting that too many heads can slow down convergence. The analysis shows how the multi-head mechanism allows the transformer to approximate the required matrix operations for ridge regression.

Strengths

- Setting : The paper overcomes several limiting assumptions made in previous theoretical work, such as requiring very long prompts, orthogonal input data, restrictive initialization conditions, or unrealistically wide models. Key technical innovations include a novel reformulation of the loss function to remove expectations, proving smoothness and the Polyak-Łojasiewicz condition for the loss landscape, and carefully bounding various matrix norms and eigenvalues throughout training. - Intuition and connection to ridge regression : The analysis provides insight into how the regularization strength in the implicit ridge regression depends on the ratio of the feature dimension m to the number of examples N in each prompt. Also, the authors show that the transformer acquires two types of generalization capabilities: contextual generalization to unseen examples within a task by learning to infer and apply the underlying template function, and generalization to unseen tasks by learning a general strategy (ridge regression) that works across different λ vectors. The paper provides rigorous mathematical analysis in a realistic setting. In doing so it offers novel insights, in particular the notion of implicitly learning to perform ridge regression and acquire generalizable knowledge about the representation function. Also, the paper provides the first convergence guarantees for transformers with multi-head softmax attention on ICL tasks, which is a significant theoretical contribution.

Weaknesses

- Limited model architecture (one-layer transformer) which is understandable - Lack of empirical validation: The paper is purely theoretical and does not include any experimental results to validate its predictions. - Focus on regression : The analysis is limited to regression tasks, which represent only a subset of the problems typically addressed by transformers and in-context learning. Many real-world applications involve classification, sequence generation, or more complex structured prediction tasks. It's not immediately clear how the insights about ridge regression would translate to these other task types. Lack of coomparison to other ICL approaches: The paper doesn't provide a comprehensive comparison to other theoretical approaches to in-context learning. While it does mention some previous work, a more in-depth discussion of how this approach relates to or improves upon other theoretical frameworks for ICL would have provided valuable context.

Questions

Have the authors considered adding any toy domain experiments that would illustrate and further validate their claims ?

Rating

7

Confidence

4

Soundness

3

Presentation

3

Contribution

2

Limitations

- Simplified model: The analysis is limited to a one-layer transformer, which is much simpler than state-of-the-art models used in practice. - Lack of empirical validation: While the theoretical results are impressive, the paper doesn't include experimental results to validate its predictions. - Focus on regression: The paper only considers regression tasks, and it's not immediately clear how these results would generalize to classification or more complex tasks.

Reviewer vG3D2024-08-09

Thank you for the reply. I am satisfied with the other answers except the one to W1. I still think the requirement that $H\geq N$ is too strong and not impractical. It is also not verified how loose the bound $H\geq N$ is by experiments. Can you at least verify by experiments that with a larger context length $N$, the number of heads $H$ should be larger to ensure a small loss?

Authorsrebuttal2024-08-12

kind reminder as the rebuttal deadline approaches

Dear Reviewer TyrV, As the author-reviewer discussion period will end soon, we would like to check whether our responses have properly addressed your concerns? If so, could you please kindly consider increasing your initial score accordingly? Certainly, we are more than happy to answer your further questions. Thank you for your time and effort in reviewing our work! Best Regards, Authors

Authorsrebuttal2024-08-13

Dear Reviewer TyrV, The author-reviewer discussion period will end in less than one day. We sincerely hope to receive your feedback and see if our responses have properly addressed your concerns. If so, could you please kindly consider increasing your initial score accordingly? Certainly, we are more than happy to answer your further questions. Thank you for your time and effort in reviewing our work! Best Regards, Authors

Reviewer MzSs2024-08-12

Thank you for the valuable comments, and for the clarification on the limitation (the relation between $m$ and $N$ affecting the performance) to be a strength of the paper. I agree with you. Despite the clean proof and the result of the fast convergence of the training, the finite dictionary still seems to be a limitation as the transformer can essentially 'memorize' the basis vectors into the value transforms in the heads. As such, the paper is more about how an attention mechanism can 'memorize' efficiently (possibly better than other architectures), which I agree to be novel. However, direct impact of the paper is unclear. Moreover, the construction using a large number of heads is not very appealing. Taking all of these into consideration, I shall keep my (positive) score.

Reviewer dubr2024-08-14

Thanks for your rebuttal and the additional toy experiments, which I find sensible (as exemplified by discussion with Reviewer vG3D) and illustrative. I have therefore increased my score.

Program Chairsdecision2024-09-25

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC