A Formal Hierarchy of RNN Architectures

William Merrill, Gail Weiss, Yoav Goldberg, Roy Schwartz, Noah A. Smith, Eran Yahav

Introduction

While neural networks are central to the performance of today’s strongest NLP systems, theoretical understanding of the formal properties of different kinds of networks is still limited. It is established, for example, that the Elman (1990) RNN is Turing-complete, given infinite precision and computation time Siegelmann and Sontag (1992, 1994); Chen et al. (2018). But tightening these unrealistic assumptions has serious implications for expressive power (Weiss et al., 2018), leaving a significant gap between classical theory and practice, which theorems in this paper attempt to address.

Recently, Peng et al. (2018) introduced rational RNNs, a subclass of RNNs whose internal state can be computed by independent weighted finite automata (WFAs). Intuitively, such models have a computationally simpler recurrent update than conventional models like long short-term memory networks (LSTMs; Hochreiter and Schmidhuber, 1997). Empirically, rational RNNs like the quasi-recurrent neural network (QRNN; Bradbury et al., 2016) and unigram rational RNN (Dodge et al., 2019) perform comparably to the LSTM, with a smaller computational budget. Still, the underlying simplicity of rational models raises the question of whether their expressive power is fundamentally limited compared to other RNNs.

In a separate line of work, Merrill (2019) introduced the saturated RNNOriginally referred to as the asymptotic RNN. as a formal model for analyzing the capacity of RNNs. A saturated RNN is a simplified network where all activation functions have been replaced by step functions. The saturated network may be seen intuitively as a “stable” version of its original RNN, in which the internal activations act discretely. A growing body of work—including this paper—finds that the saturated theory predicts differences in practical learnable capacity for various RNN architectures (Weiss et al., 2018; Merrill, 2019; Suzgun et al., 2019a).

We compare the expressive power of rational and non-rational RNNs, distinguishing between state expressiveness (what kind and amount of information the RNN states can capture) and language expressiveness (what languages can be recognized when the state is passed to a classifier). To do this, we build on the theory of saturated RNNs.

We introduce a unified hierarchy (Figure 1) of the functions expressible by the states of rational and non-rational RNN encoders. The hierarchy is defined by two formal properties: space complexity, which is a measure of network memory,Space complexity measures the number of different configurations an RNN can reach as a function of input length. Formal definition deferred until section 2. and rational recurrence, whether the internal structure of the RNN can be described by WFAs. The hierarchy reveals concrete differences between LSTMs and QRNNs, and further separates both from a class containing convolutional neural networks (CNNs, Lecun and Bengio, 1995; Kim, 2014), Elman RNNs, and gated recurrent units (GRU; Cho et al., 2014).

We provide the first formal proof that LSTMs can encode functions that rational recurrences cannot. On the other hand, we show that the saturated Elman RNN and GRU are rational recurrences with constant space complexity, whereas the QRNN has unbounded space complexity. We also show that an unrestricted WFA has rich expressive power beyond any saturated RNN we consider—including the LSTM. This difference potentially opens the door to more expressive RNNs incorporating the computational efficiency of rational recurrences.

Language expressiveness

When applied to classification tasks like language recognition, RNNs are typically combined with a “decoder”: additional layer(s) that map their hidden states to a prediction. Thus, despite differences in state expressiveness, rational RNNs might be able to achieve comparable empirical performance to non-rational RNNs on NLP tasks. In this work, we consider the setup in which the decoders only view the final hidden state of the RNN.This is common, but not the only possibility. For example, an attention decoder observes the full sequence of states. We demonstrate that a sufficiently strong decoder can overcome some of the differences in state expressiveness between different models. For example, an LSTM can recognize anbna^{n}b^{n} with a single decoding layer, whereas a QRNN provably cannot until the decoder has two layers. However, we also construct a language that an LSTM can recognize without a decoder, but a QRNN cannot recognize with any decoder. Thus, no decoder can fully compensate for the weakness of the QRNN compared to the LSTM.

Experiments

Finally, we conduct experiments on formal languages, justifying that our theorems correctly predict which languages unsaturated recognizers trained by gradient descent can learn. Thus, we view our hierarchy as a useful formal tool for understanding the relative capabilities of different RNN architectures.

Roadmap

We present the formal devices for our analysis of RNNs in section 2. In section 3 we develop our hierarchy of state expressiveness for single-layer RNNs. In section 4, we shift to study RNNs as language recognizers. Finally, in section 5, we provide empirical results evaluating the relevance of our predictions for unsaturated RNNs.

Building Blocks

In this work, we analyze RNNs using formal models from automata theory—in particular, WFAs and counter automata. In this section, we first define the basic notion of an encoder studied in this paper, and then introduce more specialized formal concepts: WFAs, counter machines (CMs), space complexity, and, finally, various RNN architectures.

2 WFAs

The weights are used to encode any string x∈Σ∗x\in\Sigma^{*}:

Let π\pi be a path of the form q0→x1q1→x2⋯→xtqtq_{0}\rightarrow_{x_{1}}q_{1}\rightarrow_{x_{2}}\cdots\rightarrow_{x_{t}}q_{t} through WFA AA. The score of π\pi is given by

By Π(x)\Pi(x), denote the set of paths producing xx.

The encoding computed by a WFA AA on string xx is

where ⋅{\cdot} denotes concatenation. It is sometimes convenient to treat HfH_{f} as though it is directly indexed by Σ∗\Sigma^{*}, e.g. [Hf]αi,ωj=f(αi⋅ωj)[H_{f}]_{\alpha_{i},\omega_{j}}=f(\alpha_{i}{\cdot}\omega_{j}), or refer to a sub-block of a Hankel matrix, row- and column- indexed by prefixes and suffixes P,S⊆Σ∗P,S\subseteq\Sigma^{*}. The following result relates the Hankel matrix to WFAs:

Rational series (Sakarovitch, 2009)

