arXiv:2611.02817cs.LGNov 2026

Manuscript Offprint

arXiv:2611.02817

In-context learning of solution to simple linear regression task using transformer with masked linear self-attention

Katsuyuki Hagiwara

Abstract

In-context learning is a remarkable property of transformers and has recently received a lot of interest. In many studies of in-context learning, it has been shown that transformers are capable of implementing solvers for linear and non-linear regression problems, in which the most of them implement gradient descent algorithm. However, it is still unclear whether those implementations have actually been acquired through training. In this paper, we constructed a transformer with masked linear self-attention, which in-context learns the least squares estimate in a simple regression task. The point here is that the closed form (analytical) solution is obtained by using layer normalization rather than an approximate solution based on the gradient descent algorithm. Then, we showed an experimental example, in which, by taking the non-uniqueness of the transformer representation into account, our implementation for a closed form solution is used in the transformer trained with the ℓ1\ell_1 regularization when the target output is the least squares estimate.

keywords in-context learning, transformer, linear self-attention, a simple linear regression, layer normalization, mask

1 Introduction

In-context learning is a remarkable property of transformers, which form the basis of large language models such as GPT-3 [4], and it has been the subject of recent research. In-context learning is a capability of transformers to learn new tasks at inference time solely by processing context (sequence) provided in the prompt, without modifying the model's underlying weights or undergoing additional training. It is natural to think that transformers acquire, through training, the algorithms to solve tasks via in-context learning. In this regard, it has been demonstrated that transformers are capable of implementing various algorithms, particularly in regression tasks [1, 2, 3, 6, 12].

[6] empirically studied the in-context learning abilities of transformers for various function classes in machine learning, including the linear function class. In particular, for linear functions, a trained transformer performs similarly to the least squares solution. [12] provided an explicit construction of a single linear self-attention layer that implements a single step of the gradient descent algorithm on the mean squared error loss. Additionally, they empirically showed that several self-attention layers can iteratively perform curvature correction, improving upon the plain gradient descent algorithm. [2] proved that a transformer can implement a gradient descent algorithm and a single Sherman-Morrison update for a ridge regression problem. While the works of [6, 12, 2] do not consider the training phase, [1] proved the global minimum of the training objective implements a single iteration of preconditioned gradient descent when the target is a linear function. In [1], even though the training objective is the expected error, the results of the numerical experiments are shown to closely match the theory. The similar result has obtained also in [11]. More recently, [3] showed that transformers can implement a broad class of standard machine learning algorithms in context, such as least squares, ridge regression, and Lasso. In contrast to [2], [3] precisely evaluated the prediction performance in terms of network size, and shown a near-optimal predictive power. [3] also demonstrated the algorithm selection ability of transformers, such as regularization selection according to validation error for ridge regression.

These works have investigated regression problems, finding that the solver implemented by a transformer is basically the gradient descent algorithm except for a part of [2]. This is natural since the attention mechanism in a transformer computes the product of the inputs, which is exactly what is required for the gradient descent method for especially linear regression problems. In a linear regression problem, however, the least squares estimate is analytically obtained by calculating the matrix inverse, which requires division. Consequently, the gradient descent algorithm implicitly calculates this division by repeatedly performing multiplication and addition. On the other hand, [2] showed that a transformer can implement a single Sherman-Morrison update for a ridge regression problem, in which the division is implemented using layer normalization. Applying this idea, [8] showed that a transformer with linear self-attention can approximately implement a closed form solution to a simple regression task. Although it is found that the implementation is mainly used in the transformer trained with ℓ1\ell_{1} regularization when the target output is the least squares estimate, unfortunately, the trained transformer is not fully explained. This may be caused by the assumption in the implementation, which is that the sequence length in the prompt is sufficiently large. In this paper, we provide a construction of a transformer with a masked linear-self attention, which implements a closed form (analytical) solution to the least squares estimate of a simple regression task. The implementation does not require the above assumption in [8] due to the introduction of a mask. We then present a numerical example, in which this implementation is actually used in the transformer trained with the ℓ1\ell_{1} regularization when the target is the least squares estimate.

This paper is organized as follows. Section 2 formulates our problem setting. Section 3 presents the construction of a transformer that calculates the least squares estimate based on in-context samples. Section 4 provides a numerical example of training to demonstrate that this construction is actually valid. Finally, Section 5 concludes the paper and discusses future work.