On the Turing Completeness of Modern Neural Network Architectures

Jorge Pérez, Javier Marinković, Pablo Barceló

Introduction

There is an increasing interest in designing neural network architectures capable of learning algorithms from examples (Graves et al., 2014; Grefenstette et al., 2015; Joulin & Mikolov, 2015; Kaiser & Sutskever, 2016; Kurach et al., 2016; Dehghani et al., 2018). A key requirement for any such an architecture is thus to have the capacity of implementing arbitrary algorithms, that is, to be Turing complete. Turing completeness often follows for these networks as they can be seen as a control unit with access to an unbounded memory; as such, they are capable of simulating any Turing machine.

On the other hand, the work by Siegelmann & Sontag (1995) has established a different way of looking at the Turing completeness of neural networks. In particular, their work establishes that recurrent neural networks (RNNs) are Turing complete even if only a bounded number of resources (i.e., neurons and weights) is allowed. This is based on two conditions: (1) the ability of RNNs to compute internal dense representations of the data, and (2) the mechanisms they use for accessing such representations. Hence, the view proposed by Siegelmann & Sontag shows that it is possible to release the full computational power of RNNs without arbitrarily increasing its model complexity.

Most of the early neural architectures proposed for learning algorithms correspond to extensions of RNNs – e.g., Neural Turing Machines (Graves et al., 2014) –, and hence they are Turing complete in the sense of Siegelmann & Sontag. However, a recent trend has shown the benefits of designing networks that manipulate sequences but do not directly apply a recurrence to sequentially process their input symbols. Architectures based on attention or convolutions are two prominent examples of this approach. In this work we look at the problem of Turing completeness à la Siegelmann & Sontag for two of the most paradigmatic models exemplifying these features: the Transformer (Vaswani et al., 2017) and the Neural GPU (Kaiser & Sutskever, 2016).

The main contribution of our paper is to show that the Transformer and the Neural GPU are Turing complete based on their capacity to compute and access internal dense representations of the data. In particular, neither the Transformer nor the Neural GPU requires access to an external additional memory to become Turing complete. Thus the completeness holds for bounded architectures (bounded number of neurons and parameters). To prove this we assume that internal activations are represented as rational numbers with arbitrary precision. For the case of the Transformer we provide a direct simulation of a Turing machine, while for the case of the Neural GPU our result follows by simulating standard sequence-to-sequence RNNs. Our study also reveals some minimal sets of elements needed to obtain these completeness results. The computational power of Transformers and of Neural GPUs has been compared in the current literature (Dehghani et al., 2018), but both are only informally used. Our paper provides a formal way of approaching this comparison.

For the sake of space, we only include sketch of some proofs in the body of the paper. The details for every proof can be found in the appendix.

The study of the computational power of neural networks can be traced back to McCulloch & Pitts (1943) which established an analogy between neurons with hard-threshold activations and first order logic sentences, and Kleene (1956) that draw a connection between neural networks and finite automata. As mentioned earlier, the first work showing the Turing completeness of finite neural networks with linear connections was carried out by Siegelmann & Sontag (1992; 1995). Since being Turing complete does not ensure the ability to actually learn algorithms in practice, there has been an increasing interest in enhancing RNNs with mechanisms for supporting this task. One strategy has been the addition of inductive biases in the form of external memory, being the Neural Turing Machine (NTM) (Graves et al., 2014) a paradigmatic example. To ensure that NTMs are differentiable, their memory is accessed via a soft attention mechanism (Bahdanau et al., 2014). Other examples of architectures that extend RNNs with memory are the Stack-RNN (Joulin & Mikolov, 2015), and the (De)Queue-RNNs (Grefenstette et al., 2015). By Siegelmann & Sontag’s results, all these architectures are Turing complete.

The Transformer architecture (Vaswani et al., 2017) is almost exclusively based on the attention mechanism, and it has achieved state of the art results on many language-processing tasks. While not initially designed to learn general algorithms, Dehghani et al. (2018) have advocated the need for enriching its architecture with several new features as a way to learn general procedures in practice. This enrichment is motivated by the empirical observation that the original Transformer architecture struggles to generalize to input of lengths not seen during training. We, in contrast, show that the original Transformer architecture is Turing complete, based on different considerations. These results do not contradict each other, but show the differences that may arise between theory and practice. For instance, Dehghani et al. (2018) assume fixed precision, while we allow arbitrary internal precision during computation. We think that both approaches can be complementary as our theoretical results can shed light on what are the intricacies of the original architecture, which aspects of it are candidates for change or improvement, and which others are strictly needed. For instance, our proof uses hard attention while the Transformer is often trained with soft attention (Vaswani et al., 2017). See Section 3.3 for a discussion on these differences.

The Neural GPU is an architecture that mixes convolutions and gated recurrences over tridimensional tensors. It has been shown that NeuralGPUs are powerful enough to learn decimal multiplication from examples (Freivalds & Liepins, 2018), being the first neural architecture capable of solving this problem end-to-end. The similarity of Neural GPUs and cellular automata has been used as an argument to state the Turing completeness of the architecture (Kaiser & Sutskever, 2016; Price et al., 2016). Cellular automata are Turing complete (Smith III, 1971; Ollinger, 2012) and their completeness is established assuming an unbounded number of cells. In the Neural GPU architecture, in contrast, the number of cells that can be used during a computation is proportional to the size of the input sequence (Kaiser & Sutskever, 2016). One can cope with the need for more cells by padding the Neural GPU input with additional (dummy) symbols, as much as needed for a particular computation. Nevertheless, this is only a partial solution, as for a Turing-complete model of computation, one cannot decide a priori how much memory is needed to solve a particular problem. Our results in this paper are somehow orthogonal to the previous argument; we show that one can leverage the dense representations of the Neural GPU cells to obtain Turing completeness without requiring to add cells beyond the ones used to store the input.

Preliminaries

Finally, a class N\mathcal{N} of seq-to-seq neural network architectures defines the class LN\mathcal{L}_{\mathcal{N}} composed of all the languages accepted by language recognizers that use networks in N\mathcal{N}. From these notions, the formalization of Turing completeness of a class N\mathcal{N} naturally follows.

A class N\mathcal{N} of seq-to-seq neural network architectures is Turing Complete if LN\mathcal{L}_{\mathcal{N}} is exactly the class of languages recognized by Turing machines.

where V,W,U,R{\bm{V}},{\bm{W}},{\bm{U}},{\bm{R}} are matrices, b1{\bm{b}}_{1} and b2{\bm{b}}_{2} are vectors, O(⋅)O(\cdot) is an output function, and f1f_{1} and f2f_{2} are activations functions. Equation (2) is called the RNN-encoder and (3) the RNN-decoder.

The next Theorem follows by inspection of the proof by Siegelmann & Sontag (1992; 1995) after adapting it to our formalization of encoder-decoder RNNs.

The class of encoder-decoder RNNs is Turing complete. Turing completeness holds even if we restrict to the class in which R{\bm{R}} is the zero matrix, b1{\bm{b}}_{1} and b2{\bm{b}}_{2} are the zero vector, O(⋅)O(\cdot) is the identity function, and f1f_{1} and f2f_{2} are the piecewise-linear sigmoidal activation σ\sigma.

The Transformer architecture

In this section we present a formalization of the Transformer architecture (Vaswani et al., 2017), abstracting away from specific choices of functions and parameters. Our formalization is not meant to produce an efficient implementation of the Transformer, but to provide a simple setting over which its mathematical properties can be established in a formal way.

In practice Q(⋅)Q(\cdot), K(⋅)K(\cdot), V(⋅)V(\cdot) are typically matrix multiplications, and O(⋅)O(\cdot) a feed-forward network. The + xi+\ {\bm{x}}_{i} and + ai+\ {\bm{a}}_{i} summands are usually called residual connections (He et al., 2016a; b). When the particular functions used as parameters are not important, we simply write Z=Enc⁡(X){\bm{Z}}=\operatorname{Enc}({\bm{X}}).

We use (K,V)=TEnc⁡L(X)({\bm{K}},{\bm{V}})=\operatorname{TEnc}_{L}({\bm{X}}) to denote an LL-layer Transformer encoder over the sequence X{\bm{X}}.

A single-layer decoder is similar to a single-layer encoder but with additional attention to an external pair of key-value vectors (Ke,Ve)({{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}}). The input for the single-layer decoder is a sequence Y=(y1,…,yk){\bm{Y}}=({\bm{y}}_{1},\ldots,{\bm{y}}_{k}) plus the external pair (Ke,Ve)({{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}}), and the output is a sequence Z=(z1,…,zk){\bm{Z}}=({\bm{z}}_{1},\ldots,{\bm{z}}_{k}). When defining a decoder layer we denote by Yj{\bm{Y}}_{j} the sequence (y1,…,yj)({\bm{y}}_{1},\ldots,{\bm{y}}_{j}), for 1≤j≤k1\leq j\leq k. The layer is also parameterized by four functions Q(⋅)Q(\cdot), K(⋅)K(\cdot), V(⋅)V(\cdot) and O(⋅)O(\cdot) and is defined as follows.

Notice that the first (self) attention over (K(Yi),V(Yi))(K({\bm{Y}}_{i}),V({\bm{Y}}_{i})) considers the subsequence of Y{\bm{Y}} only until index ii and is used to generate a query pi{\bm{p}}_{i} to attend the external pair (Ke,Ve)({{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}}). We denote the single-decoder layer by Dec⁡((Ke,Ve),Y;θ)\operatorname{Dec}(({{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}}),{\bm{Y}};{\bm{\theta}}).

We use z=TDec⁡L((Ke,Ve),Y){\bm{z}}=\operatorname{TDec}_{L}(({{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}}),{\bm{Y}}) to denote an LL-layer Transformer decoder.

We denote the output sequence of the transformer as Y=(y1,y2,…,yr)=Trans⁡(X,y0,r){\bm{Y}}=({\bm{y}}_{1},{\bm{y}}_{2},\ldots,{\bm{y}}_{r})=\operatorname{Trans}({\bm{X}},{\bm{y}}_{0},r).

1 Invariance under proportions

The Transformer, as defined above, is order-invariant: two input sequences that are permutations of each other produce exactly the same output. This is a consequence of the following property of the attention function: if K=(k1,…,kn){\bm{K}}=({\bm{k}}_{1},\ldots,{\bm{k}}_{n}), V=(v1,…,vn){\bm{V}}=({\bm{v}}_{1},\ldots,{\bm{v}}_{n}), and π:{1,…,n}→{1,…,n}\pi:\{1,\dots,n\}\to\{1,\dots,n\} is a permutation, then Att⁡(q,K,V)=Att⁡(q,π(K),π(V))\operatorname{Att}({\bm{q}},{\bm{K}},{\bm{V}})=\operatorname{Att}({\bm{q}},\pi({\bm{K}}),\pi({\bm{V}})) for every query q{\bm{q}}. This weakness has motivated the need for including information about the order of the input sequence by other means; in particular, this is often achieved by using the so-called positional encodings (Vaswani et al., 2017; Shaw et al., 2018), which we study below.