3 Counter Machines

We now turn to introducing a different type of encoder: the real-time counter machine (CM; Merrill, 2020; Fischer, 1966; Fischer et al., 1968). CMs are deterministic finite-state machines augmented with finitely many integer counters. While processing a string, the machine updates these counters, and may use them to inform its behavior.

A kk-counter CM is a tuple ⟨Σ,Q,q0,u,δ⟩\langle\Sigma,Q,q_{0},u,\delta\rangle with

A finite set of states QQ, with initial state q0q_{0}

A CM is Σ\Sigma-restricted iff uu and δ\delta depend only on the current input σ∈Σ\sigma\in\Sigma.

A CM is (Σ×Q)(\Sigma\times Q)-restricted iff uu and δ\delta depend only on the current input σ∈Σ\sigma\in\Sigma and the current state q∈Qq\in Q.

A CM is Σw\Sigma^{w}-restricted iff it is (Σ×Q)(\Sigma\times Q)-restricted, and the states QQ are windows over the last ww input tokens, e.g., Q=Σ≤wQ=\Sigma^{\leq w}.The states q∈Σ<wq\in\Sigma^{<w} represent the beginning of the sequence, before ww input tokens have been seen.

These restrictions prevent the machine from being “counter-aware”: uu and δ\delta cannot condition on the counters’ values. As we will see, restricted CMs have natural parallels in the realm of rational RNNs. In subsection 3.2, we consider the relationship between counter awareness and rational recurrence.

4 Space Complexity

As in Merrill (2019), we also analyze encoders in terms of state space complexity, measured in bits.

where sMθ(x)s_{M_{\theta}}(x) is a minimal representationI.e., the minimal state representation needed to compute MθM_{\theta} correctly. This distinction is important for architectures like attention, for which some implementations may retain unusable information such as input embedding order. of MM’s internal configuration immediately after xx.

We consider three asymptotic space complexity classes: Θ(1)\Theta(1), Θ(log⁡n)\Theta(\log n), and Θ(n)\Theta(n), corresponding to encoders that can reach a constant, polynomial, and exponential (in sequence length) number of configurations respectively. Intuitively, encoders that can dynamically count but cannot use more complex memory like stacks–such as all CMs–are in Θ(log⁡n)\Theta(\log n) space. Encoders that can uniquely encode every input sequence are in Θ(n)\Theta(n) space.

5 Saturated Networks

A saturated neural network is a discrete approximation of neural network considered by Merrill (2019), who calls it an “asymptotic network.” Given a parameterized neural encoder Mθ(x)M_{\theta}(x), we construct the saturated network s-Mθ(x)\textrm{s-}M_{\theta}(x) by taking

where NθN\theta denotes the parameters θ\theta multiplied by a scalar NN. This transforms each “squashing” function (sigmoid, tanh, etc.) to its extreme values (0, ±\pm1). In line with prior work (Weiss et al., 2018; Merrill, 2019; Suzgun et al., 2019b), we consider saturated networks a reasonable approximation for analyzing practical expressive power. For clarity, we denote the saturated approximation of an architecture by prepending it with s, e.g., s-LSTM.

6 RNNs

“Deep” RNNs are RNNs that have been arranged in LL stacked layers R1,...,RLR_{1},...,R_{L}. In this setting, the series of output states h1,h2,...,h∣x∣\mathbf{h}_{1},\mathbf{h}_{2},...,\mathbf{h}_{|x|} generated by each RNN on its input is fed as input to the layer above it, and only the first layer receives the original input sequence x∈Σ∗x\in\Sigma^{*} as input.

The recurrent update function gg can take several forms. The original and most simple form is that of the Elman RNN. Since then, more elaborate forms using gating mechanisms have become popular, among them the LSTM, GRU, and QRNN.

Elman RNNs (Elman, 1990)

Let xt\mathbf{x}_{t} be a vector embedding of xtx_{t}. For brevity, we suppress the bias terms in this (and the following) affine operations.

We refer to the saturated Elman RNN as the s-RNN. The s-RNN has Θ(1)\Theta(1) space (Merrill, 2019).

LSTMs (Hochreiter and Schmidhuber, 1997)

The LSTM can use its memory vector ct\mathbf{c}_{t} as a register of counters Weiss et al. (2018). Merrill (2019) showed that the s-LSTM has Θ(log⁡n)\Theta(\log n) space.

GRUs (Cho et al., 2014)

Weiss et al. (2018) found that, unlike the LSTM, the GRU cannot use its memory to count dynamically. Merrill (2019) showed the s-GRU has Θ(1)\Theta(1) space.

QRNNs

where zt,ft,ot\mathbf{z}_{t},\mathbf{f}_{t},\mathbf{o}_{t} are respectively rows of Z,F,O\mathbf{Z},\mathbf{F},\mathbf{O}. A QRNN QQ can be seen as an LSTM in which all uses of the state vector ht\mathbf{h}_{t} have been replaced with a computation over the last ww input tokens–in this way it is similar to a CNN.

The s-QRNN has Θ(log⁡n)\Theta(\log n) space, as the analysis of Merrill (2019) for the s-LSTM directly applies. Indeed, any s-QRNN is also a (Σw\Sigma^{w})-restricted CM extended with =±1{=}{\pm}1 (“set to ±1{\pm}1”) operations.

State Expressiveness

We now turn to presenting our results. In this section, we develop a hierarchy of single-layer RNNs based on their state expressiveness. A set-theoretic view of the hierarchy is shown in Figure 2.

Let R\mathcal{R} be the set of rational series. The hierarchy relates Θ(log⁡n)\Theta(\log n) space to the following sets:

RR As in Peng et al. (2018), we say that An encoder is rationally recurrent (RR) iff its state expressiveness is a subset of R\mathcal{R}.

RR-hard An encoder is RR-hard iff its state expressiveness contains R\mathcal{R}. A Turing machine is RR-hard, as it can simulate any WFA.

