Manuscript Offprint
arXiv:2611.02817In-context learning of solution to simple linear regression task using transformer with masked linear self-attention
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 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 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 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.
2.1 Notations
In this paper, is the zero matrix and is a identity matrix. Note that we use this notation also for vectors, where either or equals 1. For an matrix , is the -entry of , is the -th row vector of and is the -th column vector of for and . When is an vector, denotes its -th entry. is a submatrix of consisting of rows through . The same applies to submatrices consisting of columns.
2.2 Setting of in-context learning of transformer
We next explain the transformer-based in-context learning of a simple linear regression task. Let be the number of training data for the transformer. Let be a pair of input-output variables in a simple regression problem. At each in , a set of random samples of , which is denoted by , are generated. We denote the -th prompt (input to the transformer) by , which is an matrix and whose -th row is
where we define and that is a prediction point; e.g. see [2]. Thus, the input sequence length is and the number of in-context samples is . We assume that, for each , is generated by
where , are i.i.d. additive noises from a probability distribution with mean 0 and variance . Our goal is to obtain the least squares prediction at for the prompt . Therefore, for each , we need to calculate the least squares solution using
which is the set of in-context samples. The important point is that the regression lines can be different for each . Thus, we may assume that are sampled from a probability distribution for each . However, in the construction of the transformer, we do not make any specific assumptions on the underlying probability distribution of . We also do not make any specific assumptions on the underlying probability distribution of and .
2.3 Training data for the transformer
For the -th training data defined in (3), we define
For a simple regression problem, it is easy to see that the prediction at using the least squares solution based on is given by
(see e.g. [13]).
Below, we construct the transformer that receives as an input and outputs for . Thus, the transformer calculates the least squares estimate at the prediction point using in-context samples at each .
2.4 Linear self-attention
We define a linear self-attention (LSA) with a partial mask. It receives an matrix Q as an input and outputs matrix defined by
where is an ordered set of parameters, in which and are matrices, and is an matrix which is defined by
Here, we have
where is the Hadamard product and is an matrix which is defined by
is known as a mask. As a result,
Therefore, the last entries of the sequence in Q are set to zero by this mask. This is necessary to distinguish between the members of the sequence in the calculation. The details will be provided later. Although the above LSA partially uses the mask, in what follows, this LSA will be referred to simply as LSA. The operation using LSA without masking is precisely discussed in [7] and the insight in [7] is also used in this paper.
2.5 Transformer structure
The transformer structure considered in this paper is illustrated in Fig. 1, in which grey blocks are operation blocks. The transformer here consists of input linear transformation, stacking transformer blocks, flatten and output linear transformation (Fig. 1 (a)). The transformer block consists of multi-head LSA (MHLSA) block with layer normalization followed by a skip connection (Fig. 1 (b)). It receives the m-th prompt defined in (1) and, for simplicity, outputs a scalar value whose target is given by (8).
Let and be a input weight matrix and input bias vector respectively. The embedding of the prompt by an affine transformation with is denoted by whose size is . More precisely, we define
for . The number of layers is denoted by . For the -th training data, the input to the -th layer is denoted by . Thus, the above is the input to the first layer.
The parameter of the LSA block of the -th head in the -th layer is denoted by . The output of the MHLSA in the -th layer for -th training data is
where is the number of heads. The size of is . The number of heads is common for all layers. We employ the layer normalization along the model dimension (embedding). We define

(a) Main stream

