Inducing Regular Grammars Using Recurrent Neural Networks

Mor Cohen, Avi Caciularu, Idan Rejwan, Jonathan BerantarXiv 2017

Abstract

Grammar induction is the task of learning a grammar from a set of examples. Recently, neural networks have been shown to be powerful learning machines that can identify patterns in streams of data. In this work we investigate their effectiveness in inducing a regular grammar from data, without any assumptions about the grammar. We train a recurrent neural network to distinguish between strings that are in or outside a regular language, and utilize an algorithm for extracting the learned finite-state automaton. We apply this method to several regular languages and find unexpected results regarding the connections between the network's states that may be regarded as evidence for generalization.

Introduction

Recurrent Neural Networks (RNNs) are often treated as "black boxes"—effective at processing sequences but opaque in how they represent underlying rules. This research addresses the challenge of transparency by investigating whether RNNs can learn "regular grammars" (the simplest class in the Chomsky hierarchy) and if those learned rules can be extracted in a symbolic, interpretable form. Unlike previous attempts that required prior assumptions about the number of states, this work proposes a general, unconstrained approach to inducing Deterministic Finite Automata (DFAs) from trained RNNs.

Methodology

The proposed pipeline consists of four primary stages:

  1. Data Generation: Balanced datasets of positive and negative strings were created based on specific Regular Expressions (Regex).
  2. Learning: A single-layer RNN with tanh activation was trained to classify strings as belonging to the target language.
  3. DFA Construction:
  4. State Collection: Hidden state vectors were recorded as validation strings passed through the network.
  5. Quantization: Continuous vectors were grouped into discrete states using K-means clustering. The optimal number of states ($K$) was determined by finding the smallest $K$ where the DFA's predictions matched the RNN's predictions with at least 99% accuracy.
  6. Graph Building: Transitions between clusters were traced based on input symbols.
  7. Minimization: The Myhill-Nerode algorithm was applied to simplify the resulting graph into the smallest possible equivalent DFA.
Visualization of RNN hidden states clustered into accept and reject states.

Experimental Setup

The study utilized synthetic datasets generated from three regular expressions:

  • Simple Binary: (01)* (alternating zeros and ones).
  • Targeted Binary: (0|1)*100 (strings ending in '100').
  • Part of Speech (POS): A natural language-inspired regex: DET? ADJ* NOUN VERB (DET? ADJ* NOUN)?, simulating basic sentence structures.

For each grammar, 15,000 strings were used for training and 10,000 for validation and testing.

Results and Discussion

The extraction method yielded highly accurate symbolic representations of the RNN's internal logic. For simple binary regexes, the method produced perfect DFAs with 100% accuracy relative to the ground truth. On the more complex POS regex, the extracted DFA achieved 99.6% accuracy.

Extracted DFAs for (a) (01)* and (b) (0|1)*100.

A key insight was the emergence of cycles in the RNN's continuous state space even before quantization. In a high-dimensional continuous space, the probability of hitting the exact same vector twice is nearly zero; the fact that the RNN formed these loops suggests it learned the exact structural rules required for infinite sequences rather than merely approximating them. Furthermore, quantization acts as a form of noise reduction, making the model robust to small perturbations by identifying sets of similar vectors as a single logical state.

Conclusion

This work demonstrates that simple RNNs can effectively generalize the rules of regular grammars. While currently limited to regular grammars—as simple RNNs lack the memory structures for context-free grammars—the ability to extract clean DFAs provides a powerful tool for error analysis and model interpretation. Future work may extend these techniques to scientific domains like DNA sequence analysis or phonology, where sequence data contains hidden regularities.

Summary updated

BibTeX
@misc{cohen2017inducing,
  author = {Mor Cohen and Avi Caciularu and Idan Rejwan and Jonathan Berant},
  title = {{Inducing Regular Grammars Using Recurrent Neural Networks}},
  year = {2017},
  eprint = {1710.10453},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1710.10453},
}
Figure