On the Computational Power of RNNs

Samuel A. Korsky, Robert C. Berwick

Introduction

Recent work suggests that recurrent “neural network” models of several types perform better than sequential models in acquiring and processing hierarchical structure. Indeed, recurrent networks have achieved state-of-the-art results in a number of natural language processing tasks, including named-entity recognition , language modeling , sentiment analysis , natural language generation , and beyond.

The hierarchical structure associated with natural languages is often modeled as some variant of context-free languages, whose languages may be defined over an alphabet Σ\displaystyle\Sigma. These context-free languages are exactly those that can be recognized by pushdown automata (PDAs). Thus it is natural to ask whether these modern natural language processing tools, including simple recurrent neural networks (RNNs) and other, more advanced recurrent architectures, can learn to recognize these languages.

The computational power of RNNs has been studied extensively using empirical testing. Much of this research , focused on the ability of RNNs to recognize simple context-free languages such as anbn\displaystyle a^{n}b^{n} and anbmBmAn\displaystyle a^{n}b^{m}B^{m}A^{n}, or context-sensitive languages such as anbncn\displaystyle a^{n}b^{n}c^{n}. Related works , , focus instead on Dyck languages of balanced parenthesis, which motivates some of our methods. Gated architectures such as the Gated Recurrent Unit (GRU) and Long Short-Term Memory (LSTM) obtain high accuracies on each of these tasks. While simpler RNNs have also been tested, one difficulty is that the standard hyperbolic tangent activation function makes counting difficult. On the other hand, RNNs with ReLU activations were found to perform better, but suffer from what is known as the “exploding gradient problem” and thus are more difficult to train .

Instead of focusing on a single task, many researchers have studied the broader theoretical computational power of recurrent models, where weights are not trained but rather initialized to recognize a desired language. A celebrated result shows that a simple recurrent architecture with 1058 hidden nodes and a saturated-linear activation σ\displaystyle\sigma is a universal Turing Machine, with:

However, their architecture encodes the whole input in its internal state and the relevant computation is only performed after reading a terminal token. This differs from more common RNN variants that consume tokenized inputs at each time step. Furthermore, the authors admit that were the saturated-linear activation to be replaced with the similar and more common sigmoid or hyperbolic tangent activation functions, their methodology would fail.

More recent work suggests that single-layer RNNs with rectified linear unit (ReLU) activations and softmax outputs can also be simulated as universal Turing Machines, but this approach again suffers from the assumption that the entire input is read before computation occurs.

Motivated by these earlier theoretical results, in this report we seek to show results about the computational power of recurrent architectures actually used in practice - namely, those that read tokens one at a time and that use standard rather than specially chosen activation functions. In particular we will prove that, allowing infinite precision, RNNs with just one hidden layer and ReLU activation are at least as powerful as PDAs, and that GRUs are at least as powerful as deterministic finite automata (DFAs). Furthermore, we show that using infinite edge weights and a non-standard output function, GRUs are also at least as powerful as PDAs.

Simple RNNs

Let a simple RNN be an RNN with the following architecture:

In practice, the inputs and hidden nodes of an RNN are stored as numbers with finite precision. Including this restriction, we show the following result:

Theorem 1.1. For every language L⊆Σ∗\displaystyle L\subseteq\Sigma^{*}, L\displaystyle L is regular if and only if L\displaystyle L is the S\displaystyle S-language of some finite precision simple RNN.

set of 2mk\displaystyle 2^{mk} states Q={qh:h is a possible hidden state of the RNN}\displaystyle Q=\{q_{h}:h\ \text{is a possible hidden state of the RNN}\}

transition function δ\displaystyle\delta where δ(qh,x)=qf(Wxx+Whh+bh)\displaystyle\delta(q_{h},x)=q_{f(W_{x}x+W_{h}h+b_{h})}

set of accepting states F={qh∣Whh+bo∈S}\displaystyle F=\{q_{h}|W_{h}h+b_{o}\in S\}

It’s clear that after reading the first n\displaystyle n inputs of a word w\displaystyle w, the current state of this DFA is qhn\displaystyle q_{h_{n}}, which immediately completes the proof of this direction.

For the “only if” direction, suppose we have a DFA D=(Q,Σ,δ,q0,F)\displaystyle D=(Q,\Sigma,\delta,q_{0},F) with corresponding language L\displaystyle L. We will construct a simple RNN whose inputs are one-hotted symbols from Σ\displaystyle\Sigma, with ReLU activation function f(x)=max(0,x)\displaystyle f(x)=\text{max}(0,x), and with ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| hidden nodes whose {0}\displaystyle\{0\}-language is L\displaystyle L.

The RNN has three layers: the first layer (input layer) has ∣Σ∣+∣Q∣∣Σ∣\displaystyle|\Sigma|+|Q||\Sigma| nodes; the second layer (hidden layer) has ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| nodes; and the third layer (output layer) has one node. For the ∣Σ∣\displaystyle|\Sigma| nodes in the input layer associated with the one-hot of the current symbol, label each node with its corresponding symbol from Σ\displaystyle\Sigma. Label the ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| hidden nodes (in both the first and second layers) with all ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| symbol-state combinations (x,q)\displaystyle(x,q) for x∈Σ\displaystyle x\in\Sigma and q∈Q\displaystyle q\in Q.

For every x∈Σ\displaystyle x\in\Sigma, connect the node in the input layer with label x\displaystyle x to all nodes in the hidden layer with labels (x,q)\displaystyle(x,q) for any q∈Q\displaystyle q\in Q with edges with weight 1\displaystyle 1. For all (x,q)∈Σ×Q\displaystyle(x,q)\in\Sigma\times Q, connect the node in the input layer with label (x,q)\displaystyle(x,q) to all nodes in the hidden layer with labels (x′,q′)\displaystyle(x^{\prime},q^{\prime}) where δ(q,x′)=q′\displaystyle\delta(q,x^{\prime})=q^{\prime} with edges also of weight 1\displaystyle 1. Finally, for all (x,q)∈Σ×Q/F\displaystyle(x,q)\in\Sigma\times Q/F, connect the node in the hidden layer with label (x,q)\displaystyle(x,q) to the single node in the output layer with an edge of weight 1\displaystyle 1.

Each of the hidden nodes are initialized to 0\displaystyle 0 except a single hidden node with label (x,q0)\displaystyle(x,q_{0}) for a randomly chosen x∈Σ\displaystyle x\in\Sigma, which is initialized to 1\displaystyle 1. To complete the description of the RNN, we set bh=−1\displaystyle b_{h}=-1 and bo=0\displaystyle b_{o}=0. We claim that the following invariant is maintained: after reading some word, suppose the current state of D\displaystyle D is q\displaystyle q. Then after reading the same word, the hidden nodes of the RNN would all be equal to 0\displaystyle 0 except for one node with label (x,q)\displaystyle(x,q) for some x∈Σ\displaystyle x\in\Sigma, which would equal 1\displaystyle 1.