But before going into positional encodings, a natural question is what languages the Transformer can recognize without them. As a standard yardstick we use the well-studied class of regular languages, i.e., languages recognized by finite automata. Order-invariance implies that not every regular language can be recognized by a Transformer network. As an example, there is no Transformer network that can recognize the regular language (ab)∗(ab)^{*}, as the latter is not order-invariant. A reasonable question then is whether the Transformer can express all regular languages which are order-invariant. It is possible to show that this is not the case by proving that the Transformer actually satisfies a stronger invariance property, which we call proportion invariance.

For a string w∈Σ∗w\in\Sigma^{*} and a symbol a∈Σa\in\Sigma, we use prop⁡(a,w)\operatorname{prop}(a,w) to denote the ratio between the number of times that aa appears in ww and the length of ww. Consider now the set PropInv⁡(w)={u∈Σ∗∣prop⁡(a,w)=prop⁡(a,u) for every a∈Σ}\operatorname{PropInv}(w)=\{u\in\Sigma^{*}\mid\operatorname{prop}(a,{w})=\operatorname{prop}({a},{u})\text{ for every }a\in\Sigma\}.

As an immediate corollary we obtain the following.

Consider the order-invariant regular language L={w∈{a,b}∗∣L=\{w\in\{a,b\}^{*}\mid ww has an even number of aa symbols}\}. Then LL cannot be recognized by a Transformer network.

On the other hand, languages recognized by Transformer networks are not necessarily regular.

There is a Transformer network that recognizes the non-regular language S={w∈{a,b}∗∣S=\{w\in\{a,b\}^{*}\mid ww has strictly more symbols aa than symbols b}b\}.

That is, the computational power of Transformer networks without positional encoding is both rather weak (they do not even contain order-invariant regular languages) and not so easy to capture (as they can express counting properties that go beyond regularity). As we show in the next section, the inclusion of positional encodings radically changes the picture.

2 Positional Encodings and Completeness of the Transformer

to the Transformer encoder. Similarly, the Transformer decoder instead of receiving the sequence Y=(y0,y1,…,yt){\bm{Y}}=({\bm{y}}_{0},{\bm{y}}_{1},\ldots,{\bm{y}}_{t}) as input, it receives now the sequence

As for the case of the embedding functions, we require the positional encoding pos⁡(i)\operatorname{pos}(i) to be computable by a Turing machine working in linear time w.r.t. the size (in bits) of ii.

The main result of this section is the completeness of Transformers with positional encodings.

The class of Transformer networks with positional encodings is Turing complete.

We show that for every Turing machine M=(Q,Σ,δ,qinit,F)M=(Q,\Sigma,\delta,q_{\text{init}},F) there exists a transformer that simulates the complete execution of MM. We represent a string w=s1s2⋯sn∈Σ∗w=s_{1}s_{2}\cdots s_{n}\in\Sigma^{*} as a sequence X{\bm{X}} of one-hot vectors with their corresponding positional encodings. Denote by q(t)∈Qq^{(t)}\in Q the state of MM at time tt when processing ww, and s(t)∈Σs^{(t)}\in\Sigma the symbol under MM’s head at time tt. Similarly, v(t)∈Σv^{(t)}\in\Sigma is the symbol written by MM and m(t)∈{←,→}m^{(t)}\in\{\leftarrow,\to\} the head direction. We next describe how to construct a transformer Trans⁡M\operatorname{Trans}_{M} that with input X{\bm{X}} produces a sequence y0,y1,y2,…{\bm{y}}_{0},{\bm{y}}_{1},{\bm{y}}_{2},\ldots such that yi{\bm{y}}_{i} contains information about q(i)q^{(i)} and s(i)s^{(i)} (encoded as one-hot vectors).

The construction and proof goes by induction. Assume the decoder receives y0,…,yt{\bm{y}}_{0},\ldots,{\bm{y}}_{t} such that yi{\bm{y}}_{i} contains q(i)q^{(i)} and s(i)s^{(i)}. To construct yt+1{\bm{y}}_{t+1}, in the first layer we just implement MM’s transition function δ\delta; note that δ(q(i),s(i))=(q(i+1),v(i),m(i))\delta(q^{(i)},s^{(i)})=(q^{(i+1)},v^{(i)},m^{(i)}) thus, we use (q(i),s(i))(q^{(i)},s^{(i)}) to compute (q(i+1),v(i),m(i))(q^{(i+1)},v^{(i)},m^{(i)}) for every ii and store them in the sequence z01,…,zt1{\bm{z}}_{0}^{1},\ldots,{\bm{z}}_{t}^{1}. This computation can be done with a two-layer feed-forward network. For the next layer, lets denote by c(i)c^{(i)} the index of the cell that MM is pointing to at time ii. It can be proved that given z01,…,zt1{\bm{z}}_{0}^{1},\ldots,{\bm{z}}_{t}^{1} one can compute (a representation of) c(i)c^{(i)} and c(i+1)c^{(i+1)} for every i≤ti\leq t with a self-attention layer, and store them in z02,…,zt2{\bm{z}}_{0}^{2},\ldots,{\bm{z}}_{t}^{2}. In particular, zt2{\bm{z}}_{t}^{2} contains c(t+1)c^{(t+1)} which is the index to which MM is going to be pointing to in the next time step. By using the residual connections we also store q(i+1)q^{(i+1)} and v(i)v^{(i)} in zi2{\bm{z}}_{i}^{2}. The final piece of our construction is to compute the symbol that the tape holds at index c(t+1)c^{(t+1)}, that is, the symbol under MM’s head at time t+1t+1. For this we use the following observation: the symbol at index c(t+1)c^{(t+1)} in time t+1t+1 coincides with the last symbol written by MM at index c(t+1)c^{(t+1)}. Thus, we need to find the maximum value i⋆≤ti^{\star}\leq t such that c(i⋆)=c(t+1)c^{(i^{\star})}=c^{(t+1)} and then copy v(i⋆)v^{(i^{\star})} which is the symbol that was written by MM at time step i⋆i^{\star}. This last computation can also be done with a self-attention layer. Thus, we attend directly to position i⋆i^{\star} (hard attention plus positional encodings) and copy v(i⋆)v^{(i^{\star})} which is exactly s(t+1)s^{(t+1)}. We finally copy q(t+1)q^{(t+1)} and s(t+1)s^{(t+1)} into the output to construct yt+1{\bm{y}}_{t+1}. Figure 1 shows a high-level diagram of the decoder computation.

There are several other details in the construction, in particular, at the beginning of the computation (first nn steps), the decoder needs to attend to the encoder and copy the input symbols so they can later be processed as described above. Another detail is when MM reaches a cell that has not been visited before, then the symbol under the head has to be set as #\# (the blank symbol). We show that all these decisions can be implemented with feed-forward networks plus attention. The complete construction uses one encoder layer, three decoder layers and vectors of dimension d=2∣Q∣+4∣Σ∣+11d=2|Q|+4|\Sigma|+11 to store one-hot representations of states, symbols and some additional working space. All details can be found in the appendix. ∎

3 Differences with Vaswani et al. (2017)’s framework

Neural GPUs

where ⊙\odot denotes the element-wise product, and 1\bm{\mathsfit{1}} is a tensor with only 11’s. Neural GPUs force functions U(⋅)U(\cdot) and R(⋅)R(\cdot) to produce a tensor of the same shape as its input with all values in $.Thus,aNeuralGPUresemblesagatedrecurrentunit(Choetal.,2014),with. Thus, a Neural GPU resembles a gated recurrent unit (Cho et al., 2014), with{\bm{\mathsfit{U}}}workingastheupdategateandworking as the update gate and{\bm{\mathsfit{R}}}astheresetgate.Functionsas the reset gate. FunctionsU(\cdot),,R(\cdot),and, andF(\cdot)aredefinedasaconvolutionofitsinputwitha4−dimensionalkernelbankwithshapeare defined as a convolution of its input with a 4-dimensional kernel bank with shape(k_{H},k_{W},d,d)$ plus a bias tensor, followed by a point-wise transformation

with different kernels and biases for U(⋅)U(\cdot), R(⋅)R(\cdot), and F(⋅)F(\cdot).

To have an intuition on how the convolution K∗S{\bm{\mathsfit{K}}}*{\bm{\mathsfit{S}}} works, it is illustrative to think of S{\bm{\mathsfit{S}}} as an (h×w)(h\times w)-grid of (row) vectors and K{\bm{\mathsfit{K}}} as a (kH×kW)(k_{H}\times k_{W})-grid of (d×d)(d\times d) matrices. More specifically, let sij=Si,j,:{\bm{s}}_{ij}={\bm{\mathsfit{S}}}_{i,j,:}, and Kij=Ki,j,:,:{\bm{K}}_{ij}={\bm{\mathsfit{K}}}_{i,j,:,:}, then K∗S{\bm{\mathsfit{K}}}*{\bm{\mathsfit{S}}} is a regular two-dimensional convolution in which scalar multiplication has been replaced by vector-matrix multiplication as in the following expression

where Δ1(u)=u−⌊kH/2⌋−1\Delta_{1}(u)=u-\lfloor{k_{H}/2}\rfloor-1 and Δ2(v)=v−⌊kW/2⌋−1\Delta_{2}(v)=v-\lfloor{k_{W}/2}\rfloor-1. This intuition makes evident the similarity between Neural GPUs and cellular automata: S{\bm{\mathsfit{S}}} is a grid of cells, and in every iteration each cell is updated considering the values of its neighbors according to a fixed rule given by K{\bm{\mathsfit{K}}} (Kaiser & Sutskever, 2016). As customary, we assume zero-padding when convolving outside S{\bm{\mathsfit{S}}}.

The class of uniform Neural GPUs is Turing complete.

The proof above makes use of kernels of shape (2,1,d,d)(2,1,d,d) to obtain Turing completeness. This is, in a sense, optimal, as one can easily prove that Neural GPUs with kernels of shape (1,1,d,d)(1,1,d,d) are not Turing complete, regardless of the size of dd. In fact, for kernels of this shape the value of a cell of S{\bm{\mathsfit{S}}} at time tt depends only on the value of the same cell in time t−1t-1.

Uniform Neural GPUs with circular convolutions are not Turing complete.

Related to this last result is the empirical observation by Price et al. (2016) that Neural GPUs that learn to solve hard problems, e.g., binary multiplication, and which generalize to most of the inputs, struggle with highly symmetric (and nearly periodic) inputs. Actually, Price et al. (2016) exhibit examples of the form 11111111×1111111111111111\times 11111111 failing for all inputs with eight or more 11s. We leave as future work to explore the implications of our theoretical results on this practical observation.

