One-Layer Transformer Provably Learns One-Nearest Neighbor In Context

Transformers have achieved great success in recent years. Interestingly, transformers have shown particularly strong in-context learning capability -- even without fine-tuning, they are still able to solve unseen tasks well purely based on task-specific prompts. In this paper, we study the capability of one-layer transformers in learning one of the most classical nonparametric estimators, the one-nearest neighbor prediction rule. Under a theoretical framework where the prompt contains a sequence of labeled training data and unlabeled test data, we show that, although the loss function is nonconvex when trained with gradient descent, a single softmax attention layer can successfully learn to behave like a one-nearest neighbor classifier. Our result gives a concrete example of how transformers can be trained to implement nonparametric machine learning algorithms, and sheds light on the role of softmax attention in transformer models.

Paper

References (43)

Scroll for more · 31 remaining

Similar papers

Peer review

Reviewer FpKW4/10 · confidence 4/52024-06-19

Summary

This paper studies the gradient descent dynamics of a softmax-activated self-attention unit trained on a population loss over in-context learning (ICL) tasks. Each task entails predicting the binary label of a query input, where the true label is the label of the 1-nearest neighbor (1-NN) in the context, and the context and query come from a particular distribution. The main result is that starting from a particular initialization, the key-times-query weight matrix converges to an infinite-valued matrix that exactly implements a 1-NN predictor. An additional result bounds the error of this predictor when the context and query come from any distribution satisfying a weaker assumption than the training distribution, and the query label is again the 1-NN. Empirical results verify these theoretical results in the analyzed setting, with slight modifications.

Strengths

- The results develop understanding of three key areas that are understudied in the in-context learning (ICL) theory literature: (1) the behavior of *softmax*-activated attention (rather than linear attention), (2) ICL of tasks other than linear regression, and (3) gradient-based optimization dynamics. - Aside from typos, the results are rigorous, well-formulated and non-trivial. - Regarding non-triviality: Lemma 3 is especially helpful to show that the population loss is still nonconvex even when reduced to the loss over the two scalars. - The proof sketch is mostly well-written (see below). - The experiments are well-explained and suggest that some of the simplifications made in the analysis (specific initialization, full-gradient descent on population loss) do not fundamentally change the results.

Weaknesses

1. The required conditions on the training data distribution (context inputs that are uniform on the sphere with labels that are independent Bernoulli random variables with parameter exactly 0.5 and query label that is generated exactly by a 1-NN classifier), especially the training label distribution, are very specific. It is not clear whether the message of the paper can generalize beyond this specific training distribution. Ideally, the paper would present convergence results for a more general class of training distributions, in the same vein as the class of test distributions it considers, and perhaps even when the query label is not generated by a 1-NN classifier. An even more general result may be that the attention unit behaves as a $k$-NN predictor where $k$ depends on some property of the training distribution. 2. The training data distribution is not only specific, but also entails that the label of the query is not generated in the same way as the label of the context examples, which is inconsistent with practice. Specifically each example label must be independent of the corresponding input (as well as all other terms) while the query label does depend on the query and the context examples. The reasoning behind the statement that independence of $\{\mathbf{x}\_{i}\}\_{i\in[N+1]}$ and $\{\mathbf{y}\_{i}\}\_{i\in[N]}$ “is also essential to properly study in-context learning of one-nearest neighbor” is incorrect; $\mathbf{y}\_i$ can depend on $\mathbf{x}\_i$ and predictors of both forms mentioned will achieve 50% accuracy. 3. The distribution shift result is solid but not surprising since 1-NN should behave similarly on all distributions for which the true label of the query is in fact the label of the closest nearest neighbor in the context — it requires no knowledge of the data to achieve strong performance. 4. From Lemma 4 and 5, it is not clear how $\xi^k_1$ grows slower than $\xi^k_2$, as is claimed and as is needed for the final result. For any $\xi^k\_1,\xi\_2^k$, the upper bound on $\xi\_1^{k+1}-\xi^k\_1$ can be dominated by $\exp( \text{poly}( N, d ) \xi\_1^k)$, which can be much larger than the lower bound on $\xi\_2^{k+1}-\xi^k\_2$ of $\exp( -\text{poly}( N, d ) \xi\_2^k)$ even when $\xi_1^k = \Omega(\xi_2^k)$. Minor - The ICL tasks are binary classifications but the loss is the squared error. - In Theorem 1, the term $\log(1 - (N\sqrt{d}^{1/d}))$ should be $\log(1 - (N\sqrt{d}^{-1/d}))$? - Equation 2.6 describes gradient ascent, not gradient descent, and the step size is inverted - Lemma 4 clearly has multiple typos, one of which makes it not clear how to deduce the true statement. - The term “epoch” is improperly used in the experiments section. - The caption of Figure 1 says that error bars are plotted but this is false.