We prove the claim by induction on the length of the inputted word n\displaystyle n. The base case of n=0\displaystyle n=0 is trivial. Now assume that after reading a word of length n\displaystyle n the current state of D\displaystyle D is q\displaystyle q, and after reading that same word all hidden nodes of the RNN are equal to 0\displaystyle 0 except one node with label (x,q)\displaystyle(x,q) for some x∈Σ\displaystyle x\in\Sigma, which is equal to 1\displaystyle 1. If the next symbol is x′\displaystyle x^{\prime}, then the current state of D\displaystyle D would be q′\displaystyle q^{\prime} where δ(q,x′)=q′\displaystyle\delta(q,x^{\prime})=q^{\prime}. For the RNN, the input layer will have exactly two 1\displaystyle 1s, namely the node with label x′\displaystyle x^{\prime} and the node with label (x,q)\displaystyle(x,q). Since all edges have weight 1\displaystyle 1, that means that before adding bh\displaystyle b_{h} or applying f\displaystyle f the maximum value a node in the hidden layer can take on is 2\displaystyle 2. For this to occur it must be connected to both the nodes in the input layer with value 1\displaystyle 1, and thus by definition its label must be (x′,δ(q,x′))=(x′,q′)\displaystyle(x^{\prime},\delta(q,x^{\prime}))=(x^{\prime},q^{\prime}). By integrality every other node in the hidden layer will take on a value of at most 1\displaystyle 1, so after adding bh=−1\displaystyle b_{h}=-1 and applying f\displaystyle f we easily see that the invariant is maintained.

Utilizing this invariant it is clear that upon reading a word w∈L\displaystyle w\in L the RNN will output 0\displaystyle 0, and upon reading a word w∉L\displaystyle w\not\in L it will output 1\displaystyle 1. Thus L\displaystyle L is precisely the {0}\displaystyle\{0\}-language of the RNN and the theorem is proven.∎

Discussion 1.2. This result shows that simple RNNs with finite precision are exactly as computationally powerful as DFAs. In terms of reducing the size of the hidden layer constructed in the proof of the “only if” direction, it seems likely that ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| is optimal since δ\displaystyle\delta is defined on ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| inputs and needs to be captured fully by the RNN.

Removing the finite precision stipulation unsurprisingly increases the capabilities of RNNs. It is natural to now ask whether these simple RNNs can recognize more complicated S\displaystyle S-languages, and indeed the answer is affirmative. Thus we shift our focus to context-free languages. We begin with some preliminaries:

The Dyck language Dn\displaystyle D_{n} consists of all words over the size 2n\displaystyle 2n alphabet Σ=⋃i=1n{(i,)i}\displaystyle\Sigma=\bigcup\limits_{i=1}^{n}\{(_{i},)_{i}\} that correspond to a balanced string of n\displaystyle n types of parentheses. We also define the set of proper prefixes

so that any word in Pn\displaystyle P_{n} is the prefix of a word in Dn\displaystyle D_{n} but is itself unbalanced. We proceed with a motivating theorem:

Proof. The interested reader may find a proof in . ∎

Thus it makes sense to focus on constructing sets S\displaystyle S and simple RNNs whose S\displaystyle S-language is Dn\displaystyle D_{n}. Indeed, since Dn=g−1(D2)\displaystyle D_{n}=g^{-1}(D_{2}) for some homomorphism g\displaystyle g, we start by focusing on D2\displaystyle D_{2}, in some sense the “hardest” context-free language.

Consider the word (2(1)1(2(1)1)2)2\displaystyle(_{2}(_{1})_{1}(_{2}(_{1})_{1})_{2})_{2}. The evolution of the state as the word is read symbol by symbol is given by

This example makes it clear that this notion of state accurately captures all the relevant information about words in P2∪D2\displaystyle P_{2}\cup D_{2}.

The difficulty in capturing this notion of state in a RNN is that the constant to multiply st−1\displaystyle s_{t-1} by changes depending on the input (it can be either 2\displaystyle 2 or 1/2\displaystyle 1/2 in our example above). Thus storing st\displaystyle s_{t} in a single hidden node is impossible. Instead, we use two hidden nodes. Below, we generalize from D2\displaystyle D_{2} to Dn\displaystyle D_{n}.

Ignoring the output layer for now, consider the simple RNN defined by

where the inputs x\displaystyle x are 2n×1\displaystyle 2n\times 1 one-hots of the symbols in Σ\displaystyle\Sigma (the alphabet of Dn\displaystyle D_{n}) in the order (1,(2,…,(n,)1,)2,…,)n\displaystyle(_{1},(_{2},\dots,(_{n},)_{1},)_{2},\dots,)_{n} and the hidden states have dimension 2×1\displaystyle 2\times 1 where

for all i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\}.

This is similar to the state we defined before, though now generalized to Dn\displaystyle D_{n} and also with intentionally present blank space inserted between the digits in base 2n+1\displaystyle 2n+1. We will show the following invariant:

Lemma 1.4. Given an input word w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n}, we have ht=[st  0]T\displaystyle h_{t}=[s_{t}\ \ 0]^{T} or ht=[0  st]T\displaystyle h_{t}=[0\ \ s_{t}]^{T} for all t\displaystyle t.

Proof. We proceed by induction on t\displaystyle t. The base case of t=0\displaystyle t=0 is trivial. Now, suppose wt+1=(i\displaystyle w_{t+1}=(_{i} for some i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\} and assume without loss of generality that ht=[st  0]T\displaystyle h_{t}=[s_{t}\ \ 0]^{T}. Then

Now, since w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n} we have that st∈[0,1)\displaystyle s_{t}\in[0,1) for any t\displaystyle t, which follows immediately from the stack interpretation of the base 2n+1\displaystyle 2n+1 representation of st\displaystyle s_{t}. Thus ReLU(−2n−1+(2n+1)st)=0\displaystyle\text{ReLU}(-2n-1+(2n+1)s_{t})=0 and so

as desired. Alternatively, suppose wt+1=)i\displaystyle w_{t+1}=)_{i} for some i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\}. Again, assume without loss of generality that ht=[st  0]T\displaystyle h_{t}=[s_{t}\ \ 0]^{T}. Then

The fact that w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n} clearly implies that (2n+1)st−2i≥0\displaystyle(2n+1)s_{t}-2i\geq 0 and so we have that

A pictorial example of this RNN is depicted below for n=2\displaystyle n=2:

\displaystyle h_{1,t}\ \<span class="katex-error" title="ParseError: KaTeX parse error: Unexpected character: &#x27;\&#x27; at position 25: …tyle\ h_{2,t}\ \̲" style="color:#cc0000">\displaystyle\ h_{2,t}\ \</span>\displaystyle\ x_{1,t}\ \<span class="katex-error" title="ParseError: KaTeX parse error: Unexpected character: &#x27;\&#x27; at position 25: …tyle\ x_{2,t}\ \̲" style="color:#cc0000">\displaystyle\ x_{2,t}\ \</span>\displaystyle\ x_{3,t}\ \<span class="katex-error" title="ParseError: KaTeX parse error: Unexpected character: &#x27;\&#x27; at position 25: …tyle\ x_{4,t}\ \̲" style="color:#cc0000">\displaystyle\ x_{4,t}\ \</span>\displaystyle h_{1,t-1}<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><msub><mi>h</mi><mrow><mn>2</mn><mo separator="true">,</mo><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle h_{2,t-1}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9805em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span><span class="mpunct mtight">,</span><span class="mord mathnormal mtight">t</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span></span>\displaystyle 0.4<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mn>0.8</mn></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle 0.8</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.8</span></span></span></span></span>\displaystyle-5<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mo>−</mo><mn>5</mn></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle-5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">−</span><span class="mord">5</span></span></span></span></span>\displaystyle 0.2<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mn>0.2</mn></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle 0.2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.2</span></span></span></span></span>\displaystyle-5<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mo>−</mo><mn>5</mn></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle-5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">−</span><span class="mord">5</span></span></span></span></span>\displaystyle-2<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mo>−</mo><mn>4</mn></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle-4</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">−</span><span class="mord">4</span></span></span></span></span>\displaystyle 55\displaystyle 5 Thus we have found an efficient way to store st\displaystyle s_{t}. Now it’s clear that for any w=w1w2…wm∈Pn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n} we have sm>0\displaystyle s_{m}>0 and for any w=w1w2…wm∈Dn\displaystyle w=w_{1}w_{2}\dots w_{m}\in D_{n} we have sm=0\displaystyle s_{m}=0, so it is tempting to try and add a simple output layer to this RNN and claim that its {0}\displaystyle\{0\}-language is Dn\displaystyle D_{n}. However, this is most likely impossible to accomplish.

Indeed, consider the word w=)1(1\displaystyle w=)_{1}(_{1}. We have that s2=0\displaystyle s_{2}=0 for this word, but w∉Dn\displaystyle w\not\in D_{n}. Furthermore, consider the word w=(2)1(1)2\displaystyle w=(_{2})_{1}(_{1})_{2}. We have that st≥0\displaystyle s_{t}\geq 0 for all t\displaystyle t and s4=0\displaystyle s_{4}=0 for this word, yet w∉Dn\displaystyle w\not\in D_{n}. Hence we must be able to flag when an inappropriate closing parenthesis appears in an input and retain that information while reading the rest of the input. To that end, consider the following simple RNN, an example of which can be found in Appendix A.1:

where again the inputs x\displaystyle x are 2n×1\displaystyle 2n\times 1 one-hots of the symbols in Σ\displaystyle\Sigma (the alphabet of Dn\displaystyle D_{n}) in the order (1,(2,…,(n,)1,)2,…,)n\displaystyle(_{1},(_{2},\dots,(_{n},)_{1},)_{2},\dots,)_{n} and the hidden states have dimension 6×1\displaystyle 6\times 1 where

Because the last four elements of the first two rows of Wh\displaystyle W_{h} are all equal to 0\displaystyle 0 and otherwise the first two rows of Wx\displaystyle W_{x} and Wh\displaystyle W_{h} are the same as before, it is clear that Lemma 1.4 still applies in some form for the new simple RNN. Indeed, denoting

Corollary 1.5. With respect to a word w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n}, we have [h1,t  h2,t]=[st  0]\displaystyle[h_{1,t}\ \ h_{2,t}]=[s_{t}\ \ 0] or [h1,t  h2,t]=[0  st]\displaystyle[h_{1,t}\ \ h_{2,t}]=[0\ \ s_{t}] for all t\displaystyle t.

Lemma 1.6. For any word w∈Pn\displaystyle w\in P_{n}, there is a unique x∈{)1,)2,…,)n}\displaystyle x\in\{)_{1},)_{2},\dots,)_{n}\} such that wx∈Pn∪Dn\displaystyle wx\in P_{n}\cup D_{n}.

Proof. This immediately follows from the definition of a balanced string. Indeed, if s\displaystyle s is the state associated with w\displaystyle w then this unique x\displaystyle x is given by

Lemma 1.7. Given an input word w=w1w2…wm∈Pn∪Dn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n}\cup D_{n}, we have that h3,m=h5,m=0\displaystyle h_{3,m}=h_{5,m}=0.

Proof. We first restrict our attention to h3,m\displaystyle h_{3,m}. Note that

for any i\displaystyle i, which follows from the definition of Wh\displaystyle W_{h} and Wx\displaystyle W_{x}. Then using Corollary 1.5 we find

Now using the inequality in the proof of Lemma 1.6 we immediately obtain h3,m=0\displaystyle h_{3,m}=0 as desired.

Considering now h5,m\displaystyle h_{5,m} we notice

and doing an analysis similar to that for h3,m\displaystyle h_{3,m}, we obtain h5,m=0\displaystyle h_{5,m}=0 as desired.∎

Applying Lemma 1.6 allows us to make the following statement:

Lemma 1.8. Given a word w=w1w2…wm∈Pn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n}, consider the unique j∈{1,2,…,n}\displaystyle j\in\{1,2,\dots,n\} such that w)j∈Pn∩Dn\displaystyle w)_{j}\in P_{n}\cap D_{n}. Then with respect to a word w)i\displaystyle w)_{i} with i>j\displaystyle i>j, we have h3,m+1>0\displaystyle h_{3,m+1}>0. Similarly, with respect to a word w)i\displaystyle w)_{i} with i<j\displaystyle i<j, we have h5,m+1>0\displaystyle h_{5,m+1}>0.

Proof. First suppose i>j\displaystyle i>j. As in the proof of Lemma 1.7, we use

where we again use Corollary 1.5 and the fact that h3,m=0\displaystyle h_{3,m}=0 from Lemma 1.7. But from the proof of Lemma 1.6, since w)j∈Pn∪Dn\displaystyle w)_{j}\in P_{n}\cup D_{n} we know that

and since i>j\displaystyle i>j we have that 2i>2j+1\displaystyle 2i>2j+1 since i\displaystyle i and j\displaystyle j are integral. Thus h3,m+1>0\displaystyle h_{3,m+1}>0 as desired.

Now assume i<j\displaystyle i<j. As in the previous case we obtain

again using Corollary 1.5 and Lemma 1.7. And again using the inequality from the proof of Lemma 1.6 and the fact that i<j\displaystyle i<j we obtain h5,m+1>0\displaystyle h_{5,m+1}>0, completing the proof.∎

Thus we have constructed the desired “flags.” Indeed, hidden nodes h3\displaystyle h_{3} and h5\displaystyle h_{5} remain equal to 0\displaystyle 0 while the currently read input lies in Pn∪Dn\displaystyle P_{n}\cup D_{n}, but one of these nodes becomes positive the moment the currently read input does not lie in this set.