Freivalds & Liepins (2018) simplified Neural GPUs and proved that, by considering piecewise linear activations and bidimensional input tensors instead of the original smooth activations and tridimensional tensors used by Kaiser & Sutskever (2016), it is possible to achieve substantially better results in terms of training time and generalization. Our Turing completeness proof also relies on a bidimensional tensor and uses piecewise linear activations, thus providing theoretical evidence that these simplifications actually retain the full expressiveness of Neural GPUs while simplifying its practical applicability.

Final Remarks and Future Work

We have presented an analysis of the Turing completeness of two popular neural architectures for sequence-processing tasks; namely, the Transformer, based on attention, and the Neural GPU, based on recurrent convolutions. We plan to further refine this analysis in the future. For example, our proof of Turing completeness for the Transformer requires the presence of residual connections, i.e., the +xi+{\bm{x}}_{i}, +ai+{\bm{a}}_{i}, +yi+{\bm{y}}_{i}, and +pi+{\bm{p}}_{i} summands in Equations (6-11), while our proof for Neural GPUs heavily relies on the gating mechanism. We will study whether these features are actually essential to obtain completeness.

We presented general abstract versions of both architectures in order to prove our theoretical results. Although we closely follow their original definitions, some choices for functions and parameters in our positive results are different to the usual choices in practice, most notably, the use of hard attention for the case of the Transformer, and the piecewise linear activation functions for both architectures. As we have mentioned, Freivalds & Liepins (2018) showed that for Neural GPUs piecewise linear activations actually help in practice, but for the case of the Transformer architecture more experimentation is needed to have a conclusive response. This is part of our future work.

Although our results are mostly of theoretical interest, they might lead to observations of practical interest. For example, Chen et al. (2018) have established the undecidability of several practical problems related to probabilistic language modeling with RNNs. This means that such problems can only be approached in practice via heuristics solutions. Many of the results in Chen et al. (2018) are, in fact, a consequence of the Turing completeness of RNNs as established by Siegelmann & Sontag (1995). We plan to study to what extent our analogous undecidability results for Transformers and Neural GPUs imply undecidability for language modeling problems based on these architectures.

Finally, our results rely on being able to compute internal representations of arbitrary precision. It would be interesting to perform a theoretical study of the main properties of both architectures in a setting in which only finite precision is allowed, as have been recently carried out for RNNs (Weiss et al., 2018). We also plan to tackle this problem in our future work.

This work was supported by the Millennium Institute for Foundational Research on Data (IMFD).

References

Appendix A Proofs for Section 2

We first sketch the main idea of Siegelmann & Sontag’s proof. We refer the reader to the original paper for details. Siegelmann & Sontag show how to simulate a two-stack machine MM (and subsequently, a Turing machine) with a single RNN NN with σ\sigma as activation. They first construct a network N1N_{1} that, with 0{\bm{0}} as initial state (h0N1=0{\bm{h}}^{N_{1}}_{0}={\bm{0}}) and with a binary string w∈{0,1}∗w\in\{0,1\}^{*} as input sequence, produces a representation of ww as a rational number and stores it as one of its internal values. Their internal representation of strings encodes every ww as a rational number between and 11. In particular, they use base 44 such that, for example, a string w=100110w=100110 is encoded as (0.311331)4(0.311331)_{4} that is, its encoding is

This representation allows one to easily simulate stack operations as affine transformations plus σ\sigma activations. For instance, if xwx_{w} is the value representing string w=b1b2⋯bnw=b_{1}b_{2}\cdots b_{n} seen as a stack, then the top⁡(w)\operatorname{top}(w) operation can be defined as simply y=σ(4xw−2)y=\sigma(4x_{w}-2), since y=1y=1 if and only if b1=1b_{1}=1, and y=0y=0 if and only if b1=0b_{1}=0. Other stack operations can de similarly simulated. Using this representation, they construct a second network N2N_{2} that simulates the two-stacks machine by using one neuron value to simulate each stack. The input ww for the simulated machine MM is assumed to be at an internal value given to N2N_{2} as an initial state (h0N2)({\bm{h}}_{0}^{N_{2}}). Thus, N2N_{2} expects only zeros as input. Actually, to make N2N_{2} work for rr steps, an input of the form 0r0^{r} should be provided.

Finally, they combine N1N_{1} and N2N_{2} to construct a network NN which expects an input of the following form: (b1,1,0)(b2,1,0)⋯(bn,1,0)(0,0,1)(0,0,0)(0,0,0)⋯(0,0,0)(b_{1},1,0)(b_{2},1,0)\cdots(b_{n},1,0)(0,0,1)(0,0,0)(0,0,0)\cdots(0,0,0). The idea is that the first component contains the input string w=b1b2⋯bnw=b_{1}b_{2}\cdots b_{n}, the second component states when the input is active, and the third component is 11 only when the input is inactive for the first time. Before the input vector (0,0,1)(0,0,1) the network N1N_{1} is working. The input (0,0,1)(0,0,1) is used to simulate a change from N1N_{1} to N2N_{2}, and the rest of input vectors (0,0,0)(0,0,0) are provided to continue with N2N_{2} for as many steps as needed. The number neurons that this construction needs to simulate a machine MM with mm states, is 10m+3010m+30. The idea presented above allows one to linearly simulate MM, that is, each step of MM is simulated with a constant number of steps of the corresponding RNN. Siegelmann & Sontag show that, with a refinement of the above encoding one can simulate MM in real-time, that is, a single step of MM is simulated with a single step of the recurrent network. The 10m+3010m+30 is the bound given by a simulation with slow-down of two. See the original paper for details (Siegelmann & Sontag, 1995).

It is clear that Siegelmann & Sontag’s proof resembles a modern encoder-decoder RNN architecture, where N1N_{1} is the encoder and N2N_{2} is the decoder, thus it is straightforward to use the same construction to provide an RNN encoder-decoder N′N^{\prime} and a language recognizer AA that uses N′N^{\prime} and simulates the two-stacks machine MM. There are some details that is important to notice. Assume that N′N^{\prime} is given by the formulas in Equations (2) and (3). First, since N2N_{2} in the above construction expects no input, we can safely assume that R{\bm{R}} in Equation (3) is the null matrix. Moreover, since AA defines its own embedding function, we can ensure that every vector that we provide for the encoder part of N′N^{\prime} has a 11 in a fixed component, and thus we do not need the bias b1{\bm{b}}_{1} in Equation (2) since it can be simulated with one row of matrix V{\bm{V}}. We can do a similar construction for the bias b2{\bm{b}}_{2} (Equation (3)). Finally, Siegelmann & Sontag show that its construction can be modified such that a particular neuron of N2N_{2}, say n⋆n^{\star}, is always except for the first time an accepting state of MM is reached, in which case n⋆=1n^{\star}=1. Thus, one can consider O(⋅)O(\cdot) (Equation (3)) as the identity function and add to AA the stopping criterion that just checks if n⋆n^{\star} is 11. This completes the proof sketch of Theorem 2.3.

Appendix B Proofs for Section 3

We extend the definition of the function PropInv⁡\operatorname{PropInv} to sequences of vectors. Given a sequence X=(x1,…,xn){\bm{X}}=({\bm{x}}_{1},\ldots,{\bm{x}}_{n}) we use vals⁡(X)\operatorname{vals}({\bm{X}}) to denote the set of all vectors occurring in X{\bm{X}}. Similarly as for strings, we use prop⁡(v,X)\operatorname{prop}({\bm{v}},{\bm{X}}) as the number of times that v{\bm{v}} occurs in X{\bm{X}} divided by the length of X{\bm{X}}. Now we are ready to extend PropInv⁡\operatorname{PropInv} with the following definition:

We now have all the necessary to proceed with the proof of Proposition 3.1. We will prove it by proving the property in (16). Let X=(x1,…,xn){\bm{X}}=({\bm{x}}_{1},\ldots,{\bm{x}}_{n}) be an arbitrary sequence of vectors, and let X′=(x1′,…,xm′)∈PropInv⁡(X){\bm{X}}^{\prime}=({\bm{x}}^{\prime}_{1},\ldots,{\bm{x}}^{\prime}_{m})\in\operatorname{PropInv}({\bm{X}}). Moreover, let Z=(z1,…,zn)=Enc⁡(X;θ){\bm{Z}}=({\bm{z}}_{1},\ldots,{\bm{z}}_{n})=\operatorname{Enc}({\bm{X}};{\bm{\theta}}) and Z′=(z1′,…,zm′)=Enc⁡(X′;θ){\bm{Z}}^{\prime}=({\bm{z}}^{\prime}_{1},\ldots,{\bm{z}}^{\prime}_{m})=\operatorname{Enc}({\bm{X}}^{\prime};{\bm{\theta}}). We first prove the following property:

Lets (i,j)(i,j) be a pair of indices such that xi=xj′{\bm{x}}_{i}={\bm{x}}_{j}^{\prime}. From Equations (6-7) we have that zi=O(ai)+ai{\bm{z}}_{i}=O({\bm{a}}_{i})+{\bm{a}}_{i} where ai=Att⁡(Q(xi),K(X),V(X))+xi{\bm{a}}_{i}=\operatorname{Att}(Q({\bm{x}}_{i}),K({\bm{X}}),V({\bm{X}}))+{\bm{x}}_{i}. Thus, since xi=xj′{\bm{x}}_{i}={\bm{x}}^{\prime}_{j}, in order to prove zi=zj′{\bm{z}}_{i}={\bm{z}}_{j}^{\prime} it is enough to prove that Att⁡(Q(xi),K(X),V(X))=Att⁡(Q(xj′),K(X′),V(X′))\operatorname{Att}(Q({\bm{x}}_{i}),K({\bm{X}}),V({\bm{X}}))=\operatorname{Att}(Q({\bm{x}}_{j}^{\prime}),K({\bm{X}}^{\prime}),V({\bm{X}}^{\prime})). By equations (4-5) and the restriction over the form of normalization functions we have that

with α=∑v∈vals⁡(X)pvXfρ(score⁡(Q(v),K(v)))\alpha=\sum_{{\bm{v}}\in\operatorname{vals}({\bm{X}})}p_{{\bm{v}}}^{{\bm{X}}}f_{\rho}(\operatorname{score}(Q({\bm{v}}),K({\bm{v}}))). By a similar reasoning we can write

Which completes the proof of Property (17) above.