(b) Transformer block
Figure 1: Structure of transformer
Then, the output of the layer normalization with these parameters is denoted by whose -entry is
for at each . Finally, the output of the -th layer is given by
where the first term of the right-hand side comes from a skip connection. Note that, in our model, the layer normalization is applied before the skip connection.
is the output of the last layer. It is then flattened and obtain which is a vector. Let and be a output weight matrix and output bias respectively. We then obtain the transformer output by
which is a scalar value.
We refer to this transformer as TF , where D, L and K are the model dimension, the number of layers and heads respectively.
3 A construction of transformer
We here gives a construction of the transformer that calculates the least squares estimate at the prediction point. We here fix the structure of the transformer, in which the model dimension , the number of layers and the number of heads . Therefore, the transformer is TF(4,2,2). Now, the -th prompt to TF(4,2,2) is defined in (1).
3.1 Input embedding
Let be a real positive number. We assume that can be chosen sufficiently large below. is important for implementing the closed form solution based on [2].
In the input embedding, we set
and . By (14), the n-th row of the input embedding is
which is the input to the transformer.
3.2 Output of the first layer
We here construct the first layer.
We consider the first head. We have
(27)
Therefore, we have
(28) where the irrelevant elements are indicated by “-”. We set in which
Then, by (9) and (16), the n-th row of the output of the first head is
On the other hand, for the second head, we set in which
Then, by (9) and (16), the n-th row of the output of the second head is
Hence, there is no contribution of the second head.
By (15), the n-th row of the output of the MHLSA in the first layer is
Now, we consider the layer normalization. By (17), (18) and (35), for , we have
(37) since we can set a sufficiently large value for ; e.g. , where is a sufficiently large positive integer.
By (19), (35), (36) and (37), the n-th row of the output of the layer normalization is
Then, by (20), (23) and (38), the n-th row of the output of the first layer is
where
3.3 Output of the second layer
We next construct the second layer.
By (39), we have
(42)
(43)
Thus, we have
We construct the first head. To do this, we set , in which
Then, by (9), (16) and (44), the n-th row of the output of the first head is
On the other hand, we consider the second head. We set , in which
Then, by (9), (16) and (44), the n-th row of the output of the second head is
Therefore, by (15), (48) and (52), the n-th row of the output of the MHLSA in the second layer is
Now, we consider the layer normalization. By (17), (18) and (53), for , we have
since R is sufficiently large. By (19), the n-th row of the output of the layer normalization is
The division performed by the layer normalization is actually used for this purpose in [2].
Then, by (20), (39) and (56), the n-th row of the output of the second layer is
where we omit the -term in .
3.4 Model output
Since and , we have
by (57). Now, is flattened into . Then, we have . Hence, if we set
then the transformer output for the m-th prompt is, approximately,
by (21) and (58). This is consistent with the least squares estimate at in (8).
3.5 Discussion
We have several remarks.
-
We introduced a partial masking to LSA. This is because, for example as in (25), we need to eliminate the effect of the prediction point on the calculation of the mean of the in-context input samples . In other words, it is necessary to treat the in-context samples and prediction point separately here. This is not possible since the attention mechanism calculates the inner product of the two sequences, which implies that information on the sequence dependent property disappears; e.g. see (9). In large language models, this fact is generally important since we may need masking and/or positional encoding to control the sequence dependent property that cannot be controlled by the attention mechanism.
-
It is interesting that the features such as the mean of inputs and outputs for a simple regression problem are extracted in the first (shallow) layer and those are used to calculate in (8) in the second (deep) layer. This is a natural arrangement in constructing and stems from computational limitations in an attention layer; i.e. the number of multiplications.
-
A large value for is significantly required for performing division by layer normalization. Note that this enforces the absolute values of weights to be either close to zero or extremely large.
-
Our construction may be relatively compact. As shown in Appendix A, the representation of LSA is not unique in the sense that, it produces the same output for different inputs. Therefore, it might be possible to create the same output using a more verbose expression, in which the most weight values are not zeros. Actually, this will be seen in the numerical experiment.
4 Numerical experiment
We here show several numerical experiments to demonstrate that, given the non-uniqueness of the expression noted in Appendix A, our construction in the previous section is actually used in the trained transformer.
4.1 Setting of experiment
The transformer defined in this paper is coded by PyTorch, in which we omit the biases in the input and output linear transformation. For the training data defined by (3), we set for the noise variance and, at each , the inputs including prediction points are randomly drawn from a standard normal distribution . And, we generate and according to independently at each . The length of the input sequence is (the number of in-context samples is ). The target output (teacher) for the transformer is the least squares estimate at the prediction point. This is a more direct instruction than the output data at the prediction point. The transformer is TF(4, 2, 2). For the training, the number of training data is
, the number of validation data is 1000, the batch size is 500, the algorithm is Adam[5] with the learning rate of 0.005, the maximum number of training epochs is 20000. We apply the layer normalization without an element-wise affine setting in torch.nn.LayerNorm. As shown in Appendix A, there is a lack of uniqueness in the LSA representation. Therefore, we apply the regularization to the output weights, by which non-contributing weights are set to zero and important weights may be highlighted. The regularization parameter here is 0.00001. This may make one expect a relatively compact expression as obtained in our construction. We employ the transformer that minimizes the validation error calculated every 100 epochs.
4.2 Verifying the use of layer normalization
We execute 10 different training runs under the above setting, in which both of data and initialization differ for each run. We generate 1000 new data of pairs of the prompt and the least squares estimate, in which the prompts are i.i.d. samples from the same distribution as the prompt in the training data. Then, for each prompt, we obtain the output of the trained transformer and can calculate the squared test error between the transformer output and the least squares estimate at the prediction point. Thus, in each run, we can obtain the average test error for 1000 new data.
Simultaneously, for each -th prompt in the new data, we can calculate defined in (15). The variance along the embedding used in the layer normalization is the variance of for the -th sequence, which is as defined in (18). Also, we can calculate defined in (6) as the variance of in-context input samples at each in the new data. In our construction, the division by in (8) is executed using the division by the standard deviation in the layer normalization in (56). To see this, we calculate the correlation coefficient between and for in each run.
The results for 10 runs are summarized in Table 1. We can see that the correlation coefficient is larger than 0.9 in the first and fifth runs. This implies that the division by is possible to be calculated using the layer normalization for these two runs. Additionally, the trained transformer in the first run shows a better predictive performance in terms of the average test error. Then, we further analyze the trained transformer in the first run below.
4.3 Output weights
In Fig. 2, for the first run in Table 1, we show the heat map of the absolute values of the output weights which are denoted by in this paper. The absolute weight values are shown on a logarithmic scale. Although is a 204-dimensional vector in our modeling, we here convert it into a matrix whose row and column correspond to the indices of embeddings and sequences respectively. The converted output weight matrix is referred to as . We refer to the final-layer-cell whose corresponding output weight is as the cell, which is the cell at the -th embedding and the -th sequence.
In Fig. 2, we can see that the absolute weight values of the first embedding cells are relatively large and, especially, the absolute value of is extremely large
Table 1: Average squared test error and correlation between reciprocals of standard deviation in layer normalization and variance of in-context input samples
| No. | Average test error | Correlation |
|---|---|---|
| 1 | 0.000058 | 0.966911 |
| 2 | 0.000269 | 0.417908 |
| 3 | 0.000307 | 0.535359 |
| 4 | 0.000101 | 0.662808 |
| 5 | 0.000109 | 0.988581 |
| 6 | 0.000095 | 0.354986 |
| 7 | 0.000096 | 0.816449 |
| 8 | 0.000114 | 0.802375 |
| 9 | 0.003176 | 0.223738 |
| 10 | 0.000090 | 0.457691 |
compared to the other values. Therefore, the [1, 51] cell is the dominant factor for the transformer output. Note that the contribution of the first embedding are larger than the contributions of the other embeddings overall.