RR-complete Finally, an encoder is RR-complete iff its state expressiveness is equivalent to R\mathcal{R}. A trivial example of an RR-complete encoder is a vector of kk WFAs.

The different RNNs are divided between the intersections of these classes. In subsection 3.1, we prove that the s-LSTM, already established to have Θ(log⁡n)\Theta(\log n) space, is not RR. In subsection 3.2, we demonstrate that encoders with restricted counting ability (e.g., QRNNs) are RR, and in subsection 3.3, we show the same for all encoders with finite state (CNNs, s-RNNs, and s-GRUs). In subsection 3.4, we demonstrate that none of these RNNs are RR-hard. In Appendix F, we extend this analysis from RNNs to self attention.

Let #a−b(x)\#_{a-b}(x) denote the number of aas in string xx minus the number of bbs.

Consider the Hankel sub-block An\mathbf{A}_{n} of HfH_{f} with prefixes Pn={ai}i≤nP_{n}=\{a^{i}\}_{i\leq n} and suffixes Sn={bj}j≤nS_{n}=\{b^{j}\}_{j\leq n}. An\mathbf{A}_{n} is lower-triangular:

For a string aibja^{i}b^{j}, the update in (26) is equivalent to the CM in Figure 3. Thus, by Lemma 1, the s-LSTM (and the general CM) is not RR. ∎

2 Rational Counting

While the counter awareness of a general CM enables it to compute non-rational functions, CMs that cannot view their counters are RR.

A WFA computing [ct]i[\mathbf{c}_{t}]_{i} is shown in Figure 4. ∎

The WFA in Figure 4 also underlies unigram rational RNNs (Peng et al., 2018). Thus, Σ\Sigma-restricted CMs are actually a special case of unigram WFAs. In Appendix A, we show the more general result:

Any (Σ×Q)(\Sigma\times Q)-restricted CM is RR.

In many rational RNNs, the updates at different time steps are independent of each other outside of a window of ww tokens. Theorem 4 tells us this independence is not an essential property of rational encoders. Rather, any CM where the update is conditioned by finite state (as opposed to being conditioned by a local window) is in fact RR.

Furthermore, since (Σw)(\Sigma^{w})-restricted CMs are a special case of (Σ×Q)(\Sigma\times Q)-restricted CMs, Theorem 4 can be directly applied to show that the s-QRNN is RR. See Appendix A for further discussion of this.

3 Finite-Space RR

Theorem 4 motivates us to also think about finite-space encoders: i.e., encoders with no counters” where the output at each prefix is fully determined by a finite amount of memory. The following lemma implies that any finite-space encoder is RR:

Since ff is computable in Θ(1)\Theta(1) space, there exists a DFA AfA_{f} whose accepting states are isomorphic to the range of ff. We convert AfA_{f} to a WFA by labelling each accepting state by the value of ff that it corresponds to. We set the starting weight of the initial state to 11, and for every other state. We assign each transition weight 11. ∎

Since the CNN, s-RNN, and s-GRU have finite state, we obtain the following result:

4 RR Completeness

While “rational recurrence” is often used to indicate the simplicity of an RNN architecture, we find in this section that WFAs are surprisingly computationally powerful. Figure 5 shows a WFA mapping binary string to their numeric value, proving WFAs have Θ(n)\Theta(n) space. We now show that none of our RNNs are able to simulate an arbitrary WFA, even in the unsaturated form.

Both the saturated and unsaturated RNN, GRU, QRNN, and LSTMAs well as CMs. are not RR-hard.

Consider the function fbf_{b} mapping binary strings to their value, e.g. 101↦5101\mapsto 5. The WFA in Figure 5 shows that this function is rational.

The value of fbf_{b} grows exponentially with the sequence length. On the other hand, the value of the RNN and GRU cell is bounded by 11, and QRNN and LSTM cells can only grow linearly in time. Therefore, these encoders cannot compute fbf_{b}. ∎

In contrast, memory networks can have Θ(n)\Theta(n) space. Appendix G explores this for stack RNNs.

5 Towards Transformers

Appendix F presents preliminary results extending saturation analysis to self attention. We show saturated self attention is not RR and consider its space complexity. We hope further work will more completely characterize saturated self attention.

Language Expressiveness

Having explored the set of functions expressible internally by different saturated RNN encoders, we turn to the languages recognizable when using them with a decoder. We consider the following setup:

A decoder function maps the last state ht\mathbf{h}_{t} to an accept/reject decision, respectively: {1,0}\{1,0\}.

We say that a language LL is decided by an encoder-decoder pair e,d\mathbf{e},\mathbf{d} if d(e(x))=1\mathbf{d}(\mathbf{e}(x))=1 for every sequence x∈Lx\in L and otherwise d(e(x))=0\mathbf{d}(\mathbf{e}(x))=0. We explore which languages can be decided by different encoder-decoder pairings.

Some related results can be found in Cortes and Mohri (2000), who study the expressive power of WFAs in relation to CFGs under a slightly different definition of language recognition.

Let d1\mathbf{d}_{1} be the single-layer linear decoder

2 A Decoder Adds Power

The Hankel sub-block of the indicator function for L≤L_{\leq} over P=a∗P=a^{*}, S=b∗S=b^{*} is lower triangular. Therefore, no RR encoder can compute it.

However, adding the D1D_{1} decoder allows us to compute this indicator function with an s-QRNN, which is RR. We set the s-QRNN layer to compute the simple series ct=#a−b(x)c_{t}=\#_{a-b}(x) (by increasing on aa and decreasing on bb). The D1D_{1} layer then checks ct≤0c_{t}\leq 0. So, while the indicator function for L≤L_{\leq} is not itself rational, it can be easily recovered from a rational representation. Thus, L≤∈D1L_{\leq}\in D_{1}(s-QRNN).

We compare the language expressiveness of several rational and non-rational RNNs on the following:

anbna^{n}b^{n} is more interesting than L≤L_{\leq} because the D1D_{1} decoder cannot decide it simply by asking the encoder to track #a−b(x)\#_{a-b}(x), as that would require it to compute the non-linearly separable =0{=}0 function. Thus, it appears at first that deciding anbna^{n}b^{n} with D1D_{1} might require a non-rational RNN encoder. However, we show below that this is not the case.

Let ∘\circ denote stacking two layers. We will go on to discuss the following results:

QRNNs (Appendix C)

Although anbn∈D1(WFA)a^{n}b^{n}\in D_{1}(\textrm{WFA}), it does not follow that every rationally recurrent model can also decide anbna^{n}b^{n} with the help of D1D_{1}. Indeed, in Theorem 9, we prove that anbn∉D1(s-QRNN)a^{n}b^{n}\notin D_{1}(\textrm{s-QRNN}), whereas anbn∈D1(s-LSTM)a^{n}b^{n}\in D_{1}(\textrm{s-LSTM}) (Theorem 13).

It is important to note that, with a more complex decoder, the QRNN could recognize anbna^{n}b^{n}. For example, the s-QRNN can encode c1=#a−b(x)c_{1}=\#_{a-b}(x) and set c2c_{2} to check whether xx contains baba, from which a D2D_{2} decoder can recognize anbna^{n}b^{n} (Theorem 10).

This does not mean the hierarchy dissolves as the decoder is strengthened. We show that anbnΣ∗a^{n}b^{n}\Sigma^{*}—which seems like a trivial extension of anbna^{n}b^{n}—is not recognizable by the s-QRNN with any decoder.

This result may appear counterintuitive, but in fact highlights the s-QRNN’s lack of counter awareness: it can only passively encode the information needed by the decoder to recognize anbna^{n}b^{n}. Failing to recognize that a valid prefix has been matched, it cannot act to preserve that information after additional input tokens are seen. We present a proof in Theorem 11. In contrast, in Theorem 14 we show that the s-LSTM can directly encode an indicator for anbnΣ∗a^{n}b^{n}\Sigma^{*} in its internal state.

Proof sketch:

anbnΣ∗∉D(s-QRNN)a^{n}b^{n}\Sigma^{*}\notin D(\textrm{s-QRNN}). A sequence s1∈anbnΣ∗s_{1}\in a^{n}b^{n}\Sigma^{*} is shuffled to create s2∉anbnΣ∗s_{2}\notin a^{n}b^{n}\Sigma^{*} with an identical multi-set of counter updates.Since QRNN counter updates depend only on the ww-grams present in the sequence. Counter updates would be order agnostic if not for reset operations, and resets mask all history, so extending s1s_{1} and s2s_{2} with a single suffix ss containing all of their ww-grams reaches the same final state. Then for any DD, D(s-QRNN)D(\textrm{s-QRNN}) cannot separate them. We formalize this in Theorem 11.

We refer to this technique as the suffix attack, and note that it can be used to prove for multiple other languages L∈D2(s-QRNN)L\in D_{2}(\textrm{s-QRNN}) that L⋅Σ∗L{\cdot}\Sigma^{*} is not in D(s-QRNN)D(\textrm{s-QRNN}) for any decoder DD.

-layer QRNNs

Adding another layer overcomes the weakness of the 1-layer s-QRNN, at least for deciding anbna^{n}b^{n}. This follows from the fact that anbn∈D2(s-QRNN)a^{n}b^{n}\in D_{2}(\textrm{s-QRNN}): the second QRNN layer can be used as a linear layer.

Similarly, we show in Theorem 10 that a 2-layer s-QRNN can recognize anbnΣ∗∪{ϵ}a^{n}b^{n}\Sigma^{*}\cup\{\epsilon\}. This suggests that adding a second s-QRNN layer compensates for some of the weakness of the 1-layer s-QRNN, which, by the same argument for anbnΣ∗a^{n}b^{n}\Sigma^{*} cannot recognize anbnΣ∗∪{ϵ}a^{n}b^{n}\Sigma^{*}\cup\{\epsilon\} with any decoder.

4 Arbitrary Decoder

Finally, we study the theoretical case where the decoder is an arbitrary recursively enumerable (RE) function. We view this as a loose upper bound of stacking many layers after a rational encoder. What information is inherently lost by using a rational encoder? WFAs can uniquely encode each input, making them Turing-complete under this setup; however, this does not hold for rational s-RNNs.

Assuming an RR-complete encoder, a WFA like Figure 5 can be used to encode each possible input sequence over Σ\Sigma to a unique number. We then use the decoder as an oracle to decide any RE language. Thus, an RR-complete encoder with an RE decoder is Turing-complete.

Bounded space

However, the Θ(log⁡n)\Theta(\log n) space bound of saturated rational RNNs like the s-QRNN means these models cannot fully encode the input. In other words, some information about the prefix x:tx_{:t} must be lost in ct\mathbf{c}_{t}. Thus, rational s-RNNs are not Turing-complete with an RE decoder.

Experiments

In subsection 4.3, we showed that different saturated RNNs vary in their ability to recognize anbna^{n}b^{n} and anbnΣ∗a^{n}b^{n}\Sigma^{*}. We now test empirically whether these predictions carry over to the learnable capacity of unsaturated RNNs.https://github.com/viking-sudo-rm/rr-experiments We compare the QRNN and LSTM when coupled with a linear decoder D1D_{1}. We also train a 22-layer QRNN (“QRNN2”) and a 11-layer QRNN with a D2D_{2} decoder (“QRNN+”).

We train on strings of length 6464, and evaluate generalization on longer strings. We also compare to a baseline that always predicts the majority class. The results are shown in Figure 6. We provide further experimental details in Appendix E.

We use the following language, which has similar formal properties to anbna^{n}b^{n}, but with a more balanced label distribution:

In line with (34), the LSTM decides L5L_{5} perfectly for n≤64n\leq 64, and generalizes fairly well to longer strings. As predicted in (35), the QRNN cannot fully learn L5L_{5} even for n=64n=64. Finally, as predicted in (36) and (37), the 22-layer QRNN and the QRNN with D2D_{2} do learn L5L_{5}. However, we see that they do not generalize as well as the LSTM for longer strings. We hypothesize that these multi-layer models require more epochs to reach the same generalization performance as the LSTM.As shown by the baseline, generalization is challenging because positive labels become less likely as strings get longer.

Experiment 2

We also consider anbnΣ∗a^{n}b^{n}\Sigma^{*}. As predicted in (38) and (40), the LSTM and 22-layer QRNN decide anbnΣ∗a^{n}b^{n}\Sigma^{*} flawlessly for n=64n=64. A 11-layer QRNN performs at the majority baseline for all nn with both a 11 and 22-layer decoder. Both of these failures were predicted in (39). Thus, the only models that learned anbnΣ∗a^{n}b^{n}\Sigma^{*} were exactly those predicted by the saturated theory.

Conclusion

We develop a hierarchy of saturated RNN encoders, considering two angles: space complexity and rational recurrence. Based on the hierarchy, we formally distinguish the state expressiveness of the non-rational s-LSTM and its rational counterpart, the s-QRNN. We show further distinctions in state expressiveness based on encoder space complexity.

Moreover, the hierarchy translates to differences in language recognition capabilities. Strengthening the decoder alleviates some, but not all, of these differences. We present two languages, both recognizable by an LSTM. We show that one can be recognized by an s-QRNN only with the help of a decoder, and that the other cannot be recognized by an s-QRNN with the help of any decoder.

While this means existing rational RNNs are fundamentally limited compared to LSTMs, we find that it is not necessarily being rationally recurrent that limits them: in fact, we prove that a WFA can perfectly encode its input—something no saturated RNN can do. We conclude with an analysis that shows that an RNN architecture’s strength must also take into account its space complexity. These results further our understanding of the inner working of NLP systems. We hope they will guide the development of more expressive rational RNNs.

Acknowledgments

We appreciate Amir Yehudayoff’s help in finding the WFA used in Theorem 8. We also thank our anonymous reviewers, Tobias Jaroslaw, Ana Marasović, and other researchers at the Allen Institute for AI. The project was supported in part by NSF grant IIS-1562364, Israel Science Foundation grant no.1319/16, and the European Research Council under the EU’s Horizon 2020 research and innovation program, grant agreement No. 802774 (iEXTRACT).

References

Appendix A Rational Counting

We extend the result in Theorem 3 as follows.

Any (Σ×Q)(\Sigma\times Q)-restricted CM is rationally recurrent.

We present an algorithm to construct a WFA computing an arbitrary counter in a (Σ×Q)(\Sigma\times Q)-restricted CM. First, we create two independent copies of the transition graph for the restricted CM. We refer to one copy of the CM graph as the add graph, and the other as the multiply graph.

The initial state in the add graph receives a starting weight of 11, and every other state receives a starting weight of . Each state in the add graph receives an accepting weight of , and each state in the multiply graph receives an accepting weight of 11. In the add graph, each transition receives a weight of 11. In the multiply graph, each transition receives a weight of if it represents ×0\times 0, and 11 otherwise. Finally, for each non-multiplicative update σ/+m\sigma/{+}mNote that m=−1m=-1 for the −1{-}1 counter update. from qiq_{i} to qjq_{j} in the original CM, we add a WFA transition σ/m\sigma/m from qiq_{i} in the add graph to qjq_{j} in the multiply graph.

Each counter update creates one path ending in the multiply graph. The path score is set to if that counter update is “erased” by a ×0{\times}0 operation. Thus, the sum of all the path scores in the WFA equals the value of the counter. ∎

This construction can be extended to accommodate =m{=}m counter updates from qiq_{i} to qjq_{j} by adding an additional transition from the initial state to qjq_{j} in the multiplication graph with weight mm. This allows us to apply it directly to s-QRNNs, whose update operations include =1{=}1 and =−1{=}{-}1.

Appendix B WFAs

We prove this by showing a function whose Hankel matrix has finite rank that, when combined with the identity transformation (i.e., w=1,b=0w=1,b=0) followed by thresholding, is an indicator for anbna^{n}b^{n}. Using the shorthand σ(x)=#σ(x)\sigma(x)=\#_{\sigma}(x), the function is:

We now show 33 series r,s,tr,s,t and a set of series they can be combined to create. These series will be used to create the base vectors for the rank 33 matrices.

For i∈{0,1,2}i\in\{0,1,2\}, ri,sir_{i},s_{i} and tit_{i} collapse to a ‘select’ operation, giving the true statement ci(k)=ci(k)⋅1c_{i}^{(k)}=c_{i}^{(k)}\cdot 1. We now consider the case i>2i>2. Substituting the series definitions in the right side of the equation gives

Reordering the first component and partially opening the other two gives

We can now show that ff is computable by a WFA, proving Theorem 8. By Theorem 1, it is sufficient to show that HfH_{f} has finite rank.

For every P,S⊆{a,b}∗P,S\subseteq\{a,b\}^{*}, denote

Using regular expressions to describe P,SP,S, we create the 3 finite rank matrices which sum to HfH_{f}:

We now consider AA. Denote PA=a∗,SA=a∗b∗P_{A}=a^{*},S_{A}=a^{*}b^{*}. AA is non-zero only on indices u∈PA,v∈SAu\in P_{A},v\in S_{A}, and for these, u⋅v∈a∗b∗u{\cdot}v\in a^{*}b^{*} and Au,v=0.5+f(u⋅v)=1−2(a(u)+a(v)−b(v))2A_{u,v}=0.5+f(u{\cdot}v)=1-2(a(u)+a(v)-b(v))^{2}. This gives that for every u∈PA,v∈SAu\in P_{A},v\in S_{A},