Questions

N/A

Rating

4

Confidence

4

Soundness

3

Presentation

2

Contribution

2

Limitations

The authors have addressed limitations.

Reviewer FWXF6/10 · confidence 3/52024-07-12

Summary

This paper investigates the ability of single-layer transformers to learn the one-nearest neighbor (1-NN) prediction rule through in-context learning. It focuses on how transformers can handle nonparametric methods like 1-NN classification, moving beyond simpler tasks like linear regression that previous studies have focused on. The main contributions include establishing that a transformer with a softmax attention layer can minimize the training loss to zero in a highly non-convex landscape and behave like a 1-NN predictor under data distribution shifts. The authors establish rigorous theoretical results in line with their investigation.

Strengths

**Originality.** To the best of my knowledge, this paper is the first to theoretically study the behavior of in context learning under distribution shifts for softmax attention transformers. In general, most theoretical works in related areas study either one layer model or linear attention models. **Quality.** The paper is well written and the claims are well-substantiated with detailed proofs and theorems **Clarity.** The paper is well-organized and clear. Notations and background are provided well for better understanding. Although I suggest the authors do a grammatical pass since there are minor mistakes throughout the paper.

Weaknesses

1. I like the intuitions and reasoning provided for justifying assumption 1. However, assuming that the data lies on a hypersphere and assuming no class imbalance seems rather strict to me. Further, it is far from practical. I would like to see more insights with respect to relaxing these assumptions. 2. The results show that the model converges to 1-NN predictor on the training data even under SGD and random initialization. How well does this generalize to relaxing conditions on the input data lying on the hypersphere?

Questions

1. Authors mention in the caption of Figure 5 about “error bars”. However, I don’t see any shaded error curve in their figure.

Rating

6

Confidence

3

Soundness

3

Presentation

3

Contribution

3

Limitations

Please refer to the weaknesses section.

Reviewer cVwG6/10 · confidence 3/52024-07-12

Summary

This submission considers learning to implement 1-NN in-context with a single self-attention layer. In particular, they consider training on in-context learning (ICL) sequences of form $(x1, y1), (x2, y2), … (x_N, y_N), x_{N+1}$, where $x_i$ are sampled from the $d$-dimensional unit sphere independently and with uniform probability, $y_i$ are i.i.d. $\pm 1$ labels independent of $x_i$, and $x_{N+1}$ is a test point with a prediction target equal to the prediction of 1-NN. The authors show that with a specific weight initialization gradient descent on the population loss converges to a global minimum that corresponds to implementing 1-NN in-context. The proof relies on the observation that under the proposed weight initialization most parameters stay zero during optimization and it becomes possible to describe the dynamics with only 2 variables. While the loss function written as a function of these 2 variables is nonconvex, they show that these two variables converge to infinity, with their difference converging to infinity too. This limit corresponds to the 1-NN algorithm. The authors also prove that the learned algorithm is robust to distribution shifts, with increasing robustness with the number of gradient descent iterations. Finally, they conduct experiments with random weight initialization and show that a single self-attention layer can be trained to implement 1-NN in-context and be robust to distribution shifts.

Strengths

1. Overall the paper is well-written. The related work is properly referenced. 2. Understanding what learning algorithms transformers can implement in-context and what in-context learning algorithms are learned during training is highly important. A large body of work shows that transformers can implement many of the standard supervised learning algorithms. The main findings of this submission are a good contribution to this body of work and show that transformers can implement 1-NN and under certain conditions learn to implement 1-NN when trained on ICL instances.

Weaknesses

