# Re-examining Learning Linear Functions in Context

Omar Naim, Guilhem Fouilhé, Nicholas Asher

► To cite this version:

Omar Naim, Guilhem Fouilhé, Nicholas Asher. Re-examining Learning Linear Functions in Context. KI 2025, Sep 2025, Potsdam (Germany), Germany. pp.104-117, 10.1007/978-3-032-02813-6_8. hal-05374550

HAL Id: hal-05374550

https://hal.science/hal-05374550v1

Submitted on 20 Nov 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# Re-examining Learning Linear Functions
in Context

Omar Naim¹,²(✉) ID, Guilhem Fouilhé¹,² ID, and Nicholas Asher¹,³ ID

¹ Institut de Recherche en Informatique de Toulouse, IRIT, Toulouse, France
omar.naim/docs@gmail.com, {guilhem.fouilhe-lafforgue,nicholas.asher}@irit.fr

² Université de Toulouse, Toulouse, France

³ CNRS, Toulouse, France

**Abstract.** We explore in-context learning (ICL), a popular paradigm for inference with Large Language Models (LLMs), in a controlled experimental setup using synthetic training data. Using a range of small transformer models trained from scratch, we focus on a mathematical task with simple yet precise prompts: learning a linear function $f$ from a sequence of inputs $x_i$ and their corresponding function values $f(x_i)$. Our findings challenge the prevailing narrative that transformers adopt algorithmic approaches like linear regression to in-context learn (ICL) a linear function. We observe that all models have “boundary values” that limit generalizability. While we can extend boundary values with training distributions over a wider range, we lose the precision of models trained on distributions with more restricted ranges. Thus, we see a dilemma for ICL at least in some tasks: either models will lack generalizability or precision.

**Keywords:** Transformers · LLMs · In-context learning

## 1 Introduction

Large language models (LLMs) exhibit a remarkable capability to learn by analogy, known as in-context learning (ICL) [6]. This approach is especially useful for adapting a model to a task with very few resources at inference time. Despite several theoretical reconstructions of ICL, there have been few studies on exactly how ICL works in practice. Are models actually learning to reason or are they simply memorizing training data, as [13] have suggested for other tasks? We address this gap by focusing on a simple mathematical task, the in-context learning of one-dimensional linear functions, where the training regime and data are straightforward and clean. This allows us to have greater control over the data, enabling a more effective analysis of the model's ICL behavior.

We distinguish between two notions of “learning a function”. The first, assumed by the literature since [9], says that a model has learned a function if, when sampling values and coefficients of linear functions at test time from the same distribution used in training the model yields close to 0 expected error. Our models can perform ICL for linear functions in this sense, which we call $ICL_1$. On the other hand, another notion, call it $ICL_2$, might say that learning a linear function requires specifying the parameters
$a, b$, where $f(x) = ax + b$, from data points in the prompt, something which is clearly possible using a variety of techniques like linear regression. [1, 2, 12, 22] claimed that transformers use such techniques and so exhibit ICL$_2$. However, we give strong empirical evidence that none of our transformer models use such techniques or are performing ICL$_2$ of linear functions.

The reason our models cannot ICL$_2$ is that, across all training distributions, they have “boundary values” $B^-$, $B^+$ that constrain their predictions. When the inputs $x_i$ and the function $f$ parameters are chosen such that $f(x_i) \gg B^+$ or $\ll B^-$, model performance degrades substantially. We investigate the conditions under which these boundary values arise. These specific values, frequently near the maximum and minimum observed during training, indicate that the model has likely memorized them. But while boundary values are defined by training, the models’ degraded predictions do not come from the training data but are rather a sort of reasoning-based hallucination.

We studied ICL in over 30 models, ranging from transformer architectures with a single layer and one attention head (AH) to models with 12 layers and 8 AHs, as well as small attention-only models [17]. To train our models, we use sequences generated by functions $f(x) = ax + b$ where $a, b$ are sampled from a distribution over function parameters and $x$ is drawn from a distribution over inputs. The model learns an algorithm on that training data, and we then evaluate its predictions on a test distribution at inference time. Since ICL depends on a model’s pretraining, such an investigation requires training from scratch, and that has constrained our study to relatively small transformer architectures.

## 2 Related Work

[6] introduced ICL as a paradigm where the model learn at inference from the prompt by analogy, with no changes to the parameters set during training. Despite its promise and popularity, this phenomenon is still not fully explored. [7] surveys the successes and challenges for ICL and argue that so far, research has only studied simple tasks like learning linear or Boolean functions.

[9] showed that a transformer trained from scratch (GPT-2 [20] with an embedding size of 256) performed in-context learning of linear functions given identical train and test Gaussian distributions.