Similarly, we find that the nonzero entries of BB satisfy

Finally, Hf=A+B+CH_{f}=A+B+C, and so by the subadditivity of rank in matrices,

Appendix C s-QRNNs

An ifo s-QRNN can be expressed as a Σk\Sigma^{k}-restricted CM with the additional update operations {≔−1,≔1}\{\coloneqq-1,\coloneqq 1\}, where kk is the window size of the QRNN. So it is sufficient to show that such a machine, when coupled with the decoder D1D_{1} (linear translation followed by thresholding), cannot recognize anbna^{n}b^{n}.

We now consider the linear thresholder, defined by weights and bias w,b\mathbf{w},b. In order to recognise anbna^{n}b^{n}, the thresholder must satisfy:

but this gives 9w∣J⋅v∣J<10w∣J⋅v∣J>11w∣J⋅v∣J9w|_{J}{\cdot}\mathbf{v}|_{J}<10w|_{J}{\cdot}\mathbf{v}|_{J}>11w|_{J}{\cdot}\mathbf{v}|_{J}, which is impossible.

However, this does not mean that the s-QRNN is entirely incapable of recognising anbna^{n}b^{n}. Increasing the decoder power allows it to recognise anbna^{n}b^{n} quite simply:

For the two-layer decoder D2D_{2}, anbn∈D2a^{n}b^{n}\in D_{2}(s-QRNN).

Let #ba(x)\#_{ba}(x) denote the number of baba 22-grams in xx. We use s-QRNN with window size 22 to maintain two counters:

[ct]2[\mathbf{c}_{t}]_{2} can be computed provided the QRNN window size is ≥2\geq 2. A two-layer decoder can then check

No s-QRNN and decoder can recognize the language anbnΣ∗=anbn(a∣b)∗a^{n}b^{n}\Sigma^{*}=a^{n}b^{n}(a|b)^{*}, n>0n>0, i.e., anbnΣ∗∉L(a^{n}b^{n}\Sigma^{*}\notin L(s-QRNN)) for any decoder LL.

The proof will rely on the s-QRNN’s inability to “freeze” a computed value, protecting it from manipulation by future input.

As in the proof for Theorem 9, it is sufficient to show that no Σk\Sigma^{k}-restricted CM with the additional operations {≔−1,≔1}\{{\coloneqq}{-}1,{\coloneqq}1\} can recognize anbnΣ∗a^{n}b^{n}\Sigma^{*} for any decoder LL.

We now denote j=k+10j=k+10 and consider the sequences w1=ajbjajbjajbjw_{1}=a^{j}b^{j}a^{j}b^{j}a^{j}b^{j}, w2=ajbj−1ajbj+1ajbjw_{2}=a^{j}b^{j-1}a^{j}b^{j+1}a^{j}b^{j}. w2w_{2} is obtained from w1w_{1} by removing the 2j2j-th token of w1w_{1} and reinserting it at position 4j4j.

As all of w1w_{1} is composed of blocks of ≥k\geq k identical tokens, the windows preceding all of the other tokens in w1w_{1} are unaffected by the removal of the 2j2j-th token. Similarly, being added onto the end of a substring bkb^{k}, its insertion does not affect the windows of the tokens after it, nor is its own window different from before. This means that overall, the set of all operations uiu_{i} performed on the counters is identical in w1w_{1} and in w2w_{2}. The only difference is in their ordering.

w1w_{1} and w2w_{2} begin with a shared prefix aka^{k}, and so necessarily the counters are identical after processing it. We now consider the updates to the counters after these first kk tokens, these are determined by the windows of kk tokens preceding each update.

First, consider all the counters that undergo some assignment (≔\coloneqq) operation during these sequences, and denote by {w}\{w\} the multiset of windows in w∈Σkw\in\Sigma^{k} for which they are reset. w1w_{1} and w2w_{2} only contain kk-windows of types axbk−xa^{x}b^{k-x} or bxak−xb^{x}a^{k-x}, and so these must all re-appear in the shared suffix bjajbjb^{j}a^{j}b^{j} of w1w_{1} and w2w_{2}, at which point they will be synchronised. It follows that these counters all finish with identical value in c(w1)c(w_{1}) and c(w2)c(w_{2}).

All the other counters are only updated using addition of −1,1-1,1 and , and so the order of the updates is inconsequential. It follows that they too are identical in c(w1)c(w_{1}) and c(w2)c(w_{2}), and therefore necessarily that c(w1)=c(w2)c(w_{1})=c(w_{2}).

From this we have w1,w2w_{1},w_{2} satisfying w1∈anbnΣ∗,w2∉anbnΣ∗w_{1}\in a^{n}b^{n}\Sigma^{*},w_{2}\notin a^{n}b^{n}\Sigma^{*} but also c(w1)=c(w2)c(w_{1})=c(w_{2}). Therefore, it is not possible to distinguish between w1w_{1} and w2w_{2} with the help of any decoder, despite the fact that w1∈anbnΣ∗w_{1}\in a^{n}b^{n}\Sigma^{*} and w2∉anbnΣ∗w_{2}\notin a^{n}b^{n}\Sigma^{*}. It follows that the CM and s-QRNN cannot recognize anbnΣ∗a^{n}b^{n}\Sigma^{*} with any decoder. ∎

For the opposite extension Σ∗anbn\Sigma^{*}a^{n}b^{n}, in which the language is augmented by a prefix, we cannot use such a “suffix attack”. In fact, Σ∗anbn\Sigma^{*}a^{n}b^{n} can be recognized by an s-QRNN with window length w≥2w\geq 2 and a linear threshold decoder as follows: a counter counts #a−b(x)\#_{a-b}(x) and is reset to 11 on appearances of baba, and the decoder compares it to .

Note that we define decoders as functions from the final state to the output. Thus, adding an additional QRNN layer does not count as a “decoder” (as it reads multiple states). In fact, we show that having two QRNN layers allows recognizing anbnΣ∗a^{n}b^{n}\Sigma^{*}.

