A Neural Model for Regular Grammar Induction

Peter Belcak, David Hofer, Roger Wattenhofer

Abstract

Grammatical inference—the process of learning formal rules from string examples—remains a foundational challenge in computational learning theory. While neural networks excel at pattern recognition, their "black-box" nature often obscures the underlying logic they acquire. Researchers from ETH Zürich have introduced a novel neural architecture designed to bridge this gap. By mimicking the structure of a formal parser and employing a specialized sharpening loss, the model induces regular grammars that are not only accurate but also fully explainable. The internal weights of the trained network can be directly translated into human-readable production rules, achieving exact recovery of target grammars in 85% of experimental cases.

Introduction

In the realm of formal languages, regular grammars represent the simplest tier of the Chomsky hierarchy, yet inducing these rules from data is a complex task. Traditional symbolic methods often struggle with scalability, while standard neural approaches, such as Recurrent Neural Networks (RNNs), lack transparency. When a typical RNN classifies a string as valid, it provides no insight into the underlying grammar it has internalized.

This paper addresses the "interpretability gap" by proposing a modular neural architecture that functions as a differentiable parser. The goal is to create a system with the learning efficiency of a neural network and the formal transparency of a logic-based system.

Architectural Framework

The model's design is inspired by the CYK algorithm, structured to process strings through three specialized units that represent the components of a formal grammar:

  1. Terminal Grammar Unit (TGU): This unit processes the initial character of a string. It maps the input to a "belief vector" representing potential non-terminal symbols that could initiate the sequence (e.g., determining that an initial 'a' likely corresponds to a specific non-terminal $A$).
  2. Non-terminal Grammar Unit (NGU): Functioning as the recurrent core of the model, the NGU processes subsequent characters. It utilizes learned matrices to represent production rules of the form $A \to Bc$. As each character is consumed, the unit updates the internal belief state, effectively tracking the transition between non-terminals.
  3. Start Selector Unit (SSU): Upon reaching the end of the string, the SSU evaluates the final belief state. It determines if the state corresponds to the designated "start" symbol of the grammar, resulting in a binary accept or reject verdict.

Explainability through Sharpening

The defining feature of this architecture is its "Explainability by Design." To ensure the model does not settle into a fuzzy, probabilistic state, the authors implement a sharpening loss. This optimization technique forces the network's weights to migrate toward binary values (0 or 1) during the training process.

Once training is complete, these weights act as discrete switches. Because the architecture is constrained to match the mathematical structure of a regular grammar, researchers can "extract" the learned weights and transcribe them directly into formal production rules. The model does not merely simulate a grammar; it effectively becomes a symbolic representation of it.

Experimental Evaluation

The researchers evaluated the model using synthetically generated regular grammars, varying parameters such as the number of terminals ($t$), non-terminals ($n$), and production density ($p$).

To ensure the model learned the actual structural rules rather than simple heuristics (like string length), the authors employed a sophisticated "hard" negative example strategy. This involved generating strings with invalid prefixes, infixes, and postfixes. By training on these nuanced counter-examples, the model was forced to internalize the specific recurrence and loops inherent in the target grammar.

Results and Insights

The experimental results demonstrate the robustness of the neural induction approach:

  • Exact Recovery: In 85% of all experimental runs, the model achieved exact recovery, producing a grammar functionally identical (isomorphic) to the ground truth.
  • The Complexity Paradox: An unexpected finding was that more complex grammars—those with a higher density of productions—were often easier for the model to learn. The authors suggest that higher complexity provides more "negative constraints," which helps the sharpening loss narrow down the correct rule set more rapidly.
  • Scaling with Length: The model showed a steady performance increase (2-5% accuracy gain) as the length of training examples increased, indicating a strong ability to capture the long-term dependencies and recursive structures of regular languages.

Conclusion and Future Directions

While currently limited to Type-3 (regular) grammars, this research provides a blueprint for interpretable neural-symbolic integration. The model's requirement for balanced positive and negative examples remains a limitation in natural language contexts where negative data is scarce. However, the ETH Zürich team views this as a critical stepping stone. Future work aims to extend this architecture to induce Type-0 grammars, which are computationally equivalent to Turing machines. Such an advancement could lead to fully human-readable neural program synthesis, where a network learns to write code that a human can verify and understand.

Summary updated

Abstract

Grammatical inference is a classical problem in computational learning theory and a topic of wider influence in natural language processing. We treat grammars as a model of computation and propose a novel neural approach to induction of regular grammars from positive and negative examples. Our model is fully explainable, its intermediate results are directly interpretable as partial parses, and it can be used to learn arbitrary regular grammars when provided with sufficient data. We find that our method consistently attains high recall and precision scores across a range of tests of varying complexity.
BibTeX
@inproceedings{belcak2022neural,
  author = {Peter Belcak and David Hofer and Roger Wattenhofer},
  title = {{A Neural Model for Regular Grammar Induction}},
  booktitle = {ICMLA},
  year = {2022},
  url = {https://openreview.net/forum?id=lMhVnP5FV0F},
}
Figure