Consider now the complete encoder TEnc⁡\operatorname{TEnc}. Let (K,V)=TEnc⁡(X)({\bm{K}},{\bm{V}})=\operatorname{TEnc}({\bm{X}}) and (K′,V′)=TEnc⁡(X′)({\bm{K}}^{\prime},{\bm{V}}^{\prime})=\operatorname{TEnc}({\bm{X}}^{\prime}), and let q{\bm{q}} be an arbitrary vector. We will prove now that Att⁡(q,K,V)=Att⁡(q,K′,V′)\operatorname{Att}({\bm{q}},{\bm{K}},{\bm{V}})=\operatorname{Att}({\bm{q}},{\bm{K}}^{\prime},{\bm{V}}^{\prime}). By following a similar reasoning as for proving Property (17) (plus induction on the layers of TEnc⁡\operatorname{TEnc}) we obtain that if xi=xj′{\bm{x}}_{i}={\bm{x}}^{\prime}_{j} then ki=kj′{\bm{k}}_{i}={\bm{k}}^{\prime}_{j} and vi=vj′{\bm{v}}_{i}={\bm{v}}^{\prime}_{j}, for every i∈{1,…,n}i\in\{1,\ldots,n\} and j∈{1,…,m}j\in\{1,\ldots,m\}. Thus, there exists a mapping MK:vals⁡(X)→vals⁡(K)M_{K}:\operatorname{vals}({\bm{X}})\to\operatorname{vals}({\bm{K}}) such that MK(xi)=kiM_{K}({\bm{x}}_{i})={\bm{k}}_{i} and MK(xj′)=kj′M_{K}({\bm{x}}^{\prime}_{j})={\bm{k}}^{\prime}_{j} and similarly a mapping MV:vals⁡(X)→vals⁡(V)M_{V}:\operatorname{vals}({\bm{X}})\to\operatorname{vals}({\bm{V}}) such that MV(xi)=viM_{V}({\bm{x}}_{i})={\bm{v}}_{i} and MV(xj′)=vj′M_{V}({\bm{x}}^{\prime}_{j})={\bm{v}}^{\prime}_{j}, for every i∈{1,…,n}i\in\{1,\ldots,n\} and j∈{1,…,m}j\in\{1,\ldots,m\}. Lets focus now on Att⁡(q,K,V)\operatorname{Att}({\bm{q}},{\bm{K}},{\bm{V}}). We have:

with α=∑i=1nfρ(score⁡(q,ki)).\alpha=\sum_{i=1}^{n}f_{\rho}(\operatorname{score}({\bm{q}},{\bm{k}}_{i})). Similarly as before, we can rewrite this as

with α=∑v∈vals⁡(X)pvXfρ(score⁡(q,MK(v))).\alpha=\sum_{{\bm{v}}\in\operatorname{vals}({\bm{X}})}p_{{\bm{v}}}^{{\bm{X}}}f_{\rho}(\operatorname{score}({\bm{q}},M_{K}({\bm{v}}))). Similarly for Att⁡(q,K′,V′)\operatorname{Att}({\bm{q}},{\bm{K}}^{\prime},{\bm{V}}^{\prime}) we have

And finally using that X′∈PropInv⁡(X){\bm{X}}^{\prime}\in\operatorname{PropInv}({\bm{X}}) we obtain

To complete the rest proof, consider Trans⁡(X,y0,r)\operatorname{Trans}({\bm{X}},{\bm{y}}_{0},r) which is defined by the recursion

To prove that Trans⁡(X,y0,r)=Trans⁡(X′,y0,r)\operatorname{Trans}({\bm{X}},{\bm{y}}_{0},r)=\operatorname{Trans}({\bm{X}}^{\prime},{\bm{y}}_{0},r) we use an inductive argument. We know that

Now TDec⁡\operatorname{TDec} only access (K,V)({\bm{K}},{\bm{V}}) via attentions of the form Att⁡(q,K,V)\operatorname{Att}({\bm{q}},{\bm{K}},{\bm{V}}) and for the case of y1{\bm{y}}_{1} the vector q{\bm{q}} can only depend on y0{\bm{y}}_{0}, thus, from Att⁡(q,K,V)=Att⁡(q,K′,V′)\operatorname{Att}({\bm{q}},{\bm{K}},{\bm{V}})=\operatorname{Att}({\bm{q}},{\bm{K}}^{\prime},{\bm{V}}^{\prime}) we have that

The rest of the steps follow by a simple induction on kk.

B.2 Proof of Corollary 3.2

To obtain a contradiction, assume that there is a language recognizer AA that uses a Transformer network and such that L=L(A)L=L(A). Now consider the strings w1=aabbw_{1}=aabb and w2=aaabbbw_{2}=aaabbb. Since w1∈PropInv⁡(w2)w_{1}\in\operatorname{PropInv}(w_{2}) by Proposition 3.1 we have that w1∈L(A)w_{1}\in L(A) if and only if w2∈L(A)w_{2}\in L(A) which is a contradiction since w1∈Lw_{1}\in L but w2∉Lw_{2}\notin L. This completes the proof of the corollary.

B.3 Proof of Proposition 3.3

Assume that the output for the encoder part of the transformer is X=(x1,…,xn){\bm{X}}=({\bm{x}}_{1},\ldots,{\bm{x}}_{n}). First we use an encoder layer that implements the identity function. This can be trivially done using null functions for the self attention and through the residual connections this encoder layer shall preserve the original xi{\bm{x}}_{i} values. For the final V(⋅)V(\cdot) and K(⋅)K(\cdot) functions of the Transformer encoder (Equation (8)), we use V(x)=xV({\bm{x}})={\bm{x}} the identity function and K(x)=K({\bm{x}})=, giving Ve=X{\bm{V}}^{\bm{e}}={\bm{X}} and Ke=(,,…,){\bm{K}}^{\bm{e}}=(,,\ldots,).

For the decoder we use a similar approach. We consider the identity in the self attention plus the residual (which can be done by just using the null functions for the self attention). Considering the external attention, that is the attention over (Ke,Ve)({\bm{K}}^{\bm{e}},{\bm{V}}^{\bm{e}}), we let score⁡\operatorname{score} and ρ\rho be arbitrary scoring and normalization functions. And finally for the function O(⋅)O(\cdot) (Equation (11)) we use a single layer neural network implementing the affine transformation O([x,y])=[y−x,−y]O([x,y])=[y-x,-y] such that O([x,y])+[x,y]=[y,0]O([x,y])+[x,y]=[y,0]. The final function F(⋅)F(\cdot) is just the identity function.

In order to complete the proof we introduce some notation. Lets denote by #a(w)\#_{a}(w) as the number of aa’s in ww, and similarly #b(w)\#_{b}(w) for the number of bb’s in ww. Lets call cwc_{w} as the value #a(w)−#b(w)n\frac{\#_{a}(w)-\#_{b}(w)}{n}. We now prove that, for any string w∈{a,b}∗w\in\{a,b\}^{*} if we consider f(w)=X=(x1,…,xn)f(w)={\bm{X}}=({\bm{x}}_{1},\ldots,{\bm{x}}_{n}) as the input sequence for Trans⁡\operatorname{Trans} and we use initial value s={\bm{s}}= for the decoder, the complete network shall compute a sequence y1,y2,…,yr{\bm{y}}_{1},{\bm{y}}_{2},\ldots,{\bm{y}}_{r} such that:

We proceed by induction. The base case trivially holds since y0=s={\bm{y}}_{0}={\bm{s}}=. Assume now that we are at step rr and the input for the decoder is (y0,y1,…,yr)({\bm{y}}_{0},{\bm{y}}_{1},\ldots,{\bm{y}}_{r}). We will show that yr+1=[cw,0]{\bm{y}}_{r+1}=[c_{w},0]. Since we consider the identity in the self attention (Equation (9)), we have that pi=yi{\bm{p}}_{i}={\bm{y}}_{i} for every ii in {0,…,i}\{0,\ldots,i\}. Now considering the external attention, that is the attention over (Ke,Ve)({\bm{K}}^{\bm{e}},{\bm{V}}^{\bm{e}}), Since all key vectors in Ke{\bm{K}}^{\bm{e}} are $,theexternalattentionwillproducethesamescorevalueforallpositions.Thatis,, the external attention will produce the same score value for all positions. That is,\operatorname{score}({\bm{p}}_{i},{\bm{k}}_{j_{1}})=\operatorname{score}({\bm{p}}_{i},{\bm{k}}_{j_{2}})foreveryfor everyj_{1},j_{2}.Letscallthisvalue. Lets call this values^{\star}$. Thus we have that

Then, since Ve=X{\bm{V}}^{\bm{e}}={\bm{X}} we have that

for every i∈{0,…,r}i\in\{0,\ldots,r\}. The last equality holds since our embedding are f(a)=f(a)= and f(b)=f(b)=, and so every aa in ww sums one and every bb subtracts one. Thus, we have that

for every i∈{0,…,r}i\in\{0,\ldots,r\}. In the next step, after the external attention plus the residual connection (Equation (10)) we have

Applying function O(⋅)O(\cdot) plus the residual connection (Equation (11)) we have

Finally, yr+1=F(zr)=zr=[cw,0]{\bm{y}}_{r+1}=F({\bm{z}}_{r})={\bm{z}}_{r}=[c_{w},0] which is exactly what we wanted to prove.

B.4 Proof of Theorem 3.4

Let M=(Q,Σ,δ,qinit,F)M=(Q,\Sigma,\delta,q_{\text{init}},F) be a Turing machine with a infinite tape and assume that the special symbol #∈Σ\#\in\Sigma is used to mark blank positions in the tape. We make the following assumptions about how MM works when processing an input string:

MM always moves its head either to the left or to the right (it never stays at the same cell).

MM begins at state qinitq_{\text{init}} pointing to the cell immediately to the left of the input string.

MM never makes a transition to the left of the initial position.

QQ has a special state qreadq_{\text{read}} used to read the complete input.

Initially (time ), MM makes a transition to state qreadq_{\text{read}} and move its head to the right.

While in state qreadq_{\text{read}} it moves to the right until symbol #\# is read.

There are no transitions going out from accepting states (states in FF).

It is easy to prove that every general Turing machine is equivalent to one that satisfies the above assumptions. We prove that one can construct a transformer network Trans⁡M\operatorname{Trans}_{M} that is able to simulate MM on every possible input string.

The construction is somehow involved and uses several helping values, sequences and intermediate results. To make the reading more easy we divide the construction and proof in three parts. We first give a high-level view of the strategy we use. Then we give some details on the architecture of the encoder and decoder needed to implement our strategy, and finally we formally prove that every part of our architecture can be actually implemented.