Further research has offered several reconstructions for how ICL might work for the class of linear functions $\mathcal{L}$ in transformers. [1, 2, 12, 22] provided a construction to show that transformers could learn via gradient descent to do linear regression in ICL, which would solve the task. [8] showed that transformers could perform ICL in virtue of using higher-order optimization techniques. [18, 24, 25, 27] argued that ICL follows from Bayesian principles. [4] show that transformers can under certain assumptions implement many algorithms with near-optimal predictive power on various in-context data distributions. Given [19]’s result that full transformers with linear attention are Turing complete, however, such theoretical demonstrations are not surprising. Whereas most prior research has concentrated on what transformer models can or could do on this task, we examine how ICL works in practice under different training and testing distributions in order to establish what transformers actually do.
[25, 26] show that when the training and inference distributions are shifted, ICL performance degrades. This work is closely aligned with our approach, as is the work of [10]. However, [10, 26] make important modifications to transformer architectures by working with linear attention, whereas we look at attention layers as they are more commonly implemented with softmax. In addition, [26] uses a new kind of optimization or training with gradients and a special fixed initial point. This means that their architecture and training are quite different from what normally happens with transformers; they are interested in getting a revised transformer-like model to learn linear functions, while we want to find out whether existing transformers learn linear functions or something else. As we detail below, the results for the architectures of [10, 26] are quite different from those we have for standard transformers. Finally, our proposed mathematical model for ICL differs from any of the proposed methods.

[5] trained small GPT-2-like models from scratch to show that transformers can ICL simple boolean functions, while their performance deteriorates on more complex tasks. [21] investigated how ICL in models evolves as the number of pretraining examples increases, in a setting where the training and test distributions are identical.

[17] offer an in-depth analysis of ICL across tasks using a general evaluation measure on prompt length. They propose that ICL is enabled by a learned copying and comparison mechanism for ICL. In Sect. 6, we show their proposal does not fit our task.

## 3 Experimental Setup

We investigate transformer models that are trained directly on an in-context learning objective. We trained several small decoder-only transformer models from scratch to perform ICL of linear functions.<fnref n="1" /> Our models feature from 1 to 18 layers (L) and from 1 to 8 attention heads (AH) with an embedding size of 64 to 256.

The model’s task is to predict the next value for $f(x_i)$ through a prompt of type $(x_1, f(x_1), ..., x_i)$. We refer to that prediction as $\hat{f}(x_i)$. To train the model to perform ICL $\mathcal{L}$, we looked for a $\theta^*$ that optimizes the following auto-regressive objective:

$$ \theta^* = \arg \min_{\theta} \mathbb{E}_{x_i \in D_I, f \in D_F} \left[ \sum_{i=0}^{k} \ell(f(x_{i+1}), \mathcal{L}_{\theta}((x_1, f(x_1), \dots, f(x_i), x_{i+1}))) \right] $$

where $\mathcal{L}_{\theta}$ is a “learner”, $\ell$ is squared error and $f: x \to ax + b$ is a linear function with $a, b$ sampled from some training distribution $D_F$ for functions. Samples $x_i$ are sampled from a training distribution for points $D_I$. We note that $f \in D_F$ and $x \in D_I$. At each training step, we choose at random, a function $f \in D_F$ and then a sequence of points $x_i \in D_I$ from a distribution $D_I$. We use a batch size of 64 and train for 500k steps. The models saw over 1.3 billion training examples for each distribution we studied. For $D_F$ and $D_I$ we used several distributions: the normal distribution $N(0, 1)$, uniform (“rectangle”) distributions over specified intervals, and bimodal distributions.

We evaluate the models across various test distributions for both function classes $D_F^t$ and data points $D_I^t$. During training, we always set $D_F = D_I$. For testing, however,

<footnote n="1">¹ Our code can be found in https://github.com/omyokun/re-examining-LF-ICL.</footnote>
we sometimes consider scenarios where $D_F^t \neq D_I^t$. To evaluate ICL performance of a model on $(D_I^t, D_F^t)$, we generate a set of $N = 100$ functions in $D_F^t$. Our data samples for testing are composed of $N_b = 64$ batches, each containing $N_p = 41$ points in $D_I^t$. For each function $f_j$, in each batch b, for all points, we predict for each $x_k^b$, $k \ge 2$, $f_j(x_k^b)$ given the prompt $(x_1^b, f_j(x_1^b), ..., x_{k-1}^b, f_j(x_{k-1}^b), x_k^b)$. We then calculate for each function the mean average over all the points $N_p$ of all batches $N_b$, then do a mean average over all functions. Formally this gives us:

$$ \epsilon_{\sigma} = \frac{1}{N} \sum_{j=1}^{N} \sum_{b=1}^{N_b} \frac{1}{N_b} \left( \frac{1}{N_p} \sum_{k=3}^{N_p} (\text{pred}_k^{b,j} - f_j(x_k^b))^2 \right) $$

We also have to define an error rate for our problem. The error rate we chose is: $r_{\epsilon} = \frac{\epsilon_{\sigma}}{|\epsilon_{*} - \epsilon_{0}|}$ where $\epsilon_{*}$ is the best $\epsilon_{\sigma}$ error for a model M with $\hat{f}(x)$ calculated with Least Squares, and $\epsilon_{0}$ is the worst $\epsilon_{\sigma}$ error for a model M such that $\hat{f}_{M}(x) = 0, \forall x$. In all error calculations, we exclude the first two predictions of each batch from the squared error calculation. We need at least two points to be able to find a linear function and the first two predictions by the model are hence almost always wrong.

# 4 Models ICL₁ but do not ICL₂ $\mathcal{L}$

When trained on $D_F = D_I = \mathcal{N}(0, 1)$ and tested on $D_F^t = D_I^t = \mathcal{N}(0, 1)$, even many small models were able to converge to near-zero average error. Error rates fluctuated across batches and were not consistently zero, and so similar to [11]'s findings on MSE estimation by transformers.