However, there are still difficulties. It is possible for h3\displaystyle h_{3} or h5\displaystyle h_{5} to become positive and later return to 0\displaystyle 0. Indeed, running the simple RNN on the word w=(2)1(1)2(2)2\displaystyle w=(_{2})_{1}(_{1})_{2}(_{2})_{2}, we compute h1,6=h2,6=h3,6=h5,6=0\displaystyle h_{1,6}=h_{2,6}=h_{3,6}=h_{5,6}=0. However, clearly w∉Pn∪Dn\displaystyle w\not\in P_{n}\cup D_{n}. Therefore we need to add architecture that retains the information as to whether the hidden nodes h3\displaystyle h_{3} or h5\displaystyle h_{5} ever become positive, and below we show that hidden nodes h4\displaystyle h_{4} and h6\displaystyle h_{6} respectively are sufficient.

Lemma 1.9. For any input w∈Σ∗\displaystyle w\in\Sigma^{*} we have

Proof. From the definition of Wx\displaystyle W_{x} and Wh\displaystyle W_{h} we have

and since h3,t,h5,t≥0\displaystyle h_{3,t},h_{5,t}\geq 0 for all t\displaystyle t (because of the ReLU) we immediately have the result by induction or direct expansion.∎

We are now ready to combine these lemmas and accomplish our original goal:

Theorem 1.10. The {0}\displaystyle\{0\}-language of the simple RNN described earlier in the section is Dn\displaystyle D_{n}.

Proof. Consider any input w=w1w2…wm∈Σ∗\displaystyle w=w_{1}w_{2}\dots w_{m}\in\Sigma^{*} into the RNN. For the remainder of the proof, remember that hi,t≥0\displaystyle h_{i,t}\geq 0 for all i,t\displaystyle i,t because of the ReLU activation. We consider three cases:

Case 1: w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n}.

In this case by Corollary 1.5 we have h1,m+h2,m=sm\displaystyle h_{1,m}+h_{2,m}=s_{m}. Furthermore, by Lemma 1.7 we have h3,m=h5,m=0\displaystyle h_{3,m}=h_{5,m}=0. By combining Lemmas 1.7 and 1.9, we have h4,m=h6,m=0\displaystyle h_{4,m}=h_{6,m}=0. Thus om=sm\displaystyle o_{m}=s_{m} which, given that w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n}, equals 0\displaystyle 0 precisely when w∈Dn\displaystyle w\in D_{n}, by the inequality from the proof of Lemma 1.6.

Case 2: w∉Pn∪Dn\displaystyle w\not\in P_{n}\cup D_{n} and w1w2…wm−1∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{m-1}\in P_{n}\cup D_{n}.

In this case we clearly must have wm=)i\displaystyle w_{m}=)_{i} for some i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\} and thus by Lemma 1.8 we have that either h3,m>0\displaystyle h_{3,m}>0 or h5,m>0\displaystyle h_{5,m}>0, so om>0\displaystyle o_{m}>0.

Case 3: w1w2…wk∉Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{k}\not\in P_{n}\cup D_{n} for some k∈{1,2,…,m−1}\displaystyle k\in\{1,2,\dots,m-1\}.

Suppose j\displaystyle j is the minimal index such that w1w2…wj∉Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{j}\not\in P_{n}\cup D_{n}. Then by minimality w1w2…wj−1∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{j-1}\in P_{n}\cup D_{n} so again by Lemma 1.8 we have that either h3,j>0\displaystyle h_{3,j}>0 or h5,j>0\displaystyle h_{5,j}>0. But since j≤k≤m−1\displaystyle j\leq k\leq m-1 by Lemma 1.9 this means that either h4,m>0\displaystyle h_{4,m}>0 or h6,m>0\displaystyle h_{6,m}>0, so om>0\displaystyle o_{m}>0.

Thus om=0\displaystyle o_{m}=0 if and only if w∈Dn\displaystyle w\in D_{n}, completing the proof of the theorem.∎

Now recall in the proof of Theorem 1.1 we showed that any regular language R\displaystyle R was the {0}\displaystyle\{0\}-language of some simple RNN, and moreover that for any input not in R\displaystyle R the output of that RNN is positive. This allows us to provide a simple proof of the main theorem of this section:

Theorem 1.11. For any context-free language L\displaystyle L, suppose we relabel and write L=Dn∩R\displaystyle L=D_{n}\cap R for some regular language R\displaystyle R, whose corresponding minimum-size DFA has r\displaystyle r states. Then there exists a simple RNN with a hidden layer of size 6+2nr\displaystyle 6+2nr whose {0}\displaystyle\{0\}-language is L\displaystyle L.

Proof. Consider the simple RNN with R\displaystyle R as its {0}\displaystyle\{0\}-language described in the proof of Theorem 1.1 and the simple RNN with Dn\displaystyle D_{n} as its {0}\displaystyle\{0\}-language constructed to prove Theorem 1.10. Merge the ∣Σ∣=2n\displaystyle|\Sigma|=2n nodes in the input layer corresponding to the input and merge the single output nodes of both RNNs. Stack the two hidden layers, and add no new edges. There were ∣Σ∣r=2nr\displaystyle|\Sigma|r=2nr hidden nodes in the first RNN and 6\displaystyle 6 in the second, so altogether the new RNN has 6+2nr\displaystyle 6+2nr hidden nodes.

The output of the new RNN is equal to the summed output of the two original RNNs, and from the proofs of Theorems 1.1 and 1.10 these outputs are always nonnegative. Thus the output of the new RNN is 0\displaystyle 0 if and only if the outputs of both old RNNs were 0\displaystyle 0, immediately proving the theorem.∎

Discussion 1.12. This result shows that simple RNNs with arbitrary precision are at least as computationally powerful as PDAs.

Gated RNNs

In practice, architectures more complicated than the simple RNNs studied above - notably gated RNNs, including the Gated Recurrent Unit (GRU) and Long Short-Term Memory (LSTM) - perform better on many natural language tasks. Thus we are motivated to explore their computational capabilities. Here we focus on the GRU, described by the equations below:

Theorem 2.1. For every language L⊆Σ∗\displaystyle L\subseteq\Sigma^{*}, L\displaystyle L is regular if and only if L\displaystyle L is the S\displaystyle S-language of some finite precision GRU.

Proof. The “if” direction can be shown in the same manner as in Theorem 1.1. So, here we focus on the “only if” direction. Suppose we have a DFA D=(Q,Σ,δ,q0,F)\displaystyle D=(Q,\Sigma,\delta,q_{0},F) with corresponding language L\displaystyle L. We will construct a GRU whose inputs are one-hotted symbols from Σ\displaystyle\Sigma with ∣Q∣∣Σ∣\displaystyle|Q||\Sigma| hidden nodes whose {0}\displaystyle\{0\}-language is L\displaystyle L.

For convenience, for all x∈Σ\displaystyle x\in\Sigma let ex\displaystyle e_{x} denote the corresponding one-hot vector for x\displaystyle x. Furthermore, let N=∣Σ∣∣Q∣\displaystyle N=|\Sigma||Q|.

