In-Context Denoising with One-Layer Transformers: Connections between Attention and Associative Memory Retrieval
Matthew Smart (Postdoc · Flatiron Institute), Alberto Bietti (Flatiron Institute), Anirvan Sengupta (Flatiron Institute)
Overview
This talk, presented by Matthew Smart and his colleagues at the Flatiron Institute, delves into a novel "in-context denoising" task designed to bridge the theoretical gap between two seemingly distinct architectures: transformer attention mechanisms and associative memory networks. The core premise is that by reframing certain in-context learning (ICL) problems as denoising tasks, a profound connection emerges, revealing that a single layer of transformer attention can precisely execute the operations characteristic of a single gradient descent step within a modern Hopfield network, a type of associative memory.

Key moments
- 0:00 Introduction: Transformers and Associative Memory connections
- 0:40 Dense Associative Memory Networks and capacity improvement
- 1:30 Established connection: Hopfield nets and transformer attention
- 2:00 Introducing the new in-context denoising task
- 2:30 Detailed setup of the in-context denoising task
- 3:30 Three elementary versions of the denoising task
- 4:00 Bayes optimal denoiser for linear manifold task
In-Context Denoising with One-Layer Transformers: Connections between Attention and Associative Memory Retrieval
Speakers: Matthew Smart, Postdoc, Flatiron Institute; Alberto Bietti, Flatiron Institute; Anirvan Sengupta, Flatiron Institute
Conference: ICML 2025
YouTube: https://slideslive.com/39044095
Overview
This talk, presented by Matthew Smart and his colleagues at the Flatiron Institute, delves into a novel "in-context denoising" task designed to bridge the theoretical gap between two seemingly distinct architectures: transformer attention mechanisms and associative memory networks. The core premise is that by reframing certain in-context learning (ICL) problems as denoising tasks, a profound connection emerges, revealing that a single layer of transformer attention can precisely execute the operations characteristic of a single gradient descent step within a modern Hopfield network, a type of associative memory.
The research presented by Smart, Bietti, and Sengupta is significant because it offers a foundational understanding of the underlying computational principles governing transformer attention, particularly in the context of ICL. Despite the widespread success and ubiquity of transformers, the precise mechanisms by which their attention layers process information and perform in-context learning have remained somewhat mysterious. By drawing this explicit link to well-understood dynamical systems like associative memory networks, the work provides a more solid theoretical footing for interpreting attention's role and capabilities, offering new avenues for analysis and potentially for the design of more interpretable and efficient AI systems.
Background
▶ Watch: Introduction: Transformers and Associative Memory connections (0:00)
The talk begins by highlighting two architectural paradigms that, on the surface, appear unrelated. The first is associative memory networks, pioneered by models like Hopfield networks. These are dynamical systems designed to store a set of patterns as minima in an energy function. In Hopfield's original formulation, the network's weights are constructed by taking the sum of outer products of the patterns. The system then performs iterative gradient descent on this energy function, allowing an initial, noisy state to "denoise" and converge to the nearest stored pattern. A significant limitation of these early models was their capacity, which scaled only linearly with the number of neurons or features. However, in the past decade, advancements in dense associative memory networks have introduced elegant tricks, such as using sharper nonlinearities and higher-order kernels, to qualitatively increase storage capacity beyond this linear bound.
The second architecture is the familiar attention layer of transformers. These layers, ubiquitous in modern deep learning, process an input stream of tokens by mapping them to key, query, and value matrices. A comparison between queries and keys determines attention weights, which are then used to project onto the values, producing the layer's output. Despite their widespread adoption and phenomenal success, the precise mechanisms and computational roles of individual attention heads or layers within a transformer architecture are not always fully understood. While some prior work, such as the discovery of "induction heads" at ICLR, has begun to shed light on specific functions, a general, unifying theoretical framework for attention's in-context learning capabilities has been largely elusive.
The motivation for this research stems from the desire to bridge these two fields. Previous work has alluded to connections between transformer attention and associative memory, particularly noting that a single step of gradient descent in a continuous Hopfield net bears a striking resemblance to the softmax self-attention mechanism. The present work aims to solidify this connection, providing a more rigorous theoretical and empirical foundation, and to identify a natural interface where this relationship manifests. This effort is crucial for demystifying the attention mechanism and advancing our understanding of how in-context learning operates within transformer architectures.
Key Findings
▶ Watch: Established connection: Hopfield nets and transformer attention (1:30)
The central contribution of this work is the introduction of a new in-context denoising task, which serves as a natural interface to study the relationship between transformer attention and associative memory. The key findings derived from this task are multi-faceted and provide a compelling theoretical and empirical link:
- Task Definition and Generality: The proposed in-context denoising task involves generating random data distributions, sampling "pure context tokens" from one such distribution, corrupting one of these tokens with isotropic Gaussian noise, and then using the uncorrupted context to denoise the corrupted token, minimizing the mean squared error (MSE). This setup is highly general and was studied across various elementary data distributions, including linear manifolds (subspaces of RN), subspheres (nonlinear tasks), and Gaussian mixtures.
- Attention Layers as Optimal Denoisers: The research rigorously demonstrates that single-layer transformer attention mechanisms are remarkably expressive. Both linear attention and softmax attention layers are shown to be sufficient structures to exactly solve these in-context denoising tasks. This means that, given an appropriate training objective, an attention layer can learn to perfectly denoise the corrupted input by leveraging the contextual information.
- Convergence to Predicted Optimal Weights: Through theoretical analysis, the authors predict the optimal weights for attention layers to match the Bayes optimal denoiser for each task. For the linear subspace denoising problem, linear attention is predicted to converge to scaled identity weights. Similarly, for the nonlinear sphere and Gaussian mixture denoising tasks, softmax attention is predicted to converge to scaled identity weights under specific limits (e.g., variances going to zero, centers on a sphere for Gaussian mixtures).
- Empirical Validation: A critical finding is the strong empirical validation of these theoretical predictions. When trained in a straightforward setup using PyTorch and the Adam optimizer from random initial weights, the attention layers not only successfully perform the denoising task, but their learned weights also precisely converge to the analytically predicted scaled identity weights. This robust agreement between theory and practice lends significant credibility to the proposed framework.
- The Associative Memory Connection: The most profound key finding is the explicit identification of the trained attention layers' operations with a single step of gradient descent in specific associative memory models:
- The trained linear attention layer, with its scaled identity weights, is shown to be equivalent to performing a single gradient descent step within a spherical Hopfield model.
- Likewise, the trained softmax attention layer, again with scaled identity weights, is identified as performing a single gradient descent step within a modern continuous Hopfield net.
- In this interpretation, the query token acts as the initial condition, and the context tokens induce the energy landscape on which this single gradient step is performed. This effectively positions attention layers as performing a form of associative memory retrieval by taking an "optimal single step" on an induced energy landscape.
These findings collectively provide a powerful new lens through which to understand the computational role of transformer attention, explicitly linking its in-context learning capabilities to the well-established dynamics of associative memory networks.
Technical Deep Dive
▶ Watch: Introducing the new in-context denoising task (2:00)
The technical core of this work lies in the formal definition of the in-context denoising task and the subsequent analytical derivation of how transformer attention mechanisms can optimally solve it, revealing their inherent connection to associative memory dynamics.
The in-context denoising task is defined as follows:
Suppose we have a method for generating random data distributions. From one such distribution, we sample $L$ pure context tokens, denoted as $X_1, \dots, X_L$. We then select one of these tokens, say $X_i$, and corrupt it with some noising process. Throughout the talk, isotropic Gaussian noise is assumed for simplicity. This results in a corrupted token, $X_i'$. The goal is to construct a task embedding by combining all $L$ pure context tokens and the single noised token, and then to learn a function $F$ that, given this task embedding, minimizes the mean squared error (MSE) between its prediction and the original uncorrupted token $X_i$. This process is repeated across many realizations of the data distribution, context tokens, and noise.
Three elementary versions of this task were studied:
- Denoising from linear manifolds/subspaces: Here, data points are sampled isotropically Gaussian, centered at the origin. A $D$-dimensional subspace is chosen, and points are projected onto it. Noise is then added. The problem is to denoise a point projected onto this subspace.
- Denoising from nonlinear manifolds (subspheres): The task involves denoising points that lie on a sphere.
- Denoising from Gaussian mixtures: The task involves denoising points sampled from a mixture of Gaussian distributions.
For the linear case, a direct approach for a human would be to project the noisy token onto the subspace spanned by the context tokens and then potentially shrink it, especially if the noise is large. However, a more general and rigorous approach for deriving the optimal denoiser is to recognize that the Bayes optimal denoiser is simply the posterior mean. For the assumed isotropic Gaussian noise, this involves evaluating a ratio of integrals, which can be analytically solved for the specific task distributions (linear subspace, subsphere, Gaussian mixture). The authors confirmed that this general approach yields the same solution as the intuitive linear algebra solution for the linear case.
The crucial insight is that single attention layers are sufficiently expressive to capture these Bayes optimal predictors.
For the linear subspace denoising problem, linear attention (where attention weights are derived from $W_{KQ}$ and values from $W_{PV}$ matrices) is shown to be a sufficient structure. The theory predicts that in the limit of large context length, the optimal attention weights will converge to scaled identity weights, matching the Bayes optimal denoiser.
For the nonlinear tasks, such as sphere and Gaussian mixture denoising, softmax attention is identified as the sufficient structure. Again, the theory predicts that scaled identity weights are what is needed for the attention mechanism to express these solutions, particularly in the limits where variances go to zero and centers are on a sphere for the Gaussian mixture case.
The bridge to associative memory dynamics is established by observing the structure of these optimal attention weights. The authors found that a trained linear attention layer, when its weights converge to the predicted scaled identity form, performs an operation identical to a single step of gradient descent in a spherical Hopfield model. This model is characterized by a particular step size that effectively cancels out the Lagrange multiplier term constraining the norm of the state.
Similarly, for the nonlinear cases, the trained softmax attention layer, with its empirically observed scaled identity weights, performs an operation equivalent to a single gradient descent step in a modern continuous Hopfield net. This connection is exact, with the query token acting as an initial condition on the energy landscape induced by the context tokens. This interpretation fundamentally reframes the attention operation not just as a weighted average, but as a dynamic process performing an optimal "single step" of memory retrieval and denoising. The context tokens effectively define the "memory bank" or the landscape, and the attention mechanism acts as the retrieval process.
Experimental Setup & Results
▶ Watch: Three elementary versions of the denoising task (3:30)
The empirical validation of the theoretical findings was conducted using a straightforward and common deep learning setup. The authors trained single-layer attention networks (both linear and softmax variants) on the proposed in-context denoising tasks.
Hardware and Software Stack:
- The models were implemented and trained using PyTorch, a widely used deep learning framework.
- The Adam optimizer was employed for training, a standard choice known for its adaptive learning rate capabilities.
- Training commenced from random initial weights, simulating a typical neural network training scenario without pre-existing biases.
Tasks and Data Generation:
- Linear Subspace Denoising: Context tokens were generated by sampling isotropically Gaussian points, projecting them onto a $D$-dimensional subspace of $\mathbb{R}^N$, and then adding isotropic Gaussian noise.
- Subsphere Denoising: Context tokens were sampled from a sphere, and noise was added.
- Gaussian Mixture Denoising: Context tokens were sampled from a mixture of Gaussian distributions, and noise was added.
- The context length $L$ and the dimension $D$ of the subspace (for the linear case) were parameters varied during experiments.
Metrics:
- The primary metric for evaluation was the Mean Squared Error (MSE) between the attention layer's denoised output and the original, uncorrupted target token. The goal was to minimize this error.
Headline Results and Observations:
The experimental results strikingly confirmed the theoretical predictions:
- Loss Convergence: The trained attention networks consistently converged to the theoretical baseline, indicated by the "pink line at the bottom" in the presented graphs. This demonstrated that the networks were effectively solving the denoising task.
- Weight Convergence: Crucially, the learned attention weights for both linear and softmax attention layers converged precisely to the analytically predicted scaled identity weights. This was shown for the linear subspace denoising task (for linear attention) and for the nonlinear sphere and Gaussian mixture tasks (for softmax attention). This exact match between empirical and analytical results provided strong evidence for the theoretical framework. The visualizations showed the trained weights aligning perfectly with the predicted target weights, even when starting from random initializations.
Iterative Denoising Example:
To further explore the connection to dynamical systems, the talk presented an example of iterative denoising using the trained softmax attention layer on a 2D sphere denoising task.
- By repeatedly applying the trained attention map (which represents a single gradient descent step), the query points (corrupted tokens) were observed to move along the manifold and eventually converge to fixed points, effectively denoising them.
- However, the speaker also highlighted a critical caveat: naively repeating this attention map operation too many times can lead to convergence to spurious minima. This happens because iterating the single-step attention map can cause a gradual loss of detailed information in the query, potentially blending it into an energy minimum implied by the context, rather than the true original uncorrupted point. This contrasts with the "optimally blending" nature of a single attention step, which is designed to optimally combine information from the query and the context without over-committing to local minima. This observation underscores the specific nature of attention as a single, optimized gradient step, rather than an iterative process.
In summary, the experimental setup was minimal yet robust, and the results provided compelling empirical validation for the deep theoretical connections drawn between transformer attention and associative memory retrieval dynamics.
Practical Implications
▶ Watch: Bayes optimal denoiser for linear manifold task (4:00)
The findings from this research offer several significant practical implications for practitioners, infrastructure teams, model builders, and deployers working with AI/ML systems, particularly those involving transformers and in-context learning.
For practitioners and model builders, this work provides a deeper, more mechanistic understanding of how transformer attention functions, moving beyond its description as a simple weighted average. By showing that attention effectively performs a single, optimal gradient descent step on an energy landscape induced by context, it recontextualizes in-context learning as a sophisticated form of associative memory retrieval and denoising. This insight can inform how practitioners interpret model behavior, especially when analyzing why a transformer "learns" from examples provided in its context window. It suggests that attention is not merely pattern matching, but actively trying to "clean up" or "complete" a noisy input based on stored "memories" (the context examples).
For infrastructure teams and those involved in deploying transformer models, this fundamental understanding could eventually lead to more efficient or specialized hardware/software designs. If attention layers are indeed performing specific types of gradient descent steps, there might be opportunities to optimize these operations at a lower level, potentially leading to faster inference or more energy-efficient computations for certain types of in-context learning tasks.
The research also highlights tradeoffs and limitations. The finding that a single attention layer constitutes an "optimal single step" of gradient descent is crucial. The talk explicitly warns against naively iterating the attention map multiple times, as demonstrated by the sphere denoising example. Such uncontrolled iteration can lead to convergence to spurious minima, where the model loses specific information from the query and blends it into a generic context-induced minimum. This suggests that while attention is powerful, its inherent single-step nature for optimal denoising should be respected. Model builders should be cautious about architectural designs that might implicitly or explicitly encourage unbounded iteration of attention operations without proper safeguards or theoretical grounding, as it could degrade performance or introduce undesirable biases.
Furthermore, while this work provides a strong theoretical foundation for attention's role in specific in-context denoising tasks, its direct applicability to all forms of in-context learning observed in large language models (LLMs) requires further investigation. The talk focuses on single-layer transformers and elementary tasks. Extending these insights to multi-layer architectures and more complex, abstract ICL phenomena (e.g., in-context instruction following, few-shot reasoning) remains an open area for future research. The speaker acknowledges this, mentioning future work on extending to multi-layers and connections to diffusion models.
In essence, this research offers a valuable conceptual framework for understanding a core component of modern AI. It provides a "bridge" between two distinct communities and offers a more analytical, less empirical, perspective on attention's capabilities, which can guide both the analysis of existing models and the design of next-generation AI architectures.
Key Takeaways
- A novel "in-context denoising" task has been introduced, serving as a natural interface to study the computational role of transformer attention.
- Single-layer attention mechanisms (both linear and softmax) are remarkably expressive, capable of exactly solving these denoising tasks by matching the Bayes optimal denoiser.
- Trained attention layers, with their empirically observed scaled identity weights, are precisely equivalent to performing a single step of gradient descent in specific associative memory models (spherical Hopfield model for linear attention, modern continuous Hopfield net for softmax attention).
- This connection reinterprets in-context learning as a process of associative memory retrieval, where the query acts as an initial condition and context tokens induce an energy landscape on which an optimal single-step denoising operation is performed.
- Empirical results using PyTorch and Adam optimizer strongly validate the theoretical predictions, showing exact convergence of trained attention weights to the analytically derived optimal forms.
- Caution is advised against naively iterating the attention map, as this can lead to convergence to spurious minima, highlighting the "optimal single-step" nature of attention for denoising.
About the Speaker(s)
The talk was presented by Matthew Smart, who is a postdoc at the Flatiron Institute. He conducted this research in collaboration with his colleagues at Flatiron, Alberto Bietti and Anirvan Sengupta. The Flatiron Institute, part of the Simons Foundation, is a research division dedicated to advancing scientific research through computational methods, particularly in areas like astrophysics, quantum physics, and computational biology. The collaborative nature of the work highlights an interdisciplinary approach to understanding fundamental aspects of artificial intelligence.
Reviews
Maya Iyer (Theoretical ML Researcher) — STRONG ACCEPT
Smart, Bietti, and Sengupta establish a clean theoretical connection between single-layer transformer attention and one step of gradient descent on an associative memory energy landscape, using a carefully constructed in-context denoising task as the interface. The core result — that optimal attention weights converge to scaled identity, and that this operation is exactly equivalent to a Hopfield gradient step — is stated precisely, empirically validated, and non-trivial. The paper earns a strong accept on the strength of its theoretical architecture and the quality of its empirical confirmation. Scope is bounded (single layer, controlled distributions), but the contribution is honest…
Chen Zhao (Applied ML Researcher & Empiricist) — SOLID
Smart, Bietti, and Sengupta introduce an in-context denoising task and use it to rigorously establish that single-layer attention — both linear and softmax — implements a single gradient descent step in specific associative memory models. The theoretical derivations are clean, the empirical validation (weight convergence to predicted scaled identity matrices) is satisfying, and the framing is honest about scope. This is careful, intellectually honest theory work with real empirical grounding. It does not, however, change what applied ML practitioners will do next week, the benchmark coverage is limited to elementary distributions, and the single-layer scope leaves the most important…
→ Top-rated talks at International Conference on Machine Learning 2025
All talks from International Conference on Machine Learning 2025