These results hold for full transformer architectures models, as well as for models without feedforward networks. In fact the 12-layers, 8-attention heads model without feedforward networks performed equivalently to the full transformer model (see Fig. 1). Together these were our best models. However, models with only MLP layers, were unable to do the task.

**Observation 1.** Attention layers are necessary and sufficient to ICL₁ the class of linear functions. Models with only MLP were unable to ICL₁.

Varying the test distributions, however, altered performance considerably. All the models had systematic and non 0 average error once we chose $f \in D_F^t = \mathcal{N}(0, \sigma)$ for $\sigma > 2$. While [26] claim shifting the distribution sampled at inference of the functions does not affect performance, Fig. 1 clearly shows that for transformer models with soft attention, this task shift at inference reduces performance dramatically. Figure 1 shows that the error rate increases substantially and non-linearly as $D_F^t = \mathcal{N}(0, \sigma)$ and $\sigma$ increases. To ensure that comparisons between models are meaningful, for each $\mathcal{N}(0, \sigma)$, we set a seed when generating the 100 random linear functions, ensuring that each model sees the same randomly chosen functions and the same set of prompting points $x_i$. The Table 1 in the Appendix contains the full scores for average error.

The heatmaps in Fig. 2 show how our models generalized outside of three different sets of training distributions. Figure 2 shows that the generalization ability of models trained on $D_F = D_I = \mathcal{N}(0, 1)$ rapidly degrades once we sample elements far
<figure><img src="image_2.png" /><figcaption>**Fig. 1.** Evolution of error rates for various models with $D_F = D_I = D_I^t = \mathcal{N}(0, 1)$ and $D_F^t$ for various $\mathcal{N}(0, \sigma)$. $y = 0$ illustrates a model that predicts $f(x_n) = 0, \forall f$ and $\forall x_n$. And LS represents linear or ridge regression, which is trivially a perfect estimator given our totally clean input data.</figcaption></figure>

away from the training distribution, such as when we set $D_F^t$ and $D_I^t$ to $\mathcal{N}(0, \sigma)$ for $\sigma > 1$. On the other hand, while the errors in the performance of models trained on $D_F = D_I = \mathcal{N}(0, 100)$ are large when tested on $\mathcal{N}(0, 1)$, performance improves as we move toward testing regimes that take elements $x_i, f(x_i)$ within intervals [a,b] that contain a higher proportion of examples seen in training (Fig. 2). This trend is even clearer in the heatmap for the model trained on $\mathcal{N}(0, 10)$ in the same figure. The model shows relatively good performance on $D_F^t = D_I^t = \mathcal{N}(0, 1)$ though not as good as the model trained on $\mathcal{N}(0, 1)$. However, the model trained on $\mathcal{N}(0, 10)$'s performance improves as the testing regime comes to include a higher proportion of functions seen in training. During testing, we also observed that models trained on distributions with a larger variance<fnref n="2" /> provided better generalization performance in the sense that such models had lower squared errors than models trained on distributions with a smaller variance. Thus, a model trained on $\mathcal{N}(0, 10)$ has a better generalization ability and more robust performance than the model trained on $\mathcal{N}(0, 1)$.

To further test our hypothesis we evaluated the performance of our models using bimodal and uniform distributions for training data. The bimodal distribution $0.5\mathcal{N}(-1, 1) + 0.5\mathcal{N}(1, 1)$ expanded the range of values $f(x)$ the model will likely see during training. Models trained on this distribution for $D_F$ or on uniform distributions with a larger span like $\mathcal{U}(-5, 5)$ had more robust performance than models trained with $D_F = \mathcal{N}(0, 1)$ at least with $D_F^t = D_I^t = \mathcal{N}(0, \sigma)$ and $n \ge 6$. The best models had superior performance with $D_F^t = \mathcal{N}(0, \sigma)$ for $\sigma \ge 3$ (see Table 1, in which $D_I^t = \mathcal{N}(0, 1)$). But they also had limited generalization capability. For error values on various distributions and models see Table 1.