In the encoder part of Trans⁡M\operatorname{Trans}_{M} we receive as input the string w=s1s2…snw=s_{1}s_{2}\ldots s_{n}. We first use an embedding function to represent every sis_{i} as a one-hot vector and add a positional encoding for every index. The encoder produces output (Ke,Ve)({\bm{K}}^{\bm{e}},{\bm{V}}^{\bm{e}}) where Ke=(k1e,…,kne){\bm{K}}^{\bm{e}}=({\bm{k}}^{\bm{e}}_{1},\ldots,{\bm{k}}^{\bm{e}}_{n}) and Ve=(v1e,…,vne){\bm{V}}^{\bm{e}}=({\bm{v}}^{\bm{e}}_{1},\ldots,{\bm{v}}^{\bm{e}}_{n}) are sequences of keys and values such that vie{\bm{v}}^{\bm{e}}_{i} contains the information of sis_{i} and kie{\bm{k}}^{\bm{e}}_{i} contains the information of the ii-th positional encoding. We later show that this allows us to attend to every specific position and copy every input symbol from the encoder to the decoder (Lemma B.1).

In the decoder part of Trans⁡M\operatorname{Trans}_{M} we simulate a complete execution of MM over w=s1s2⋯snw=s_{1}s_{2}\cdots s_{n}. For this we define the following sequences (for i≥0i\geq 0):

For the case of m(i)m^{(i)} we assume that −1-1 represents a movement to the left and 11 represents a movement to the right. In our construction we show how to build a decoder that computes all the above values for every time step ii using self attention plus attention over the encoder part. Since the above values contain all the needed information to reconstruct the complete history of the computation, we can effectively simulate MM.

In particular our construction produces the sequence of output vectors y1,y2,…{\bm{y}}_{1},{\bm{y}}_{2},\ldots such that, for every ii, the vector yi{\bm{y}}_{i} contains information about q(i)q^{(i)} and s(i)s^{(i)} encoded as one-hot vectors. The construction and proof goes by induction. We begin with an initial vector y0{\bm{y}}_{0} that represents the state of the computation before it has started, that is q(0)=qinitq^{(0)}=q_{\text{init}} and s(0)=#s^{(0)}=\#. For the induction step we assume that we have already computed y1,…,yr{\bm{y}}_{1},\ldots,{\bm{y}}_{r} such that yi{\bm{y}}_{i} contains information about q(i)q^{(i)} and s(i)s^{(i)}, and we show how with input (y0,y1,…,yr)({\bm{y}}_{0},{\bm{y}}_{1},\ldots,{\bm{y}}_{r}) the decoder produces the next vector yr+1{\bm{y}}_{r+1} containing q(r+1)q^{(r+1)} and s(r+1)s^{(r+1)}.

The overview of the construction is as follows. First notice that the transition function δ\delta relates the above values with the following equation:

We prove that we can use a two-layer feed-forward network to mimic the transition function δ\delta (Lemma B.2). Thus, given that the input vector yi{\bm{y}}_{i} contains q(i)q^{(i)} and s(i)s^{(i)}, we can produce the values q(i+1)q^{(i+1)}, v(i)v^{(i)} and m(i)m^{(i)} (and store them as values in the decoder). In particular, since yr{\bm{y}}_{r} is in the input, we can produce q(r+1)q^{(r+1)} which is part of what we need for yr+1{\bm{y}}_{r+1}. In order to complete the construction we also need to compute the value s(r+1)s^{(r+1)}, that is, we need to compute the symbol under the head of machine MM at the next time step (time r+1r+1). We next describe at a high level, how this symbol can be computed with two additional decoder layers.

We first make some observations about s(i)s^{(i)} that are fundamental in our computation. Assume that at time ii the head of MM is pointing to the cell at index kk. Then we have three possibilities:

If i≤ni\leq n, then s(i)=sis^{(i)}=s_{i} since MM is still reading its input string.

If i>ni>n and MM has never written at index kk, then s(i)=#s^{(i)}=\#, the blank symbol.

In other case, that is, if i>ni>n and time ii is not the first time that MM is pointing to index kk, then s(i)s^{(i)} is the last symbol written by MM at index kk.

For the case (1) we can produce s(i)s^{(i)} by simply attending to position ii in the encoder part. Thus, if r+1≤nr+1\leq n to produce s(r+1)s^{(r+1)} we can just attend to index r+1r+1 in the encoder and copy this value to yr+1{\bm{y}}_{r+1}. For cases (2) and (3) the solution is a bit more complicated, but almost all the important work is to compute what is the index that MM is going to be pointing to in time r+1r+1.

Notice that value c(i)c^{(i)} satisfies that c(i)=c(i−1)+m(i−1)c^{(i)}=c^{(i-1)}+m^{(i-1)}. If we unroll this equation and assuming that c(0)=0c^{(0)}=0 we obtain that

Then, at the step ii in the decoder we have all the necessary to compute value c(i)c^{(i)} but also the necessary to compute c(i+1)c^{(i+1)}. We actually show that the computation (of a representation) of c(i)c^{(i)} and c(i+1)c^{(i+1)} can be done by using one layer of self attention (Lemma B.3).

We have described at a high-level a decoder that, with input (y0,y1,…,yr)({\bm{y}}_{0},{\bm{y}}_{1},\ldots,{\bm{y}}_{r}), computes the values q(r+1)q^{(r+1)} and s(r+1)s^{(r+1)} which is what we need to produce yr+1{\bm{y}}_{r+1}. We next show all the details of this construction.

In this section we give more details on the architecture of the encoder and decoder needed to implement our strategy. We let several intermediate claims as lemmas that we formally prove in Section B.4.3.

Attention mechanism

For our attention mechanism we use the following non-linear function:

Thus, when computing hard attention with the function score⁡φ(⋅)\operatorname{score}_{\varphi}(\cdot) we essentially select the vector vj{\bm{v}}_{j} such that the dot product ⟨q,kj⟩\langle{\bm{q}},{\bm{k}}_{j}\rangle is as close to as possible. If there is more than one index, say indexes j1,j2,…,jrj_{1},j_{2},\ldots,j_{r}, that minimizes the dot product ⟨q,kj⟩\langle{\bm{q}},{\bm{k}}_{j}\rangle then we have that

Thus, in the extreme case in which all dot products are equal ⟨q,kj⟩\langle{\bm{q}},{\bm{k}}_{j}\rangle for every index jj, attention behaves just as an average of all value vectors, that is Att⁡(q,K,V)=1n∑j=1nvj\operatorname{Att}({\bm{q}},{\bm{K}},{\bm{V}})=\frac{1}{n}\sum_{j=1}^{n}{\bm{v}}_{j}. We use all these properties of the hard attention in our proof.

Vectors and encodings

Embeddings and positional encodings

Thus, given an input sequence s1s2⋯sn∈Σ∗s_{1}s_{2}\cdots s_{n}\in\Sigma^{*}, we have that

We denote this last vector by xi{{\bm{x}}}_{i}. That is, if MM receives the input string w=s1s2⋯snw=s_{1}s_{2}\cdots s_{n}, then the input for Trans⁡M\operatorname{Trans}_{M} is the sequence (x1,x2,…,xn)({{\bm{x}}}_{1},{{\bm{x}}}_{2},\ldots,{{\bm{x}}}_{n}). The need for using a positional encoding having values 1/i1/i and 1/i21/i^{2} will be clear when we formally prove the correctness of our construction.

We need a final preliminary notion. In the formal construction of Trans⁡M\operatorname{Trans}_{M} we also use the following helping sequences:

These are used to identify when MM is still reading the input string.

The encoder part of Trans⁡M\operatorname{Trans}_{M} is very simple. For TEnc⁡M\operatorname{TEnc}_{M} we use a single-layer encoder, such that TEnc⁡M(x1,…,xn)=(Ke,Ve)\operatorname{TEnc}_{M}({{\bm{x}}}_{1},\ldots,{{\bm{x}}}_{n})=({{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}}) where Ke=(k1,…,kn){{\bm{K}}}^{\textbf{e}}=({\bm{k}}_{1},\ldots,{\bm{k}}_{n}) and Ve=(v1,…,vn){{\bm{V}}}^{\textbf{e}}=({{\bm{v}}}_{1},\ldots,{{\bm{v}}}_{n}) such that

It is straightforward to see that these vectors can be produced with a single encoder layer by using a trivial self attention, taking advantage of the residual connections in Equations (6) and (7), and then using linear transformations for V(⋅)V(\cdot) and K(⋅)K(\cdot) in Equation (8).

When constructing the decoder we use the following property.

We next show how to construct the decoder part of Trans⁡M\operatorname{Trans}_{M} to produce the sequence of outputs y1,y2,…{\bm{y}}_{1},{\bm{y}}_{2},\ldots, where yi{\bm{y}}_{i} is given by:

That is, yi{\bm{y}}_{i} contains information about the state of MM at time ii, the symbol under the head of MM at time ii, and the last direction followed by MM (the direction of the head movement at time i−1i-1). The need to include m(i−1)m^{(i-1)} will be clear in the construction.

We consider as the starting vector for the decoder the vector

We are assuming that m(−1)=0m^{(-1)}=0 to represent that previous to time there was no head movement. Our construction resembles a proof by induction; we describe the architecture piece by piece and at the same time we show how for every r≥0r\geq 0 our architecture constructs yr+1{\bm{y}}_{r+1} from the previous vectors (y0,…,yr)({\bm{y}}_{0},\ldots,{\bm{y}}_{r}).

Thus, assume that y0,…,yr{\bm{y}}_{0},\ldots,{\bm{y}}_{r} satisfy the properties stated above. Since we are using positional encodings, the actual input for the first layer of the decoder is the sequence

We denote by y‾i\overline{{\bm{y}}}_{i} the vector yi{\bm{y}}_{i} plus its positional encoding. Thus we have that

For the first self attention in Equation (9) we just produce the identity which can be easily implemented with a trivial attention plus the residual connection. Thus, we produce the sequence of vectors (p01,p11,…,pr1)({\bm{p}}^{1}_{0},{\bm{p}}^{1}_{1},\ldots,{\bm{p}}^{1}_{r}) such that pi1=y‾i{{\bm{p}}}^{1}_{i}=\overline{{\bm{y}}}_{i}.

Since pi1{\bm{p}}^{1}_{i} is of the form [A‾,…,A‾,1,i+1,A‾,A‾][\underline{\phantom{A}},\ldots,\underline{\phantom{A}},1,i+1,\underline{\phantom{A}},\underline{\phantom{A}}] by Lemma B.1 we know that if we use pi1{\bm{p}}^{1}_{i} to attend over the encoder we obtain

Thus in Equation (10) we finally produce the vector ai1{\bm{a}}^{1}_{i} given by

As the final piece of the first decoder layer we use a function O1(⋅)O_{1}(\cdot) (Equation (11)) that satisfies the following lemma.

That is, function O1(⋅)O_{1}(\cdot) simulates transition δ(q(i),s(i))\delta(q^{(i)},s^{(i)}) to construct ⟦ q(i+1) ⟧\llbracket\ q^{(i+1)}\ \rrbracket, ⟦ v(i) ⟧\llbracket\ v^{(i)}\ \rrbracket, and m(i)m^{(i)} besides some other linear transformations.