My only concern with this submission is the generality of findings. * While the technique is interesting, it depends critically on the initialization scheme. As the experimental results hint, a single self-attention might be able to learn to implement 1-NN in-context even with standard initialization. It would be great to see a discussion on how the technique employed in this work can be useful for proving convergence under standard initialization. * As I understand the employed technique is also tied to the *k=1* case of k-NN. It is unclear whether the technique is general enough to be useful for $k>1$.

Questions

* Lines 127-133: I recommend expanding this part a bit. Also, $W_{3,3}$ should be $-\xi_2$ so that the softmax attention peaks on the example with highest dot product (i.e., the closest point as all points are on the unit sphere). * In the first equation of Lemma 4, the rightmost term should be $\exp(2\xi_1^k - \xi_2^k)$. * Denoting sequences with index $k$ as $\xi^k_1$ and $\xi^k_2$ is confusing. I recommend using either $\xi^{(k)}_1$ and $\xi^{(k)}_2$ notation or even better, $\xi_k$ and $\zeta_k$.

Rating

6

Confidence

3

Soundness

3

Presentation

3

Contribution

3

Limitations

This submission would benefit form a discussion on the generality of the technique (see the "weaknesses" section).

Reviewer cdZv6/10 · confidence 2/52024-07-14

Summary

This paper studies the theoretical ability of attention layers to implement a 1-nearest-neighbor (1-NN) classifier via in-context learning (ICL). While prior work has studied the ability for transformers to implement algorithms such as linear regression, this paper is the first to establish that attention layers can also implement the 1-NN prediction rule in-context -- a non-parametric learning algorithm, for which in-context learning with attention seems particularly well-suited. The paper contributes the following: a convergence analysis of 1-NN via ICL, a characterization of how ICL 1-NN performs under distribution shift, and under careful initialization, the non-convexity of transformer optimization becomes tractable.

Strengths

- The analysis seems quite complete -- the authors provide theoretical claims surrounding training and initialization, test-time ICL, and out-of-distribution ICL at test time. - The fact that the convergence analysis is done for 1-NN, a non-parametric learner, is conceptually interesting. To me, this idea makes a lot of sense because attention seems to do some form of non-parametric learning in-context at test time anyway. Overall, this gives me some hope that the analysis could be a useful tool for understanding the in-context learning ability of transformers, more generally. - The fact that the convergence analysis, which occurs in a non-convex setting, is solvable using more careful analysis is also interesting.

Weaknesses

- It would be great for the authors to contextualize the work a bit more in terms of understanding transformers as language models, more generally. I understand that this work is primarily theoretical, but I feel that it hints at a key point that isn't coming through very strongly in the text: in-context learning with attention, seems to do a form of non-parametric learning at test time. Understanding how attention implements basic non-parametric learning methods such as 1-NN is a great first step toward understanding how attention does this, and it would be nice to include some commentary (or even speculation) on how this paper could fit into this broader narrative. - Can the analysis be extended in any trivial way to k-NN (e.g. using multiple heads)? There seems to be a lack of commentary on this, and if this is doable in some simple way, this result should be included. If it turns out to be non-trivial, the paper could benefit from commentary on this as well. - Throughout the paper, the authors refer to the input examples (xs and ys) as being either independent or not independent. It was somewhat unclear to me whether independence was being used throughout, or not.

Questions

- In equation 2.2, the softmax output is directly multiplied with the embedding matrix, meaning that the W_v matrix is set to the identity. While this is spelled out in the text, including this in the equation would improve clarity. - In Assumption 2, I think $\sigma_1$ should just be $\sigma$. - Some of the equations run off of the right-hand side of the page in the appendix.

Rating

6

Confidence

2

Soundness

3

Presentation

3

Contribution

3

Limitations

As a theoretical work, social impact is non-applicable. However, the authors do not seem to discuss any limitations of their analysis, more broadly.

Reviewer FWXF2024-08-10

Many thanks for the rebuttal that addresses many of the weaknesses identified and questions raised. I emphasize that all clarifications made during this rebuttal should be made in any revised manuscript to improve clarity of the work. Given my already positive review, I maintain my score.

Authorsrebuttal2024-08-12

Thank you for your positive review. We will carefully revise the paper, and make sure that all clarifications made in the rebuttal are included in the revised version.