<footnote n="2">² A larger variance means that a larger interval includes a large majority if not the totality of the examples seen in training.</footnote>
<table><caption>**Table 1.** Comparison to show the evolution of squared $\epsilon$ type error depending on the distribution according to which we take the parameters, without taking into account the error of the prediction of the first and second prompts. Embedding size for all models is 64 except for 12L and 18L models (256K). For models N, $D_F = \mathcal{N}(0, 1)$; $D_i = D_i^t = \mathcal{N}(0, 1)$. For B, $D_F = 0.5\mathcal{N}(-1, 1)+0.5\mathcal{N}(1, 1)$ and U, $D_F = \mathcal{U}(-5, 5)$. For B and U testing, $D_i^t = \mathcal{U}(-1, 1)$ and $D_F^t = \mathcal{N}(0, \sigma)$. We show error rates for models prompted without and with the natural ordering on the prompts [sorted], for the large model size. 3NN refers to [17]'s method which generates values through the average of the 3 nearest neighbors.</caption><thead><tr><th>models \ σ</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th></tr></thead><tbody><tr><td>1L1AHN</td><td>0.1</td><td>0.8</td><td>5.1</td><td>13.1</td><td>26.9</td><td>39.7</td><td>53.0</td><td>84.8</td><td>120.0</td><td>153.2</td></tr><tr><td>1L4AHN</td><td>0.0</td><td>0.2</td><td>2.7</td><td>8.7</td><td>19.9</td><td>32.0</td><td>42.8</td><td>64.5</td><td>92.3</td><td>131.2</td></tr><tr><td>2L1AHN</td><td>0.0</td><td>0.1</td><td>2.0</td><td>4.9</td><td>13.7</td><td>27.0</td><td>36.1</td><td>64.9</td><td>99.0</td><td>134.0</td></tr><tr><td>2L4AHN</td><td>0.0</td><td>0.0</td><td>0.9</td><td>2.6</td><td>7.5</td><td>19.3</td><td>27.3</td><td>51.8</td><td>90.2</td><td>119.4</td></tr><tr><td>3L1AHN</td><td>0.0</td><td>0.0</td><td>0.9</td><td>3.0</td><td>8.2</td><td>16.8</td><td>24.4</td><td>48.4</td><td>76.7</td><td>113.2</td></tr><tr><td>3L4AHN</td><td>0.0</td><td>0.0</td><td>0.6</td><td>1.9</td><td>5.5</td><td>13.8</td><td>20.4</td><td>42.2</td><td>70.3</td><td>100.4</td></tr><tr><td>6L4AHN</td><td>0.0</td><td>0.0</td><td>0.5</td><td>1.6</td><td>4.6</td><td>11.6</td><td>16.8</td><td>33.7</td><td>58.3</td><td>87.9</td></tr><tr><td>12L8AHN</td><td>0.0</td><td>0.0</td><td>0.3</td><td>1.1</td><td>2.9</td><td>7.9</td><td>11.9</td><td>28.3</td><td>46.9</td><td>73.5</td></tr><tr><td>18L8AHN</td><td>0.0</td><td>0.0</td><td>0.2</td><td>1.1</td><td>2.8</td><td>7.1</td><td>10.3</td><td>22.9</td><td>40.3</td><td>64.6</td></tr><tr><td>6L4AHB,</td><td>0.01</td><td>0.04</td><td>0.23</td><td>0.44</td><td>1.19</td><td>2.15</td><td>3.08</td><td>4.8</td><td>9.98</td><td>18.01</td></tr><tr><td>6L4AHU,</td><td>0.02</td><td>0.04</td><td>0.11</td><td>0.24</td><td>0.57</td><td>1.36</td><td>1.82</td><td>4.62</td><td>10.23</td><td>15.07</td></tr><tr><td>12L8AHN,</td><td>0.0</td><td>0.0</td><td>0.32</td><td>1.34</td><td>3.14</td><td>8.8</td><td>12.13</td><td>30.14</td><td>49.37</td><td>73.93</td></tr><tr><td>sorted 12L8AHN</td><td>0.0</td><td>0.01</td><td>0.32</td><td>1.63</td><td>3.69</td><td>8.39</td><td>10.06</td><td>27.11</td><td>43.23</td><td>58.56</td></tr><tr><td>12L8AHB</td><td>0.0</td><td>0.01</td><td>0.08</td><td>0.29</td><td>0.78</td><td>2.23</td><td>3.66</td><td>9.04</td><td>18.68</td><td>30.23</td></tr><tr><td>sorted 12L8AHB</td><td>0.01</td><td>0.03</td><td>0.18</td><td>0.25</td><td>0.74</td><td>2.27</td><td>2.62</td><td>6.87</td><td>13.73</td><td>20.8</td></tr><tr><td>12L8AHU</td><td>0.0</td><td>0.01</td><td>0.13</td><td>0.71</td><td>1.92</td><td>6.78</td><td>10.92</td><td>27.91</td><td>38.75</td><td>64.39</td></tr><tr><td>sorted 12L8AHU</td><td>0.01</td><td>0.01</td><td>0.13</td><td>0.75</td><td>2.12</td><td>6.18</td><td>10.5</td><td>26.8</td><td>36.3</td><td>53.48</td></tr><tr><td>REFN: y=0</td><td>2.19</td><td>7.05</td><td>19.22</td><td>33.94</td><td>52.23</td><td>73.08</td><td>86.02</td><td>127.43</td><td>165.27</td><td>199.31</td></tr><tr><td>REFU: y=0</td><td>1.52</td><td>4.43</td><td>13.55</td><td>19.94</td><td>30.81</td><td>44.75</td><td>52.71</td><td>76.11</td><td>105.43</td><td>128.52</td></tr><tr><td>3NN</td><td>0.03</td><td>0.14</td><td>0.27</td><td>0.66</td><td>1.09</td><td>1.32</td><td>1.75</td><td>2.45</td><td>2.95</td><td>4.01</td></tr></tbody></table>

To sum up our findings,

**Observation 2.** (i) Models have better performance over intervals that contain a larger proportion of examples in the training distribution. (ii) Models trained on a distribution with larger variance (up to a certain point) had better generalization ability but less accuracy than models trained on distributions with smaller variance.