We finally produce as the output of the first decoder layer, the sequence (z01,z11,…,zr1)({\bm{z}}^{1}_{0},{\bm{z}}^{1}_{1},\ldots,{\bm{z}}^{1}_{r}) such that

Notice that zr1{\bm{z}}^{1}_{r} already holds info about q(r+1)q^{(r+1)} and m(r)m^{(r)} which we need for constructing vector yr+1{\bm{y}}_{r+1}. The single piece of information that we still need to construct is s(r+1)s^{(r+1)}, that is, the symbol under the head of machine MM at the next time step (time r+1r+1). We next describe how this symbol can be computed with two additional decoder layers.

Recall that c(i)c^{(i)} is the cell to which MM is pointing to at time ii, and that it satisfies that c(i)=m(0)+m(1)+⋯+m(i−1)c^{(i)}=m^{(0)}+m^{(1)}+\cdots+m^{(i-1)}. We can take advantage of this property to prove the following lemma.

Let Zi1=(z01,z11,…,zi1){\bm{Z}}^{1}_{i}=({\bm{z}}^{1}_{0},{\bm{z}}^{1}_{1},\ldots,{\bm{z}}^{1}_{i}). There exists functions Q2(⋅)Q_{2}(\cdot), K2(⋅)K_{2}(\cdot), and V2(⋅)V_{2}(\cdot) defined by feed-forward networks such that

Lemma B.3 essentially shows that one can construct a representation for values c(i)c^{(i)} and c(i+1)c^{(i+1)} for every possible index ii. In particular we will know the value c(r+1)c^{(r+1)} that represents the cell to which the machine is pointing to in the next time step.

Continuing with the decoder layer, when using the self attention above and after adding the residual in Equation (9) we obtain the sequence of vectors (p02,p12,…,pr2)({\bm{p}}^{2}_{0},{\bm{p}}^{2}_{1},\ldots,{\bm{p}}^{2}_{r}) such that:

From vectors (p02,p12,…,pr2)({\bm{p}}^{2}_{0},{\bm{p}}^{2}_{1},\ldots,{\bm{p}}^{2}_{r}) and by using the residual connection in Equation (10) plus the output function O(⋅)O(\cdot) in Equation (11) it is not difficult to produce the sequence of vectors (z02,z12,…,zr2)({\bm{z}}^{2}_{0},{\bm{z}}^{2}_{1},\ldots,{\bm{z}}^{2}_{r}) such that zi2=pi2{\bm{z}}^{2}_{i}={\bm{p}}^{2}_{i}, as the output of the second decoder layer. That is, we have that

There exists functions Q3(⋅)Q_{3}(\cdot), K3(⋅)K_{3}(\cdot), and V3(⋅)V_{3}(\cdot) defined by feed-forward networks such that

From vectors (p03,p13,…,pr3)({\bm{p}}^{3}_{0},{\bm{p}}^{3}_{1},\ldots,{\bm{p}}^{3}_{r}) and by using the residual connection in Equation (10) plus the output function O(⋅)O(\cdot) in Equation (11)) it is not difficult to produce the sequence of vectors (z03,z13,…,zr3)({\bm{z}}^{3}_{0},{\bm{z}}^{3}_{1},\ldots,{\bm{z}}^{3}_{r}) such that zi3=pi3{\bm{z}}^{3}_{i}={\bm{p}}^{3}_{i}, as the output of the third and final decoder layer, and thus we have that

We finish our construction by using the final transformation function F(⋅)F(\cdot) in Equation (12) in the following lemma.

We prove Lemma B.5 as follows (details in the next section). We show that one can construct a feed-forward network that with input zr3{\bm{z}}^{3}_{r} implements the following to produce yr+1{\bm{y}}_{r+1}. We move ⟦ q(r+1) ⟧\llbracket\ q^{(r+1)}\ \rrbracket and m(r)m^{(r)} to its corresponding position in yr+1{\bm{y}}_{r+1}. Then

if β(r+1)=r+1\beta^{(r+1)}=r+1 then we let ⟦ s(r+1) ⟧=⟦ α(r+1) ⟧\llbracket\ s^{(r+1)}\ \rrbracket=\llbracket\ \alpha^{(r+1)}\ \rrbracket,

Final step

where α(j)\alpha^{(j)} and β(j)\beta^{(j)} are defined as

Recall that Ke=(k1,…,kn){{\bm{K}}}^{\textbf{e}}=({\bm{k}}_{1},\ldots,{\bm{k}}_{n}) is such that ki=[ 0,…,0,i,−1,0,0 ]{\bm{k}}_{i}=[\ 0,\ldots,0,i,-1,0,0\ ]. Then we have that

Notice that, if j≤nj\leq n, then the above expression is maximized when i=ji=j. Otherwise, if j>nj>n then the expression is maximized when i=ni=n. Then Att⁡(q,Ke,Ve)=vi⋆\operatorname{Att}({\bm{q}},{{\bm{K}}}^{\textbf{e}},{{\bm{V}}}^{\textbf{e}})={\bm{v}}_{i^{\star}} where i⋆=ji^{\star}=j if j≤nj\leq n and i⋆=ni^{\star}=n if j>nj>n. We note that i⋆i^{\star} as just defined is exactly β(j)\beta^{(j)}. Thus, given that vi{\bm{v}}_{i} is defined as

In order to prove the lemma we need some intermediate notions and properties. Assume that the enumeration π1:Σ→{1,…,∣Σ∣}\pi_{1}:\Sigma\to\{1,\ldots,|\Sigma|\} is the one used to construct the one-hot vectors ⟦ s ⟧\llbracket\ s\ \rrbracket for s∈Σs\in\Sigma, and that π2:Q→{1,…,∣Q∣}\pi_{2}:Q\to\{1,\ldots,|Q|\} is the one used to construct ⟦ q ⟧\llbracket\ q\ \rrbracket with q∈Qq\in Q. Using π1\pi_{1} and π2\pi_{2} one can construct an enumeration for the pairs in Q×ΣQ\times\Sigma and then construct one-hot vectors for pairs in this set. Formally, given (q,s)∈Q×Σ(q,s)\in Q\times\Sigma we denote by ⟦ (q,s) ⟧\llbracket\ (q,s)\ \rrbracket a one-hot vector with a 11 in position (π1(s)−1)∣Q∣+π2(q)(\pi_{1}(s)-1)|Q|+\pi_{2}(q) and a in every other position. To simplify the notation we use π(q,s)\pi(q,s) to denote (π1(s)−1)∣Q∣+π2(q)(\pi_{1}(s)-1)|Q|+\pi_{2}(q). One can similarly construct an enumeration π′\pi^{\prime} for Q×Σ×{−1,1}Q\times\Sigma\times\{-1,1\} such that π′(q,s,m)=π(q,s)\pi^{\prime}(q,s,m)=\pi(q,s) if m=−1m=-1 and π′(q,s,m)=∣Q∣∣Σ∣+π(q,s)\pi^{\prime}(q,s,m)=|Q||\Sigma|+\pi(q,s) if m=1m=1. We denote by ⟦ (q,s,m) ⟧\llbracket\ (q,s,m)\ \rrbracket the corresponding one-hot vector for every (q,s,m)∈Q×Σ×{−1,1}(q,s,m)\in Q\times\Sigma\times\{-1,1\}. We next prove three helping properties. In every case q∈Qq\in Q, s∈Σs\in\Sigma, m∈{−1,1}m\in\{-1,1\}, and δ(⋅,⋅)\delta(\cdot,\cdot) is the transition function of machine MM.

To show (1), lets denote by Si{\bm{S}}_{i}, with i∈{1,…,∣Σ∣}i\in\{1,\ldots,|\Sigma|\}, a matrix of dimensions ∣Σ∣×∣Q∣|\Sigma|\times|Q| such that Si{\bm{S}}_{i} has its ii-th row with 11’s and it is everywhere else. We note that for every s∈Σs\in\Sigma it holds that ⟦ s ⟧Si=1\llbracket\ s\ \rrbracket{\bm{S}}_{i}={\bm{1}} if and only if i=π1(s)i=\pi_{1}(s) and it is 0{\bm{0}} otherwise. Now, consider the vector v(q,s){\bm{v}}_{(q,s)}

Vector g1([ ⟦ q ⟧,⟦ s ⟧ ])g_{1}([\ \llbracket\ q\ \rrbracket,\llbracket\ s\ \rrbracket\ ]) has a 11 only at position (π1(s)−1)∣Q∣+π2(q)=π(q,s)(\pi_{1}(s)-1)|Q|+\pi_{2}(q)=\pi(q,s) and it is less than or equal to in every other position. Thus, to construct f1(⋅)f_{1}(\cdot) we apply the piecewise-linear sigmoidal activation σ(⋅)\sigma(\cdot) (see Equation (1)) to obtain

Now, to show (2), lets denote by Mδ{\bm{M}}^{\delta} a matrix of dimensions (∣Q∣∣Σ∣)×(2∣Q∣∣Σ∣)(|Q||\Sigma|)\times(2|Q||\Sigma|) constructed as follows. For (q,s)∈Q×Σ(q,s)\in Q\times\Sigma, if δ(q,s)=(p,r,m)\delta(q,s)=(p,r,m) then Mδ{\bm{M}}^{\delta} has a 11 at position (π(q,s),π′(p,r,m))(\pi(q,s),\pi^{\prime}(p,r,m)) and it has a in every other position, that is

It is straightforward to see that ⟦ (q,s) ⟧Mδ=⟦ δ(q,s) ⟧\llbracket\ (q,s)\ \rrbracket{\bm{M}}^{\delta}=\llbracket\ \delta(q,s)\ \rrbracket, and thus we can define f2(⋅)f_{2}(\cdot) as

To show (3), consider the matrix A{\bm{A}} of dimensions (2∣Q∣∣Σ∣)×(∣Q∣+∣Σ∣+1)(2|Q||\Sigma|)\times(|Q|+|\Sigma|+1) such that

We are now ready to begin with the proof of the lemma. Recall that ai1{\bm{a}}^{1}_{i} is given by

We first use function h1(⋅)h_{1}(\cdot) that works as follows. Lets denote by m^(i−1)\hat{m}^{(i-1)} the value 12m(i−1)+12\frac{1}{2}m^{(i-1)}+\frac{1}{2}. Note that m^(i−1)\hat{m}^{(i-1)} is if m(i−1)=−1m^{(i-1)}=-1, it is 12\frac{1}{2} if m(i−1)=0m^{(i-1)}=0 and it is 11 if m(i−1)=1m^{(i-1)}=1. We use this transformation just to represent m(i−1){m}^{(i-1)} with a value between and 11. Now, consider h1(ai1)h_{1}({\bm{a}}^{1}_{i}) defined by