Let ϵ\epsilon be the empty string. Then,

We construct a two-layer s-QRNN from which anbnΣ∗a^{n}b^{n}\Sigma^{*} can be recognized. Let \denotetheleftedgeofthestring.Thefirstlayercomputestwoquantitiesdenote the left edge of the string. The first layer computes two quantitiesd_{t}andande_{t}$ as follows:

Note that ete_{t} can be interpreted as a binary value checking whether the first token was bb. The second layer computes ctc_{t} as a function of dt,et,d_{t},e_{t}, and xtx_{t} (which can be passed through the first layer). We will demonstrate a construction for ctc_{t} by creating linearly separable functions for the gate terms ftf_{t} and ztz_{t} that update ctc_{t}.

Now, the update function utu_{t} to ctc_{t} can be expressed

Finally, the decoder accepts iff ct≤0c_{t}\leq 0. To justify this, we consider two cases: either xx starts with bb or aa. If xx starts with bb, then et=0e_{t}=0, so we increment ctc_{t} by 11 and never decrement it. Since 0<ct0<c_{t} for any tt, we will reject xx. If xx starts with aa, then we accept iff there exists a sequence of bbs following the prefix of aas such that both sequences have the same length. ∎

Appendix D s-LSTMs

In contrast to the s-QRNN, we show that the s-LSTM paired with a simple linear and thresholding decoder can recognize both anbna^{n}b^{n} and anbnΣ∗a^{n}b^{n}\Sigma^{*}.

Assuming a string aibia^{i}b^{i}, we set two units of the LSTM state to compute the following functions using the CM in Figure 3:

We also add a third unit [ct]3[\mathbf{c}_{t}]_{3} that tracks whether the 22-gram baba has been encountered, which is equivalent to verifying that the string has the form aibia^{i}b^{i}. Allowing ht=tanh⁡(ct)\mathbf{h}_{t}=\tanh(\mathbf{c}_{t}), we set the linear threshold layer to check

We use the same construction as Theorem 13, augmenting it with

We decide xx according to the (still linearly separable) equation

Appendix E Experimental Details

Models were trained on strings up to length 6464, and, at each index tt, were asked to classify whether or not the prefix up to tt was a valid string in the language. Models were then tested on independent datasets of lengths 64,128,256,512,1024,64,128,256,512,1024, and 20482048. The training dataset contained 100000100000 strings, and the validation and test datasets contained 1000010000. We discuss task-specific schemes for sampling strings in the next paragraph. All models were trained for a maximum of 100100 epochs, with early stopping after 1010 epochs based on the validation cross entropy loss. We used default hyperparameters provided by the open-source AllenNLP framework (Gardner et al., 2018). The code is available at https://github.com/viking-sudo-rm/rr-experiments.

For the language L5L_{5}, each token was sampled uniformly at random from Σ={a,b}\Sigma=\{a,b\}. For anbnΣ∗a^{n}b^{n}\Sigma^{*}, half the strings were sampled in this way, and for the other half, we sampled nn uniformly between and 3232, fixing the first 2n2n characters of the string to anbna^{n}b^{n} and sampling the suffix uniformly at random.

Experimental cost

The originally reported experiments were run for 20 GPU hours on Quadro RTX 8000.

Appendix F Self Attention

We place saturated self attention (Vaswani et al., 2017) into the state expressiveness hierarchy. We consider a single-head self attention encoder that is computed as follows:

At time tt, compute queries qt\mathbf{q}_{t}, keys kt\mathbf{k}_{t}, and values vt\mathbf{v}_{t} from the input embedding xt\mathbf{x}_{t} using a linear transformation.

Compute attention head ht\mathbf{h}_{t} by attending over the keys and values up to time tt (K:t\mathbf{K}_{:t} and V:t\mathbf{V}_{:t}) with query qt\mathbf{q}_{t}.

Let ∥⋅∥L\left\lVert\cdot\right\rVert_{L} denote a layer normalization operation (Ba et al., 2016).

This simplified architecture has only one attention head, and does not incorporate residual connections. It is also masked (i.e., at time tt, can only see the prefix X:t\mathbf{X}_{:t}), which enables direct comparison with unidirectional RNNs. For simplicity, we do not add positional information to the input embeddings.

Saturated masked self attention is not RR.

Let #σ(x)\#_{\sigma}(x) denote the number of occurences of σ∈Σ\sigma\in\Sigma in string xx. We construct a self attention layer to compute the following function over {a,b}∗\{a,b\}^{*}:

Since the Hankel sub-block over P=a∗,S=b∗P=a^{*},S=b^{*} has infinite rank, f∉Rf\not\in\mathcal{R}.

Fix vt=xt\mathbf{v}_{t}=\mathbf{x}_{t}. As shown by Merrill (2019), saturated attention over a prefix of input vectors X:t\mathbf{X}_{:t} reduces to sum of the subsequence for which key-query similarity is maximized, i.e., denoting I={i∈[t]∣ki⋅qt=m}I=\{i\in[t]\mid\mathbf{k}_{i}\cdot\mathbf{q}_{t}=m\} where m=max⁡{ki⋅qt∣i∈[t]}m=\max\{\mathbf{k}_{i}\cdot\mathbf{q}_{t}|i\in[t]\}:

For all tt, set the key and query kt,qt=1k_{t},q_{t}=1. Thus, all the key-query similarities are 11, and we obtain:

Since saturated self attention can represent f∉Rf\notin\mathcal{R}, it is not RR. ∎

Space Complexity

We show that self attention falls into the same space complexity class as the LSTM and QRNN. Our method here extends Merrill (2019)’s analysis of attention.

Saturated single-layer self attention has Θ(log⁡n)\Theta(\log n) space.