We also tested an alternative hypothesis to that in Observation 2 on which the total number of points observed in the distribution, rather than the proportion, is the critical factor. We trained one model on $\mathcal{U}(-1, 1)$ for 100k steps and another on $\mathcal{U}(-5, 5)$ for 500k steps, ensuring that both models were exposed to a statistically equivalent number
<figure><img src="image_3.png" /><figcaption>**Fig. 2.** Heatmaps showing the evolution of log error rates for a model trained on $D_I = D_F = \mathcal{N}(0, 1)$ (left), one trained on $D_I = D_F = \mathcal{N}(0, 10)$ (middle) and one trained on $D_I = D_F = \mathcal{N}(0, 100)$ (right). During testing, inputs and weights are drawn independently from $D_I \sim \mathcal{N}(0, \sigma_1)$ and $D_F \sim \mathcal{N}(0, \sigma_2)$, with $\sigma_1, \sigma_2 \in \{1, \dots, 10\}$. Note that while darker shades are always better, the shades have different values in the figures above to help with interpretability.</figcaption></figure>

of points within the interval $[-1, 1]$. The results remained similar, showing that the total number of points observed is not the critical factor.<fnref n="3" />

Model performance on out-of-distribution examples also improved somewhat when the sequence of $x_i$ in the training prompts ($p_1$) was sorted, to follow the natural order on $\mathbb{R}$, especially for larger models. Error rates with sorting are lower, by up to a third—compared to error rates without sorting for small values of $\sigma$, with $D_F^t = \mathcal{N}(0, \sigma)$. We also tested prompts ($p2$) on a 12L8AH model in a kind of “curriculum learning”; models with $p2 < p1$ did less well than models with $p1 = p2$.

We did a heatmap analysis of the weights of the elements in the attention matrices of the last layer of our model. The results are in Fig. 3. As can be seen, the model learns to pretty much ignore the first element and its prediction on that (the first two elements in its context) after the 6th prediction. Attention weights are largely uniform over the rest of the elements in the sequence. This suggests that it uses most if not all contextually given pairs of input function values, $x_i, y_i = f(x_i)$, to predict a value $y_n$ for input $x_n$.

All this leads to the following observation.

**Observation 3.** A model takes into account the whole sequence to predict $\hat{f}(x_n)$, not just some small, fixed subsequence.

Contrary to what [2,22] have suggested, our observations show that our models did not use linear regression to ICL the target function. If they had, we would not observe the error patterns or variations in performance, nor would the models disregard the class form of the target function in making their predictions; otherwise, the use of all elements in the sequence would have been superfluous. In conclusion, all models tested failed to $\text{ICL}_2 \mathcal{L}$. Moreover, we have seen a trade off between accuracy and generalizability from Observation 2; given current transformer models, we cannot have both accuracy and generalizability.

<footnote n="3">³ A checkpoint analysis further confirmed this indicating that optimal performance is typically reached at a fixed training step number, beyond which model performance fluctuates and does not really improve.</footnote>
<figure><img src="image_4.png" /><figcaption>**Fig. 3.** Heatmap showing the evolution of attention weights in an attention head of the last layer of 12AL8AH model</figcaption></figure>

# 5 Error Analysis and Boundary Values

During all our experiments, we noticed that all our models presented what we called *boundary values*, which are values that the model cannot exceed during inference predictions (see Fig. 4). These *boundary values* act as a filtering mechanism, restricting the model to generate outputs only within a specific range. This constraint prevents the generation of values beyond these limits, effectively preventing the model from generalizing its good performance on the task over a small interval to larger values outside that interval. This behavior can be seen also in [10]'s figures, despite their use of a transformer based on linear attention.

Since these models are trained from scratch, the only factor that can determine the boundary values is the training process itself. We conjecture that the model memorizes some bounds that are the largest and smallest values encountered during training, and then sets them as boundary limits. This effectively functions as a bandpass filter.

<figure><img src="image_5.png" /><figcaption>**Fig. 4.** Plots for model 12L8AH, trained on $D_I = D_F = \mathcal{N}(0, 1)$ for $f(x) = x$ for high values (left) of $x$ and $f(x) = 10x$ for normal (middle) then for low values of $x$ (right)</figcaption></figure>
However, determining the smallest and largest values a model could have encountered by training for data and coefficients in Gaussian distributions is impossible. As a result, to have control over this parameter, we trained from scratch models on uniform distributions. These experiments enabled us to confirm our hypothesis that boundary values correspond to the smallest and largest values that the model has encountered and memorized. One advantage of training on a uniform distribution is that we know exactly what the smallest and largest values that the model could have seen in its training. For example, setting $D_F, D_I$ to $\mathcal{U}(-5, 5)$, the largest value the model could have seen is $30 = 5 \times 5 + 5$ and the smallest value it could have seen is $-30$. Most likely it saw values significantly $> -30$ and $< 30$. All our models trained on $\mathcal{U}(-5, 5)$ estimate the target function more or less well for $x$ with $f(x) \in [-30, 30]$. But once we took inputs $x_i$ or coefficients of our target function $f$ to force $f(x_i) \notin [B^{-}, B^{+}]$, the estimations become either constant functions or chaotic. Figure 5 with equation $f(x) = 40x + 40$ provides another example.

Boundary values hold for all transformer models tested including attention only models. Linear transformer architectures also show boundary values [10]. For example with $D_F = D_I = \mathcal{U}(-5, 5)$, consider as an illustrative example the target function, $f(x) = 9x$, with our largest trained model. The model approximates $f(x)$ well within a certain range $[B^{-}, B^{+}] = [-30, 30]$ that corresponds to the maximal and minimal values it could have seen. On the other hand, it predicts $\hat{f}(x)$ as a constant function for $x$ such that $\hat{f}(x) \notin [B^{-}, B^{+}]$ within a certain range (See Fig. 5). Indeed, model errors depended on how many functions and values sampled forced the result outside $[B^{-}, B^{+}]$, as well as on the proportion of a model's training sequences in the test distribution (see Observation 2).

