Transformers as Adaptive Kernel Learners across Tasks for In-Context Learning
Organizations: Department of Mathematics and Statistics, Utah State University, Logan, UT 84322 · Department of Mathematics, Purdue University, West Lafayette, IN 47907 · School of Mathematics, Georgia Institute of Technology, Atlanta, GA 30332
Abstract
Transformers have emerged as a dominant architecture in modern machine learning, with in-context learning (ICL) enabling models to adapt to new tasks by conditioning on prompts, without any parameter updates. To understand the attention mechanism in transformer blocks, a growing line of theoretical work interprets ICL through the lens of classical statistical estimators. Recent works have established a connection between the attention mechanism and kernel methods, while the kernel function is usually assumed to be fixed. In practice, the query and key matrices in the attention mechanism are trained, which allows the attention mechanism to serve as an adaptive kernel method. In this work, we demonstrate that transformers can act as adaptive anisotropic kernel learners. Specifically, we study ICL for low-rank linear and multi-index nonlinear regressions, and conduct numerical simulations to show that the learned attention scores closely resemble the oracle kernels for these function classes. We then develop a general framework to analyze the generalization error of ICL by connecting transformers to the oracle kernel estimator for each function class. Our theory gives an explicit and constructive implementation of the oracle kernel estimators for these function classes using a wide and shallow transformer architecture, illustrating how the prediction is carried out by the interaction between the query token and the context tokens. Our work complements existing studies by providing numerical and theoretical evidence of how the attention mechanism adapts to anisotropic kernels in both linear and nonlinear settings.
Figures & tables
| Function classes | Interaction kernels |
| ( 16 ) | ( 18 ) |
| ( 41 ) (special case of with ) | ( 42 ) |
| ( 43 ) (general case of , studied | |
| in hsu2026understanding with polynomial | ( 44 ) |
| and spline feature maps ) | |
| ( 19 ) | ( 21 ) |
| Kernel function | Pearson correlation coefficient | -value |
| anisotropic Gaussian | 0.64 0.24 | 0.09 0.19 |
| isotropic Gaussian | 0.44 0.24 | 0.21 0.27 |
| low-rank linear | 0.66 0.26 | 0.09 0.20 |
| linear | 0.41 0.23 | 0.24 0.27 |
| Kernel function | Pearson correlation coefficient | -value |
| anisotropic Gaussian | 0.76 0.11 | 0.004 0.013 |
| isotropic Gaussian | 0.39 0.24 | 0.27 0.29 |
| low-rank linear | 0.51 0.24 | 0.17 0.25 |
| linear | 0.33 0.19 | 0.31 0.29 |
Appendix figures & tables1 asset
Supplementary material from the paper’s appendix.
Appendix
| Symbol | Description |
| ambient dimension of the input | |
| / norm of the input for the low-rank linear model / multi-index model | |
| norm of the parameter for the low-rank linear model | |
| subspace in the low-rank linear model | |
| subspace in the multi-index model | |
| dimension of the subspace in the low-rank linear model and multi-index model |
Transformer Attention
Transformer attention lets models relate tokens by weighting information from other positions, enabling context-dependent sequence and multimodal processing.
See all 319 papers