What Recurrent Networks Can Compute
Finite precision, counters and the Chomsky hierarchy in practice.
With unbounded precision a recurrent network can simulate a Turing machine; with the 16 or 32 bits of a real computer it is a finite automaton. Between those two facts lies the question that matters in practice: what do trained networks actually compute? Counting separates LSTMs from GRUs, memory structure separates architectures on the Chomsky hierarchy, and state tracking separates nonlinear recurrences from the linear ones that train in parallel.
What you’ll learn
- Explain why a finite-precision RNN is a finite automaton, and count the states a language requires with Myhill–Nerode classes: 2 for parity, for .
- Construct a counter in an LSTM's cell, explain why squashing RNNs and GRUs cannot count reliably at finite precision, and place architectures in the saturated-RNN hierarchy of Merrill et al. (2020).
- Interpret the Chomsky-hierarchy length-generalization experiments: which architectures generalize at each level, and what the results do and do not show.
- State the state-tracking limits of linear recurrences, and explain how negative eigenvalues and non-diagonal transitions restore parity and other regular languages.
Precision decides
Siegelmann & Sontag (1995) showed that a fixed, finite recurrent network with rational weights and saturated-linear units can simulate any Turing machine in real time, by storing stacks in the digits of its state. After pushes the state must resolve differences of order , so the construction needs unbounded precision. Real networks store each unit in a fixed number of bits: units with bits each can be in at most states. A finite-precision RNN, together with whatever classifier reads its state, is therefore a finite automaton, and the useful questions become how many states a task needs and whether training finds them.
Parity (is the number of 1s odd?) has two classes, "even so far" and "odd so far", so 2 states, one bit, suffice at every length. Counting is different. In the prefixes are pairwise inequivalent ( is in the language but is not), so there are infinitely many classes: the language is not regular, and no fixed finite-precision network recognizes it at every length. A bound makes it regular again, at a price that grows with .
How many states does the smallest complete deterministic automaton for need, counting the "reject forever" state?
Show the answer
22
The classes are: for (11 classes, since after 's the automaton must remember ); "already reading 's, with more 's needed", (10 classes; accepts); and the dead class. Total , which a brute-force minimization confirms. Parity needs 2 states at every length, while bounded counting needs about bits: memory that grows with the range.
This is why length generalization is the test that matters. On short training strings a network can memorize a bounded solution; only a mechanism that extrapolates, such as a counter limited by numerical range rather than by its training data, keeps working on much longer inputs.
Counting: LSTM versus GRU
An LSTM can count exactly in one cell dimension (Weiss, Goldberg & Yahav, 2018). With saturated gates, set the input and forget gates to 1 and the candidate to on an and on a ; on other symbols set the input gate to 0. The cell, a running sum that is never squashed, then holds the count, and the output gate exposes it so a readout can compare it with zero:
A squashing RNN cannot do this: every state lies in , and at finite precision that interval holds only finitely many values, so any counter it builds saturates at a bound. A GRU is no better, because its update is a convex combination of values in and stays there. A ReLU RNN, whose states are unbounded, can count. Weiss and colleagues tested this on 10-unit, one-layer networks. Trained on with , the LSTM recognized the language well up to and used dedicated counting dimensions; the GRU did not capture it even inside its training range, accepting for . Trained on with , the LSTM held up until while the GRU failed early. Gers & Schmidhuber (2001) had already trained LSTMs on such languages.
Why can an LSTM learn to count far beyond its training lengths while a GRU cannot, at finite precision?
- The GRU has fewer parameters per unit
- The GRU's state is a convex combination of bounded values, confined to , so it can take only finitely many values; the LSTM's cell is an unsquashed running sum that can hold any integer up to the float range
- The GRU cannot be trained by backpropagation through time
- The LSTM uses a larger learning rate
Show the answer
B. The GRU's state is a convex combination of bounded values, confined to , so it can take only finitely many values; the LSTM's cell is an unsquashed running sum that can hold any integer up to the float range
Counting needs a state whose number of distinguishable values grows with the input. The cell update with adds without squashing; the GRU interpolates between its old state and a tanh output, so it stays bounded and its counts collide at finite precision.
The Chomsky hierarchy in experiments
Delétang et al. (2023) turned these questions into a benchmark. Fifteen tasks are grouped by the weakest automaton that solves them: regular (R; a finite automaton), deterministic context-free (DCF; a finite automaton plus a stack) and context-sensitive (CS; a linear-bounded tape). Each of several architectures is trained on inputs of length up to 40 and tested on lengths 41 to 500; a model "generalizes" if its accuracy averaged over the test lengths reaches 90% (best of 10 seeds). In all, 20,910 models were trained. Two of the architectures add differentiable memory to a recurrent controller: a Stack-RNN, in the spirit of Joulin & Mikolov (2015), and a Tape-RNN with a memory tape.
| Architecture | R (4 tasks) | DCF (4 tasks) | CS (7 tasks) |
|---|---|---|---|
| RNN | 4 | 0 | 0 |
| LSTM | 4 | 0 | 1 (Bucket Sort, a counting task) |
| Stack-RNN | 4 | 3 | 0 |
| Tape-RNN | 4 | 3 | 4 |
| Transformer | 1 (Even Pairs) | 0 | 1 (Bucket Sort) |
The pattern follows the hierarchy closely: recurrent networks generalize up to the regular level, the Stack-RNN up to the deterministic context-free level and the Tape-RNN into the context-sensitive level, while the LSTM adds counting tasks to the regular ones. The transformers failed to length-generalize on most tasks, including Parity Check (52%, chance level), with every positional encoding the authors tried. Apart from Bucket Sort, which needs only counting, only networks with structured memory generalized on the context-free and context-sensitive tasks.
State tracking and linear recurrences
State tracking means composing a sequence of transformations: following which cup hides the ball after a series of swaps, updating a chess board from moves, or executing code line by line. Its cleanest form is the word problem of a finite group: read group elements and output their product. A finite automaton with one state per group element solves it, and so does a nonlinear RNN with saturating units, since such networks can simulate any finite automaton (a classical equivalence going back to Kleene, 1956, and Minsky, 1967). Linear recurrences, the ones that train with parallel scans, are a different matter.
Even parity, a group of size two, is out of reach for the most common linear recurrences. In Mamba each state is multiplied at each step by with , a number in . Grazzi et al. (2025), building on Sarrof, Veitsman & Hahn (2024), prove that finite-precision linear RNNs whose transition matrices have only positive eigenvalues cannot solve parity. A single negative eigenvalue repairs it.
Take one unit with an input-dependent multiplier, and no additive input. Let the transition be on a 1 and on a 0:
The sign of is the parity, exactly and at every length, and the recurrence is still linear in , so it can be computed with a parallel scan. With multipliers restricted to the product of positive numbers can never change sign, which is the intuition behind the impossibility result.
Non-diagonal transitions go further. DeltaNet's transition with has eigenvalue along and 1 elsewhere, so letting range over instead of allows eigenvalues in . Grazzi et al. prove that counting modulo 3 needs transition matrices that are not triangular, and that products of such generalized Householder matrices, with eigenvalues in , let linear RNNs learn any regular language. Extending the ranges of Mamba and DeltaNet to negative values let both solve parity and improved their state tracking in experiments.
Key takeaways
- Turing completeness needs unbounded precision (Siegelmann & Sontag, 1995); a finite-precision RNN is a finite automaton. Myhill–Nerode classes count the states a language needs: 2 for parity, for , infinitely many for unbounded counting.
- An LSTM cell with and is an exact counter, while squashing RNNs and GRUs keep their states in and cannot count far; trained LSTMs generalize counting well beyond their training range (Weiss et al., 2018), and saturated LSTMs are provably non-rational counter machines (Merrill et al., 2020).
- On length generalization, RNNs reach the regular level, LSTMs add counting, Stack-RNNs reach deterministic context-free tasks and Tape-RNNs context-sensitive ones, while transformers fail on most tasks, including parity (Delétang et al., 2023): evidence about inductive bias, not capacity.
- Linear recurrences such as S4 and Mamba lie in and cannot solve the word problem unless ; with only positive eigenvalues they cannot even do parity, which a negative eigenvalue fixes, and products of generalized Householder transitions reach every regular language (Grazzi et al., 2025).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
An RNN has 64 units, each stored as a 16-bit float. Which statement is correct?
- It is Turing complete, by Siegelmann & Sontag's theorem
- With an LSTM cell it recognizes at every length
- It is a finite automaton with at most states, so it cannot recognize a non-regular language such as at every length
- Its number of reachable states grows without bound as the inputs get longer
Show the answer
C. It is a finite automaton with at most states, so it cannot recognize a non-regular language such as at every length
bits give at most states. Non-regular languages have infinitely many Myhill–Nerode classes, so some long inputs must be confused. In practice the useful question is whether a network's mechanism extends far beyond its training lengths.
Which setting makes one LSTM cell dimension count for each and for each , ignoring other symbols?
- On and : input gate 1, forget gate 1, candidate on and on ; on other symbols input gate 0, forget gate 1
- Forget gate 0 at every step, candidate
- Input gate 0 and forget gate 1 at every step
- Output gate 0 at every step, so the count stays hidden
Show the answer
A. On and : input gate 1, forget gate 1, candidate on and on ; on other symbols input gate 0, forget gate 1
With the cell keeps its value and with it adds , so . Setting on other symbols leaves the count unchanged. Saturated sigmoids and tanh reach these values approximately, which is easy to learn and maintain (Weiss et al., 2018).
In the Chomsky-hierarchy experiments of Delétang et al. (2023), which architecture generalized to longer inputs on context-sensitive tasks such as Duplicate String?
- The LSTM
- The transformer, with a suitable positional encoding
- The Stack-RNN
- The Tape-RNN
Show the answer
D. The Tape-RNN
Only the Tape-RNN, whose controller reads and writes a differentiable memory tape, generalized on Duplicate String, and on four of the seven context-sensitive tasks in all. The LSTM and the transformer generalized only on Bucket Sort, a counting task, and the Stack-RNN stopped at the deterministic context-free level.
Why can a diagonal linear recurrence whose transition values lie in , as in Mamba, not solve parity at finite precision, and what fixes it?
- It has too few state dimensions; a larger state fixes it
- Multiplying by positive numbers never flips the state's sign, so it cannot alternate with each 1; allowing transition values in lets one unit multiply by on every 1
- It cannot represent the input; adding a nonlinearity to the readout fixes it
- Parity is not a regular language, so no recurrent model can solve it
Show the answer
B. Multiplying by positive numbers never flips the state's sign, so it cannot alternate with each 1; allowing transition values in lets one unit multiply by on every 1
The single unit with , computes exactly. Grazzi et al. (2025) prove that positive eigenvalues cannot solve parity at finite precision, and that extending the range to negative values fixes it while keeping the recurrence linear and scannable.
Further reading
- H. T. Siegelmann & E. D. Sontag, "On the computational power of neural nets," Journal of Computer and System Sciences 50(1), 132–150 (1995).
- G. Weiss, Y. Goldberg & E. Yahav, "On the practical computational power of finite precision RNNs for language recognition," ACL (2018).
- W. Merrill, G. Weiss, Y. Goldberg, R. Schwartz, N. A. Smith & E. Yahav, "A formal hierarchy of RNN architectures," ACL (2020).
- G. Delétang, A. Ruoss, J. Grau-Moya, T. Genewein, L. K. Wenliang, E. Catt, C. Cundy, M. Hutter, S. Legg, J. Veness & P. A. Ortega, "Neural networks and the Chomsky hierarchy," ICLR (2023).
- W. Merrill, J. Petty & A. Sabharwal, "The illusion of state in state-space models," ICML (2024).
- R. Grazzi, J. Siems, A. Zela, J. K. H. Franke, F. Hutter & M. Pontil, "Unlocking state-tracking in linear RNNs through negative eigenvalues," ICLR (2025).