<figure><img src="image_6.png" /><figcaption>**Fig. 5.** Plots for $f(x) = 9x$ and $f(x) = 40x + 40$ for a 12L8ah model trained on $\mathcal{U}(-5, 5)$</figcaption></figure>

As shown in the left plot of Fig. 5, $\hat{f}^{+}(v) \approx 30$ for values $v$ such that the ground truth target function $f$ satisfies $30 \le f(v)$, and the model predicts an approximately constant function $\hat{f}^{-}(v) \approx -30$ for values $v$ on which $f(v) \le -30$. Different models trained on different uniform distributions give different boundary values (see below). To summarize:

**Observation 4.** (i) Boundary values $B^{-}, B^{+}$ are determined by maximal and minimal elements seen during training. (ii) For all models $M$ and for values $v$ such that $B^{+} <$
$f(v) < B^+ + \alpha$, where $\alpha$ is a constant determined by $M$, $\hat{f}_M(v) \approx B^+$, and for $B^- - \alpha < f(v) < B^-$, $\hat{f}_M(v) \approx B^-$. (iii) For functions and data samples when the values of $f(v)$ in the prompt sequence are such that $f(v) > B^+ + \alpha$ or $< B^- - \alpha$, the model assigns $\hat{f}(v)$ a random value within $[B^-, B^+]$.

The parameter $\alpha$ in Observation 4 accounts for the fact that larger models trained on the same distribution and the same number of data will ICL $\mathcal{L}$ functions over a slightly larger number of intermediate values than smaller models, as Fig. 1 suggests. Figure 7 in the Appendix shows plots of the predictions of two models (12L8AH, and 6L4AH) for $D_F, D_I = \mathcal{N}(0, 1)$ for target $f(x) = 10x$. The larger model has boundary values $\approx -13.7, 13.7$, the smaller one boundary values $\approx -12, 12$.

Since a model's predictions do not go beyond the boundary values defined by training, a consequence of Observation 4 is that error rates depend on the distance of the target function's values from the boundary values and hence the majority of the data points in the model's training. Figures 1 and 2 verify this progressive increase in error rate as the testing distribution gets larger and larger.

Training with distributions over much larger intervals, for example $\mathcal{N}(0, 10)$ or $\mathcal{N}(0, 100)$, can extend boundary values. Models trained on $\mathcal{N}(0, 10)$ show much better generalization ability than the models trained on $\mathcal{N}(0, 1)$. On the other hand, they are less accurate on small values as Fig. 2 shows. Models trained with $D_F = \mathcal{U}(-10, 10) = D_I$ generalized even better, but their performance on $\mathcal{N}(0, 1)$ was significantly worse than models trained on $\mathcal{N}(0, 1)$. The $\mathcal{N}(0, 100)$ performed poorly in our testing scenarios but improved as values got larger. Our observations thus present a dilemma: training on restricted intervals is needed for high accuracy, but it inevitably hampers generalizability. Generalizability requires training over larger intervals but that reduces accuracy.

As evident from Sect. 2, we are not the first to investigate out of distribution behavior. [10] investigated out of distribution behavior but only on $D_I \neq D_I^t$ (not shifts in $D_F$). Unlike [10], we found that larger models had better performance than smaller ones, when $f(x), x$ were within boundary values. Our results also differed from [26]'s shifting from $D_I$. They shift the prompt distribution but not that of the query. We shift both prompt and query distributions. With our shifts, we found that the choice of points is important and model performance degrades considerably when the values of the functions on the chosen points lie beyond the boundary values, which [26] do not. As far as we know, we are the first to take boundary values and their dependence on model parameters as key indications of what is actually going on in ICL.

# 6 What and how are the Models Learning?

In Sect. 4, we gave our reasons why the hypotheses of [2, 22] about models being able to ICL linear functions via linear regression are incorrect. The boundary values $B^-, B^+$ of Sect. 5 provide further evidence that models are not performing linear regression to define a linear function; otherwise, the models' predictions within $[B^-, B^+]$ should carry over to intervals larger than those defined by the majority of their training samples.
A lack of full generalizability is perhaps expected<fnref n="4" /> but the limitations on learnability we have seen are quite strong.

The presence of boundary values suggests that our models have memorized at least a portion of their training. Do our models simply overfit the data? We argue no. The pretraining data has no noise, and it is too large to be memorized by our models (our largest models with 256 size embeddings have 32M parameters; each parameter would have to encode on average sequences for over 100 different functions). Moreover, our models performed similarly on several different training distributions for $D_F$ and $D_I$ and tested on $\mathcal{N}(0, \sigma)$ for $\sigma \in \{1, 2\}$. To test overfitting, we added noise to our input, but that did not improve error rates. This implies that the models did not overfit to their training regimes. Our models seem to reason as well. Given 100 samplings with $D_F^t = \mathcal{N}(0, 1)$ nets on average 20 functions with coefficients the model with $D_F = D_I = \mathcal{U}(-1, 1)$ has not seen in training, we would expect the model’s performance to degrade more substantially than it did if only memory were involved. Moreover, models trained on $\mathcal{N}(0, 10)$ had substantially improved generalization ability over $\mathcal{N}(0, \sigma)$ for all $\sigma$ we tested, as can be seen in Fig. 2, even though the odds of having a sequence from training repeated on the test are very low.

