Simple Algorithm
Deep Learning · Emergence · 28 min

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, 2m+22m+2 for {akbk:k≤m}\{a^kb^k:k\le m\}.
  • 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 mm pushes the state must resolve differences of order 4−m4^{-m}, so the construction needs unbounded precision. Real networks store each unit in a fixed number of bits: nn units with bb bits each can be in at most 2nb2^{nb} 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 {akbk:k≥0}\{a^kb^k:k\ge0\} the prefixes a0,a1,a2,…a^0,a^1,a^2,\dots are pairwise inequivalent (aibia^ib^i is in the language but ajbia^jb^i 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 k≤mk\le m makes it regular again, at a price that grows with mm.

Predict before you look

How many states does the smallest complete deterministic automaton for L10={akbk:0≤k≤10}L_{10}=\{a^kb^k:0\le k\le10\} need, counting the "reject forever" state?

Show the answer

22

The classes are: aka^k for k=0,…,10k=0,\dots,10 (11 classes, since after kk aa's the automaton must remember kk); "already reading bb's, with rr more bb's needed", r=0,…,9r=0,\dots,9 (10 classes; r=0r=0 accepts); and the dead class. Total 2m+2=222m+2=22, which a brute-force minimization confirms. Parity needs 2 states at every length, while bounded counting needs about log⁡2(2m)\log_2(2m) 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 +1+1 on an aa and −1-1 on a bb; 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:

ct=ft⏟1 ct−1+it⏟1 gt⏟±1⟹cT=#a−#bc_t=\underbrace{f_t}_{1}\,c_{t-1}+\underbrace{i_t}_{1}\,\underbrace{g_t}_{\pm1}\quad\Longrightarrow\quad c_T=\#a-\#b
Two such dimensions recognize anbncna^nb^nc^n. Integer counts stay exact as long as every integer up to the count is representable, which in 32-bit floats holds up to 2242^{24}, far beyond any training length.

A squashing RNN cannot do this: every state lies in (−1,1)(-1,1), 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 ht=zt⊙ht−1+(1−zt)⊙tanh⁡(⋅)\mathbf h_t=\mathbf z_t\odot\mathbf h_{t-1}+(1-\mathbf z_t)\odot\tanh(\cdot) is a convex combination of values in [−1,1][-1,1] 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 anbna^nb^n with n≤100n\le100, the LSTM recognized the language well up to n=256n=256 and used dedicated counting dimensions; the GRU did not capture it even inside its training range, accepting anbn+1a^nb^{n+1} for n>38n>38. Trained on anbncna^nb^nc^n with n≤50n\le50, the LSTM held up until n=100n=100 while the GRU failed early. Gers & Schmidhuber (2001) had already trained LSTMs on such languages.

Check your understanding

Why can an LSTM learn to count far beyond its training lengths while a GRU cannot, at finite precision?

  1. The GRU has fewer parameters per unit
  2. The GRU's state is a convex combination of bounded values, confined to [−1,1][-1,1], 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
  3. The GRU cannot be trained by backpropagation through time
  4. The LSTM uses a larger learning rate
Show the answer

B. The GRU's state is a convex combination of bounded values, confined to [−1,1][-1,1], 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 ct=ftct−1+itgtc_t=f_tc_{t-1}+i_tg_t with ft=it=1f_t=i_t=1 adds ±1\pm1 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.

ArchitectureR (4 tasks)DCF (4 tasks)CS (7 tasks)
RNN400
LSTM401 (Bucket Sort, a counting task)
Stack-RNN430
Tape-RNN434
Transformer1 (Even Pairs)01 (Bucket Sort)
Number of tasks per level on which each architecture generalized (Delétang et al., 2023). Regular: Even Pairs, Modular Arithmetic (Simple), Parity Check, Cycle Navigation. DCF: Stack Manipulation, Reverse String, Modular Arithmetic, Solve Equation. CS: Duplicate String, Missing Duplicate, Odds First, Binary Addition, Binary Multiplication, Compute Sqrt, Bucket Sort. No architecture generalized on Solve Equation, Binary Multiplication or Compute Sqrt.

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 g1,…,gTg_1,\dots,g_T 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 eΔtae^{\Delta_ta} with a<0a<0, a number in (0,1)(0,1). 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.

DerivationParity with one negative eigenvalue
  1. ht=a(xt) ht−1,a(1)=−1, a(0)=+1⟹hT=∏t=1Ta(xt)=(−1)#{t: xt=1}h_t=a(x_t)\,h_{t-1},\qquad a(1)=-1,\ a(0)=+1\qquad\Longrightarrow\qquad h_T=\prod_{t=1}^Ta(x_t)=(-1)^{\#\{t:\,x_t=1\}}

    Take one unit with an input-dependent multiplier, h0=1h_0=1 and no additive input. Let the transition be −1-1 on a 1 and +1+1 on a 0:

  2. a(xt)∈(0,1] for all t⟹sign⁡(hT)=sign⁡(h0)a(x_t)\in(0,1]\ \text{for all }t\quad\Longrightarrow\quad\operatorname{sign}(h_T)=\operatorname{sign}(h_0)

    The sign of hTh_T is the parity, exactly and at every length, and the recurrence is still linear in hh, so it can be computed with a parallel scan. With multipliers restricted to (0,1](0,1] the product of positive numbers can never change sign, which is the intuition behind the impossibility result.

  3. eig⁡(I−βkk⊤)={1−β, 1,…,1},β∈(0,2) ⟹ 1−β∈(−1,1)\operatorname{eig}\big(I-\beta\mathbf k\mathbf k^\top\big)=\{1-\beta,\ 1,\dots,1\},\qquad\beta\in(0,2)\ \Longrightarrow\ 1-\beta\in(-1,1)

    Non-diagonal transitions go further. DeltaNet's transition I−βkk⊤I-\beta\mathbf k\mathbf k^\top with ∥k∥=1\lVert\mathbf k\rVert=1 has eigenvalue 1−β1-\beta along k\mathbf k and 1 elsewhere, so letting β\beta range over (0,2)(0,2) instead of (0,1)(0,1) allows eigenvalues in (−1,1)(-1,1). 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 [−1,1][-1,1], 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, 2m+22m+2 for {akbk:k≤m}\{a^kb^k:k\le m\}, infinitely many for unbounded counting.
  • An LSTM cell with i=f=1i=f=1 and g=±1g=\pm1 is an exact counter, while squashing RNNs and GRUs keep their states in [−1,1][-1,1] 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 TC0\mathsf{TC}^0 and cannot solve the S5S_5 word problem unless TC0=NC1\mathsf{TC}^0=\mathsf{NC}^1; 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.

Question 1

An RNN has 64 units, each stored as a 16-bit float. Which statement is correct?

  1. It is Turing complete, by Siegelmann & Sontag's theorem
  2. With an LSTM cell it recognizes {akbk}\{a^kb^k\} at every length
  3. It is a finite automaton with at most 210242^{1024} states, so it cannot recognize a non-regular language such as {akbk:k≥0}\{a^kb^k:k\ge0\} at every length
  4. 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 210242^{1024} states, so it cannot recognize a non-regular language such as {akbk:k≥0}\{a^kb^k:k\ge0\} at every length

64×16=1,02464\times16=1{,}024 bits give at most 210242^{1024} 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.

Question 2

Which setting makes one LSTM cell dimension count +1+1 for each aa and −1-1 for each bb, ignoring other symbols?

  1. On aa and bb: input gate 1, forget gate 1, candidate +1+1 on aa and −1-1 on bb; on other symbols input gate 0, forget gate 1
  2. Forget gate 0 at every step, candidate ±1\pm1
  3. Input gate 0 and forget gate 1 at every step
  4. Output gate 0 at every step, so the count stays hidden
Show the answer

A. On aa and bb: input gate 1, forget gate 1, candidate +1+1 on aa and −1-1 on bb; on other symbols input gate 0, forget gate 1

With ft=1f_t=1 the cell keeps its value and with it=1i_t=1 it adds gt=±1g_t=\pm1, so ct=#a−#bc_t=\#a-\#b. Setting it=0i_t=0 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).

Question 3

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?

  1. The LSTM
  2. The transformer, with a suitable positional encoding
  3. The Stack-RNN
  4. 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.

Question 4

Why can a diagonal linear recurrence whose transition values lie in (0,1)(0,1), as in Mamba, not solve parity at finite precision, and what fixes it?

  1. It has too few state dimensions; a larger state fixes it
  2. Multiplying by positive numbers never flips the state's sign, so it cannot alternate with each 1; allowing transition values in (−1,1)(-1,1) lets one unit multiply by −1-1 on every 1
  3. It cannot represent the input; adding a nonlinearity to the readout fixes it
  4. 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 (−1,1)(-1,1) lets one unit multiply by −1-1 on every 1

The single unit ht=a(xt)ht−1h_t=a(x_t)h_{t-1} with a(1)=−1a(1)=-1, a(0)=+1a(0)=+1 computes (−1)#1(-1)^{\#1} 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).