We study language acquisition by transformers trained on the Random Language Model: a synthetic hierarchical grammar with random production rules, whose sparsity is controlled by a single parameter and whose conditional entropies are known exactly. On depth-two grammars, we find that sparsity sets both the order and the speed of learning. Dense grammars are learnt one level at a time: a token is predicted from its sibling after t∝V steps and from the full context only after t∝V3, so the two stages are separated by a plateau that widens with V. Sparse grammars are learnt all at once, at t∝V, with a smaller exponent than even the first stage of a dense one. The exponents 1 and 1/2 already appear at depth one, where we derive them for a linear model trained with Adam.
Figures & tables
Figure 1: The sparsity of the grammar sets both the schedule and the time complexity. Left: The RLM. A root symbol R is expanded recursively through the production rules Qa of Eq. ( 1 ) (black squares); first into a pair of latent variables (Z1,Z2) , then into the observed sequence (X1,…,X4) . Right: Learning dynamics of a deep transformer for a dense ( β=1.2 , V=48 , blue) and a sparse ( β=6.0 , V=48 , red) grammar. The test loss is normalised, Lˉ=(L−H1)/(lnV−H1) : it equals 1 for the uniform predictor and 0 at the sibling entropy H1=H(X4∣X3) (dashed). Dotted lines mark the full-context floor H2=H(X4∣X1,X2,X3) of each grammar. The dense grammar descends in two stages separated by a plateau at H1 , the sparse one in a single step. Markers locate the two time complexities, t1∗ (circles) and t2∗ (squares), shown against V in the insets. Averages over 8 realisations of the grammar and transformer initialisation; bands give one standard deviation.
Figure 2: A single parameter controls the sparsity of the grammar. (a, b) The rule matrix Qa(b,c) of one parent symbol at V=32 and β=1.2,6.0 on a shared logarithmic colour scale. At β=1.2 , many pairs carry appreciable weight, so the grammar is dense ; at β=6 the weight condenses onto a handful of pairs and the grammar is sparse . (c) The effective number of rules per symbol, M(β) of Eq. ( 2 ), averaged over parent symbols and over realisations of the couplings. Dashed lines give the V→∞ prediction of Eq. ( 3 ), with the freezing transition at β=2 [ 21 ] marked by a vertical line.
Figure 3: Both regimes are present at L=1 . Transformers trained on a depth- 1 RLM to predict X2 from X1 . (a) Normalised test loss (L−H)/(lnV−H) at V=128 , with H=H(X2∣X1) computed exactly from the rules of each realisation, for three values of β in the dense phase ( 1.2 , 1.4 , 1.6 ; blues) and two in the sparse phase ( 3 , 6 ; reds). Dotted lines mark the levels 1 and 1/2 . (b) The crossing time t∗ against V for V=32 to 256 . Solid lines show the two scalings t∗∝V and t∗∝V derived for a linear model trained with Adam in App. C ; within a phase, different β shift t∗ by an O(1) factor without changing the exponent. Averages over repetitions, bands giving one standard deviation; protocol in App. A .
Appendix figures & tables8 assets
Supplementary material from the paper’s appendix.
Appendix
Figure 4: First stage of the dense grammar, β=1.2 . Normalised loss between lnV and H1=H(X4∣X3) , for V=12 to 64 with 8 realisations each, against the training step (left) and against t/Vs1 with s1=1.00±0.02 (right). The collapse identifies the first time scale as linear in the vocabulary size.
Figure 5: Second stage of the dense grammar, β=1.2 . Normalised loss between H1 and H2=H(X4∣X1,X2,X3) , for V=12 to 48 with 8 realisations each, against the training step (left) and against t/Vs2 with s2=3.04±0.34 (right). The curves sit at the plateau for t≪V3 and reach the floor only for t≫V3 , two powers of V later than the first stage. V=64 is omitted: it does not complete this stage within the available budget. Curves are smoothed with a 5 -point moving average in lnt , the second stage being the noisier of the two.
Figure 6: First stage, β=2 . Same quantities as Fig. 4 , for V=16 to 48 , with s1=0.73±0.04 . These runs use the smaller model of App. A and are not to be compared on the same axes with the β=1.2 and β=6 curves.
Figure 7: Second stage, β=2 . Same quantities as Fig. 5 , with s2=2.06±0.37 . At V=48 the stage only partly completes within the available budget, so the fit relies on the half-way crossing rather than on the approach to the floor, and the uncertainty on s2 is correspondingly larger. At β=2 the two stages are separated by roughly 1.3 powers of V , against 2 powers at β=1.2 : the schedule is already contracting.
Figure 8: Single stage of the sparse grammar, β=6 . Normalised loss between lnV and H2 , for V=12 to 48 with 8 realisations each, against the training step (left) and against t/Vs with s=0.47 (right); fitting the single-stage curves directly gives s=0.44±0.03 . No plateau at H1 is visible: the sibling and the cousins are acquired together, and one exponent describes the whole descent.
Figure 9: Linear model at small batch size, B=2 . Normalised loss (L−H)/(lnV−H) of the linear model, averaged over 8 grammars, for V=128 to 512 : (a) β=6 against t/(VlnV) , (b) β=1.2 against t/V . (c) Time t∗ at which the averaged loss first reaches 1/2 , with Eqs. ( 11 ) and ( 17 ), t∗∝VlnV and t∗∝V , prefactors fitted. Adam with its default parameters ( β1=0.9 , β2=0.999 , ϵ=10−8 , no weight decay; App. A ), learning rate η=3×10−5 , fresh samples at every step. At β=1.2 the loss levels off above the floor at late times and then rises (faint lines), more so at larger V ; this does not affect t∗ .
Figure 10: At the beta 2.0, depth helps. Depth- 1 and depth- 2 transformers trained on the same grammars at β=2 , for V=16,24,32,48 with 8 repetitions each. The two networks are indistinguishable while the loss falls from the uniform predictor, and separate at long times: the depth- 2 network reaches the full-context floor, whereas the depth- 1 network stalls above it. Smaller model of App. A (embedding dimension 128 , batch size 64 ).
Figure 11: In the sparse regime, depth is irrelevant. The same comparison at β=6 , for V=12,16,24,48 with 6 paired repetitions each, on a logarithmic vertical scale so that the approach to the floor is visible over three decades. The two depths are indistinguishable throughout, the depth- 1 network being at most marginally slower. Protocol of the main text (embedding dimension 256 , batch size 128 ); the depth- 1 runs were trained on the rule tensors of the depth- 2 runs, read from file, so the grammars agree exactly.
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.
Variability in syntactic structure and parsing ambiguity are fundamental aspects of natural language which modern LLMs have to learn to deal with proficiently. Previous "post-mortem" studies have shown that fully trained Transformers indeed approximate canonical parsing algorithms such as inside-outside, but little is established theoretically about how this is actually learned. Here, we make a first step towards addressing this by studying the learning dynamics in synthetic context-free grammars with variable parse tree topology. The stagewise learning curves are fully interpretable, in the sense that all of the loss stages a) can be computed analytically, and b) can be clearly associated to the representation of a hidden latent tree variable, which allows to lift ambiguity and utilize information from wider contexts. By probing representations, we visualize the emergence of inside-outside computation across depth and during training: while during early stages local (bigram/trigram) information is used and involves only a few initial layers, gradually the Transformer develops an encoder-decoder structure where the most abstract latent information is encoded in the middle, and the latter half of the network is used to progressively refine the final token prediction. This organization of computation is reminiscent of findings in real-world LLMs, and importantly applies here to a synthetic model which allows full analytical control of the data statistics, thus opening the door to theoretical progress.
Jack T. Parley
Institute of Physics École Polytechnique Fédérale de Lausanne (EPFL) Lausanne, Switzerland
We present the Prior-Fitted Language Model (PFLM), a 300M-parameter byte-level transformer pretrained only on samples from a synthetic non-linguistic prior. Given a prefix of real text, it learns to predict the language in context with frozen weights, having never seen a word of any real language. Every training sequence is generated by a recurrent structural causal model drawn fresh from a distribution over such models. The model never sees the same language twice during training, so the only way to predict the continuation is to infer the language from the prefix. Samples from this prior share the statistical signatures of natural text: Zipfian frequencies, slow entropy-rate convergence, and long-range dependence. On Wikipedia in six languages, bits per byte fall from the uniform eight to between 0.9 and 2.4 at one million bytes of context. Given numerals instead of text, PFLM learns to count, to compare magnitudes, and to add approximately. It predicts deterministic sequences like Rudin-Shapiro or the prime indicator, and it compresses six non-text domains, from source code to speech, below gzip and PPMd. The model has not learned a language. It has learned to learn one.
Lennart Carstens-Behrens, Holger Fröhlich
Fraunhofer Institute for Algorithms and Scientific Computing SCAI · University Hospital Bonn