First set Wz=Wh=0\displaystyle W_{z}=W_{h}=0 and Uz=Ur=0\displaystyle U_{z}=U_{r}=0 and bz=br=bh=0\displaystyle b_{z}=b_{r}=b_{h}=0, so the simplified GRU is given by:

Now, define an arbitrary bijective map g:{1,2,…,∣Q∣}→Q\displaystyle g:\{1,2,\dots,|Q|\}\rightarrow Q. Then construct ∣Q∣\displaystyle|Q| vectors

where for all i∈{1,2,…,∣Q∣}\displaystyle i\in\{1,2,\dots,|Q|\} and k∈{1,2,…,N}\displaystyle k\in\{1,2,\dots,N\} we set

Our goal will be to find Wr\displaystyle W_{r} and Uh\displaystyle U_{h} such that if ht−1=si\displaystyle h_{t-1}=s_{i} for some i\displaystyle i, and xt\displaystyle x_{t} is the one-hot encoding of some x∈Σ\displaystyle x\in\Sigma, then ht=sj\displaystyle h_{t}=s_{j} where if g(i)=q\displaystyle g(i)=q for some q∈Q\displaystyle q\in Q then g(j)=δ(q,x)\displaystyle g(j)=\delta(q,x). If this is possible, then we could set h0=sg−1(q0)\displaystyle h_{0}=s_{g^{-1}(q_{0})} and be able to track the current state of the DFA effectively.

The strategy for accomplishing this is essentially to pick a simple Wr\displaystyle W_{r}, and then solve a system of equations to produce the desired Uh\displaystyle U_{h}.

For convenience, define the natural map h:{1,2,…,∣Σ∣}→Σ\displaystyle h:\{1,2,\dots,|\Sigma|\}\rightarrow\Sigma where h(i)=x\displaystyle h(i)=x if and only if the i\displaystyle ith element of ex\displaystyle e_{x} is equal to 1\displaystyle 1.

for all k∈{1,2,…,N}\displaystyle k\in\{1,2,\dots,N\} and j∈{1,2,…,∣Σ∣}\displaystyle j\in\{1,2,\dots,|\Sigma|\}. Now consider the N\displaystyle N equations

where g(j)=δ(g(i),x)\displaystyle g(j)=\delta(g(i),x), for every i∈{1,2,…,∣Q∣}\displaystyle i\in\{1,2,\dots,|Q|\} and x∈Σ\displaystyle x\in\Sigma. Let

for all i∈{1,2,…,∣Q∣}\displaystyle i\in\{1,2,\dots,|Q|\} and j∈{1,2,…,∣Σ∣}\displaystyle j\in\{1,2,\dots,|\Sigma|\} and k∈{1,2,…,N}\displaystyle k\in\{1,2,\dots,N\}. Letting

The N\displaystyle N earlier equations can now be combined as a single matrix equation given by

where Cj\displaystyle C_{j} is a ∣Σ∣×∣Σ∣\displaystyle|\Sigma|\times|\Sigma| matrix for each j∈{1,2,…,∣Σ∣}\displaystyle j\in\{1,2,\dots,|\Sigma|\}. In particular, we have that

Using basic row operations it is easy to see that det(Cj)=0.1∣Σ∣(∣Σ∣+1)\displaystyle\text{det}(C_{j})=0.1^{|\Sigma|}(|\Sigma|+1) for all j\displaystyle j, so

and thus C−1\displaystyle C^{-1} is well-defined. Furthermore, since si,k∈{0,0.25}\displaystyle s_{i,k}\in\{0,0.25\} for each i,k\displaystyle i,k, the inputs into all inverse hyperbolic tangents in B\displaystyle B lie in (−1,1)\displaystyle(-1,1) and so B\displaystyle B is well-defined as well. Thus our expression for Uh\displaystyle U_{h} is well-defined.

Now, given our choices for the si,Wr\displaystyle s_{i},W_{r}, and Uh\displaystyle U_{h}, after reading any input w=w1w2…wm\displaystyle w=w_{1}w_{2}\dots w_{m}, if q\displaystyle q is the current state of the DFA associated with L\displaystyle L, then hm=sg−1(q)\displaystyle h_{m}=s_{g^{-1}(q)}. Now because the si\displaystyle s_{i} are clearly linearly independent, we can find a Wo\displaystyle W_{o} such that

for all i∈{1,2,…,Q}\displaystyle i\in\{1,2,\dots,Q\} and it’s clear that the {0}\displaystyle\{0\}-language of the resulting GRU will be L\displaystyle L, as desired.∎

Discussion 2.2. In the above proof, we are implicitly assuming that the activation functions of the GRU are not actually the sigmoid and hyperbolic tangent functions but rather finite precision analogues for which the equations we solved are all consistent. However, for the remainder of this section we can drop this assumption.

To account for both of these issues, instead of keeping track of the state st\displaystyle s_{t} as we read a word, we will instead keep track of the state st′\displaystyle s^{\prime}_{t} of a word w=w1w2…wm∈Σ∗\displaystyle w=w_{1}w_{2}\dots w_{m}\in\Sigma^{*} defined by

for all i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\}, for some predetermined sufficiently large k\displaystyle k. We have the following relationship between st′\displaystyle s^{\prime}_{t} and st\displaystyle s_{t}:

Lemma 2.3. For any word w=w1w2…wm∈Σ∗\displaystyle w=w_{1}w_{2}\dots w_{m}\in\Sigma^{*} we have st=(2n+1)ktst′\displaystyle s_{t}=(2n+1)^{kt}s^{\prime}_{t} for all t∈{1,2,…,m}\displaystyle t\in\{1,2,\dots,m\}.

Proof. Multiplying the recurrence relationship for st′\displaystyle s^{\prime}_{t} by (2n+1)kt\displaystyle(2n+1)^{kt} we recover the recurrence relationship for st\displaystyle s_{t} in Section 1, implying the desired result.∎

Thus the state s′\displaystyle s^{\prime} allows us to keep track of the old state s\displaystyle s without having to multiply by any constant greater than 1\displaystyle 1. Furthermore, for large k\displaystyle k, s′\displaystyle s^{\prime} will be extremely small, allowing us to abuse the fact that tanh(x)∼x\displaystyle\text{tanh}(x)\sim x for small values of x\displaystyle x. In terms of the stack of digits interpretation of s\displaystyle s, s′\displaystyle s^{\prime} is the same except between every pop or push we add k\displaystyle k zeros to the top of the stack.

Again we wish to construct a GRU from whose hidden state we can recover st′\displaystyle s^{\prime}_{t}. Ignoring the output layer for now, consider the GRU defined by

where h1,0≥0\displaystyle h_{1,0}\geq 0 will be determined later, the inputs x\displaystyle x are again 2n×1\displaystyle 2n\times 1 one-hots of the symbols in Σ\displaystyle\Sigma in the order (1,(2,…,(n,)1,)2,…,)n\displaystyle(_{1},(_{2},\dots,(_{n},)_{1},)_{2},\dots,)_{n} and the hidden states have dimension 3×1\displaystyle 3\times 1 where