where g1(⋅)g_{1}(\cdot) is the function defined above in Equation (25). It is clear that h1(⋅)h_{1}(\cdot) is an affine transformation. Moreover, we note that except for g1([⟦ q(i) ⟧,⟦ s(i) ⟧])g_{1}([\llbracket\ q^{(i)}\ \rrbracket,\llbracket\ s^{(i)}\ \rrbracket]) all values in h1(ai1)h_{1}({\bm{a}}^{1}_{i}) are between and 11. Thus if we apply function σ(⋅)\sigma(\cdot) to h1(ai1)h_{1}({\bm{a}}^{1}_{i}) we obtain

Then we can define h2(⋅)h_{2}(\cdot) such that

Finally we can apply a function h4(⋅)h_{4}(\cdot) to just reorder the values and multiply some components by −1-1 to complete our construction

We note that we applied a single non-linearity and all other functions are affine transformations. Thus O1(⋅)O_{1}(\cdot) can be implemented with a two-layer feed-forward network.

Recall that zi1{\bm{z}}^{1}_{i} is the following vector

Then, since K2(zj1)K_{2}({\bm{z}}^{1}_{j}) is the vector with only zeros, then score⁡φ(Q2(zi1),K2(zj1))=0\operatorname{score}_{\varphi}(Q_{2}({\bm{z}}^{1}_{i}),K_{2}({\bm{z}}^{1}_{j}))=0 for every j∈{0,…,i}j\in\{0,\ldots,i\}. Thus, we have that the attention Att⁡(Q2(zi1),K2(Zi1),V2(Zi1))\operatorname{Att}(Q_{2}({\bm{z}}^{1}_{i}),K_{2}({\bm{Z}}^{1}_{i}),V_{2}({\bm{Z}}^{1}_{i})) that we need to compute is just the average of all the vectors in V2(Zi1)=(V2(z01,…,zi1)V_{2}({\bm{Z}}^{1}_{i})=(V_{2}({\bm{z}}^{1}_{0},\ldots,{\bm{z}}^{1}_{i}), that is

Then, since m(0)+⋯+m(i)=c(i+1)m^{(0)}+\cdots+m^{(i)}=c^{(i+1)} and m(−1)+m(0)+⋯+m(i−1)=c(i)m^{(-1)}+m^{(0)}+\cdots+m^{(i-1)}=c^{(i)} we have that

which is exactly what we wanted to show. ∎

Recall that zi2{\bm{z}}^{2}_{i} is the following vector

We need to construct functions Q3(⋅)Q_{3}(\cdot), K3(⋅)K_{3}(\cdot), and V3(⋅)V_{3}(\cdot) such that

It is clear that the three functions are linear transformations and thus they can be defined by feed-forward networks. Consider now the attention Att⁡(Q3(zi2),K3(Zi2),V3(Zi2))\operatorname{Att}(Q_{3}({\bm{z}}^{2}_{i}),K_{3}({\bm{Z}}^{2}_{i}),V_{3}({\bm{Z}}^{2}_{i})). In order to compute this value, and since we are considering hard attention, we need to find the value j∈{0,1,…,i}j\in\{0,1,\ldots,i\} that maximizes

Actually, assumming that such value is unique, lets say j⋆j^{\star}, then we have that

To simplify the notation, we denote by χji\chi_{j}^{i} the dot product ⟨Q3(zi2),K3(zj2)⟩\langle Q_{3}({\bm{z}}^{2}_{i}),K_{3}({\bm{z}}^{2}_{j})\rangle. Thus, we need to find j⋆=arg max⁡jφ(χji)j^{\star}=\operatorname*{arg\,max}_{j}\varphi(\chi_{j}^{i}). Moreover, given the definition of φ\varphi (see Equation (20))we have that

Now, by our definition of Q3(⋅)Q_{3}(\cdot) and K3(⋅)K_{3}(\cdot) we have that

where εk=1(k+1)\varepsilon_{k}=\frac{1}{(k+1)}. We next prove the following auxiliary property.

In order to prove (26), assume first that j1∈{0,…,i}j_{1}\in\{0,\ldots,i\} is such that c(j1)≠c(i+1)c^{(j_{1})}\neq c^{(i+1)}. Then we have that ∣c(i+1)−c(j1)∣≥1|c^{(i+1)}-c^{(j_{1})}|\geq 1 since c(i+1)c^{(i+1)} and c(j1)c^{(j_{1})} are integer values. From this we have two possibilities for χj1i\chi_{j_{1}}^{i}:

Notice that 1≥εj1≥εi>01\geq\varepsilon_{j_{1}}\geq\varepsilon_{i}>0. Then we have that εiεj1≥(εiεj1)2>13(εiεj1)2\varepsilon_{i}\varepsilon_{j_{1}}\geq(\varepsilon_{i}\varepsilon_{j_{1}})^{2}>\frac{1}{3}(\varepsilon_{i}\varepsilon_{j_{1}})^{2}, and thus

Finally, and using again that 1≥εj1≥εi>01\geq\varepsilon_{j_{1}}\geq\varepsilon_{i}>0, from the above equation we obtain that

If c(i+1)−c(j1)≥1c^{(i+1)}-c^{({j_{1}})}\geq 1, then χj1i≥εiεj1+13(εiεj1)2\chi_{j_{1}}^{i}\geq\varepsilon_{i}\varepsilon_{j_{1}}+\frac{1}{3}(\varepsilon_{i}\varepsilon_{j_{1}})^{2} and since 1≥εj1≥εi>01\geq\varepsilon_{j_{1}}\geq\varepsilon_{i}>0 we obtain that ∣χj1i∣≥εiεj1≥εiεi≥23(εi)2|\chi_{j_{1}}^{i}|\geq\varepsilon_{i}\varepsilon_{j_{1}}\geq\varepsilon_{i}\varepsilon_{i}\geq\frac{2}{3}(\varepsilon_{i})^{2}.

Thus, we have that if c(j1)≠c(i+1)c^{({j_{1}})}\neq c^{(i+1)} then ∣χj1i∣≥23(εi)2|\chi_{j_{1}}^{i}|\geq\frac{2}{3}(\varepsilon_{i})^{2}.

Now assume j2∈{0,…,i}j_{2}\in\{0,\ldots,i\} is such that c(j2)=c(i+1)c^{(j_{2})}=c^{(i+1)}. In this case we have that

We showed that if c(j1)≠c(i+1)c^{({j_{1}})}\neq c^{(i+1)} then ∣χj1i∣≥23(εi)2|\chi_{j_{1}}^{i}|\geq\frac{2}{3}(\varepsilon_{i})^{2} and if c(j2)=c(i+1)c^{({j_{2}})}=c^{(i+1)} then ∣χj2i∣≤13(εi)2|\chi_{j_{2}}^{i}|\leq\frac{1}{3}(\varepsilon_{i})^{2} which implies that ∣χj2i∣<∣χj1i∣|\chi_{j_{2}}^{i}|<|\chi_{j_{1}}^{i}|. This completes the proof of the property in (26).

Assume first that there exists j≤ij\leq i such that c(j)=c(i+1)c^{(j)}=c^{(i+1)}. By (26) we know that

On the contrary, assume that for every j≤ij\leq i it holds that c(j)≠c(i+1)c^{(j)}\neq c^{(i+1)}. We will prove that in this case ∣χij∣<∣χii∣|\chi_{i}^{j}|<|\chi_{i}^{i}| for every j<ij<i and thus arg min⁡j∈{0,…,i}∣χji∣=i\operatorname*{arg\,min}_{j\in\{0,\ldots,i\}}|\chi_{j}^{i}|=i. Now, since c(j)≠c(i+1)c^{(j)}\neq c^{(i+1)} for every j≤ij\leq i, then c(i+1)c^{(i+1)} is a cell that has never been visited before by MM. Given that MM never makes a transition to the left of its initial cell, then cell c(i+1)c^{(i+1)} is a cell to the right of every other previously visited cell. This implies that c(i+1)>c(j)c^{(i+1)}>c^{(j)} for every j≤ij\leq i. Thus, for every j≤ij\leq i we have c(i+1)−c(j)≥1c^{(i+1)}-c^{(j)}\geq 1. This implies that ∣χji∣=χji≥εiεj+13(εiεj)2|\chi_{j}^{i}|=\chi_{j}^{i}\geq\varepsilon_{i}\varepsilon_{j}+\frac{1}{3}(\varepsilon_{i}\varepsilon_{j})^{2}. Moreover, notice that if j<ij<i then εj>εi\varepsilon_{j}>\varepsilon_{i} and thus, if j<ij<i we have that

which implies that arg min⁡j∈{0,…,i}∣χji∣=i\operatorname*{arg\,min}_{j\in\{0,\ldots,i\}}|\chi_{j}^{i}|=i. Summing it up, we have shown that

Before going to the proof of Lemma-B.5 we prove the following helping result that allows us to implement a particular type of if statement with a feed-forward network.

where 1{\bm{1}} is the nn-dimensional vector with only ones. Thus, we have that

Now, since x{\bm{x}}, y{\bm{y}} and z{\bm{z}} are all binary vectors, it is easy to obtain that

We note that f1(⋅)f_{1}(\cdot) and f2(⋅)f_{2}(\cdot) are affine transformations, and thus f(⋅)=f2(σ(f1(⋅)))f(\cdot)=f_{2}(\sigma(f_{1}(\cdot))) is a two-layer feed-forward network. This completes our proof. ∎

We can now continue with the proof of Lemma B.5. Recall that zr3{\bm{z}}^{3}_{r} is the following vector

Lets denote by ⟦ m(r) ⟧\llbracket\ m^{(r)}\ \rrbracket a vector such that

We first consider the function f1(⋅)f_{1}(\cdot) such that

Then, we can use the if function in Lemma B.6 to implement a function f2(⋅)f_{2}(\cdot) such that

We can use again the if function in Lemma B.6 to implement a function f3(⋅)f_{3}(\cdot) such that

The final piece of the proof is to just convert ⟦ m(r) ⟧\llbracket\ m^{(r)}\ \rrbracket back to its value m(r)m^{(r)}, reorder the values and add ’s to obtain yr+1{\bm{y}}_{r+1}. We do all this with a final linear transformation f4(⋅)f_{4}(\cdot) such that

Appendix C Proofs for Section 4

The formulas of the Neural GPU in detail are as follows (with S0{\bm{\mathsfit{S}}}^{0} the initial input tensor):

With U(⋅)U(\cdot), R(⋅)R(\cdot), and F(⋅)F(\cdot) defined as

Consider now an RNN encoder-decoder NN of dimension dd and composed of the equations

with h0=0{\bm{h}}_{0}={\bm{0}} and g0=hn{\bm{g}}_{0}={\bm{h}}_{n} where nn is the length of the input.

Constructing the Neural GPU to simulate N𝑁N

We now describe how to construct the kernel banks KU{\bm{\mathsfit{K}}}^{U}, KR{\bm{\mathsfit{K}}}^{R} and KF{\bm{\mathsfit{K}}}^{F} of shape (2,1,3d+3,3d+3)(2,1,3d+3,3d+3). Notice that for each kernel KX{\bm{\mathsfit{K}}}^{X} we essentially have to define two matrices K1,1,:,:X{\bm{\mathsfit{K}}}^{X}_{1,1,:,:} and K2,1,:,:X{\bm{\mathsfit{K}}}^{X}_{2,1,:,:} each one of dimension (3d+3)×(3d+3)(3d+3)\times(3d+3). We begin by defining every matrix in KF{\bm{\mathsfit{K}}}^{F} as block matrices. When defining the matrices, all blank spaces are considered to be .

where F1{\bm{F}}_{1} and F2{\bm{F}}_{2} are 3×33\times 3 matrices defined by

Tensors KU{\bm{\mathsfit{K}}}^{U} and KR{\bm{\mathsfit{K}}}^{R} are considerable simpler. For the case of KU{\bm{\mathsfit{K}}}^{U} we have

where U1{\bm{U}}_{1} and U2{\bm{U}}_{2} are 3×33\times 3 matrices defined by

and where A{\bm{A}} is the 3×d3\times d matrix defined by

Finally, we define KR{\bm{\mathsfit{K}}}^{R} as

where R2{\bm{R}}_{2} is the 3×33\times 3 matrix defined by

and where B{\bm{B}} is the 3×d3\times d matrix defined by

The bias tensors BU{\bm{\mathsfit{B}}}^{U} and BF{\bm{\mathsfit{B}}}^{F} are 0\bm{\mathsfit{0}} (the tensor with all values ). Finally, to construct tensor BR{\bm{\mathsfit{B}}}^{R} we consider the matrix D{\bm{D}} of dimension 1×(3d+3)1\times(3d+3) such that

Then we let Bi,:,:R=D{\bm{\mathsfit{B}}}^{R}_{i,:,:}={\bm{D}} for all ii. Finally, we consider fU=fR=fF=σf_{U}=f_{R}=f_{F}=\sigma. The constructed Neural GPU is a uniform Neural GPU.

Before continuing with the proof we note that for every kernel KX{\bm{\mathsfit{K}}}^{X} and tensor S{\bm{\mathsfit{S}}} we have that

Correctness of the construction

We now prove that the following properties hold for every t≥0t\geq 0:

where αjk\bm{\alpha}^{k}_{j} is given by the recurrence α0k=hk\bm{\alpha}^{k}_{0}={\bm{h}}_{k} and αjk=σ(αj−1kU)\bm{\alpha}^{k}_{j}=\sigma(\bm{\alpha}^{k}_{j-1}{\bm{U}}). Notice that gj=αjn{\bm{g}}_{j}=\bm{\alpha}_{j}^{n}. That is, we are going to prove that our construction actually simulates NN. By (30) one can see that the intuition in our construction is to use the first dd components to simulate the encoder part, the next dd components to communicate data between the encoder and decoder simulation, and the next dd components to simulate the decoder part. The last three components are needed as gadgets for the gates to actually simulate a sequencial read of the input, and to ensure that the hidden state of the encoder and decoder are updated properly.

We prove the above statement by induction in tt. First notice that the property trivially holds for S0{\bm{\mathsfit{S}}}^{0}. Now assume that this holds for t−1t-1 and lets prove it for tt. We know that Ut{\bm{\mathsfit{U}}}^{t} is computed as

Now, notice that K1,1,:,:U{\bm{\mathsfit{K}}}^{U}_{1,1,:,:} and K2,1,:,:U{\bm{\mathsfit{K}}}^{U}_{2,1,:,:} are not zero only in its three last rows, thus we can focus on the three last components of the vectors in St−1{\bm{\mathsfit{S}}}^{t-1}, and then we can compute Ui,1,:t{\bm{\mathsfit{U}}}^{t}_{i,1,:} as

Now, for Rt{\bm{\mathsfit{R}}}^{t} we have

and thus for Ri,1,:t{\bm{\mathsfit{R}}}^{t}_{i,1,:} we have

where we deleted the term with K1,1,:,:R{\bm{\mathsfit{K}}}^{R}_{1,1,:,:} since it is the null matrix. Then by using the definition of Si,1,:t−1{\bm{\mathsfit{S}}}^{t-1}_{i,1,:} above (Equation (34)) we have

We can now compute Si,1,:t{\bm{\mathsfit{S}}}^{t}_{i,1,:}. By the definition of St{\bm{\mathsfit{S}}}^{t} we have

where we dropped BF{\bm{\mathsfit{B}}}^{F} that has only zeros. First, by using what we already computed for Ui,1,:t{\bm{\mathsfit{U}}}^{t}_{i,1,:} we have that

When i=ti=t we have that Si,1,:t−1=[xi,0,0,1,0,0]{\bm{\mathsfit{S}}}^{t-1}_{i,1,:}=[{\bm{x}}_{i},{\bm{0}},{\bm{0}},1,0,0] (Equation (34)), thus [0,0,1,0,0,1]⊙Si,1,:t−1=[0,0,0,0,0,0]{[}{\bm{0}},{\bm{0}},\bm{1},0,0,1]\odot{\bm{\mathsfit{S}}}^{t-1}_{i,1,:}=[{\bm{0}},{\bm{0}},{\bm{0}},0,0,0]. Then

We are almost done with the inductive step, we only need to compute σ((KF∗(Rt⊙St−1))i,1,:)\sigma(({\bm{\mathsfit{K}}}^{F}*({\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1}))_{i,1,:}). Given what we have for Rt{\bm{\mathsfit{R}}}^{t} and St−1{\bm{\mathsfit{S}}}^{t-1} we have that Rt⊙St−1{\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1} is

Lets Tt=σ(KF∗(Rt⊙St−1)){\bm{\mathsfit{T}}}^{t}=\sigma({\bm{\mathsfit{K}}}^{F}*({\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1})). Notice that from Equation (45) we actually need to know the values in Ti,1,:t{\bm{\mathsfit{T}}}^{t}_{i,1,:} only for i≤ti\leq t. Now we have that

Putting the value of Ti,1,:t{\bm{\mathsfit{T}}}^{t}_{i,1,:} in Equation (45) we obtain

which is exactly what we needed to prove (Equation (30)).

Now, lets focus on Sn,1,:n+t{\bm{\mathsfit{S}}}^{n+t}_{n,1,:} for t≥1t\geq 1. By what we have just proved, we obtain that

which is the decoder part of the RNN NN. Thus, we can simulate the complete network NN with a Neural GPU.

C.2 Proof of Proposition 4.2

Now lets K{\bm{\mathsfit{K}}} be an arbitrary kernel bank of shape (kH,kW,d,d)(k_{H},k_{W},d,d). Let T=K⊛S{\bm{\mathsfit{T}}}={\bm{\mathsfit{K}}}\circledast{\bm{\mathsfit{S}}} where ⊛\circledast denotes the circular convolution. We prove next that

and then, Ti,:,:=Ti+p,:,:{\bm{\mathsfit{T}}}_{i,:,:}={\bm{\mathsfit{T}}}_{i+p,:,:}.

Consider now an arbitrary uniform Neural GPU that processes tensor S{\bm{\mathsfit{S}}} above, and assume that S1,S2,…,Sr{\bm{\mathsfit{S}}}^{1},{\bm{\mathsfit{S}}}^{2},\ldots,{\bm{\mathsfit{S}}}^{r} is the sequence produced by it. Next we prove that for every tt and for every ii it holds that Si,:,:t=Si+p,:,:t{\bm{\mathsfit{S}}}^{t}_{i,:,:}={\bm{\mathsfit{S}}}^{t}_{i+p,:,:}. We prove it by induction in tt. For the case S0{\bm{\mathsfit{S}}}^{0} it holds by definition. Thus assume that St−1{\bm{\mathsfit{S}}}^{t-1} satisfies the property. Let

Since we are considering uniform Neural GPUs, we know that there exist three matrices BU{\bm{B}}^{U}, BR{\bm{B}}^{R} and BF{\bm{B}}^{F} such that for every ii it holds that Bi,:,:U=BU{\bm{\mathsfit{B}}}^{U}_{i,:,:}={\bm{B}}^{U}, Bi,:,:R=BR{\bm{\mathsfit{B}}}^{R}_{i,:,:}={\bm{B}}^{R}, and Bi,:,:F=BF{\bm{\mathsfit{B}}}^{F}_{i,:,:}={\bm{B}}^{F}. It is easy to prove that Ui,:,:t=Ui+p,:,:t{\bm{\mathsfit{U}}}^{t}_{i,:,:}={\bm{\mathsfit{U}}}^{t}_{i+p,:,:}. First note that by inductive hypothesis, we have that Si,:,:t−1=Si+p,:,:t−1{\bm{\mathsfit{S}}}^{t-1}_{i,:,:}={\bm{\mathsfit{S}}}^{t-1}_{i+p,:,:} and thus by the property proved above we have that (KU∗St−1)i,:,:=(KU∗St−1)i+p,:,:({\bm{\mathsfit{K}}}^{U}*{\bm{\mathsfit{S}}}^{t-1})_{i,:,:}=({\bm{\mathsfit{K}}}^{U}*{\bm{\mathsfit{S}}}^{t-1})_{i+p,:,:}. Thus we have that

With a similar argument we can prove that Ri,:,:t=Ri+p,:,:t{\bm{\mathsfit{R}}}^{t}_{i,:,:}={\bm{\mathsfit{R}}}^{t}_{i+p,:,:}. Moreover, notice that (Rt⊙St−1)i,:,:=(Rt⊙St−1)i+p,:,:({\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1})_{i,:,:}=({\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1})_{i+p,:,:}, and thus (KF∗(Rt⊙St−1))i,:,:=(KF∗(Rt⊙St−1))i+p,:,:({\bm{\mathsfit{K}}}^{F}*({\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1}))_{i,:,:}=({\bm{\mathsfit{K}}}^{F}*({\bm{\mathsfit{R}}}^{t}\odot{\bm{\mathsfit{S}}}^{t-1}))_{i+p,:,:}. With all this we finally we have that

This completes the first part of the proof.

From this it is easy to prove that uniform Neural GPUs will no be able to recognize the length of periodic inputs. Thus assume that there is a language recognizer AA defined by of a uniform neural GPU NN such that L(A)L(A) contains all strings of even length. Assume that uu is an arbitrary string in Σ\Sigma such that ∣u∣=p|u|=p with pp an odd number, and let w=uuw=uu and w′=uuuw^{\prime}=uuu. Notice that ∣w∣=2p|w|=2p and thus w∈L(A)w\in L(A), but ∣w′∣=3p|w^{\prime}|=3p and thus w′∉L(A)w^{\prime}\not\in L(A).