To go back to the question of what models are learning, [17] conjectures that an attention layer only model generates values by averaging the three nearest neighbors. We tested [16]'s conjecture by using it to calculate the output. The results were too far from the values we got experimentally (Table 1). Using [17]'s suggestion also gives results that make the wrong predictions for points at or around boundary values and other points (see Fig. 4 of $f(x) = x$ for high values in the Appendix).

Instead given a prompt sequence $(x_1, y_1(=f(x_1)), x_2, y_2, \dots, x_n, ?)$, we hypothesize that the model optimizes a projection $\pi$ based on stored projections $\pi_n : Z \mapsto Y$, where $Z$ are inputs it has seen and $Y$ are the corresponding outputs $y_i$ it has seen paired with $x_i$ in $X$, to predict a value for $y_n \in [B^-, B+]$. Given Observation 1, this projection can be fully determined by the attention layers in the model. Our observations show that more attention heads, but even more importantly more attention layers, improve model performance. This is, we conjecture, because they provide more memory for storing nearby sequences from which the model can draw to optimize $\pi$. To sketch a possible projection, we mimic the autoregressive reasoning. We start just with a target input $x_1^t$. The attention matrices have encoded a projection in their weights that takes $g_0 : x_1^t \mapsto z_i^m$, where $z_i^m = x_1^t \pm e$ for which there is also a stored projection $p_0 : x_1^m \mapsto y_i^m$. It then sets $\pi_0$ such that $\pi_0(x_1^t) = p_0(g_0(x_i^m)) = y_i^m \pm d$, where $d$ is close to $e$. This is usually wrong, since there are many possible choices for $g_0$ and $p_0$ from training. Now suppose the model has received $x_1^t, y_1^t, x_2^t$. It now tries to predict $\pi_1(x_2^t)$. To do this it must find projections $g_1, p_1$, and $\pi_1$ such that $\pi_1(x_2^t) = p_1 \circ g_1(x_2^t)$ and $\pi_1(x_1^t) = y_1^t$. Several choices for $p_1, g_1, \pi_1$ may obey these constraints and so the model may interpolate between them. $\pi_1$ at level $l$ is defined via the context matrix given by attention [14].

Now, what happens when the model has to predict a value for $x_i^t$ such that either $x_i$ or the projection lies beyond the boundary values it has learned in training? The

<footnote n="4">⁴ A linear function is a $\Pi_1^0$ set in the Borel hierarchy, which [3] show is not learnable using ordinary LLM assumptions.</footnote>
boundary values and the parameter $\alpha_M$ from Observation 4 define the limits of what a “close” element is. If $\pi(x_i) = y_i$ is not too far from $[B^-, B^+]$, then the closest element the model can find is the boundary value or something near it. Far away elements are $\text{Ker}(\pi)$ and so sent to 0 or some nearby value. Observation 4 establishes that the projection is not linear over intervals larger than $[B^-, B^+]$, and that’s what our algorithm also predicts.

So, even though memory is a crucial component of ICL₁, it’s not everything. The autoregressive component of our algorithm above resembles basic causal language modeling, but it incorporates a basic reasoning element of closeness. The closer a sequence stored in memory of a model M *z* is to the target sequence, the higher the probability that $\pi_M(x_n) = ay_n^z$, for $a < \alpha_M$.

# 7 Conclusion

While transformers are theoretically capable of learning linear regression to perform ICL *L*, we have demonstrated they do not do this in practice. Our models achieve strong statistical results comparable to state-of-the-art algorithms when training and test distributions are the same. However, we aimed to assess whether our models truly learn the underlying class representation, and they do not. All tested models failed to robustly learn the class form $f(x) = ax+b$ of linear functions on both noisy and non-noisy data. Had models used linear regression on our clean data, they would have exactly inferred the class form of linear functions and correctly estimated the parameters *a*, *b*.

We also found that attention layers were both necessary and sometimes sufficient for good ICL performance. More layers and more heads improved performance. Generalization ability improved as perhaps expected when models were trained on distributions with larger variance.

We also analyzed model errors and emphasized the importance of boundary values that define intervals within which the vast majority of all of values of the training sequences occur. End users, who typically lack knowledge about the training data distribution for the models they use, need to know about model sensitivity to training and testing regimes. Otherwise, they may unwittingly employ ICL on tasks significantly beyond the original training domain with a consequent substantial loss of accuracy. This limitation will affect larger models working on different tasks.

Finally, we have offered a hypothesis about how models learn. Our hypothesis *H* explains our observations, but we have not fully shown that transformer models actually implement this hypothesis. In future work, we will attempt to isolate various stages of the algorithm by probing what models do at various layers, following [15, 23].

**Acknowledgments.** We gratefully acknowledge the support of the EU grant TUPLES, the grants SARER, Summ-RE (ANR-20-CE23-0017), and the AI Cluster ANITI (ANR-19-PI3A-0004). This work used the HPC resources from CALMIP (Grant 2016-P23060).

