RNNs can generate bounded hierarchical languages with optimal memory
John Hewitt, Michael Hahn, Surya Ganguli, Percy Liang, Christopher D. Manning
Introduction
Recurrent neural networks (RNNs; Elman (1990)) trained on large datasets have demonstrated a grasp of natural language syntax Karpathy et al. (2015); Kuncoro et al. (2018). While considerable empirical work has studied RNN language models’ ability to capture syntactic properties of language Linzen et al. (2016); Marvin and Linzen (2018); Hewitt and Manning (2019); van Schijndel and Linzen (2018), their success is not well-understood theoretically. In this work, we provide theoretical insight into RNNs’ syntactic success, proving that they can efficiently generate a family of bounded hierarchical languages. These languages form the scaffolding of natural language syntax.
Hierarchical structure characterized by long distance, nested dependencies, lies at the foundation of natural language syntax. This motivates, for example, context-free languages Chomsky (1956), a fundamental paradigm for describing natural language syntax. A canonical family of context-free languages (CFLs) is Dyck-, the language of balanced brackets of types, since any CFL can be constructed via some Dyck- Chomsky and Schützenberger (1959).
However, while context-free languages like Dyck- describe arbitrarily deep nesting of hierarchical structure, in practice, natural languages exhibit bounded nesting. This is clear in, e.g., bounded center-embedding Karlsson (2007); Jin et al. (2018) (Figure 1). To reflect this, we introduce and study Dyck-(,), which adds a bound on the number of unclosed open brackets at any time. Informally, the ability to efficiently generate Dyck-(,) suggests the foundation of the ability to efficiently generate languages with the syntactic properties of natural language. See §(1.1) for further motivation for bounded hierarchical structure.
In our main contribution, we prove that RNNs are able to generate Dyck-(,) as memory-efficiently as any model, up to constant factors. Since Dyck-(,) is a regular (finite-state) language, the application of general-purpose RNN constructions trivially proves that RNNs can generate the language Merrill (2019). However, the best construction we are aware of uses hidden units Horne and Hush (1994); Indyk (1995), where is the vocabulary size and is the nesting depth, which is exponential.For hard-threshold neural networks, the lower-bound would be if Dyck-(,) were an arbitrary regular language. We provide an explicit construction proving that a Simple (Elman; Elman (1990)) RNN can generate any Dyck-(,) using only hidden units, an exponential improvement. This is not just a strong result relative to RNNs’ general capacity; we prove that even computationally unbounded models generating Dyck-(,) require hidden units, via a simple communication complexity argument.
Our proofs provide two explicit constructions, one for the Simple RNN and one for the LSTM, which detail how these networks can use their hidden states to simulate stacks in order to efficiently generate Dyck-(,). The differences between the constructions exemplify how LSTMs can use exclusively their gates, ignoring their Simple RNN subcomponent entirely, to reduce the memory by a factor of 2 compared to the Simple RNN.We provide implementations at https://github.com/john-hewitt/dyckkm-constructions/.
We prove these results under a theoretical setting that aims to reflect the realistic settings in which RNNs have excelled in NLP. First, we assume finite precision; the value of each hidden unit is represented by bits. This has drastic implications compared to existing work Siegelmann and Sontag (1992); Weiss et al. (2018); Merrill (2019); Merrill et al. (2020). It implies that only regular languages can be generated by any machine with hidden units, since they can take on only states Korsky and Berwick (2019). This points us to focus on whether languages can be implemented memory-efficiently. Second, we consider networks as language generators, not acceptors;Acceptors consume a whole string and then decide whether the string is in the language; generators must decide which tokens are possible continuations at each timestep. informally, RNNs’ practical successes have been primarily as generators, like in language modeling and machine translation Karpathy et al. (2015); Wu et al. (2016).
Finally, we include a preliminary study in learning Dyck-(,) with LSTM LMs from finite samples, finding for a range of and that learned LSTM LMs extrapolate well given the hidden sizes predicted by our theory.
In summary, we prove that RNNs are memory optimal in generating a family of bounded hierarchical languages that forms the scaffolding of natural language syntax by describing mechanisms that allow them to do so; this provides theoretical insight into their empirical success.
Hierarchical structure is central to human language production and comprehension, showing up in grammatical constraints and semantic composition, among other properties Chomsky (1956). Agreement between subject and verb in English is an intuitive example:
... The Dyck- languages—well-nested brackets of types—are the prototypical languages of hierarchical structure; by the Chomsky-Schützenberger Theorem Chomsky and Schützenberger (1959), they form the scaffolding for any context-free language. They have a simple structure:
..... However, human languages are unlike Dyck- and other context-free languages in that they exhibit bounded memory requirements. Dyck- requires storage of an unboundedly long list of open brackets in memory. In human language, as the center-embedding depth grows, comprehension becomes more difficult, like in our example sentence above Miller and Chomsky (1963). Empirically, center-embedding depth of natural language is rarely greater than Jin et al. (2018); Karlsson (2007). However, it does exhibit long-distance, shallow hierarchical structure:
.. Our Dyck-(,) language puts a bound on depth in Dyck-, capturing the long-distance hierarchical structure of natural language as well as its bounded memory requirements.For further motivation, we note that center-embedding directly implies bounded memory requirements in arc-eager left-corner parsers Resnik (1992).
Related Work
This work contributes primarily to the ongoing theoretical characterization of the expressivity of RNNs. Siegelmann and Sontag (1992) proved that RNNs are Turing-complete if provided with infinite precision and unbounded computation time. Recent work in NLP has taken an interest in the expressivity of RNNs under conditions more similar to RNNs’ practical uses, in particular assuming one “unrolling” of the RNN per input token, and precision bounded to be logarithmic in the sequence length. In this setting, Weiss et al. (2018) proved that LSTMs can implement simplified counter automata; the implications of which were explored by Merrill (2019, 2020). In this same regime, Merrill et al. (2020) showed a strict hierarchy of RNN expressivity, proving among other results that RNNs augmented with an external stack Grefenstette et al. (2015) can recognize hierarchical (Context-Free) languages like Dyck-, but LSTMs and RNNs cannot.
Korsky and Berwick (2019) prove that, given infinite precision, RNNs can recognize context-free languages. Their proof construction uses the floating point precision to simulate a stack, e.g., implementing push by dividing the old floating point value by , and pop by multiplying by . This implies that one can recognize any language requiring a bounded stack, like our Dyck-(,), by providing the model with precision that scales with stack depth. In contrast, our work assumes that the precision cannot scale with the stack depth (or vocabulary size); in practice, neural networks are used with a fixed precision Hubara et al. (2017).
Our work also connects to empirical studies of what RNNs can learn given finite samples. Considerable evidence has shown that LSTMs can learn languages requiring counters (but Simple RNNs do not) Weiss et al. (2018); Sennhauser and Berwick (2018); Yu et al. (2019); Suzgun et al. (2019), and neither Simple RNNs nor LSTMs can learn Dyck-. In our work, this conclusion is foregone because Dyck- requires unbounded memory while RNNs have finite memory; we show that LSTMs extrapolate well on Dyck-(,), the memory-bounded variant of Dyck-. Once augmented with an external (unbounded) memory, RNNs have been shown to learn hierarchical languages Suzgun et al. (2019); Hao et al. (2018); Grefenstette et al. (2015); Joulin and Mikolov (2015). Finally, considerable study has gone into what RNN LMs learn about natural language syntax Lakretz et al. (2019); Khandelwal et al. (2018); Gulordava et al. (2018); Linzen et al. (2016).
Preliminaries and definitions
A formal language is a set of strings over a fixed vocabulary, (with the end denoted by special symbol ). We denote an arbitrary string as , where is the string length.
The Dyck- language is the language of nested brackets of types, and so has words in its vocabulary: \Sigma=\{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i},{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}\}_{i\in[k]}. Any string in which brackets are well-nested, i.e., each {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} is closed by its corresponding {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, is in the language. Formally, we can write it as the strings generated by the following context-free grammar:
where is the empty string.And appended to the end. The memory necessary to generate any string in Dyck- is proportional to the number of unclosed open brackets at any time. We can formalize this simply by counting how many more open brackets than close brackets there are at each timestep:
where is the number of times occurs in . We can now define Dyck-(,) by combining Dyck- with a depth bound, as follows: {restatable}[Dyck-(,) ]defndefn_dyckkm For any positive integers , Dyck-(,) is the set of strings
2 Recurrent neural networks
We now provide definitions of recurrent neural networks as probability distributions over strings, and define what it means for an RNN to generate a formal language. We start with the most basic form of RNN we consider, the Simple (Elman) RNN: {restatable}[Simple RNN (generator)]defndefnsimplernn A Simple RNN (generator) with hidden units is a probability distribution of the following form:
The Long Short-Term Memory (LSTM) model Hochreiter and Schmidhuber (1997) is a popular extension to the Simple RNN, intended to ease learning by resolving the vanishing gradient problem. In this work, we’re not concerned with learning but with expressivity. We study whether the LSTM’s added complexity enables it to generate Dyck-(,) using less memory. {restatable}[LSTM (generator)]defndefnlstmgenerator An LSTM (generator) with hidden units is a probability distribution of the following form:
3 Formal language generation
With this definition of RNNs as generators, we now define what it means for an RNN (a distribution) to generate a language (a set). Intuitively, since a formal language is a set of strings , our definition should be such that a distribution generates if its probability mass on the set of all strings is concentrated on the set . So, we first define the set of strings on which a probability distribution concentrates its mass. The key intuition is to control the local token probabilities , not the global , which must approach zero with sequence length. {restatable}[locally -truncated support]defndefnepsilontruncated Let be a probability distribution over , with conditional probabilities . Then the locally -truncated support of the distribution is the set
This is the set of strings such that the model assigns at least probability mass to each token conditioned on the prefix leading up to that token. A distribution generates a language, then, if there exists an such that the locally truncated support of the distribution is equal to the language:We also note that any generates multiple languages, since one can vary the parameter ; for example, any softmax-defined distribution must generate with small because they assign positive mass to all strings.
A probability distribution over generates a language if there exists such that the locally -truncated support of is .
Formal results
For our results, we first present two theorems for the Simple RNN and LSTM that use hidden units by simulating a stack of -dimensional vectors, which are useful for discussing the constructions. Then we show how to reduce to via an efficient encoding of symbols in space.
Using the same mechanisms as in the proofs above but using an efficient encoding of each stack element in units, we achieve the following.
While we have emphasized expressive power under memory constraints—what functions can be expressed, not what is learned in practice—neural networks are frequently intentionally overparameterized to aid learning Zhang et al. (2017); Shwartz-Ziv and Tishby (2017). Even so, known constructions for Dyck-(,) would require a number of hidden units far beyond practicality. Consider if we were to use a vocabulary size of , and a practical depth bound of . Then if we were using a hidden unit construction to generate Dyck-(,), we would need hidden units. By using our LSTM construction, however, we would need only hidden units, suggesting that networks of the size commonly used in practice are large enough to learn these languages.
Lower bound.
Stack constructions in Simple RNNs
The memory needed to close all the brackets in a Dyck-(,) prefix can be represented as a stack of (yet unclosed) open brackets [{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}], ; reading each new parenthesis either pushes or pops from this stack. Informally, all of our efficient RNN constructions generate Dyck-(,) by writing to and reading from an implicit stack that they encode in their hidden states. In this section, we present some challenges in a naive approach, and then describe a construction to solve these challenges. We provide only the high-level intuition; rigorous proofs are provided in the Appendix.
We start by describing what is achievable with an extended model family, second-order RNNs Rabusseau et al. (2019); Lee et al. (1986), which allow their recurrent matrix to be chosen as a function of the input (unlike any of the RNNs we consider.) Under such a model, we show how to store a stack of up to -dimensional vectors in memory. Such a representation can be thought of as the concatenation of -dimensional vectors in the hidden state, like this:
We call each -dimensional component a “stack slot”. If we want the first stack slot to always represent the top of the stack, then there’s a natural way to implement pop and push operations. In a push, we want to shift all the slots toward the bottom, so there’s room at the top for a new element. We can do this with an off-diagonal matrix :Note that only needing to store things means that when we push, there should be nothing in slot ; otherwise, we’d be pushing element .
This would implement the part of the Simple RNN equation. We can then write the new element (given by ) to the first slot. If we wanted to pop, we could do so with another off-diagonal matrix , shifting everything towards the top to get rid of the top element:
This won’t work for a Simple RNN because it only has one .
2 A Simple RNN Stack in 2mk2𝑚𝑘2mk memory
Our construction gets around the limitation of only having a single matrix in the Simple RNN by doubling the space to . Splitting the space into two -sized partitions, we call one , the place where we write the stack if we see a pop operation, and the other analogously for the push operation. If one of or is empty (equal to ) at any time, we can try reading from both of them, as follows:
Our matrix is actually the concatenation of two of the and matrices. Now we have two candidates, both and ; but we only want the one that corresponds to push if w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, or pop if w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}. We can zero out the unobserved option using the term , adding a large negative value to every hidden unit in the stack that doesn’t correspond to push if is an open bracket {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, or pop if is a close bracket {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}:
So Ux_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}}=[e_{i};0,\dots,-\beta-1,\dots], where is a one-hot representation of {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, and Ux_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}}=[-\beta-1,\dots,-\beta-1,0,\dots], where is the very large value we specified in our finite-precision arithmetic. Thus, when we apply the sigmoid to the new , whichever of doesn’t correspond to the true new state is zeroed out.For whichever of that is not zeroed out, . Hence, we scale all of and to be large, such that .
Stack construction in LSTMs
We could implement our Simple RNN construction in an LSTM, but its gating functions suggest more flexibility in memory management, and Levy et al. (2018) claim that the LSTM’s modeling power stems from its gates. With an LSTM, we achieve the of the oracle we described, all while exclusively using its gates.
To implement a stack using the LSTM’s gates, we use the same intuitive description of the stack as before: stack slots, each of dimensionality . However while the top of the stack is the first slot in the Simple RNN, the bottom of the stack is the first slot in the LSTM. Before we discuss mechanics, we introduce the memory dynamics of the model. Working through the example in Figure 2, when we push the first open bracket, it’s assigned to slot ; then the second and third open brackets are assigned to slots and . Then a close bracket is seen, so slot is erased. In general, the stack is represented in a contiguous sequence of slots, where the first slot represents the bottom of the stack. Thus, the top of the stack could be at any of the stack slots. So to allow for ease of linearly reading out information from the stack, we store the full stack only in the cell state , and let only the slot corresponding to the top of the stack, which we’ll refer to as the top slot, through the output gate to the hidden state .
pop mechanics.
To implement a pop operation, the forget gate finds the top slot, which is the slot farthest from slot that isn’t empty (that is, that encodes some {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}.) In practice, we do this by guaranteeing that the forget gate is equal to for all stack slots before (but excluding) the last non-empty stack slot. Since this last non-empty stack slot encodes the top of the stac,, for it and all subsequent (empty) stack slots, the forget gate is set to .The input gate and the output gate are both set to . This erases the element at the top of the stack, summarized in the following diagram:
output mechanics.
We’ve so far described how the new cell state is determined. So that it’s easy to tell what symbol is at the top of the stack, we only want the top slot of the stack passing through the output gate. We do this by guaranteeing that the output gate is equal to for all stack slots from the first slot to the top slot (exclusive). The output gate is then set to for this top slot (and all subsequent empty slots,) summarized in the following diagram:
Defining the generating distribution
So far, we’ve discussed how to implement implicit stack-like memory in the Simple RNN and the LSTM. However, the formal claims we make center around RNNs’ ability to generate Dyck-(,).
Assume that at any timestep , our stack mechanism has correctly pushed and popped each open and close bracket, as encoded in . We still need to prove that our probability distribution,
assigns greater than probability only to symbols that constitute continuations of some string in Dyck-(,), by specifying the parameters and .
If and only if fewer than elements are on the stack, all {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} must be assigned probability. This encodes the depth bound. In our constructions, elements are on the stack if and only if stack slot is non-zero. So, each row V_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}} is zeros except for slot , where each dimension is a large negative number, while the bias term is positive.
Observing the end of the string ω𝜔\omega.
If and only if elements are on the stack, the string can end. The row detects if any stack slot is non-empty.In particular, the bias term is positive, but the sum is negative if the stack is not empty.
The close bracket {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i} can be observed if and only if the top stack slot encodes {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}. Both the Simple RNN construction and the LSTM construction make it clear which stack slot encodes the top of the stack. In the Simple RNN, it’s always the first slot. In the LSTM, it’s the only non-empty slot in . In our stack constructions, we assumed each stack slot is a -dimensional one-hot vector to encode symbol {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}. So in the Simple RNN, V_{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}{}_{i} reads the top of the stack through a one-hot vector in slot , while in the LSTM it does so through in all slots. This ensures that V_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}}^{\top}h_{t} is positive if and only if {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} is at the top of the stack.
2 Generation in O(mlogk)𝑂𝑚𝑘O(m\log k) memory
Thus, we can always detect which symbol {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} is encoded by setting b_{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}{}_{i}=\log k-0.5.In actuality, we use a slightly less compact encoding, spending more hidden units set to , to incrementally subtract from all logits. Then the bias terms b_{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}{}_{i} are set to , avoiding possible precision issues with representing the float .
Experiments
Our proofs have concerned constructing RNNs that generate Dyck-(,); now we present a short study connecting our theory to learning Dyck-(,) from finite samples. In particular, for and , we use our theory to set the hidden dimensionality of LSTMs to , and train them as LMs on samples from a distributionDefined in Appendix H over Dyck-(,). For space, we provide an overview of the experiments, with details in the Appendix. We evaluate the models’ abilities to extrapolate to unseen lengths by setting a maximum length of for , and for , and testing on sequences longer than those seen at training time.Up to twice as long as the training maximum. For our evaluation metric, let be the probability that the model predicts the correct closing bracket given that tokens separate it from its open bracket. We report , to evaluate the model’s bracket-closing memory.
For all configurations, we find that the LSTMs using our memory limit achieve error less than when trained on 20 million tokens. Strikingly, this is despite the fact that for large and , a small fraction of the possible stack states is seen at training time;See Table 3. this shows that the LSTMs are not simply learning structureless DFA states. Learning curves are provided in Figure 3.
Discussion and conclusion
We proved that finite-precision RNNs can generate Dyck-(,), a canonical family of bounded-depth hierarchical languages, in memory, a result we also prove is tight. Our constructions provide insight into the mechanisms that RNNs and LSTMs can implement.
The Chomsky hierarchy puts all finite memory languages in the single category of regular languages. But humans generating natural language have finite memory, and context-free languages are known to be both too expressive and not expressive enough Chomsky (1959); Joshi et al. (1990). We thus suggest the further study of what structure networks can encode in their memory (here, stack-like) as opposed to (just) their position in the Chomsky hierarchy. While we have settled the representation question for Dyck-(,), many open questions still remain: What broader class of bounded hierarchical languages can RNNs efficiently generate? Our experiments point towards learnability; what class of memory-bounded languages are efficiently learnable? We hope that answers to these questions will not just demystify the empirical success of RNNs but ultimately drive new methodological improvements as well.
Code for running our experiments is available at https://github.com/john-hewitt/dyckkm-learning. An executable version of the experiments in this paper is on CodaLab at https://worksheets.codalab.org/worksheets/0xd668cf62e9e0499089626e45affee864.
Acknowledgements
The authors would like to thank Nelson Liu, Amita Kamath, Robin Jia, Sidd Karamcheti, and Ben Newman. JH was supported by an NSF Graduate Research Fellowship, under grant number DGE-1656518. Other funding was provided by a PECASE award.
References
Appendix A Appendix outline
This Appendix has the following order. In (§B), we provide a definition of Dyck-(,) equivalent to that in the main text but more useful for our proofs. In (§C), we state preliminary definitions and assumptions, and prove the lower-bound of hidden units to generate Dyck-(,). In (§D), we formally introduce our Simple RNN stack construction, and prove its correctness in a lemma. In (§E), we formally introduce our LSTM stack construction, and prove its correctness in a lemma. In (§F), we prove that a linear (+softmax) decoder on the Simple RNN and LSTM hidden states can be used to generate Dyck-(,) in hidden units using a -hot encoding of stack elements. In this section we also prove that a general RNN construction of DFAs allows for generation of Dyck-(,) in hidden units. In (§G), we provide an alternative encoding of elements in our stack constructions for the Simple RNN and LSTM that uses space per element, and prove that our stack constructions still hold using this encoding. We provide a linear (+softmax) decoder on the hidden states of the Simple RNN and LSTM (when using the stack element encoding) that can be used as a drop-in replacement for the decoder from the -hot representations, thus generating Dyck-(,). This proving that both the Simple RNN and LSTM generate Dyck-(,) in hidden units.
Appendix B The Dyck-(k𝑘k,m𝑚m) languages
To better understand the success of neural networks on natural language syntax, we aim for a formal language that models the unbounded recursiveness of natural language while also reflecting its bounded memory requirements. We thus introduce the Dyck-(,) languages, corresponding to sequences of balanced brackets of types with a maximal number of unclosed brackets at any point in the sequences (yielding a bound stack depth of to parse such sentences).
Though in the main text we defined Dyck-(,) by intersecting Dyck- with a language that simply bounds the difference between the number of open brackets and the number of close brackets, here we provide an equivalent definition that will aid in our proofs. Each Dyck-(,) language, specified by fixing a value of and , is defined using a deterministic finite automaton (DFA). Here, we provide a general description of any Dyck-(,) DFA.
Formally, we define each language by the deterministic finite automaton . The vocabulary consists of types of open brackets: \{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}\}_{i=1,\dots,k} corresponding closing brackets \{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}\}_{i=1,\dots,k}. Strings over the vocabulary are , where is a special symbol representing the end of the sequence. are collectively referred to as symbols.
This slightly nonstandard requirement allows for a natural connection with language models, which must estimate the probability that a string ends at any given token.But crucially, since , the language model need not define a distribution after is seen; it can only be the last token. Overloading notation, we’ll also use to refer to the language (the set of strings) itself, defined as the strings accepted by the DFA.
We now define the states . First, we define reject state , and accept state . Each other state is uniquely identified by a list of open bracket symbols of length up to ; thus the full set of states is provided by:
The number of states is thus , where all but two states reflect a list of open brackets. We will denote each list [{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}}{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{2}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}], as a stack state with elements, and the value of {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}} as the top element of the stack. We let q_{0}=[]=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}}{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{2}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}], , the empty stack state.
B.2 Transition function, δ𝛿\delta
We now define the transition function, .
The state can transition either to the accept state or to another stack state:
while any other symbol transitions to the reject state.
For any state of the form [{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}}{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{2}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}], where , an open bracket can be pushed to the list (since ), or the last open bracket can be removed, by observing its corresponding close bracket.
All other symbols transition to the reject state.
For any state of the form [{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}}{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{2}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m}}], that is, the list is of length and thus full, the close bracket of the top of the stack removes that bracket,
All other symbols transition to the reject state.
The reject state transitions to itself for all symbols.
No transitions from the accept state need be defined, since only , and must be the last symbol of any string in the universe, and can only occur once.
This accounts for the transition function from all states in , and completes our definition of the DFA . We are now prepared to formally define the language:
Overloading notation, for any , for any , we’ll say , denoting the state that is in after consuming .
Appendix C Preliminaries
* Through the notion of -truncated support, a language model specifies which tokens are allowable continuations of each string prefix by assigning them greater than probability. With this, we’re ready to connect RNNs and formal languages:
A probability distribution generates a formal language if there exists such that the -truncated support is equal to .
Under our finite-precision setting, a reasonable assumption about the properties of the sigmoid and hyperbolic tangent functions make our claim considerably simpler. The sigmoid function, , has range , excluding its boundaries . However, in a finite-precision arithmetic, cannot become arbitrarily close to or . In fact, in popular deep learning library PyTorchUnder the float datatype, PyTorch v1.3.1 Paszke et al. (2019)., is exactly equal to for all . We define the floating point sigmoid function to equal or if the absolute value of its input is greater or equal in absolute value to some threshold :
Similarly for the hyperbolic tangent function, we define:
For the rest of this paper, we will refer to as , and as tanh.
C.2 Lower bound of Ω(mlogk)Ω𝑚𝑘\Omega(m\log k) for generation of Dyck-(k𝑘k,m𝑚m)
We provide a communication complexity argument. Assume for contradiction that there exists such a machine with . Consider any string w_{1:m}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m}} of open brackets, where each is one of types. There are such strings, but because , we have that the model has possible representations, so at least two such strings must share the same representation. Let such a pair be , where likewise is defined as {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i^{\prime}_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i^{\prime}_{m}}. Consider the sequence of open bracket indices that defines : . Let be the string {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i_{m}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i_{1}} of close brackets in the reverse order of the indices of , and likewise for .
The string , where denotes concatenation, is in Dyck-(,). However, is not in Dyck-(,) as it breaks the well-nested brackets condition wherever {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{j}}\not={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i^{\prime}_{j}}. This must occur at least once, since . Since the model assigns them the same representation even though they must be distinguished to generate Dyck-(,), the model does not generate Dyck-(,).
Appendix D Proving Simple RNN stack correspondence in 2mk2𝑚𝑘2mk units
In this section, we provide a formal description of our Simple RNN stack construction, and introduce and prove the stack correspondence lemma, to guarantee its correctness. \defnsimplernn*
where denotes concatenation of vectors into a -dimensional vector. We note that is injective, that is, the inverse map is well-defined on all vectors in the image of .
Later, when constructing the efficient memory encoding in Section G.1, we will swap the one-hot vectors for other vectors that will also have entries in .Looking ahead to the more complex LSTM construction, we’ll introduce notation to denote these vectors e_{i}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}), and look to replace when we develop the construction.
If , then the definition guarantees that .
Defining Transition Matrices
To define the RNN (apart from , ), we have to specify the matrices , and the vector .
To specify the matrices and , we need to fix an assignment to the integers to the symbols in . We will assign the integers to the opening brackets (i.e., {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{1}\mapsto 1,\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{k}\mapsto k), and the integers to the closing brackets (i.e., {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{1}\mapsto k+1,\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{k}\mapsto 2k).
Here, we write for the identity matrix, and for a matrix filled entirely with ones.
Proof of Correctness
To prove correctness of the construction, the first step is to show that the transition dynamics of the Simple RNN correctly simulates the dynamics of the stack when consuming one symbol.
Let \phi({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})=push, \phi({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i})=pop.
Assume that the hidden state encodes a stack :
Let be the new input symbol, and assume that is a valid prefix of a word in Dyck-(,). Let be the next hidden state, after reading . Then
Before showing this lemma, we note the following property of the activation function , under the finite precision assumption:
We can write . Set . By construction of ,
At this point, we note that the only relevant property of the encodings for this proof is that their entries are in , not that they are one-hot vectors. This will make it possible to plug in a more efficient encoding later in Section G.1.
Case 1:
Assume w_{t+1}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}. We have
which is equal to S(push,\delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})).
Case 2:
Assume w_{t+1}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}. We have
which is equal to S(push,\delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i})). ∎
From the previous lemma, we can derive the following Stack Correspondence lemma, which asserts that the Simple RNN correctly reflects stack dynamics over an entire input string:
For all strings in Dyck-(,), for all , let be the state of after consuming prefix . Let be the RNN hidden state after consuming . Then if ,
The claim is shown by induction over , applying the previous lemma in each step. We will show the claim for all , setting to be the zero vector .
As , this proves the claim for . To prove the inductive step, we assume has already been shown.
Noting , we obtain
As noted in the definition of above (D.3), this entails
Appendix E Proving LSTM stack correspondence in mk𝑚𝑘mk units
In this section, we prove our lemma describing the stack construction implemented by an LSTM. To do so, we first define the LSTM: \defnlstmgenerator*
First, we provide notation for describing the structure of the memory cell () that will maintain. Next, we proceed to describe precisely, but without referring to the LSTM equations, how this memory changes over time. We then describe how these high-level dynamics are implemented by the LSTM equations, assuming that we’re able to set the values of the intermediate values (gates and new cell candidate) as desired. We then explicitly construct LSTM weight matrices that provide the desired intermediate values. This completes the proof of the memory dynamics, which we formalize in a stack correspondence lemma once we’ve introduced the notation.
Applying this function to each stack slot allows us to map from to stack states:
where is the maximal integer such that . Intuitively, this means we filter out any empty stack slots at the end of the sequence (those slots where .) We’ll also make use of the inverse, , mapping DFA stack states to stack slot lists,
where slots are the zero vector because only the first slots encode a symbol {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, and .
These one-hot encodings () of symbols ({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) allow for clear exposition, but are not fundamental to the construction; in (§ G), we replace these -dimensional one-hot encodings given by with -dimensional encodings. Throughout our description of the stack construction, we’ll explicitly rely on the following property, so we can easily replace this particular in (§ G) by providing another with the same property.
that is, the sum of all dimensions of the representation is equal to , and
that is, the representation takes on values only in and , and
that is, the empty stack slot is encoded by the zero vector, and
that is, encodings of symbols {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} are unique, and none are equal to to the encoding of the empty symbol .
The encoding we’ve so far provided, \psi^{-1}(e_{i})={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, obeys these properties: the sum of a single one-hot vector is , no two such vectors are the same, one-hot vectors only take on values in , and we let .
E.2 Description of stack state dynamics
Before we describe the memory dynamics, we introduce a useful property of the stack slots.
A cell state composed of stack slots is -top if there exists such that s_{t,j}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}), and for all , .
Intuitively, we’ll enforce the constraint that a cell state that is -top encodes the element {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{m^{\prime}} at the top of the stack [{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{m^{\prime}}] in stack slot .
We’re now ready to describe the memory dynamics of . The memory is initialized to . So for all ,
The recurrent equation of stack slots is then
Intuitively, the first case of Equation E.10 implements pop, the second case implements push (if pushing to an otherwise empty stack) and the third case implement push (if pushing to a non-empty stack). The fourth case specifies that slots that are not pushed to or popped are maintained from timestep to timestep .
Intuitively, this means only the top of the stack is stored in the hidden state, and it’s stored in whichever units correspond to the -top slot in . Since \psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) takes on values in , \text{tanh}(\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})) takes on values in .
We’re now ready to formalize the first part of our proof in a lemma that encompasses how structures and manipulates its memory.
For all strings in Dyck-(,), for all , let be the state of after consuming prefix . Let be the hidden state of the LSTM after consuming . Then if ,
and letting q_{t}=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}] for without loss of generality,
where if , then and for all .
Note again that the value of is not specified by the lemma; whereas so far we’ve used \psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})=e_{i}, a one-hot encoding, in (§ G) we’ll replace while maintaining the lemma.
Intuitively, Equation E.12 states that keeps an exact representation of the stack of unclosed open brackets so far, a statement solely concerning the cell state . However, all intermediate values in the LSTM equations are functions of the hidden state , not the cell state directly; Equation E.13 states how the top of the stack is represented in the hidden state, . We state these as one lemma since it is convenient to prove them by induction together.
E.3 Proof of the stack correspondence lemma assuming stack slot dynamics
We first prove Lemma 4 under the assumption that the stack slot dynamics given in Equation E.10 hold. We proceed by induction on the prefix length.
When the prefix length is , the DFA is in state , and for all , so , as required. Further, we have , as required. This completes the base case.
Assume for the sake of induction that Lemma 4 holds for strings up to length . Now consider a prefix . From it, we can take the prefix . Running , we have . By the induction hypothesis, we have that \mathcal{Q}(s_{t,1},\dots,s_{t,m})=q_{t}=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}], (Equation E.12) as well as that
by Equation E.13. Now, the symbol can be one of symbols: any of the open brackets {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, the close brackets {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, or . The proof now proceeds by each of these three cases.
Without loss of generality, we let q_{t}=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}] for . Strict inequality is guaranteed since if , then \delta(q_{t},{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})=r, meaning cannot be a prefix of any string in Dyck-(,), contradicting a premise of the lemma. Through the inverse of the stack mapping Equation E.3, we have
the stack slots of at timestep . Since s_{t,m^{\prime}}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{j}}) and , we have that is -top. Thus, since w_{t+1}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, the second or third condition in Equation E.10 (depending on whether ) dictate that s_{t+1,m^{\prime}+1}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) (where the is because w_{t+1}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}). All other stack slots fall under the condition .
With this, we can reason about the DFA state encoded by at timestep :
which is the DFA’s state at timestep , as required.
Finally, we reason about . We’ve just shown that that is -top, and that s_{t+1,m^{\prime}+1}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}). By Equation E.11, we have that h_{t+1,m^{\prime}+1}=\text{tanh}(\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})), and for all as required, completing this case.
We have q_{t}=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}] for . this inequality is strict since if , then , and \delta(q_{t},{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i})=r, meaning is not a prefix of any string in . Thus, we have that is -top. Since w_{t+1}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, we have that , and for all . Thus, we have
and thus the DFA state corresponding to the stack slots is:
Which is \delta(q_{t},{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}), as required. By the same reasoning as for case w_{t+1}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, since is -top and that s_{t+1,m^{\prime}-1}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{m^{\prime}-1}), we have that h_{t+1,m^{\prime}-1}=\text{tanh}(\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})) and for all , as required. This completes the case.
𝑡1𝜔w_{t+1}=\omega: If , then by the definition of , ; hence the premises of Lemma 4 don’t hold, so the lemma vacuously holds.
Summary
In this section we’ve proved the stack correspondence lemma for our LSTM construction, assuming we can implement the dynamics of the model as specified in Equations E.10 E.11; we have yet to show that the LSTM update can implement the equations as promised.
E.4 Implementation of stack state dynamics in LSTM equations assuming intermediate values
In this subsection, we define how the dynamics defined in Equations E.10, E.11 are implemented in the LSTM equations, referring to intermediate gate values, assuming such values can be reached with some setting of LSTM parameters.
How these fulfill Equations E.10, E.11? We can set the gate values to do so as follows. First, the forget gate is used to detect the condition under which slot should be erased:
Second, the new cell candidate expression is used to give the option of writing a new open bracket, to any stack slot:
Third, the input gate determines which stack slot is the first non-empty slot in the sequence, and only allows the information of the new open bracket to be written to that slot:
Finally, the output gate is set to identify the top of the stack and not let any other non- (i.e., all stack slots below the top) through:
Many of these conditions refer to values, like and , not available when computing the gates (as only and are available); it is convenient to refer to these conditions and later show how they are implemented with the available values.
E.5 Proof of stack state dynamics given gate and new cell candidate values
In this section, we prove that Equations E.10, E.11 are implemented by the LSTM given the gate and new cell candidate values defined in the previous subsection.
We start with Equation E.10, the definition of the stack slot dynamics, proceeding by each of the four cases for defining in Equation E.10.
where the last equality hold because of the condition that is -top, which implies .
Proving Equation E.11 is implemented
Next, we prove that Equation E.11 is implemented by the LSTM , given that Equation E.10 is. First, we must show is equal to if is -top. If is -top, it must be that for some . Since is -top, we have , and . Plugging these values into the hidden state expression in Equation E.15, we get:
If that condition does not hold, we must have that not -top. thus, is either -top for some or , or .
If is -top for , then we have that , by the definition of -top. In this case, we have
If is -top for , then we have that , by Equation E.19. In this case, we have
Finally, if , meaning it is not -top for any , then
Summary
So far, we’ve proved the stack correspondence lemma’s induction step for LSTMs assuming that we can provide parameters of the network such that the values assumed in Equations E.16, E.17, E.18, E.19, that is, the gates and new cell candidate, are achieved. We have yet to provide the settings of parameters to do so.
E.6 Construction and proof of LSTM parameters providing gate and new cell candidate values
The equation defining is as follows:
and likewise for the second condition. The third condition relies on the fact that .
that is, each row of is equal to the sum, over all close brackets, of the embedding of that close bracket (transposed.) Since all x_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}} are orthogonal and unit norm (by the definition of ), this gives the following intermediate value:
that is, all units of the bias are equal to the same value.
As we stated earlier, Equation E.19 refers to conditions on , which do not show up in the computation of ; instead, is a function of , and . As such, we re-write Equation E.19 in terms of these values, in particular splitting the condition on being -top for into two separate conditions,
Recall that h_{t,j^{\prime}}=\text{tanh}(\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})) indicates that is -top, by the induction hypothesis. With our equation above we outline two different conditions that we’ll show together are necessary and sufficient to guarantee that the top slot is located at some . Under each condition, we set the output gate to because of that (since if the top slot is at , then slot is not equal to , and must not be let through the gate.)
While we cannot condition directly on the value of , we know that is -top for under two conditions on , and thus on . First, can be -top or greater; since only one element can be popped at once, can be no less than -top. Second, can be -top or -top, and the input is not some {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}; that is, it does not pop the top element off of the stack. Because is not {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, it must be an open bracket, {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, pushing to the stack. Hence, under these conditions, must be at least -top. If neither of these conditions hold, then cannot be -top for , since if is -top or -top and w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, then must be at most -top. And if is -top for , then it is impossible since only one bracket can be pushed at a time. Thus Equation E.27 is equivalent to Equation E.19, as required.
We now prove that parameters implement Equation E.19, by implementing Equation E.27. In the first condition, h_{t-1,j^{\prime}}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) for some and ; we want to show that . Based on our construction of parameters, we have that is upper-bounded by the following, when w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}:
where the last equality holds under our finite precision by setting the scaling factor such that , that is .
In the next condition, we have that for , and , and x_{t}\not={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}. In this case, we can write out the value of as follows:
Finally, if neither of those conditions hold, we have that h_{t-1,j^{\prime}}\not=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) for any , and so . In this case, we can lower-bound the value of :
as required. This completes the proof of the output gate.
The equation defining is as follows:
We define as follows:
again relying on the embedding-validity property.
that is, each row of is equal to the negative of the sum, over all close brackets, of the embedding of that close bracket (transposed.) Since all x_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}} are orthogonal and unit norm, this gives the following intermediate value:
that is, all units of the bias are equal to the same value.
We now prove that, as given, the parameters implement Equation E.16 assuming the induction hypothesis of Lemma 4.
Equation E.16 has two cases. In the first case, is -top and w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, and we need to prove . From the induction hypothesis, this means that h_{t-1,j}=\text{tanh}(\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})), and all other slots . The value of the gate is as follows:
where the last equality holds by setting as already stated: .
The second case is whenever the conditions of the first case don’t hold, in which case we must prove . Thus, we either have not -top, in which case,
or we have w_{t}\not={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, in which case,
The equation defining is as follows:
We define as follows:
once again relying only on the encoding-validity property.
Since all x_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}} are orthogonal and unit norm, this gives the following intermediate value:
We now prove that the parameters , when plugged into , implement Equation E.18 assuming the induction hypothesis of Lemma 4. Equation E.18 has three cases.
The condition of the first case is that , , and w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}; we need to show that . Since , we have that . We can compute as follows:
note the bias term is only set that way for .
The condition of the second case is that is -top, and w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}; we need to show that . From the induction hypothesis, we have that h_{t-1,j-1}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i^{\prime}}) for some , and is elsewhere. We calculate as follows,
where we do not have to consider the case since cannot be -top, so this case cannot hold.
Finally, in the third case, none of the above conditions hold, and we need to prove . There are a number of possibilities here to enumerate. First, let and , but w_{t}\not={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}. Then,
Next, we let and w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, but . Then,
Next, we let , w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, but . Thus is -top for some .
If the second case of Equation E.18 doesn’t hold, then either is not -top, or w_{t}\not={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}; we need to prove . If is not -top and , then we can upper-bound the value of as follows:
If is -top but w_{t}\not={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, then the value of is as follows,
Summary
We’ve now specified all parameters of the LSTM and completed the induction step in our proof of the stack correspondence lemma, Lemma 4; this completes the proof of the lemma.
Appendix F Proving generation in O(mk)𝑂𝑚𝑘O(mk) hidden units
For both the Simple RNN and the LSTM, we’ve proven stack correspondence lemmas, Lemmas 3, 4. These guarantee that for all prefixes of strings in Dyck-(,), we can rely on properties of the hidden states of each model to be perfectly informative about the state of the DFA stack.
The two constructions differ in where they make information accessible; we describe those differences here and then provide a general proof that is agnostic to which construction is used.
Recall that we must show that the -truncated support of (for both Simple RNN and LSTM) is the same set as Dyck-(,). The token-level probability distribution conditioned on the history is given as follows:
and we denote this distribution . We first specify by row; a single row exists for each of the words in the vocabulary. We let refer to the row of word , and the columns that will participate in the dot product with the rows of stack slot in .
We start with the only difference in the softmax matrix between the Simple RNN and the LSTM. For the Simple RNN:
because the top of the stack is guaranteed to be in the first stack slot, and where is a positive scaling constant we’ll define later. And for the LSTM,
for all , since exactly one slot is guaranteed to be non-empty (if the stack is non-empty) and it could be at any of the slots. The rest of the softmax construction is common between the Simple RNN and the LSTM:
So that we can swap out when we swap out for a more efficient encoding, we state here a properties of we rely on (once is fixed):
where specifies the stack slot where the construction (Simple RNN or LSTM) stores the element at the top of the stack; for the Simple RNN, , for the LSTM, is such that is -top, as defined. This ensures that the softmax matrix correctly distinguishes between the symbol, {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, that is encoded in the top of the stack, from any other symbol. Further, we rely on
The softmax matrix we’ve provided obeys the softmax validity property with respect to since , and , . Further, for all .
We’re now prepared to state the final lemma in our proof of Theorems 4, 4.
Let . For , let . Then for all ,
Intuitively, Lemma 5 states that assigns greater than probability mass in context to tokens such that the prefix is the prefix of some member of .
We proceed in cases by the state, . We’ll show that a lower-bound on the probabilities of allowed symbols is greater than an upper-bound on the probabilities of disallowed symbols. We should note, however, that this is effectively a technicality to ensure our construction is fully constructive – that is, we provide concrete values for each parameter in the model; else, we could simply indicate that as grows, the probability mass on such that converges to , while the mass on all other symbols converges 1 over the number of such symbols.
Case q=[]𝑞q=[]
First, consider the case that . For all {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i})=r, and for all other symbols , . By the stack correspondence lemmas, Lemma 4 3, we have that (Simple RNN) or (LSTM), and .
For all {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}, we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i})=r, and we have logits:
as required. For all w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})\not=r, and we have logits:
Finally, for , , and we have logits:
as required. With the logits specified for our whole vocabulary, we can compute the partition function of the softmax function by summing over all open brackets, all close brackets, and the END bracket, to determine probabilities for each token,
while the probability for any {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} and is p_{f_{\theta}}(w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}|h_{t-1})=e^{0.5\zeta\gamma}/Z_{[]}. The partition function is computed as multiplied by the number of words such that (in this case, open brackets and the end bracket), plus a quantity upper bounded by multiplied by the number of words such that (in this case, close brackets.) All probabilities under this model are of this form.
Next, consider the case that q=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},...,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}] for . For the Simple RNN, we have by Lemma 3 that h_{t-1,1}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{m}^{\prime}) (the top of the stack.) Likewise for the LSTM, we have by Lemma 3, that , and . Either way, we have v_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}}^{\top}h_{t-1}\geq\zeta\gamma by the softmax validity property.In particular, for the Simple RNN, this expression is equal to due to the softmax validity property; because of the tanh in in the LSTM expression, it’s equal to where .
As with the first case, we construct the logits for each of the symbols. First, we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{m^{\prime}})\not=r; this is the top element of the stack, which can be popped with a close bracket. We have logits:
as required. For w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i} where , we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})=r (since {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} is not the top of the stack, seeing close bracket {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i} would break the well-balancing requirement), and the logits:
again by the softmax validity property. For w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})\not=r, and the logits:
as required, since , and by the softmax validity property, v_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}}^{\top}h_{t,m^{\prime}<m}=\mathbf{0}. Finally, for , we have , and the logits
by the softmax validity property, as required. To reason about the probabilities, we again need to construct the partition function of the softmax
where stands for -“partial”, for a partially full stack. ( open brackets are allowed, plus 1 close bracket; close brackets are disallowed, as well as the word.)
Next, consider the case that q=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},...,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m}}], that is, a full stack with elements. For the Simple RNN, by Lemma 3, we have that h_{t-1,1}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}), and h_{t-1,m}=\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{j}) for some . (The top of the stack is {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, and the stack is full.) And for the LSTM, by Lemma 4, we have that h_{t-1,m}=\text{tanh}(\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m}})), and . The logits for each symbol are as follows. Identical to the last case, we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i_{m}})\not=r, and p_{f_{\theta}}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i_{m}}|h_{t-1})\propto e^{0.5\zeta\gamma} as required; this is the element at the top of the stack. Also identically, all other brackets have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i\not=i_{m}})=r, and the values for those symbols are , as required. For w_{t}={\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, we have \delta(q,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})=r; this is because with elements, the -bound means no more elements can be pushed. The logits are:
by the softmax validity property, as required. Finally, for , identically to the previous case, we have , and logits , as required. The partition function is as follows:
where stands for -“full”, for a full stack.
Case q=[ω]𝑞delimited-[]𝜔q=[\omega]
Last, consider the case that . In this case, if at timestep , then symbol , since the only transition to state is . Because of this and the definition of the universe of strings as , need not be defined on strings progressing from . Intuitively, this is because indicates that the string has terminated. Hence this case vacuously holds.
We thus have three partition functions, , , and , whose values we only have bounds for. However, we can see that as we scale large, they converge to , , because the contributions from the disallowed symbols converges to zero. Thus, the probability assigned to the lowest-probability allowed symbol converges to as grows large, while the probability assigned to the highest-probability disallowed symbol converges to . So, we choose ,and let , so that . Under this, the smallest probability assigned to any allowed symbol is lower-bounded by , and the largest probability assigned to any disallowed symbol is upper-bounded by . This completes the proof of Lemma 5.
F.2 Completing proofs of generation (Theorems 4, 4)
Now that we’ve proved the probability correctness lemma, we’re ready to complete our proof that our Simple RNN and LSTM constructions generate Dyck-(,).
Recall that we’ve overloaded notation, calling the set of strings defining the language Dyck-(,). We must show that the -truncated support of , which we’ll call is equal to . We show both inclusions.
Let . We’ll show . For all prefixes , , let , the state of the DFA after consuming all tokens of the prefix except the last. For the Simple RNN, by Lemma 3, we have that . For the LSTM, by Lemma 4, we have that . Given this, for either construction, by Lemma 5, we have that . Since this is true for all , we have that is in the -truncated support of , and so .
Let . We’ll show by proving the contrapositive.
Let . We have that by definition. From the transition function , we know that , that is, transitions from any state either to the accept state or the reject state. Because , it must be that , that is, . We have no guarantee about , however, since we don’t know whether is a prefix of some string in Dyck-(,). However, we do know that there must be some such that the first time is for , that is, the first timestep in which a disallowed symbol is seen and transitions to the reject state (after which it self-loops in by definition.)
Consider then the prefix . We know that . So without loss of generality, let q_{t^{\prime}-1}=[{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{1}},\dots,{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i_{m^{\prime}}}]. We then construct a string:
which simply closes all the brackets on the stack represented by . By recursive application of , we have that , and thus , meaning .
Coming back to our prefix , we now know that it is a prefix of a string . Thus, for our Simple RNN, by Lemma 3, we have that . Likewise for our LSTM, by Lemma 4, we have that . And by Lemma 5, since , we have . And so, . This completes the proof of Theorems 4, 4. ∎
𝑚1O(k^{m+1}) As a corrolary of the above, we now formalize our proof that a general DFA construction in RNNs, using hidden units to simulate the DFA of Dyck-(,), permits an RNN construction that generates Dyck-(,). Formally, \thmnaivegeneration*
A general construction of any DFA in an RNN has states, Merrill (2019); Giles et al. (1990). For Dyck-(,), . Each state of the DFA is represented times, once for each word in the vocabulary. If , then the hidden state is , a 1-hot vector, equal to one at an index specified by and unique to . By defining the mapping , this construction obeys a stack correspondence lemma. Finally, since the state of the RNN specifies the DFA state as a 1-hot encoding, we can define the softmax matrix as follows. For each state , we can simply set all rows of corresponding to state to explicitly encode the log-probabilities of probability distributions we just proved in (§ F). ∎
Appendix G Extending to generation in O(mlogk)𝑂𝑚𝑘O(m\log k) hidden units
In this section, we prove an upper bound on the number of hidden units necessary to capture Dyck-(,) with an RNN, matching the lower-bound. This is accomplished by defining a new mapping from slots to open brackets {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} such that we can encode open brackets using just hidden units while maintaining the stack correspondence lemmas and Dyck-(,) generation properties. Formally, for the Simple RNN: \thmsimplernnmlogk* Next, for the LSTM: \thmlstmmlogk*
Intuitively, a simple way to encode elements in space without making use of floating-point precision is to assign each element one of the binary configurations of . Our construction will build off of this.
Where the semicolon denotes concatenation. This can be efficiently implemented in our simple RNN construction by modifying the matrix to encode each . Each row of the softmax matrix is as follows:
where is for all , and all slots not specified are equal to .
Now it suffices to prove the stack correspondence lemma and probability correctness lemmas using and .
As noted in , the only relevant property of the encodings used for symbols {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} in Lemma 3 is that it take on values in . This is also true of , so the lemma still holds.
Proof of probability correctness lemma, Lemma 5.
It suffices to show that obeys the softmax validity property (Definition 7) with respect to to prove Lemma 5 using new encoding . First, we have that
Where v_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{j},1} is specified since the top of the stack is always in slot , ensuring the first requirement. Intuitively, this dot product simply counts up the number of bits that agree between the row and the symbol encoded; if they’re the same, all bits agree; if not, at least 1 must disagree.
Counting hidden units
Since encodes each symbol in space, and the Simple RNN construction constructs a stack in space, we have that Simple RNNs can generate Dyck-(,) in space. This proves Theorem 4.
G.2 O(logk)𝑂𝑘O(\log k) vocabulary encoding in the LSTM
We’ll use a slight variation of the construction we used for the Simple RNN, made possible since the LSTM can encode the value due to the hyperbolic tangent. First, for the encoding, we swap the last values for :
And negate the corresponding values of the softmax matrix, as follows:
where is for all , and all slots not specified are equal to .
Now it suffices to prove the stack correspondence lemma and probability correctness lemmas using and .
It suffices to show that obeys the encoding-validity property (Definition 5). First, for all , we have,
as required. This was possible because we could use the value in the encoding . Second, we see that \psi_{*}^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i})\in\{0,1\}^{3\lceil\log k\rceil-1}, as required. Third, we have still that . Fourth and finally, we have by construction that all encodings \psi_{*}^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) are distinct and none are equal to zero, by construction, since each is assigned a different bit configuration (and each bit configuration is concatenated to its negation.)
So, the encoding-validity property holds on , and the stack correspondence lemma, Lemma 4 holds.
Proof of probability correctness lemma, Lemma 5.
The proof of the probability correctness lemma holds as an immediate corollary of the proof of the same lemma for the Simple RNN using . In particular, the only difference between the and used for the Simple RNN and that used for the LSTM is the swapping of a factor of from a span of to that of , so we still have
where picks out the top of the stack from the stack correspondence lemma. We identically have
again from the proof for the Simple RNN, and likewise for v_{\omega,j}^{\top}\psi^{-1}({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}) for all . Finally, we have v_{{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i},m^{\prime}}=\mathbf{0}, for , again from the proof of the simple RNN. This proves that obeys the softmax validity properties, so Lemma 5 still holds for our LSTM construction.
Counting hidden units
Since encodes each symbol in space, and the LSTM construction constructs a stack in space, we have that LSTM can generate Dyck-(,) in space. This proves Theorem 4.
G.3 Accounting of finite precision.
Appendix H Experiment Details
In this section, we provide detail on our preliminary study on LSTM LMs learning Dyck-(,) from samples.
We run experiments on Dyck-(,) for and .
As Dyck-(,) is an (infinite) set, in order to train language models on it, we must define a probability distribution over it. Intuitively, we sample tokens conditioned on the current DFA (stack) state. Depending on the stack state, one or two of the actions push {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}, pop, end are possible. For example, in the empty stack state, push {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} and end are possible. We sample uniformly at random from the possible actions. Conditioned on choosing the push {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} action, we sample uniformly at random from the open brackets {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i}. Conditioned on choosing the pop action, we generate with probability 1 the {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i} that corresponds to the {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\langle}_{i} on the top of the DFA stack (to obey the well-balancing condition. Upon choosing the end action, we generate and terminate.
We choose this distribution to provide the following statistical properties. Consider the markov chain with nodes to representing , the number of symbols on the DFA state stack at timestep . (This is a Markov chain because is sufficient to describe the probability distribution over all suffixes after timestep .) From the probability distribution we’ve defined, for any timestep where the stack is neither empty nor full, that is, there is probability of advancing in the markov chain towards , that is , and probability of retreating towards , that is, . This hitting time of state from state is the expected number of timesteps it takes for the generation sequence to start at the empty stack state and end up at a full stack state . Because of the probability of advancing or retreating along the markov chain, the hitting time is .
Length conditions.
Suzgun et al. (2018) showed that the choice of formal language training lengths has a significant effect on the generalization properties of trained LSTMs. We choose training lengths carefully, keeping in mind the hitting time from empty to full stack states, to empirically ensure that in expectation, the longest training sequences traverse from the empty state to a full stack state and back to the empty stack state at least three times. This ensures that models are not able to use simple heuristics at training time, like remembering the first open bracket in the sequence to close the last close bracket. The exact length statistics are provided in Table 2. Length statistics for training and testing sets are shown in Figure 5..
DFA state analysis.
Since Dyck-(,) is a regular language, it is reasonable to believe it may learn equivalences between strings that result in the same DFA state, but fail to generalize to DFA states not seen during training time. In Table 3, we see that for equal to an , all DFA states are seen at training time. Equivalently, every possible stack configuration of or brackets, of stack sizes up to or , are seen during training time. For , however, only of all possible DFA states are seen at training time, and only of DFA states seen at testing time are also seen at training time. For , the numbers are even more stark, where of all possible states, and of states seen at testing time are also seen at training time. Thus, the ability of models to generalize to the test set for equal to and shows that the learned LSTMs are not simply memorizing DFA states from training time.There are over 34 billion possible DFA states for . Instead, we speculate that they’re performing stack-like operations, and leave further investigation to future work.
Sample counts
To test sample efficiency of learning, we study four dataset sizes: tokens for training for each combination. In all training settings, we use identical development and test sets of size and tokens, respectively. The development set is sampled from the training distribution.
H.2 Models
We use LSTMs, defined as in the main text with a linear readout layer, and implemented in PyTorch Paszke et al. (2019). We set the hidden dimensionality of the LSTM to , and the input dimensionality to .
H.3 Training
We use the default LSTM initialization provided by PyTorch. We train using Adam Kingma and Ba (2014), using a starting learning rate of for all training sets less than tokens. Based on hand hyperparameter optimization, we found that for , at tokens it was better to use a starting learning rate of . For training sets of size tokens, we use a starting learning rate of for all settings of . We use a batch size of for all experiments. We evaluate perplexity on the development set after every epoch, restarting Adam with a learning rate decayed by if the development perplexity does not achieve a new minimum. After three consecutive epochs without a new minimum development perplexity, we stop training. We use no explicit regularization.
H.4 Evaluation
We’re interested in evaluating the behavior of the LSTM LMs in hierarchical memory management, that is, in remembering what type of bracket {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i} can come next. The model cannot possibly do better than random at predicting the next open bracket. Other aspects of the language, like whether the string can end, or if the stack is full, can be solved easily with a counter, which LSTMs are known to implement Weiss et al. (2018); Suzgun et al. (2019). We thus evaluate whether, for each observed close bracket, the LM is confident about which close bracket (that is, for {\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{i}) it is, since this is deterministic; it must be the close bracket corresponding to the open bracket at the top of the stack of . We do this by normalizing the probability assigned by the LM to the correct close bracket by the sum of probabilities assigned to any close bracket:
We evaluate whether the model is confident, when we define as p({\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle}_{j}|{\color[rgb]{0,0.453125,0.8515625}\definecolor[named]{pgfstrokecolor}{rgb}{0,0.453125,0.8515625}\rangle})>0.8.
We train with three independent seeds for each training setting (training set size, , and ), and report the median across seeds of our bracket closing metric in Figure 3.