where σ−1(x)=−ln⁡(x−1−1)\displaystyle\sigma^{-1}(x)=-\ln(x^{-1}-1) is the inverse of the sigmoid function. For sufficiently large k\displaystyle k, clearly our use of σ−1\displaystyle\sigma^{-1} is well-defined. We will show the following invariant:

Lemma 2.4. Given an input word w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n}, if h1,0=0\displaystyle h_{1,0}=0 then we have ht≈[st′  (2n+1)−kt  (2n+1)−kt]T\displaystyle h_{t}\approx[s^{\prime}_{t}\ \ (2n+1)^{-kt}\ \ (2n+1)^{-kt}]^{T} for all t\displaystyle t.

Proof. As in Section 1, let zt=[z1,t  z2,t  z3,t]T\displaystyle z_{t}=[z_{1,t}\ \ z_{2,t}\ \ z_{3,t}]^{T} and rt=[r1,t  r2,t  r3,t]T\displaystyle r_{t}=[r_{1,t}\ \ r_{2,t}\ \ r_{3,t}]^{T} and ht=[h1,t  h2,t  h3,t]T\displaystyle h_{t}=[h_{1,t}\ \ h_{2,t}\ \ h_{3,t}]^{T}. First, we will show h2,t=(2n+1)−kt\displaystyle h_{2,t}=(2n+1)^{-kt} for all t∈{1,2,…,m}\displaystyle t\in\{1,2,\dots,m\} by induction on t\displaystyle t. The base case is trivial, so note

so by induction h2,t+1=(2n+1)−k(t+1)\displaystyle h_{2,t+1}=(2n+1)^{-k(t+1)} as desired. Similarly, we obtain h3,t=(2n+1)−kt\displaystyle h_{3,t}=(2n+1)^{-kt} for all t\displaystyle t.

Now we restrict our attention to h1,t\displaystyle h_{1,t}. Note that

and so using the definition of Uh\displaystyle U_{h} we obtain

If we removed the tanh from the above expression, it would simplify to

which is exactly the recurrence relation satisfied by st′\displaystyle s^{\prime}_{t}. Since the expressions inside the hyperbolic tangents are extremely small (on the order of 2−kt\displaystyle 2^{-kt}), this implies that h1,t\displaystyle h_{1,t} is a good approximation for st′\displaystyle s^{\prime}_{t} as desired. This will be formalized in the next lemma.∎

Lemma 2.5. For any input word w∈Pn∪Dn\displaystyle w\in P_{n}\cup D_{n}, if h1,0=0\displaystyle h_{1,0}=0 then we have ∣(2n+1)kth1,t−st∣<2(2n+1)−2k+7\displaystyle|(2n+1)^{kt}h_{1,t}-s_{t}|<2(2n+1)^{-2k+7} for all t\displaystyle t.

Proof. Let ϵt=(2n+1)kth1,t−st\displaystyle\epsilon_{t}=(2n+1)^{kt}h_{1,t}-s_{t} for all t\displaystyle t. Then we easily find that

Now define ϵt′\displaystyle\epsilon^{\prime}_{t} by the recurrence

with ϵ0′=ϵ0=0\displaystyle\epsilon^{\prime}_{0}=\epsilon_{0}=0. Because tanh(x)<x\displaystyle\text{tanh}(x)<x for all x>0\displaystyle x>0 it is easy to see that ϵt′≥∣ϵt∣\displaystyle\epsilon^{\prime}_{t}\geq|\epsilon_{t}| for all t\displaystyle t.

Now by a Taylor expansion, tanh(x)=x−x33+2x515+O(x7)\displaystyle\text{tanh}(x)=x-\frac{x^{3}}{3}+\frac{2x^{5}}{15}+O(x^{7}), so we have that

for x>0\displaystyle x>0. Thus we obtain the bound

Since 2i<2n+1\displaystyle 2i<2n+1 and (2n+1)k+1−1≥(2n+1)k\displaystyle(2n+1)^{k+1}-1\geq(2n+1)^{k} we also have

Since again 2i<2n+1\displaystyle 2i<2n+1 and (2n+1)k−1−1≥(2n+1)k−2\displaystyle(2n+1)^{k-1}-1\geq(2n+1)^{k-2} we also have

Thus if we define at\displaystyle a_{t} by the recurrence

with a0=ϵ0′=0\displaystyle a_{0}=\epsilon^{\prime}_{0}=0, then at≥ϵt′\displaystyle a_{t}\geq\epsilon^{\prime}_{t} for all t\displaystyle t.

Now we wish to upper bound at\displaystyle a_{t}. Since i\displaystyle i is not present in the recurrence for at\displaystyle a_{t}, assume without loss of generality that all parenthesis in an input word w=w1w2…wm∈Pn∪Dn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n}\cup D_{n} lie in {(1,)1}\displaystyle\{(_{1},)_{1}\}. Suppose that )1(1\displaystyle)_{1}(_{1} was a substring of w\displaystyle w, so that w=x)1(1y\displaystyle w=x)_{1}(_{1}y. Then we would have

However, for the word w′=x(1)1y\displaystyle w^{\prime}=x(_{1})_{1}y (which would clearly still lie in Pn∪Dn\displaystyle P_{n}\cup D_{n}) we would have

which is larger. Thus to upper bound at\displaystyle a_{t} it suffices to consider only words that do not contain the substring )1(1\displaystyle)_{1}(_{1}, which are words in the form

with r\displaystyle r open parentheses followed by s≤r\displaystyle s\leq r closing parentheses. Furthermore, adding extra closing parenthesis where suitable clearly increases the final at\displaystyle a_{t} so we can assume s=r\displaystyle s=r. We can then exactly calculate a2r\displaystyle a_{2r} as

Considering each sum separately we have for sufficiently large k\displaystyle k that

And therefore 2(2n+1)−2k+7\displaystyle 2(2n+1)^{-2k+7} is an upper bound on at\displaystyle a_{t}. Thus

Corollary 2.6. For any input word w=w1w2…wm∈Pn∪Dn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n}\cup D_{n}, if w1w2…wt\displaystyle w_{1}w_{2}\dots w_{t} contains at\displaystyle a_{t} open parentheses and bt≤at\displaystyle b_{t}\leq a_{t} closing parentheses then

with ∣ϵ∣<2(2n+1)−2k+7\displaystyle|\epsilon|<2(2n+1)^{-2k+7} for all t\displaystyle t.

Proof. This follows directly from the computations in the proof of Lemma 2.5 and the recurrence for h1,t\displaystyle h_{1,t}.∎

Now, set h1,0=3(2n+1)−2k+7\displaystyle h_{1,0}=3(2n+1)^{-2k+7}. We then have the following useful analogues of Lemmas 1.7 and 1.8:

Corollary 2.7. For any input word w=w1w2…wm∈Pn∪Dn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n}\cup D_{n} we have h1,m>0\displaystyle h_{1,m}>0.