# References
1. Ahn, K., Cheng, X., Daneshmand, H., Sra, S.: Transformers learn to implement preconditioned gradient descent for in-context learning. In: Advances in Neural Information Processing Systems, vol. 36, pp. 45614–45650 (2023)

2. Akyürek, E., Schuurmans, D., Andreas, J., Ma, T., Zhou, D.: What learning algorithm is in-context learning? Investigations with linear models. arXiv preprint arXiv:2211.15661 (2022)

3. Asher, N., Bhar, S., Chaturvedi, A., Hunter, J., Paul, S.: Limits for learning with large language models. In: 12th Joint Conference on Lexical and Computational Semantics (*Sem). Association for Computational Linguistics (2023)

4. Bai, Y., Chen, F., Wang, H., Xiong, C., Mei, S.: Transformers as statisticians: provable in-context learning with in-context algorithm selection. In: Advances in Neural Information Processing Systems, vol. 36 (2024)

5. Bhattamishra, S., Patel, A., Blunsom, P., Kanade, V.: Understanding in-context learning in transformers and LLMs by learning to learn discrete functions. arXiv preprint arXiv:2310.03016 (2023)

6. Brown, T., et al.: Language models are few-shot learners. In: Advances in Neural Information Processing Systems, vol. 33, pp. 1877–1901 (2020)

7. Dong, Q., et al.: A survey on in-context learning. arXiv preprint arXiv:2301.00234 (2022)

8. Fu, D., Chen, T.Q., Jia, R., Sharan, V.: Transformers learn higher-order optimization methods for in-context learning: a study with linear models. arXiv preprint arXiv:2310.17086 (2023)

9. Garg, S., Tsipras, D., Liang, P.S., Valiant, G.: What can transformers learn in-context? A case study of simple function classes. In: Advances in Neural Information Processing Systems, vol. 35, pp. 30583–30598 (2022)

10. Giannou, A., Yang, L., Wang, T., Papailiopoulos, D., Lee, J.D.: How well can transformers emulate in-context newton's method? arXiv preprint arXiv:2403.03183 (2024)

11. Liu, J.W., Grogan, J., Dugan, O.M., Arora, S., Rudra, A., Re, C.: Can transformers solve least squares to high precision? In: ICML 2024 Workshop on In-Context Learning (2024)

12. Mahankali, A., Hashimoto, T.B., Ma, T.: One step of gradient descent is provably the optimal in-context learner with one layer of linear self-attention. arXiv preprint arXiv:2307.03576 (2023)

13. Mirzadeh, I., Alizadeh, K., Shahrokhi, H., Tuzel, O., Bengio, S., Farajtabar, M.: GSM-symbolic: understanding the limitations of mathematical reasoning in large language models. arXiv preprint arXiv:2410.05229 (2024)

14. Naim, O., Asher, N.: On explaining with attention matrices. In: ECAI 2024, pp. 1035–1042. IOS Press (2024)

15. Nanda, N., Chan, L., Lieberum, T., Smith, J., Steinhardt, J.: Progress measures for grokking via mechanistic interpretability. arXiv preprint arXiv:2301.05217 (2023)

16. Olausson, T.X., et al.: Linc: a neurosymbolic approach for logical reasoning by combining language models with first-order logic provers. In: The 2023 Conference on Empirical Methods in Natural Language Processing (2023)

17. Olsson, C., et al.: In-context learning and induction heads. arXiv preprint arXiv:2209.11895 (2022)

18. Panwar, M., Ahuja, K., Goyal, N.: In-context learning through the Bayesian prism. arXiv preprint arXiv:2306.04891 (2023)

19. Pérez, J., Barceló, P., Marinkovic, J.: Attention is turing-complete. J. Mach. Learn. Res. 22(75), 1–35 (2021)

20. Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., Sutskever, I., et al.: Language models are unsupervised multitask learners. OpenAI Blog 1(8), 9 (2019)

21. Raventós, A., Paul, M., Chen, F., Ganguli, S.: Pretraining task diversity and the emergence of non-Bayesian in-context learning for regression. In: Advances in Neural Information Processing Systems, vol. 36 (2024)
22. Von Oswald, J., et al.: Transformers learn in-context by gradient descent. In: International Conference on Machine Learning, pp. 35151–35174. PMLR (2023)

23. Wang, K., Variengien, A., Conmy, A., Shlegeris, B., Steinhardt, J.: Interpretability in the wild: a circuit for indirect object identification in GPT-2 small. arXiv preprint arXiv:2211.00593 (2022)

24. Wu, J., Zou, D., Chen, Z., Braverman, V., Gu, Q., Bartlett, P.L.: How many pretraining tasks are needed for in-context learning of linear regression? arXiv preprint arXiv:2310.08391 (2023)

25. Xie, S.M., Raghunathan, A., Liang, P., Ma, T.: An explanation of in-context learning as implicit Bayesian inference. arXiv preprint arXiv:2111.02080 (2021)

26. Zhang, R., Frei, S., Bartlett, P.L.: Trained transformers learn linear models in-context. J. Mach. Learn. Res. 25(49), 1–55 (2024)

27. Zhang, Y., Zhang, F., Yang, Z., Wang, Z.: What and how does in-context learning learn? Bayesian model averaging, parameterization, and generalization. arXiv preprint arXiv:2305.19420 (2023)