The construction from Theorem 15 can reach a linear (in sequence length) number of different outputs, implying a linear number of different configurations, and so that the space complexity of saturated self attention is Ω(log⁡n)\Omega(\log n). We now show the upper bound O(log⁡n)O(\log n).

A sufficient representation for the internal state (configuration) of a self-attention layer is the unordered group of key-value pairs over the prefixes of the input sequence.

Note that this construction does not apply if the “vocabulary” we are attending over is not finite. Thus, using unbounded positional embeddings, stacking multiple self attention layers, or applying attention over other encodings with unbounded state might reach Θ(n)\Theta(n).

While it eludes our current focus, we hope future work will extend the saturated analysis to self attention more completely. We direct the reader to Hahn (2020) for some additional related work.

Appendix G Memory Networks

All of the standard RNN architectures considered in section 3 have O(log⁡n)O(\log n) space in their saturated form. In this section, we consider a stack RNN encoder similar to the one proposed by Suzgun et al. (2019b) and show how it, like a WFA, can encode binary representations from strings. Thus, the stack RNN has Θ(n)\Theta(n) space. Additionally, we find that it is not RR. This places it in the upper-right box of Figure 1.

Classically, a stack is a dynamic list of objects to which elements v∈Vv\in V can be added and removed in a LIFO manner (using push and pop operations). The stack RNN proposed in Suzgun et al. (2019b) maintains a differentiable variant of such a stack, as follows:

In a differentiable stack, the update operation takes an element sts_{t} to push and a distribution πt\pi_{t} over the update operations push, pop, and no-op, and returns the weighted average of the result of applying each to the current stack. The averaging is done elementwise along the stacks, beginning from the top entry. To facilitate this, differentiable stacks are padded with infinite ‘null entries’. Their elements must also have a weighted average operation defined.

Initialize the stack S\mathbf{S} to an infinite list of null entries, and denote by StS_{t} the stack value at time tt. Using 11-indexing for the stack and denoting [St−1]0≜st\mathbf{[}S_{t-1}]_{0}\triangleq\mathbf{s}_{t}, the geometric kk-stack RNN recurrent update is:Intuitively, [πt]a[\pi_{t}]_{a} corresponds to the operations push, no-op, and pop, for the values a=1,2,3a=1,2,3 respectively.

In this work we will consider the case where the null entries are 0\mathbf{0} and the encoding ct\mathbf{c}_{t} is produced as a geometric-weighted sum of the stack contents,

This encoding gives preference to the latest values in the stack, giving initial stack encoding c0=0\mathbf{c}_{0}=\mathbf{0}.

Space Complexity

The memory introduced by the stack data structure pushes the encoder into Θ(n)\Theta(n) space. We formalize this by showing that, like a WFA, the stack RNN can encode binary strings to their value.

The saturated stack RNN can compute the converging binary encoding function, i.e., 101↦1⋅1+0.5⋅0+0.25⋅1=1.25101\mapsto 1\cdot 1+0.5\cdot 0+0.25\cdot 1=1.25.

Choose k=1k=1. Fix the controller to always push xtx_{t}. Then, the encoding at time tt will be

This is the value of the prefix x:tx_{:t} in binary. ∎

Rational Recurrence

We provide another construction to show that the stack RNN can compute non-rational series. Thus, it is not RR.

Like similar functions we analyzed in section 3, the Hankel matrix Hf2H_{f_{2}} has infinite rank over the sub-block aibja^{i}b^{j}.

The saturated stack RNN can compute f2f_{2}.

Choose k=1k=1. Fix the controller to push 11 for xt=ax_{t}=a, and pop otherwise. ∎

Appendix H Erratum

We present corrections for experimental results originally reported in section 5. Thanks to David Chiang for helping to identify these mistakes. The QRNN used in our theoretical analysis was the ifo-QRNN, whereas QRNN used for the original experimental results was the fo-QRNN (by our definition, a QRNN where i=1−f\mathbf{i}=1-\mathbf{f}). We redo our experiments as originally intended with the ifo-QRNN instead of the weaker fo-QRNN.

Results are presented in Figure 8. Overall, the trend is similar to what was originally reported. For L5L_{5}, all four models achieve 100% accuracy at the training length of 6464. However, the QRNN performance drops earlier than for the other networks. This matches the theoretical result that the s-QRNN cannot recognize L5L_{5}, whereas the other three saturated networks can. For anbnΣ∗a^{n}b^{n}\Sigma^{*}, the LSTM and 22-layer QRNN reach similar accuracy at all lengths. On the other hand, the 11-layer QRNN, with either a 11 or 22-layer decoder, performs worse. This is predicted by the fact that the s-QRNN cannot recognize anbnΣ∗a^{n}b^{n}\Sigma^{*} for any decoder.

While the results are mostly similar to the original results, one difference is that the ifo-QRNN reaches 100% accuracy on L5L_{5} whereas the original QRNN did not reach 100% even at n=64n=64. We consider the generalization accuracy for n>64n>64 to be a better indicator of whether the network has learned the language rather than the in-distribution test accuracy on strings of length 6464. This is because, if we evaluate at the same length, a finite-state model can still in principle do well since it is unlikely that the test set will contain prefixes with configurations unseen during training.

We formalize this for L5L_{5}, defined as

Define the configuration c(x)c(x) of a string x∈{a,b}∗x\in\{a,b\}^{*} as #a(x)−#b(x)\#_{a}(x)-\#_{b}(x). Intuitively, c(x)c(x) represents all the information needed solve the recognition task. As a function of string length nn, c(x)c(x) follows a random walk where the motion of each discrete time step is 11 with probability 1/21/2 and −1-1 otherwise. Thus, c(x)c(x) is a random variable with a binomial distribution with mean and variance n/4n/4. So, roughly 95% of strings with length 64 will have ∣c(x)∣≤64=8\left\lvert c(x)\right\rvert\leq\sqrt{64}=8. Only by increasing the length nn can we force the model to contend with new configurations.