Proof. This follows immediately from Corollary 2.6 and the fact that h1,0>2(2n+1)−2k+7\displaystyle h_{1,0}>2(2n+1)^{-2k+7}. ∎

Lemma 2.8. Given a word w1w2…wm∈Pn\displaystyle w_{1}w_{2}\dots w_{m}\in P_{n}, consider the unique j∈{1,2,…,n}\displaystyle j\in\{1,2,\dots,n\} such that w)j∈Pn∪Dn\displaystyle w)_{j}\in P_{n}\cup D_{n}. Then for an input word w)i\displaystyle w)_{i} with i>j\displaystyle i>j, we have h1,m+1<0\displaystyle h_{1,m+1}<0.

so multiplying both sides by (2n+1)k(m+1)\displaystyle(2n+1)^{k(m+1)} and using the inequality from the proof of Lemma 2.5 we have

where we used the inequality from the proof of Lemma 1.6 and the fact that h1,0=3(2n+1)−2k+7\displaystyle h_{1,0}=3(2n+1)^{-2k+7}. Therefore

Since i>j\displaystyle i>j we have that 2j+1−2i≤−1\displaystyle 2j+1-2i\leq-1 and so for sufficiently large k\displaystyle k we then have

With these results in hand, consider the larger GRU, an example of which can be found in Appendix A.2, defined by

where the inputs x\displaystyle x are again 2n×1\displaystyle 2n\times 1 one-hots of the symbols in Σ\displaystyle\Sigma in the order (1,(2,…,(n,)1,)2,…,)n\displaystyle(_{1},(_{2},\dots,(_{n},)_{1},)_{2},\dots,)_{n} and the hidden states have dimension 8×1\displaystyle 8\times 1 where

As before, with respect to a word w∈Σ∗\displaystyle w\in\Sigma^{*} define st\displaystyle s_{t} by

for all i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\} and all t\displaystyle t. Similarly define s‾t\displaystyle\overline{s}_{t} by

For our new GRU, let ht=[h1,t  h2,t  h3,t  h4,t  h5,t  h6,t  h7,t  h8,t]T\displaystyle h_{t}=[h_{1,t}\ \ h_{2,t}\ \ h_{3,t}\ \ h_{4,t}\ \ h_{5,t}\ \ h_{6,t}\ \ h_{7,t}\ \ h_{8,t}]^{T}. We then have the following results:

Lemma 2.9. For any input word w∈Σ∗\displaystyle w\in\Sigma^{*} we have h2,t=h3,t=h6,t=h7,t=(2n+1)−kt\displaystyle h_{2,t}=h_{3,t}=h_{6,t}=h_{7,t}=(2n+1)^{-kt}.

Proof. This follows immediately from the proof of Lemma 2.4.∎

Lemma 2.10. For any input word w=w1w2…wm∈Pn∪Dn\displaystyle w=w_{1}w_{2}\dots w_{m}\in P_{n}\cup D_{n}, if w1w2…wt\displaystyle w_{1}w_{2}\dots w_{t} contains at\displaystyle a_{t} open parentheses and bt≤at\displaystyle b_{t}\leq a_{t} closing parenthesis then

with ∣ϵ1∣,∣ϵ2∣<2(2n+1)−2k+7\displaystyle|\epsilon_{1}|,|\epsilon_{2}|<2(2n+1)^{-2k+7} for all t\displaystyle t.

Proof. This follows immediately from the proof of Corollary 2.6 and the new Wr\displaystyle W_{r}, since h5,t\displaystyle h_{5,t} behaves exactly like h1,t\displaystyle h_{1,t} if each input (i\displaystyle(_{i} or )i\displaystyle)_{i} were (n−i\displaystyle(_{n-i} or )n−i\displaystyle)_{n-i} respectively, instead. ∎

Lemma 2.11. For any input word w=w1w2…wm∈Σ∗\displaystyle w=w_{1}w_{2}\dots w_{m}\in\Sigma^{*} we have h4,m,h8,m∈{0,1}\displaystyle h_{4,m},h_{8,m}\in\{0,1\} and h4,m=h8,m=1\displaystyle h_{4,m}=h_{8,m}=1 if and only if w1w2…wm−1∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{m-1}\in P_{n}\cup D_{n}.

Proof. From our chosen Uz\displaystyle U_{z} we see that

Since h4,0=h8,0=1\displaystyle h_{4,0}=h_{8,0}=1 and since the fourth and eighth rows of Uh\displaystyle U_{h} are identically 0\displaystyle 0, the equation

which immediately implies that h4,m,h8,m∈{0,1}\displaystyle h_{4,m},h_{8,m}\in\{0,1\}. Now, suppose w1w2…wm−1∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{m-1}\in P_{n}\cup D_{n}. Then from Corollary 2.7 and its analogue for h5,t\displaystyle h_{5,t} we see that z4,t=z8,t=1\displaystyle z_{4,t}=z_{8,t}=1 for all t∈{1,2,…,m}\displaystyle t\in\{1,2,\dots,m\}, so h4,m=h8,m=1\displaystyle h_{4,m}=h_{8,m}=1 as desired.

Otherwise, there exists some minimal k∈{0,1,…,m−2}\displaystyle k\in\{0,1,\dots,m-2\} such that w1w2…wk+1∉Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{k+1}\not\in P_{n}\cup D_{n}. Then wk+1=)i\displaystyle w_{k+1}=)_{i} for some i∈{1,2,…,n}\displaystyle i\in\{1,2,\dots,n\}. Consider the unique j≠i\displaystyle j\neq i such that w1w2…wk)j∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{k})_{j}\in P_{n}\cup D_{n}. If i>j\displaystyle i>j then from the proof of Lemma 2.8 we have that h1,k+1<0\displaystyle h_{1,k+1}<0 and so z4,k+2=0\displaystyle z_{4,k+2}=0. Since k+2≤m\displaystyle k+2\leq m this means that h4,m=0\displaystyle h_{4,m}=0. If i<j\displaystyle i<j then from the analogue of the proof of Lemma 2.8 for h5,t\displaystyle h_{5,t}, we obtain h8,m=0\displaystyle h_{8,m}=0. This completes the proof. ∎

We are now ready to combine these lemmas to prove an important result, the analogue of Theorem 1.10 for GRUs:

Theorem 2.12. The (0,(2n+1)−1)\displaystyle(0,(2n+1)^{-1})-language of the GRU described earlier in the section is Dn\displaystyle D_{n}.

Proof. Consider any input word w=w1w2…wm∈Σ∗\displaystyle w=w_{1}w_{2}\dots w_{m}\in\Sigma^{*} into the GRU. We consider four cases:

In this case, we clearly have sm=0\displaystyle s_{m}=0 and h1,m>0\displaystyle h_{1,m}>0 from the proof of Corollary 2.7, so by Lemmas 2.9 and 2.10 we have that