Reviewer FpKW2024-08-11

I thank the authors for their thorough response, and for amending the typo in Lemma 4. I would not be opposed to seeing this paper accepted, since it provides novel, sound and non-trivial results towards addressing an important issue. Also, I am convinced that extensions to input data that is not uniform on the hypersphere and k-NN are highly non-trivial and probably worthy of separate papers. However, my main concern remains in that the results only apply to tasks generated by a very specific 1-NN-based data distribution in which the labels of the context examples are (unrealistically) independent of the input, whereas it is reasonable to expect that one softmax attention unit learns to behave like a 1-NN regressor in other settings as well, e.g. if the tasks are sinusoid regressions with high frequency, the best that softmax attention should be able to do is predict the label of the nearest input example.

Authorsrebuttal2024-08-12

Thank you for your constructive comments, and for clarifying that you are not opposed to accepting our work. We agree that generalizing our current data distribution would greatly strengthen our results. After a careful examination of our proof, we believe that the data assumptions on $y_i$ in Assumption 1 could be extended to the following form: (1) $\mathbb{E}(y_i y_j | \mathbf{x}\_{1:N}] = 0$ and $\mathbb{E}[y_i^2 | \mathbf{x}\_{1:N}] = 1$ for all $i \neq j, i,j \in[N]$. (2) $\mathbb{P}(y\_{1:N} | \mathbf{x}\_{1:N}) = \mathbb{P}(y\_{1:N} | -\mathbf{x}\_{1:N}) $. Note that such an assumption holds for a wide range of data distributions beyond the case where $\mathbf{x}_i$ and $y_i$ are independent. For example, the following data generating process gives $y_i$ that depends on $\mathbf{x}_i$, but the conditions (1) and (2) above still hold: Consider an arbitrary fixed vector $\mathbf{a} \in R^d$ with $\|a\|_2 >2$. Suppose that $\mathbf{x}_i$, $i=1,\ldots,N$ are independently generated from the uniform distribution on the unit sphere, and supposed that given $\mathbf{x}_i$, $y_i$ is generated as follows: - $y_i = 0 $ with probability $1- \frac{1}{ \max \\{\langle \mathbf{a}, \mathbf{x}_i \rangle^2, 1\\}} $, - $y_i = \max \\{|\langle \mathbf{a}, \mathbf{x}_i \rangle|, 1\\}$ with probability $\frac{1}{2 \max \\{\langle \mathbf{a}, \mathbf{x}_i \rangle^2, 1\\}} $, - $y_i = -\max \\{|\langle \mathbf{a}, \mathbf{x}_i \rangle|, 1\\}$ with probability $\frac{1}{2 \max \\{\langle \mathbf{a}, \mathbf{x}_i \rangle^2, 1\\}} $. It is easy to verify that $\mathbb{E}[y_i^2|\mathbf{x}\_{1:N}] = \mathbb{E}[y_i^2|\mathbf{x}_i] = 1$, $\mathbb{E}[y_i y_j|\mathbf{x}\_{1:N}] = \mathbb{E}[y_i y_j|\mathbf{x}\_{i},\mathbf{x}\_{j}] = 0$ and $\mathbb{P}(y\_{1:N} | \mathbf{x}\_{1:N}) = \mathbb{P}(y\_{1:N} | -\mathbf{x}\_{1:N}) $. Moreover, $\mathbf{x}_i$ and $y_i$ are not independent, since $\mathbb{E}[y_i^4| \mathbf{x}_i ] = \max\\{\langle \mathbf{a}, \mathbf{x}_i \rangle^2, 1 \\}$ is a function of $\mathbf{x}_i$. We will update the paper to include the more general setting under conditions (1),(2) above. We assure that such an extension only requires minor modifications in the paper, and the proofs do not need any significant change. We believe that including such an extension can significantly strengthen our paper, and we hope that it can address your concerns on the limitation of our data models.

Reviewer cVwG2024-08-11

Thank you for the rebuttal. I will maintain my score.

Authorsrebuttal2024-08-12

Thank you for your positive feedback. In our revision, we will make sure to add clarifications and discussions about the points you have mentioned.

Program Chairsdecision2024-09-25

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC