arXiv:2610.20337cs.LGOct 2026

Manuscript Offprint

arXiv:2610.20337

Training Dynamics of Transformers on Unbounded Inputs: Attention Learning Can Lag Output Layer Learning

Yaomengxi Han, Debarghya Ghoshdastidar

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

The success of transformers is attributed to several factors, including their capacity to express formal languages over variable-length inputs, and ability of the attention mechanism to focus on relevant input features. However, there is limited understanding of the training dynamics of shallow transformers, specifically how to decouple the learning of the attention and the output (feedforward) layer. A natural extension of the theory of one-step gradient feature learning in feedforward networks suggests that one could first learn the attention weights with a single gradient step, and then train the output layer. We show that this fails for a shallow transformer trained to recognize a star-free language from unbounded-length input. We prove that the transformer can be trained to recognize the language with a three-phase gradient descent: the output layer is updated first, followed by an attention update, and the output layer is fine-tuned in the third phase. In contrast, the language cannot be learned by training only the attention or output layer, or by a single attention-layer update followed by finetuning the output layer. We empirically show that “attention learning lagging output layer learning” is beneficial in other language recognition, regression, and in-context learning tasks as well.

1 INTRODUCTION

How do neural architectures learn features and attentions? What is the impact of the learning dynamics on the generalization abilities of the trained model? These are major open questions in deep learning theory. Significant progress has been made to address these questions in feedforward neural networks, both from a broad optimization perspective—for instance, implicit biases of gradient descent (Lyu and Li, 2020)—as well as from the viewpoint of learning specific tasks, like learning parity, sparse polynomial or gaussian mixture classification (Daniely and Malach, 2020; Abbe et al., 2023; Frei et al., 2023). A notable theoretical insight has emerged in this context, sometimes referred to as one-step gradient feature learning (Ba et al., 2022; Shi et al., 2023)—for learning tasks using a shallow feedforward network, it often suffices to perform one step of gradient descent on the input layer to learn the features and then train the output layer till convergence. Decoupling the training of different layers helps to study simplicity bias, lottery tickets, etc., in feedforward networks (Abbe et al., 2023; Shi et al., 2023).

Much less is understood about how attention learning happens in transformers. There are studies on implicit biases and training dynamics (see Relation to prior work), but it is not clear what the ideal way is to theoretically decouple the training of attention and output layers in a shallow transformer, comprising a softmax attention layer followed by a feedforward output layer. For example, a natural extension of the gradient feature learning paradigm in a shallow transformer is to perform a single gradient descent update of the attention weights, followed by training of the output layer. It is not clear if a “one-step attention learning” paradigm is suitable for training shallow transformers or a different multi-phase decoupled training is needed, for example, in context of the prototypical task of learning formal languages (Strobl et al., 2024).

Contributions. We study how attention learning happens in a shallow transformer trained to recognize formal language, where the training consists of words of varying length (potentially unbounded). Despite active research on the theory of attention learning, little is known about the training dynamics of transformers when trained on variable-length inputs. We aim to address if a one-step attention learning paradigm—akin to gradient feature learning in feedforward networks (Shi et al., 2023)—is an appropriate simplification of how shallow transformers learn to recognize a formal language. We have the following key contributions.

Multi-phase gradient descent dynamics in learning the occurrence language. We consider a minimalist example of learning to recognize the occurrence language As=Σ∗sΣ∗A_s = \Sigma^* s \Sigma^* on the finite alphabet Σ={1,…,d}\Sigma = \{1, \ldots, d\}, i.e., the language of all words containing ss, where s∈Σs \in \Sigma is not known apriori (Figure 1a). The occurrence language can be expressed by a shallow transformer with dd-variate encoding of each bit, a single attention head, and a linear output layer (Proposition 1). Empirically, for this task, gradient descent (GD) first learns the output layer before the attention weights (Figure 1b). Theorem 2 formally shows that it is possible to learn the optimal transformer using multi-phase GD: (Phase 1) one-step GD update of output layer; (Phase 2) one-step GD update of attention weights; (Phase 3) training output layer till convergence. This result indicates that a one-step attention learning works in transformers, but when the attention learning happens after output layer is partly trained (see Figure 1d-e).

Impossibility results for simpler learning approaches. It is natural to question if the single-head transformer can also be trained using a simpler gradient-based rule. Propositions 2 and 3 show that, if either only the output or the attention layer is trained with the other held fixed, then a shallow transformer cannot learn uniformly over the set of dd languages {As:s∈Σ}\{A_s : s \in \Sigma\}. The same also holds for transformers with O(d)O(\sqrt{d}) heads (Proposition 4), but arbitrary fixed attention, showing that lazy training fails for this task, unless with a large number of heads. Furthermore, Theorem 1 shows that a two-phase approach of one step of GD update of attention, followed by training the output, cannot learn the language. Thus a naïve extension of gradient feature learning principle does not work in this case.

Finite sample analysis and empirical study on other languages. While the aforementioned results consider GD on population loss, Corollary 1 extends the learnability result to finite sample setting, showing that the three-phase training also works with O(dln⁡d)O(d \ln d) samples for any fixed tolerance. We also numerically study other regression, language recognition, or in-context learning tasks with shallow transformers, and show that in most cases, the three-phase approach (with one-step GD update of output layer before one-step attention learning) outperforms other decoupled training of attention and output layers. Hence, we hypothesize that this approach is potentially a reasonable theoretical simplification of transformer training dynamics.