with ∣ϵ∣<2(2n+1)−2k+7\displaystyle|\epsilon|<2(2n+1)^{-2k+7}. Furthermore from Lemma 2.11 we have that h4,m=h8,m=1\displaystyle h_{4,m}=h_{8,m}=1 so since h1,0=3(2n+1)−2k+7\displaystyle h_{1,0}=3(2n+1)^{-2k+7} we must have

for sufficiently large k\displaystyle k, as desired.

As in Case 1 we have that h1,m>0\displaystyle h_{1,m}>0 and so by Lemmas 2.9 and 2.10 we have that

with ∣ϵ∣<2(2n+1)−2k+7\displaystyle|\epsilon|<2(2n+1)^{-2k+7}. Furthermore from Lemma 2.11 we have that h4,m=h8,m=1\displaystyle h_{4,m}=h_{8,m}=1 so here

for sufficiently large k\displaystyle k, since the minimum value of sm\displaystyle s_{m} is clearly 2(2n+1)−1\displaystyle 2(2n+1)^{-1}.

Case 3: w∉Pn∪Dn\displaystyle w\not\in P_{n}\cup D_{n} and w1w2…wm−1∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{m-1}\in P_{n}\cup D_{n}.

Suppose w1w2…wm−1)j∈Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{m-1})_{j}\in P_{n}\cup D_{n} for some unique j∈{1,2,…,n}\displaystyle j\in\{1,2,\dots,n\}. If wm=)i\displaystyle w_{m}=)_{i} for some i>j\displaystyle i>j then from Lemmas 2.9 and 2.10 and the proof of Lemma 2.8 we obtain

for sufficiently large k\displaystyle k. If instead i<j\displaystyle i<j then the same technique with the inequality tanh(x)<x\displaystyle\text{tanh}(x)<x can be used to show

if k\displaystyle k is sufficiently large. As before using Lemma 2.11 we have that h4,m=h8,m=1\displaystyle h_{4,m}=h_{8,m}=1 and combining these bounds we find that

Case 4: w1w2…wk∉Pn∪Dn\displaystyle w_{1}w_{2}\dots w_{k}\not\in P_{n}\cup D_{n} for some k∈{1,2,…,m−1}\displaystyle k\in\{1,2,\dots,m-1\}

In this case we know that h2,m≥0\displaystyle h_{2,m}\geq 0 by Lemma 2.9, so we have

and by Lemma 2.11 we know that 0≤h4,m+h8,m≤1\displaystyle 0\leq h_{4,m}+h_{8,m}\leq 1 so

Thus om∈(0,(2n+1)−1)\displaystyle o_{m}\in(0,(2n+1)^{-1}) if w∈Dn\displaystyle w\in D_{n} and om>(2n+1)−1\displaystyle o_{m}>(2n+1)^{-1} otherwise, as desired.∎

We may now proceed to show the main theorem of this section, an analogue of Theorem 1.11 for GRUs:

Theorem 2.13. For any context-free language L\displaystyle L suppose we relabel and write L=Dn∩R\displaystyle L=D_{n}\cap R for some regular language R\displaystyle R, whose corresponding minimum DFA has r\displaystyle r states. Then there exists a GRU with a hidden layer of size 8+2nr\displaystyle 8+2nr whose (0,(2n+1)−1)\displaystyle(0,(2n+1)^{-1})-language is L\displaystyle L.

Proof. This follows by combining the GRUs from the proofs of Theorems 2.1 and 2.12, as we did for simple RNNs in the proof of Theorem 1.11.∎

Discussion 2.14. A critical idea in this section was to use the fact that tanh(x)=x+O(x2)\displaystyle\text{tanh}(x)=x+O(x^{2}) near x=0\displaystyle x=0, and in fact this idea can be used for any activation function with a well-behaved Taylor series expansion around x=0\displaystyle x=0.

Discussion 2.15. We “cheated” a little bit by allowing ∞\displaystyle\infty edge weights and by having ot=f(ht)\displaystyle o_{t}=f(h_{t}) where f\displaystyle f wasn’t quite linear. However, ∞\displaystyle\infty edge weights make sense in the context of allowing infinite precision, and simple nonlinear functions over the hidden nodes are often used in practice, like the common softmax activation function.

Suggestions for Further Research

We recognize two main avenues for further research. The first is to remove the necessity for infinite edge weights in the proof of Theorem 2.13, and the second is to extend the results of Theorems 1.11 and 2.13 to Turing recognizable languages.

In the proof of Lemma 2.11, edge weights of ∞\displaystyle\infty are necessary for determining whether a hidden node ever becomes negative. Merely using large but finite weights does not suffice, because the values in the hidden state that they will be multiplied with are rapidly decreasing. Their product will vanish, and thus we would not be able to utilize the squashing properties of common activation functions as we did in the proof of Lemma 2.11. Currently we believe that it is possible to prove that GRUs are as computationally powerful as PDAs without using infinite edge weights, but are unaware of a method to do so.

Because to the our knowledge there is no analogue of the Chomsky-Schu¨\displaystyle\ddot{\text{u}}tzenberger Theorem for Turing recognizable languages, it seems difficult to directly extend our methods to prove that recurrent architectures are as computationally powerful as Turing machines. However, just as PDAs can lazily be described as a DFA with an associated stack, it is well-known that Turing machines are equally as powerful as DFAs with associated queues, which can be simulated with two stacks. Such an approach using two counters was used in proofs in , to establish that RNNs with arbitrary precision can emulate Turing machines. We believe that an approach related to this fact could ultimately prove successful, but it would be more useful if set up as in the proofs above in a way that is faithful to the architecture of the neural networks. Counter automata of this sort are also quite unlike the usual implementations found for context-free languages or their extensions for natural languages. Work described in demonstrates that in practice, LSTMs cannot really generalize to recognize the Dyck language D2\displaystyle D_{2}. It remains to investigate whether any recent neural network variation does in fact readily generalize outside its training set to “out of sample” examples. This would be an additional topic for future research.

Consider the RNN described in the proof of Theorem 1.10 for n=2\displaystyle n=2. We will show the evolution of its hidden state as it reads various inputs:

Input: w=(2(1)1(1(2)2)1)2\displaystyle w=(_{2}(_{1})_{1}(_{1}(_{2})_{2})_{1})_{2}

Input: w=(1)1(2(1)1\displaystyle w=(_{1})_{1}(_{2}(_{1})_{1}

Input: w=(2)1(1)2)2\displaystyle w=(_{2})_{1}(_{1})_{2})_{2}

Consider the GRU described in the proof of Theorem 2.12 for n=2\displaystyle n=2 and k=5\displaystyle k=5. We will show the evolution of its hidden state as it reads various inputs:

Input: w=(2(1)1(1(2)2)1)2\displaystyle w=(_{2}(_{1})_{1}(_{1}(_{2})_{2})_{1})_{2}

Input: w=(1)1(2(1)1\displaystyle w=(_{1})_{1}(_{2}(_{1})_{1}

Input: w=(2)1(1)2)2\displaystyle w=(_{2})_{1}(_{1})_{2})_{2}

References