Representing Formal Languages: A Comparison Between Finite Automata and Recurrent Neural Networks 

1 INTRODUCTION . Recurrent neural networks ( RNNs ) seem “ unreasonably ” effective at modeling patterns in noisy realworld sequences . In particular , they seem effective at recognizing grammatical structure in sequences , as evidenced by their ability to generate structured data , such as source code ( C++ , LaTeX , etc . ) , with few syntactic grammatical errors ( Karpathy et al. , 2015 ) . The ability of RNNs to recognize formal languages – sets of strings that possess rigorously defined grammatical structure – is less well-studied . Furthermore , there remains little systematic understanding of how RNNs recognize rigorous structure . We aim to explain this internal algorithm of RNNs through comparison to fundamental concepts in formal languages , namely , finite automata and regular languages . In this paper , we propose a new way of understanding how trained RNNs represent grammatical structure , by comparing them to finite automata that solve the same language recognition task . We ask : Can the internal knowledge representations of RNNs trained to recognize formal languages be easily mapped to the states of automata-theoretic models that are traditionally used to define these same formal languages ? Specifically , we investigate this question for the class of regular languages , or formal languages accepted by finite automata ( FA ) . In our experiments , RNNs are trained on a dataset of positive and negative examples of strings randomly generated from a given formal language . Next , we ask if there exists a decoding function : an isomorphism that maps the hidden states of the trained RNN to the states of a canonical FA . Since there exist infinitely many FA that accept the same language , we focus on the minimal deterministic finite automaton ( MDFA ) — the deterministic finite automaton ( DFA ) with the smallest possible number of states – that perfectly recognizes the language . Our experiments , spanning 500 regular languages , suggest that such a decoding function exists and can be understood in terms of a notion of abstraction that is fundamental in classical system theory . An abstraction A of a machineM ( either finite-state , like an FA , or infinite-state , like a RNN ) is a machine obtained by clustering some of the states ofM into “ superstates ” . Intuitively , an abstraction A loses some of the discerning power of the original machineM , and as such recognizes a superset of the language thatM recognizes . We observe that the states of a RNN R , trained to recognize a regular language L , commonly exibit this abstraction behavior in practice . These states can be decoded into states of an abstractionA of the MDFA for the language , such that with high probability , A accepts any input string that is accepted byR . Figure 1 shows a t-SNE embedding ( Maaten and Hinton , 2008 ) of RNN states trained to perform language recognition on strings from the regex [ ( ( [ 4-6 ] { 2 } [ 4-6 ] + ) ? ) 3 [ 4-6 ] + ] . Although the MDFA has 6 states , we observe the RNN abstracting two states into one . Remarkably , a linear decoding function suffices to achieve maximal decoding accuracy : allowing nonlinearity in the decoder does not lead to significant gain . Also , we find the abstraction has low “ coarseness '' , in the sense that only a few of the MDFA states need be clustered , and a qualitative analysis reveals that the abstractions often have simple interpretations . 2 RELATED WORK . RNNs have long been known to be excellent at recognizing patterns in text ( Kombrink et al. , 2011 ; Karpathy et al. , 2015 ) . Extensive work has been done on exploring the expressive power of RNNs . For example , finite RNNs have been shown to be capable of simulating a universal Turing machine ( Neto et al. , 1997 ) . Funahashi and Nakamura ( 1993 ) showed that the hidden state of a RNN can approximately represent dynamical systems of the same or less dimensional complexity . In particularly similar work , Rabusseau et al . ( 2018 ) showed that second order RNNs with linear activation functions are expressively equivalent to weighted finite automata . Recent work has also explored the relationship between RNN internals and DFAs through a variety of methods . Although there have been multiple attempts at having RNNs learn a DFA structure based on input languages generated from DFAs and push down automata ( Firoiu et al. , 1998 ; Gers and Schmidhuber , 2001 ; Giles et al. , 1992 ; Miclet and de la Higuera , 1996 ; Omlin and Giles , 1996a ) , most work has focused on extracting a DFA from the hidden states of a learned RNN . Early work in this field ( Giles et al. , 1991 ) demonstrated that grammar rules for regular grammars could indeed be extracted from a learned RNN . Other studies ( Omlin and Giles , 1996b ) tried to directly extract a DFA structure from the internal space of the RNN , often by clustering the hidden state activations from input stimuli , noting the transitions from one state to another given a particular new input stimuli . Clustering was done by a series of methods , such as K-Nearest Neighbor ( Das and Mozer , 1993 ) , K-means ( Krakovna and Doshi-Velez , 2016 ) , and Density Based Spatial Clustering of Applications with Noise ( DBSCAN ) ( Das and Mozer , 1993 ; Lawrence et al. , 2000 ) . Another extraction effort ( Ayache et al. , 2018 ) uses spectral algorithm techniques to extract weighted automata from RNNs . Most recently , Weiss et al . ( 2018 ) have achieved state-of-the-art accuracy in DFA extraction by utilizing the L * query learning algorithm . Our work is different from these efforts in that we directly relate the RNN to a ground-truth minimal DFA , rather than extracting a machine from the RNN ’ s state space . The closest piece of related work is by Tin̂o et al . ( 1998 ) . Like our work , this seeks to relate a RNN state with the state of a DFA . However , the RNN in Tin̂o et al . ( 1998 ) exactly mimics the DFA ; also , the study is carried out in the context of a few specific regular languages that are recognized by automata with 2-3 states . In contrast , our work does not require exact behavioral correspondence between RNNs and DFAs : DFA states are allowed to be abstracted , leading to loss of information . Also , in our approach the mapping from RNN states to FA states can be approximate , and the accuracy of the mapping is evaluated quantitatively . We show that this allows us to establish connections between RNNs and DFAs in the setting of a broad class of regular languages that often demand significantly larger automata ( with up to 14 states ) than those studied by Tin̂o et al . ( 1998 ) . 3 DEFINITIONS . We start by introducing some definitions and notation . A formal language is a set of strings over a finite alphabet Σ of input symbols . A Deterministic Finite Automaton ( DFA ) is a tupleA = ( Q , Σ , δ , q0 , F ) where Q is a finite set of states , Σ is a finite alphabet , δ : Q× Σ→ Q is a deterministic transition function , q0 ∈ Q is a starting state and F ⊂ Q is a set of accepting states . A reads strings over Σ symbol by symbol , starting from the state q0 and making state transitions , defined by δ , at each step . It accepts the string if it reaches a final accepting state in F after reading it to the end . The set of strings accepted by a DFA is a special kind of formal language , known as a regular language . A regular language L can be accepted by multiple DFAs ; such a DFA AL is minimal if there exists no other DFA A′ 6= AL such that A′ exactly recognizes L and has fewer states than AL . It can be shown that this minimal DFA ( MDFA ) , which we denote by A0L , is unique ( Hopcroft and Ullman , 1979 ) . Abstractions . A Nondeterministic Finite Automaton ( NFA ) is similar to a DFA , except that the deterministic transition function δ is now a non-deterministic transition relation δNFA . This means that for a state q in the NFA and a ∈ Σ , we have that δNFA ( q , a ) is now a subset of NFA states . For a given regular language L we denote by AnL a Nondeterministic Finite Automaton ( NFA ) with n states that recognizes a superset of the language L. An abstraction map is a map α : Q → 2Q that combines two states in NFA AnL , resulting in an NFA A n+1 L ; that is , AnL α7−→ An+1L . Since every DFA is also an NFA , we can apply α to the MDFA A0L to obtain a NFA A1L . Intuitively , α creates a new NFA by combining two states of an existing NFA into a new ‘ superstate ’ . An NFA AnL is an abstraction of A0L , if AnL can be obtained from A0L by repeated application of an abstraction map α . Every state of AnL can be viewed as a set of the states of the MDFA A0L , i.e qn ∈ Qn =⇒ qn = { q0i } i∈I with q0i ∈ Q0 for all i . We define the coarseness of an abstraction AnL , as the number of applications of α on the MDFA required to arrive at AnL . Intuitively , repeated applications of α create NFAs that accept supersets of the language L recognized by the MDFA , and can hence be seen as coarse-grained versions of the original MDFA . The coarsest NFA , given by A ( |Q 0|−1 ) L , is a NFA with only one accepting node and it accepts all strings on the alphabet Σ . Given a regular language L , we define RL to be a RNN that is trained to recognize the language L , with a certain threshold accuracy . Each RNNRL will have a corresponding set of hidden states denoted by H . More details about the RNN are provided in §4.1 . Note that a RNN can also be viewed as a transition system with 5-tupleR = ( H , Σ , δR , h0 , FR ) , where H is a set of possible ‘ hidden ’ states ( typically H ⊂ RK ) , δR is the transition function of the trained RNN , h0 is the initial state of the RNN , and FR is the set of accepting RNN states . The key distinction between a DFA and a RNN is that the latter has a continuous state space , endowed with a topology and a metric . Decoding DFA States from RNNs . Inspired by methods in computational neuroscience ( Astrand . et al. , 2014 ) , we can define a decoding function or decoder f : H → Q0 as a function from the hidden states of a RNN RL to the states of the corresponding ( for L ) MDFA A0L = ( Q0 , Σ0 , δ0 , q00 , F 0 ) . We are interested in finding decoding functions that provide an insight into the internal knowledge representation of the RNN , which we quantify via the decoding and transitional accuracy metrics defined below . Decoding Abstraction States . Let AnL be an abstraction of A0L , obtained by applying α to A0L repeatedly n times , and let Qn be the set of states of AnL . We can define an abstraction decoding function f̂ : H → Qn , by f̂ ( h ) : = ( α|n| ◦f ) ( h ) , that is the composition of f with α|n| . The function α|n| is the function obtained by taking n compositions of α with itself . Given a dataset of input strings D ⊂ Σ∗ , we can define the decoding accuracy of a map f̂ for an abstraction AnL from RNN RL by : ρf̂ ( RL , A n L ) = 1 |D| ∑ w∈D |w|−1∑ t=0 1 ( f̂ ( ht+1 ) = α |n| ( qt+1 ) ) |w| where 1 ( C ) is the boolean indicator function that evaluates to 1 if condition C is true and to 0 otherwise , ht+1 = δR ( ht , at ) and qt+1 = δ0 ( qt , at ) . Note in particular , that for decoding abstraction states the condition is only checking if the ( t + 1 ) RNN state is mapped to the ( t + 1 ) NFA state by f̂ , which may be true even if the ( t + 1 ) RNN state is not mapped to the ( t + 1 ) MDFA state by the decoding function f . Therefore a function f̂ can have a high decoding accuracy even if the underlying f does not preserve transitions . Decoding Abstract State Transitions . We now define an accuracy measure that takes into account how well transitions are preserved by the underlying function f . Intuitively , for a given decoding function f̂ and NFA AnL , we want to check whether the RNN transition on a is mapped to the abstraction of the MDFA transition on a . We note that in the definition of the decoding function , we take into account only the states at ( t + 1 ) and not the underlying transitions in the original MDFA A0L , unlike we do here . More precisely , the transitional accuracy of a map f̂ for a given RNN and abstraction , with respect to a data-set D , is defined as : φf̂ ( RL , A n L ) = 1 |D| ∑ w∈D |w|−1∑ t=0 1 ( f̂ ( δR ( ht , at ) ) = α |n| ( δ0 ( f ( ht ) , at ) ) ) |w| Our experiments in the next section demonstrate that decoding functions with high decoding and transitional accuracies exist for abstractions with relatively low coarseness .

Summary: The authors trained RNNs to recognize formal languages defined by random regular expressions, then measured the accuracy of decoders that predict states of the minimal deterministic finite automata (MDFA) from the RNN hidden states. They then perform a greedy search over partitions of the set of MDFA states to find the groups of states which, when merged into a single decoder target, maximize prediction accuracy. For both the MDFA and the merged classes prediction problems, linear decoders perform as well as non-linear decoders.