Figure 2: Absolute output weight values
4.4 Fitting curve of in-context samples after training
Here, for the first run, we show the fitting curves produced by the trained transformer to the in-context samples.
We consider the prompt defined in (1), in which the in-context samples are and the prediction point is . If we keep the same and change only , the transformer is a function of . We define the output of the cell in the final layer for under a fixed by . Then, the transformer output for is given by
We also define
The former is the contribution of the [1,51] cell and the latter is the contribution of the other cells in the first embedding. Then, we generate 100 new data for the prompts to the trained transformer, which are for , where is generated from the same distribution as the training data and is the equidistant points in [-4, 4]. In other words, we obtain the transformer output at 100 different prediction points under the same in-context samples. Therefore, we can plot a fitting curve for the in-context samples.
In Fig. 3, we show two examples, denoted by A and B, of in-context samples (open circle) and graphs of , , (black solid line), in which (a), (c) and (e) for Example A and (b), (d), (f) for Example B. In this figure, we also show the graph of the least squares estimate of a simple regression model (gray solid line), which is obtained for the in-context samples, . Note that the graph of the least squares estimate is a straight line in a simple regression problem. From (a) and (b), we can see that the transformer output, , is well consistent with the least squares estimate. In (c) and (d), the contributions through the [1,51] cells, , slightly deviate from the least squares estimate, while the slopes of both are well consistent with the slopes of the least squares estimates. In (e) and (f), the contributions through the other cells in the first embedding, , are constant for both. Actually, the variance of , are for Example A and for Example B, which implies that there are no changes in the outputs. Therefore, captures the slope of the least squares estimates precisely and is responsible for correcting the constants. The average test error using only the first embedding was 0.000056, which is almost consistent with the error given by the transformer output of the first run in Table 1. This implies that the transformer output is almost determined by the contribution from the first embedding; i.e. . Note that, indeed, it is a little small since the contribution from the other embedding may cause problems near the edges of the input range, in which there are few training samples for transformers. In the next section, using regression analysis, we clarify how the trained transformer implements these predictions.
4.5 Regression analysis
For the first run in the numerical experiment in Section 4.2, we can obtain , and for , which are the final and intermediate outputs in the trained transformer. Then, we consider linear regression analysis of these