Relation to prior work. The expressivity and training dynamics of transformers is an active area of research in deep learning theory. While there is a rich literature on the expressivity of transformers in terms

(a)

Figure

(c)

Training PlanPhaseTrain Loss (mean ± std)Test Acc. (mean ± std)
attention(A) only11.64 ± 0.080.50 ± 0.01
output(O) only10.74 ± 0.050.86 ± 0.01
A(1x)→O20.72 ± 0.260.76 ± 0.13
O(1x)→A(1x)→O3(2 ± 1)×10-61.00 ± 0.00

(b)

(d)

Figure

(e)

Figure 1: (a) Automaton of the occurrence language As=Σ∗sΣ∗A_s = \Sigma^* s \Sigma^* with alphabet Σ={1,…,d}\Sigma = \{1, \ldots, d\} and unknown s∈Σs \in \Sigma. The language can be expressed by a transformer with one attention head and linear output layer (1), which encodes word w=(w1,…,wm)w = (w_1, \ldots, w_m) as (ew1,…,ewm)(\mathbf{e}_{w_1}, \ldots, \mathbf{e}_{w_m}), where ei∈Rd\mathbf{e}_i \in \mathbb{R}^d is ii-th standard basis vector. The optimal shallow transformer has weights of the output layer β=2es∈Rd\beta = 2\mathbf{e}_s \in \mathbb{R}^d, and attention score for symbol i∈Σi \in \Sigma is γi=1\gamma_i = 1 if i=si = s, and 0 otherwise. (b) A shallow transformer is trained to recognize AsA_s using gradient descent jointly on both layers. The output gap (βs−max⁡i≠sβi)/2(\beta_s - \max_{i \neq s} \beta_i)/2 and attention gap (γs−max⁡i≠sγi)(\gamma_s - \max_{i \neq s} \gamma_i), plotted across training epochs, show that output gap increases before attention gap appears, indicating that attention learning lags output layer learning. (c) Several training plans are considered, which decouple gradient descent on attention layer (A) and output layer (O), sometimes using a single gradient step (1x). “One-step attention learning”, where one attention update is followed by training output layer till loss converges, A(1x)→O, does not correctly learn the language. We later prove that a 3-phase training, O(1x)→A(1x)→O, correctly learns to recognize AsA_s. (d)-(e) show how our 3-phase plan reaches the ideal gaps in both layers, but training attention/output only, or attention before output do not.

of formal languages, circuit complexity or space/time complexity classes (see surveys in Strobl et al. (2024); Lin and Barceló (2025); Barceló et al. (2025)), the current research on training dynamics of transformers has predominantly focused on finite-dimensional learning problems. There are results on the lazy training regime of shallow transformers (Hron et al., 2020; Arda et al., 2026) or implicit biases towards maximum margin (Tarzanagh et al., 2023), which hold in general data settings. However, fine-grained characterization of the training dynamics requires studying specific data distributions or learning tasks, which we also assume.

Wen et al. (2025); Han and Ghoshdastidar (2026) study the training dynamics of transformers when trained on sparse parity function—a prototypical task for feedforward networks. Marion et al. (2025); Dohmatob (2025) study the joint dynamics of attention and output layer of a shallow transformer for single-location regression, while Wang et al. (2024) derive the joint training dynamics for sparse token averaging. The settings in these works correspond to finite/fixed-dimensional inputs, thereby ignoring the ability of transformers to handle varying input length. Furthermore, the attention-only or joint training dynamics do not reveal the importance of learning different layers. Bietti et al. (2023); Chen et al. (2024) study the dynamics of training induction heads in transformers for next token prediction on bigrams and in-context n-gram Markov chains. Bietti et al. (2023) empirically observe, under joint training, the induction head is learned more slowly than the global bigram model, similar to Figure 1b, and show that the induction head can be learned by sequential steps: first on output matrix, then on two attention key matrices. Chen et al. (2024) also consider a multi-phase training of different layers, involving a parameter-inefficient polynomial regression. Both works, however, restrict the problems to fixed context windows, effectively to fixed-length inputs. Moreover, these works do not explore if attention or output needs to be learned first.

Several empirical works study how transformers learn formal languages (Liu et al., 2023a,b; Butoi et al., 2025), but theoretical characterization of the training dynamics of transformers in learning formal languages is rather recent. Yang et al. (2024) study the gradient flow dynamics of learning word co-occurrence for fixed-length input, showing that the first phase mainly trains the output layer, before the joint training of output and attention happens. Huang et al. (2025) study the joint training dynamics of even-pairs and parity-check languages (the latter with chain of thought), restricting to bounded-length words, and observe a similar slow learning of attention in early phase. These works implicitly suggest that a phenomenon equivalent to one-step gradient feature learning in feedforward networks may not hold for transformers, where attention learning typically happens late. Our work takes a step towards resolving this matter by proving that one-step attention learning mechanism is indeed possible, but needs to follow a step of output layer update, which aligns with the intuition from known joint training dynamics. Such a simplifying characterization of the training dynamics is a key contribution of our work, along with the impossibility of learning via simpler attention-only or attention-first training strategies. Prior works also assume fixed- or bounded-length inputs, which our analysis does not require.