NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Learning Syntax Without Planting Trees: Understanding Hierarchical Generalization in Transformers

Kabir Ahuja, Vidhisha Balachandran, Madhur Panwar, Tianxing He, Noah A. Smith, Navin Goyal, Yulia Tsvetkov

TL;DR

This paper investigates why Transformer models, despite lacking explicit architectural biases for hierarchical structures, tend to generalize hierarchically in language tasks. They discover that the language modeling objective is a key driver of this inductive bias, and that subnetworks within the Transformer learn different generalization strategies (hierarchical vs. linear) that compete during training.

What Problem Does This Paper Address?

Natural language is inherently hierarchical: words form phrases, phrases form clauses, and so on, up to the full sentence. A crucial question in NLP is whether neural networks, particularly Transformers, can learn and generalize this underlying syntactic structure. While Transformers have shown impressive performance, it's unclear why they learn hierarchical structures when not explicitly designed to do so. Previous work has shown that some architectures (like RNNs) struggle with hierarchical generalization, while others (like tree-structured networks) succeed. This paper aims to pinpoint the factors that enable Transformers to exhibit hierarchical generalization, focusing on the role of the training objective and the internal mechanisms of the model.

Methods

The authors employ a multi-pronged approach:

  • Training Objective Analysis: They train Transformer models from scratch on five different tasks using five distinct training objectives (language modeling, sequence-to-sequence, prefix language modeling, sequence classification, and cloze completion). This allows them to isolate the impact of the objective on generalization.
  • Subnetwork Discovery via Pruning: To understand how different generalization strategies are implemented, they use attention head pruning techniques. This involves training "gates" for each attention head and then pruning them to identify subnetworks responsible for specific behaviors (e.g., hierarchical vs. linear generalization). They introduce three pruning strategies: train-prune, gen-prune, and train-gen-prune.
  • Bayesian Interpretation: They leverage a Bayesian framework (using Probabilistic Context-Free Grammars - PCFGs for hierarchical rules and regular grammars for linear rules) to model the "simplicity" of different grammars. They then correlate the Transformer's generalization behavior with which grammar provides a better Bayesian explanation (balancing likelihood and prior probability) for the training data.

Datasets

The study uses a combination of existing and synthetic datasets:

  • Tasks: Question Formation (English and German), Passivization, Tense Reinflection, and Simple Agreement.
  • Data Generation: For most tasks, they use datasets constructed from context-free grammars (CFGs) that are designed to be ambiguous, meaning they are consistent with both hierarchical and linear rules. This ambiguity is crucial for testing generalization.
  • Synthetic Datasets: For the Simple Agreement task, they construct specific CFG and regular grammars to generate datasets of varying diversity (small and large).
  • Test Sets: They create both in-distribution (ambiguous) and generalization (hierarchical-only) test sets for evaluation.

Key Insights and Ideas

  • Language Modeling is Key: The language modeling (LM) objective is the only one that consistently leads to strong hierarchical generalization across all tested tasks. Other objectives, even when achieving high in-distribution accuracy, often fail to generalize hierarchically.
  • Competing Subnetworks: Transformers learn distinct subnetworks that implement either hierarchical or linear generalization strategies. These subnetworks coexist and compete throughout training, even as the overall model's behavior shifts towards hierarchical generalization.
  • Ambiguity Drives Competition: The presence of ambiguous training data (consistent with both rules) is essential for the emergence and coexistence of these competing hierarchical and linear subnetworks. When the data is disambiguated to favor only the hierarchical rule, the linear subnetwork disappears.
  • Bayesian Simplicity Bias: A Transformer LM is more likely to generalize hierarchically if a hierarchical grammar provides a simpler and better fit to the training data (higher posterior probability) compared to a linear grammar. This suggests that Transformers, like ideal Bayesian learners, exhibit a simplicity bias.

Key Results

  • Objective Performance: Language modeling achieved consistently high generalization accuracy (around 75% on average, with some seeds reaching 100%) across all five tasks. Other objectives struggled significantly on generalization.
  • Subnetwork Dynamics: Pruning experiments revealed that subnetworks for both linear and hierarchical generalization can be identified. The linear subnetwork emerges early in training, while the hierarchical subnetwork appears later. Crucially, the linear subnetwork persists even as the model achieves high hierarchical generalization.
  • Bayesian Correlation: For tasks where the training data is diverse, the Transformer's hierarchical generalization ability correlates with the hierarchical grammar having a higher posterior probability than the linear grammar. This correlation breaks down for less diverse datasets or when the hierarchical grammar doesn't offer a significant simplicity advantage.
  • Head Capacity: The number of attention heads appears to influence the development of these generalization subnetworks, with fewer heads potentially hindering their formation.

Limitations and Future Work

  • Causality vs. Correlation: The Bayesian analysis establishes a correlation between grammar simplicity and Transformer generalization, but doesn't definitively prove that the Transformer internally learns these grammars. Future work could explore the algorithmic and implementation-level details of this process.
  • Delayed Generalization: The paper notes that hierarchical generalization is often delayed in LMs, and the reasons for this delay remain unclear.
  • Deeper Models: The study mentions that deeper Transformers sometimes fail to generalize hierarchically, which is an avenue for future exploration.
  • Broader Interpretations: The Bayesian framework used here has been applied to other human learning phenomena. Future work could explore its applicability to other forms of generalization in Transformers.
  • Complex Tasks: The Bayesian interpretation was primarily applied to the simple agreement task due to the ease of constructing grammars. Extending this to more complex transformation tasks (like question formation) would be valuable.

Summary updated

BibTeX
@article{ahuja2025learning,
  author = {Kabir Ahuja and Vidhisha Balachandran and Madhur Panwar and Tianxing He and Noah A. Smith and Navin Goyal and Yulia Tsvetkov},
  title = {{Learning Syntax Without Planting Trees: Understanding Hierarchical Generalization in Transformers}},
  journal = {Trans. Assoc. Comput. Linguistics},
  year = {2025},
  doi = {10.1162/tacl\_a\_00733},
}
Figure