On the Practical Computational Power of Finite Precision RNNs for Language Recognition
Gail Weiss, Yoav Goldberg, Eran Yahav
Introduction
Recurrent Neural Network (RNNs) emerge as very strong learners of sequential data. A famous result by Siegelmann and Sontag Siegelmann and Sontag (1992, 1994), and its extension in Siegelmann (1999), demonstrates that an Elman-RNN Elman (1990) with a sigmoid activation function, rational weights and infinite precision states can simulate a Turing-machine in real-time, making RNNs Turing-complete. Recently, Chen et al Chen et al. (2017) extended the result to the ReLU activation function. However, these constructions (a) assume reading the entire input into the RNN state and only then performing the computation, using unbounded time; and (b) rely on having infinite precision in the network states. As argued by Chen et al Chen et al. (2017), this is not the model of RNN computation used in NLP applications. Instead, RNNs are often used by feeding an input sequence into the RNN one item at a time, each immediately returning a state-vector that corresponds to a prefix of the sequence and which can be passed as input for a subsequent feed-forward prediction network operating in constant time. The amount of tape used by a Turing machine under this restriction is linear in the input length, reducing its power to recognition of context-sensitive language. More importantly, computation is often performed on GPUs with 32bit floating point computation, and there is increasing evidence that competitive performance can be achieved also for quantized networks with 4-bit weights or fixed-point arithmetics Hubara et al. (2016). The construction of Siegelmann (1999) implements pushing 0 into a binary stack by the operation . This allows pushing roughly 15 zeros before reaching the limit of the 32bit floating point precision. Finally, RNN solutions that rely on carefully orchestrated mathematical constructions are unlikely to be found using backpropagation-based training.
In this work we restrict ourselves to input-bound recurrent neural networks with finite-precision states (IBFP-RNN), trained using back-propagation. This class of networks is likely to coincide with the networks one can expect to obtain when training RNNs for NLP applications. An IBFP Elman-RNN is finite state. But what about other RNN variants?
In particular, we consider the Elman RNN (SRNN) Elman (1990) with squashing and with ReLU activations, the Long Short-Term Memory (LSTM) Hochreiter and Schmidhuber (1997) and the Gated Recurrent Unit (GRU) Cho et al. (2014); Chung et al. (2014).
The common wisdom is that the LSTM and GRU introduce additional gating components that handle the vanishing gradients problem of training SRNNs, thus stabilizing training and making it more robust. The LSTM and GRU are often considered as almost equivalent variants of each other.
We show that in the input-bound, finite-precision case, there is a real difference between the computational capacities of the LSTM and the GRU: the LSTM can easily perform unbounded counting, while the GRU (and the SRNN) cannot. This makes the LSTM a variant of a k-counter machine Fischer et al. (1968), while the GRU remains finite-state. Interestingly, the SRNN with ReLU activation followed by an MLP classifier also has power similar to a k-counter machine.
These results suggest there is a class of formal languages that can be recognized by LSTMs but not by GRUs. In section 5, we demonstrate that for at least two such languages, the LSTM manages to learn the desired concept classes using back-propagation, while using the hypothesized control structure. Figure 1 shows the activations of 10-d LSTM and GRU trained to recognize the languages and . It is clear that the LSTM learned to dedicate specific dimensions for counting, in contrast to the GRU.Is the ability to perform unbounded counting relevant to “real world” NLP tasks? In some cases it might be. For example, processing linearized parse trees Vinyals et al. (2015); Choe and Charniak (2016); Aharoni and Goldberg (2017) requires counting brackets and nesting levels. Indeed, previous works that process linearized parse trees report using LSTMs and not GRUs for this purpose. Our work here suggests that this may not be a coincidence.
The RNN Models
An RNN is a parameterized function that takes as input an input vector and a state vector and returns a state vector :
The RNN is applied to a sequence by starting with an initial vector (often the 0 vector) and applying R repeatedly according to equation (1). Let be an input vocabulary (alphabet), and assume a mapping from every vocabulary item to a vector (achieved through a 1-hot encoding, an embedding layer, or some other means). Let denote the state vector resulting from the application of to the sequence . An RNN recognizer (or RNN acceptor) has an additional function mapping states to . Typically, is a log-linear classifier or multi-layer perceptron. We say that an RNN recognizes a language L if returns 1 for all and only words .
In the Elman-RNN Elman (1990), also called the Simple RNN (SRNN), the function takes the form of an affine transform followed by a tanh nonlinearity:
Elman-RNNs are known to be at-least finite-state. Siegelmann Siegelmann (1996) proved that the tanh can be replaced by any other squashing function without sacrificing computational power.
IRNN
The IRNN model, explored by Le et al. (2015), replaces the activation with a non-squashing ReLU:
The computational power of such RNNs (given infinite precision) is explored in Chen et al. (2017).
Gated Recurrent Unit (GRU)
In the GRU Cho et al. (2014), the function incorporates a gating mechanism, taking the form:
Where is the sigmoid function and is the Hadamard product (element-wise product).
Long Short Term Memory (LSTM)
In the LSTM Hochreiter and Schmidhuber (1997), uses a different gating component configuration:
where can be either tanh or the identity.
Equivalences
The GRU and LSTM are at least as strong as the SRNN: by setting the gates of the GRU to and we obtain the SRNN computation. Similarly by setting the LSTM gates to ,, and . This is easily achieved by setting the matrices and to 0, and the biases to the (constant) desired gate values.
Thus, all the above RNNs can recognize finite-state languages.
Power of Counting
Power beyond finite state can be obtained by introducing counters. Counting languages and k-counter machines are discussed in depth in Fischer et al. (1968). When unbounded computation is allowed, a 2-counter machine has Turing power. However, for computation bound by input length (real-time) there is a more interesting hierarchy. In particular, real-time counting languages cut across the traditional Chomsky hierarchy: real-time k-counter machines can recognize at least one context-free language (), and at least one context-sensitive one (). However, they cannot recognize the context free language given by the grammar (palindromes).
For our purposes, we consider a simplified variant of k-counter machines (SKCM). A counter is a device which can be incremented by a fixed amount (Inc), decremented by a fixed amount (Dec) or compared to 0 (Comp0). Informally,Formal definition is given in Appendix A. an SKCM is a finite-state automaton extended with counters, where at each step of the computation each counter can be incremented, decremented or ignored in an input-dependent way, and state-transitions and accept/reject decisions can inspect the counters’ states using Comp0. The results for the three languages discussed above hold for the SKCM variant as well, with proofs provided in Appendix A.
RNNs as SKCMs
In what follows, we consider the effect on the state-update equations on a single dimension, . We omit the index for readability.
Finally, the counter values are exposed through , making it trivial to compare the counter’s value to 0.Some further remarks on the LSTM: LSTM supports both increment and decrement in a single dimension. The counting dimensions in are exposed through a function . For both and , it is trivial to do compare 0. Another operation of interest is comparing two counters (for example, checking the difference between them). This cannot be reliably achieved with , due to the non-linearity and saturation properties of the function, but is possible in the case. LSTM can also easily set the value of a counter to 0 in one step. The ability to set the counter to 0 gives slightly more power for real-time recognition, as discussed by Fischer et al. (1968). Relation to known architectural variants: Adding peephole connections Gers and Schmidhuber (2000) essentially sets and allows comparing counters in a stable way. Coupling the input and the forget gates () Greff et al. (2017) removes the single-dimension unbounded counting ability, as discussed for the GRU.
We note that this implementation of the SKCM operations is achieved by saturating the activations to their boundaries, making it relatively easy to reach and maintain in practice.
SRNN
The finite-precision SRNN cannot designate unbounded counting dimensions.
By properly setting U and W, one can get certain dimensions of to update according to the value of , by . However, this counting behavior is within a activation. Theoretically, this means unbounded counting cannot be achieved without infinite precision. Practically, this makes the counting behavior inherently unstable, and bounded to a relatively narrow region. While the network could adapt to set to be small enough such that counting works for the needed range seen in training without overflowing the , attempting to count to larger will quickly leave this safe region and diverge.
IRNN
Finite-precision IRNNs can perform unbounded counting conditioned on input symbols. This requires representing each counter as two dimensions, and implementing Inc as incrementing one dimension, Dec as incrementing the other, and Comp0 as comparing their difference to 0. Indeed, Appendix A in Chen et al. (2017) provides concrete IRNNs for recognizing the languages and . This makes IBFP-RNN with ReLU activation more powerful than IBFP-RNN with a squashing activation. Practically, ReLU-activated RNNs are known to be notoriously hard to train because of the exploding gradient problem.
GRU
Summary
We show that LSTM and IRNN can implement unbounded counting in dedicated counting dimensions, while the GRU and SRNN cannot. This makes the LSTM and IRNN at least as strong as SKCMs, and strictly stronger than the SRNN and the GRU.One can argue that other counting mechanisms—involving several dimensions—are also possible. Intuitively, such mechanisms cannot be trained to perform unbounded counting based on a finite sample as the model has no means of generalizing the counting behavior to dimensions beyond those seen in training. We discuss this more in depth in Appendix B, where we also prove that an SRNN cannot represent a binary counter.
Experimental Results
Can the LSTM indeed learn to behave as a k-counter machine when trained using backpropagation? We show empirically that:
LSTMs can be trained to recognize and .
These LSTMs generalize to much higher than seen in the training set (though not infinitely so).
The trained LSTM learn to use the per-dimension counting mechanism.
The GRU can also be trained to recognize and , but they do not have clear counting dimensions, and they generalize to much smaller than the LSTMs, often failing to generalize correctly even for within their training domain.
Trained LSTM networks outperform trained GRU networks on random test sets for the languages and .
Similar empirical observations regarding the ability of the LSTM to learn to recognize and are described also in Gers and Schmidhuber (2001).
We train 10-dimension, 1-layer LSTM and GRU networks to recognize and . For the training samples went up to and for up to .Implementation in DyNet, using the SGD Optimizer. Positive examples are generated by sampling in the desired range. For negative examples we sample 2 or 3 values independently, and ensuring at least one of them differs from the others. We dedicate a portion of the examples as the dev set, and train up to 100% dev set accuracy.
On , the LSTM generalizes well up to , after which it accumulates a deviation making it reject but recognize for a while, until the deviation grows.These fluctuations occur as the networks do not fully saturate their gates, meaning the LSTM implements an imperfect counter that accumulates small deviations during computation, e.g.: increasing the counting dimension by 0.99 but decreasing only by 0.98. Despite this, we see that the its solution remains much more robust than that found by the GRU — the LSTM has learned the essence of the counting based solution, but its implementation is imprecise. The GRU does not capture the desired concept even within its training domain: accepting for , and also accepting for . It stops accepting for .
On the LSTM recognizes well until . It then starts accepting also . At it stops accepting and switches to accepting , until at some point the deviation grows. The GRU accepts already , and stops accepting for .
Figure 1a plots the activations of the 10 dimensions of the -LSTM for the input . While the LSTM misclassifies this example, the use of the counting mechanism is clear. Figure 1b plots the activation for the LSTM on . Here, again, the two counting dimensions are clearly identified—indicating the LSTM learned the canonical 2-counter solution—although the slightly-imprecise counting also starts to show. In contrast, Figures 1c and 1d show the state values of the GRU-networks. The GRU behavior is much less interpretable than the LSTM. In the case, some dimensions may be performing counting within a bounded range, but move to erratic behavior at around (the network starts to misclassify on sequences much shorter than that). The state dynamics are even less interpretable.
All of this empirically supports our result, showing that IBFP-LSTMs can not only theoretically implement “unbounded” counters, but also learn to do so in practice (although not perfectly), while IBFP-GRUs do not manage to learn proper counting behavior, even when allowing floating point computations.
Conclusions
We show that the IBFP-LSTM can model a real-time SKCM, both in theory and in practice. This makes it more powerful than the IBFP-SRNN and the IBFP-GRU, which cannot implement unbounded counting and are hence restricted to recognizing regular languages. The IBFP-IRNN can also perform input-dependent counting, and is thus more powerful than the IBFP-SRNN.
We note that in addition to theoretical distinctions between architectures, it is important to consider also the practicality of different solutions: how easy it is for a given architecture to discover and maintain a stable behavior in practice. We leave further exploration of this question for future work.
Acknowledgments
The research leading to the results presented in this paper is supported by the European Union’s Seventh Framework Programme (FP7) under grant agreement no. 615688 (PRIME), The Israeli Science Foundation (grant number 1555/15), and The Allen Institute for Artificial Intelligence.
Appendix
Appendix A Simplified K-Counter Machines
We use a simplified variant of the k-counter machines (SKCM) defined in Fischer et al. (1968), which has no autonomous states and makes classification decisions based on a combination of its current state and counter values. This variant consumes input sequences on a symbol by symbol basis, updating at each step its state and its counters, the latter of which may be manipulated by increment, decrement, zero, or no-ops alone, and observed only by checking equivalence to zero. To define the transitions of this model its accepting configurations, we will introduce the following notations:
We now define the model. An SKCM is a tuple containing:
A counter update function We note that in this definition, the counter update function depends only on the input symbol. In practice we see that the LSTM is not limited in this way, and can also update according to some state-input combinations — as can be seen when it it is taught, for instance, the language We do not explore this here however, leaving a more complete characterization of the learnable models to future work.
A set of accepting maskedi.e., counters are observed only by zero-ness. configurations
The language recognized by a k-counter machine is the set of words for which the machine reaches an accepting configuration — a configuration for which .
Note that while the counters can and are increased to various non-zero values, the transition function and the accept/reject classification of the configurations observe only their zero-ness.
We show that the SKCM model can recognize the context-free and context-sensitive languages and , but not the context free language of palindromes, meaning its computational power differs from the language classes defined in the Chomsky hierarchy. Similar proofs appear in Fischer et al. (1968) for their variant of the k-counter machine.
We define the following SKCM over the alphabet :
for any :
The state is a rejecting sink state, and the states and keep track of whether the sequence is currently in the “” or “” phase. If an is seen after moving to the phase, the machine moves to (and stays in) the rejecting state. The counter is increased on input and decreased on input , and the machine accepts only sequences that reach the state with counter value zero, i.e., that have increased and decreased the counter an equal number of times, without switching from to . It follows easily that this machine recognizes exactly the language .
We define the following SKCM over the alphabet . As its state transition function ignores the counter values, we use the shorthand for , for all .
for any :
By similar reasoning as that for , we see that this machine recognizes exactly the language . We note that this construction can be extended to build an SKCM for any language of the sort , using counters and states.
Palindromes:
We prove that no SKCM can recognize the language of palindromes defined over the alphabet by the grammar . The intuition is that in order to correctly recognize this language in an one-way setting, one must be able to reach a unique configuration for every possible input sequence over (requiring an exponential number of reachable configurations), whereas for any SKCM, the number of reachable configurations is always polynomial in the input length.This will hold even if the counter update function can rely on any state-input combination.
Let be an SKCM with counters. As its counters are only manipulated by steps of 1 or resets, the maximum and minimum values that each counter can attain on any input are and , and in particular the total number of possible values a counter could reach at the end of input is . This means that the total number of possible configurations could reach on input of length is .
is polynomial in , and so there exists a value for which the number of input sequences of length over — — is greater than . It follows by the pigeonhole principle that there exist two input sequences for which reaches the same configuration. This means that for any suffix , and in particular for where is the reverse of , classifies and identically—despite the fact that is in the language and is not. This means that necessarily does not recognize this palindrome language, and ultimately that no such exists.
Note that this proof can be easily generalized to any palindrome grammar over or more characters, with or without a clear ‘midpoint’ marker.
Appendix B Impossibility of Counting in Binary
While we have seen that the SRNN and GRU cannot allocate individual counting dimensions, the question remains whether they can count using a more elaborate mechanism, perhaps over several dimensions. We show here that one such mechanism — a binary counter — is not implementable in the SRNN.
For the purposes of this discussion, we first define a binary counter in an RNN.
In an RNN with hidden state values in the range , the binary interpretation of a sequence of dimensions of its hidden state is the binary number obtained by replacing each positive hidden value in the sequence with a ‘1’ and each negative value with a ‘0’. For instance: the binary interpretation of the dimensions 3,0,1 in the hidden state vector is 110, i.e., 6.
Binary Counting
We say that the dimensions in an RNN’s hidden state implement a binary counter in the RNN if, in every transition, their binary interpretation either increases, decreases, resets to 0, or doesn’t change.We note that the SKCMs presented here are more restricted in their relation between counter action and transition, but prefer here to give a general definition. Our proof will be relevant even within the restrictions.
A similar pair of definitions can be made for state values in the range .
We first note intuitively that an SRNN would not generalize binary counting to a counter with dimensions beyond those seen in training — as it would have no reason to learn the ‘carry’ behavior between the untrained dimensions. We prove further that we cannot reasonably implement such counters regardless.
We now present a proof sketch that a single-layer SRNN with hidden size cannot implement an -dimensional binary counter that will consistently increase on one of its input symbols. After this, we will prove that even with helper dimensions, we cannot implement a counter that will consistently increase on one input token and decrease on another — as we might want in order to classify the language of all words for which .Of course a counter could also be ‘decreased’ by incrementing a parallel, ‘negative’ counter, and implementing compare-to-zero as a comparison between these two. As intuitively no RNN could generalize binary counting behavior to dimensions not used in training, this approach could quickly find both counters outside of their learned range even on a sequence where the difference between them is never larger than in training.
Consistently Increasing Counter: The proof relies on the linearity of the affine transform , and the fact that ‘carry’ is a non-linear operation. We work with state values in the range , but the proof can easily be adapted to by rewriting as , where is a vector with values in the range .
Suppose we have a single-layer SRNN with hidden size , such that its entire hidden state represents a binary counter that increases every time it receives the input symbol . We denote by the embedding of , and assume w.l.o.g. that the hidden state dimensions are ordered from MSB to LSB, e.g. the hidden state vector represents the number 110=6.
Recall that the binary interpretation of the hidden state relies only on the signs of its values. We use and to denote ‘some’ positive or negative value, respectively. Then the number 6 can be represented by any state vector .
Recall also that the SRNN state transition is
As tanh is sign-preserving, this simplifies to:
Noting the linearity of matrix multiplication and that , we obtain:
Similarly, for and , we obtain
We note that this proof does not extend to the case where additional, non counting dimensions are added to the RNN — at least not without further assumptions, such as the assumption that the counter behave correctly for all values of these dimensions, reachable and unreachable. One may argue then that, with enough dimensions, it could be possible to implement a consistently increasing binary counter on a subset of the SRNN’s state.(By storing processing information on the additional, ‘helper’ dimensions) We now show a counting mechanism that cannot be implemented even with such ‘helper’ dimensions.
Bi-Directional Counter: We show that for , no SRNN can implement an -dimensional binary counter that increases for one token, , and decreases for another, . As before, we show the proof explicitly for , and note that it can be simply expanded to any by padding.
Assume by contradiction we have such an SRNN, with dimensions, and assume w.l.o.g. that a counter is encoded along the first 3 of these. We use the shorthand to show the values of the counter dimensions explicitly while abstracting the remaining state dimensions, e.g. we write the hidden state as where .
(i.e., increases the counter and updates the additional dimensions to the values , while decreases and updates to .) Removing the sign-preserving function tanh we obtain the constraints
i.e. , and in particular . Now consider a reachable state for which the counter value is . Similarly to before, we now obtain
from which we get , and in particular , a contradiction to the previous statement. Again we conclude that no such SRNN can exist.