(a) in Example A.
(b) in Example B.

(c) in Example A.
(d) in Example B.

(e) in Example A. (f) in Example B.
Figure 3: Examples of fitting curves after training targets by
where are explanatory variables (features) and are coefficients. This is viewed as a linear probe in [2]. The explanatory variables are selected to include the terms in the solution (8) and to heuristically bring the multiple correlation coefficient between and closer to 1. In Table 2, we show the score and the estimated coefficients. We summarize the results below.
Table 2: and estimated coefficients in regression analysis of , and .
| 0.999974 | 0.997697 | 0.957023 | |
| -0.00205 | -0.018546 | 0.018853 | |
| -0.000005 | 0.000670 | -0.001094 | |
| -0.016581 | 0.374289 | -0.387986 | |
| 1.000339 | 0.046484 | 0.951257 | |
| 1.000394 | 1.003497 | -0.003356 | |
| -1.002698 | -0.243266 | -0.757685 | |
| 0.017339 | -0.251728 | 0.264673 | |
| -0.000487 | 0.721800 | -0.720420 | |
| 0.001127 | 0.193471 | -0.193076 | |
| -0.000034 | 0.134298 | -0.133731 |
4.5.1 Fitting accuracy
We can see that the scores are high for all cases. This implies that the fittings by the linear regression models are valid. Note that we do not need to worry about the effects of overfitting since the number of samples is 1000, which may be large enough for 10 coefficients.
4.5.2 Result for
For , from the score, the fitting is almost perfect. Looking at the estimated coefficients, and for the other terms. These imply that the output of the trained transformer is of the form of the least squares solution given in (8).
4.5.3 Results for and
At each explanatory variable, the sum of the estimated coefficients for and is almost consistent with the estimated coefficient for although, of course, it is not completely consistent. The slope of transformer output is determined by the term with , which is , and is almost represented by . On the other hand, the constant term of the transformer output is determined by the terms with and , which are and . The term with is almost represented by and the term with is represented by both of and . In the other term which is not related to the solution given by (8), the coefficients for and have almost the same magnitude but opposite signs. Therefore, those terms vanish in the transformer output.
4.5.4 Relation to construction of transformer
We explain the correspondence between our construction of a transformer and the numerical example. The output of the cell in the numerical example corresponds to in (57) in our construction.
First, , and are the terms correspond to , and in Table 2 respectively and are the components of the least squares solution. As mentioned above, the other terms in and cancel each other. If the coefficients are zero for these terms for both of and , then the result remains the same. Actually, if the trained transformer follows our implementation, then this is achieved. To achieve this, the values of many of the weights in the self-attention are required to be zero as in our construction. Of course, it may not actually happen in training. Nevertheless, the output of the trained transformer is consistent with the output of the transformer that we construct. This is because of a lack of uniqueness of the representation in a transformer as mentioned in Section 3.5.
Now, we here focus on the main terms in the least squares solution. By (57), consists of the sum of , and in (57) since and . Especially, as in our construction, only is responsible for that determines the slope of the least squares estimate. Actually, in Table 2, is very small for and is close to 1 for . However, and are small in and those are supplemented by . So, the question is how the terms and in are produced by for . As seen in Fig. 2, the output weight values are almost identical for in the first embedding. Therefore, in our construction, the weighted sum of the right hand side of (57) for just consists of the weighted sum of and since the sums of 's and 's reduce to the constant multiples of and respectively. This brings us the appropriate values for and in to compensate what lacks, which is a constant term in the fitting curve. This yields the results for and in Table 2. After all, the least squares solution is obtained as the trained transformer output under our construction of in (57) while the usage of in training does not follow our construction; i.e. and the other coefficients are zero for and all the coefficients are zero for . This is again due to the non-uniqueness of the LSA representation. Therefore, our construction is valid except for the factor that comes from the non-uniqueness.
5 Conclusion and future work
In this paper, we constructed the transformer that outputs the least squares estimate of a simple regression problem, in which the transformer comprises a linear transformation of the input, a stack of transformer blocks, a flatten layer, and an output linear transformation, in which the transformer block consists of multi-head masked linear self-attention blocks with layer normalization followed by a skip connection. To achieve this by TF(4, 4, 2), in the construction, we represent a closed form solution by using the layer normalization, which is an insight of [2, 8]. For in-context learning of regression tasks, most studies have considered implementations of the gradient descent algorithms. This is because multiplication, which is a basic operation of the attention mechanism, is suitable for implementing the gradient descent algorithm. However, the experimental evidence that the gradient descent algorithm is implemented through training is still unclear. In contrast, in this paper, 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 regularization when the target output is the least squares estimate.
We have several things to address along this line.
-
We applied the regularization to eliminate the effects of unnecessary weights. It seems that this helped concentrate the essential output into a single embedding. However, it does not seem natural and may causes a bias in the prediction error. Therefore, we are interested in the implementation under the training without regularization.
-
As shown in Appendix B, the gradient descent based implementation may not be comparable in TF(4, 2, 2) since the number of layers may not be enough to implement the sufficient number of gradient descent steps; e.g. see also [8]. To put it another way, the transformer obtain a closed form solution due to the limited size. The gradient descent based implementation may be effective when the size increases. The investigation of the impact of the size on the implementation is left as future works.
-
The construction under the other prompt format should be examined.
-
Although, in our experiment, we employed the least squares estimate as the target output in training, the output sample at the prediction point may be natural as the target output. The theoretical and experimental analysis of this case is also left as a future work.
References
[1] K. Ahn, X. Cheng, H. Daneshmand, S. Sra, “Transformers learn to implement preconditioned gradient descent for in-context learning”, arXiv:2306.00297, 2023.
[2] E. Akyürek, D. Schuurmans, J. Andreas, T. Ma, D. Zhou, “What learning algorithm is in-context learning? investigations with linear models”, arXiv:2211.15661, 2022.
[3] Y. Bai, F. Chen, H. Wang, C. Xiong, S. Mei, “Transformers as Statisticians: Provable in-context learning with in-context algorithm selection”, arXiv:2306.04637, 2023.
[4] T. Brown, et al., “Language models are few-shot learners”. arXiv:2005.14165, 2020.
[5] D. P. Kingma, J. L. Ba, “ADAM: A Method for Stochastic Optimization”, arXiv:1412.6980v9, 2015.
[6] S. Garg, D. Tsipras, P. Liang, G. Valiant, “What can transformers learn in-context? a case study of simple function classes”, arXiv:2208.01066, 2022.
[7] K. Hagiwara, “Effect of bias matrix in linear self-attention for in-context learning”, IEICE Trans. D, vol.E109-D, No.8, 2026.
[8] K. Hagiwara, “In-context learning of closed form solution to simple linear regression task using transformer with linear self-attention”, arXiv:2607.15819, 2026.
[9] K. He, X. Zhang, S. Ren, J. Sun, “Deep residual learning for image recognition”, arXiv:1512.03385, 2015.
[10] A. Krizhevsky, I. Sutskever, G. E. Hinton, “ImageNet classification with deep convolutional neural networks”, Communications of the ACM, vol.80, no.6, pp.86-90, 2012.
[11] A. Mahankali, T. B. Hashimoto, T. Ma, “One step of gradient descent is provably the optimal in-context learner with one layer of linear self-attention”, arXiv:2307.03576, 2023.
[12] J. von Oswald, et al., “Transformers learn in-context by gradient descent”, arXiv:2212.07677, 2022.
[13] N. Ravishanker, D. K. Dey, “A first course in linear model theory”, Chapman & Hall/CRC, 2002.
[14] A. Vaswani, et al., “Attention is All you Need”, arXiv:1706.03762v7, 2017.
Appendix
A An example of a lack of uniqueness
We repost here a simple example of [8], in which LSA produces the same output for the different inputs. Here, we consider LSA without a mask below for simplicity.
Let be an matrix whose -th row is and define and . In (9), we set this as the input matrix and we set , where
Then, the -th row of is for any . On the other hand, let be an matrix whose -th row is . In (9), by setting
the -th row of is for any . This is the same as in the first case.
B Comparison to gradient descent
We compare the trained transformer and the gradient descent method for a simple regression problem. Although a simple regression problem has two parameters which are intercept and slope, we employ the one-parameter version of the gradient descent, which is analytically derived.
We define
where is a parameter to be adjusted. By (8), is a closed form solution to . We also define
We have
By setting as an initial value, at the -th step, the update equation of the gradient descent for is given by
(70)
where is a learning rate. The maximum number of repetitions is denoted by .
For the first run in the numerical experiment in Section 4.2, at each , we obtain the linear estimate using the gradient descent method based on the in-context samples, where we set . In Table 3, we show the average squared test error of the gradient descent method for different and .
Table 3: Average squared test error of gradient descent
| test error() | test error() | |
|---|---|---|
| 0.4 | 0.080785 | 0.000425 |
| 0.45 | 0.050393 | 0.000130 |
| 0.5 | 0.042642 | 0.000061 |
| 0.55 | 0.057532 | 0.000309 |
| 0.6 | 0.095063 | 0.002125 |
In Table 3, both of and , is the best choice for the learning rate. Since the average test error for the trained transformer is 0.000058 in Table 1, the gradient descent method with is comparable to the trained transformer by Table 3 if we can choose . In other words, the gradient descent method needs four steps to show the performance that is comparable to the implementation of a closed form solution here. For the prompt employed in this paper, [8] showed that TF(4, 3, 2) can implement the two-step gradient descent, in which the learning rate is determined by the weight values of the transformer. Therefore, an optimal learning rate may be obtained by optimizing the weight values. However, by the construction in [8], TF(4, 2, 2) seems to be too simple to implement the four-step gradient descent.