# Neural Turing Machines

Neural Turing Machines couple a network to external memory it can learn to use

Alex Graves, Greg Wayne, Ivo Danihelka · arXiv · 2014-10-20

- **Problem** Recurrent networks can in principle run arbitrary procedures, but learning such procedures from examples is hard. Their storage sits inside the hidden state, which is too small and too tangled to act as a program memory.
- **Method** The NTM pairs a recurrent controller with a large memory matrix that it reads and writes through soft addressing. Every step is differentiable, so the whole system trains end to end by gradient descent.
- **Results** On copying and associative recall, NTM generalised to longer sequences than it saw in training, where LSTM degraded rapidly beyond length 20. On associative recall it stayed nearly perfect for up to 12 items, twice the training maximum of 6.

## Why a recurrent controller needs an addressable memory

Recurrent networks can in principle simulate arbitrary procedures, yet making them learn such procedures from examples is difficult in practice. The authors respond by enlarging a recurrent controller with a large addressable memory, in the spirit of a Turing machine's tape, but one that can be trained by gradient descent.

The memory alone would not solve the problem, because the network also has to decide where to read and where to write. Digital addressing picks out a single element, which cannot be trained in the way a soft weighting over all locations can, so every addressing step is made differentiable.

LSTM already gives a recurrent network storage that lasts for long periods, through a gate that controls when an integrator accepts input. That storage sits inside the hidden state, which is the gap the external memory is meant to fill.

## Controller and memory matrix

An NTM has two parts: a neural network controller that exchanges input and output vectors with the outside world, and a memory bank it accesses through read and write operations. The controller's outputs that drive these memory operations are called heads.

The memory is an N by M matrix \\(M\_t\\), where N is the number of locations and M is the vector size at each location. Each read or write head emits a weighting \\(w\_t\\) over the N locations, and these weights are non-negative and sum to one, which makes each head's weighting a normalised distribution.

A read vector is the weighted combination \\( r\_t = \\sum\_i w\_t(i)\\,M\_t(i) \\) of the memory rows, which is differentiable in both the memory and the weighting. A sharp weighting on one row returns that row, while a flat weighting returns a blend of many rows.

A write is split into an erase followed by an add, a design borrowed from the input and forget gates of LSTM. The erase step computes \\(\\tilde{M}\_t(i) = M\_{t-1}(i)\\,\[\\mathbf{1} - w\_t(i)\\,e\_t\]\\), where the erase vector \\(e\_t\\) has elements in (0, 1), so a location is reset only where both the weighting and the erase element are one. The add step then gives \\(M\_t(i) = \\tilde{M}\_t(i) + w\_t(i)\\,a\_t\\), and the order in which several heads apply their erases or adds does not change the result.

## How the weightings are produced

Two addressing mechanisms are combined: content-based addressing, which finds locations by similarity to a value the controller emits, and location-based addressing, which moves focus by position. Location addressing is needed because a variable can have arbitrary content yet still require a fixed name, as in the arithmetic example where the values of x and y are stored at addresses and retrieved later.

The content weighting is a softmax over the similarities, \\(w^c\_t(i) = \\exp(\\beta\_t K\[k\_t, M\_t(i)\]) / \\sum\_j \\exp(\\beta\_t K\[k\_t, M\_t(j)\])\\), where \\(k\_t\\) is the key and \\(\\beta\_t\\) the key strength that sharpens or flattens the focus. The similarity used is cosine similarity, \\(K\[u,v\] = u\\cdot v / (\\\|u\\\|\\,\\\|v\\\|)\\).

The content weighting is then blended with the previous weighting through an interpolation gate \\(g\_t\\) in (0, 1): \\(w^g\_t = g\_t w^c\_t + (1 - g\_t) w\_{t-1}\\). When the gate is zero the head keeps its previous weighting, and when it is one the head uses content addressing alone.

The gated weighting is rotated by circular convolution, \\(\\tilde{w}\_t(i) = \\sum\_{j=0}^{N-1} w^g\_t(j)\\,s\_t(i-j)\\), with index arithmetic modulo N. Because a soft shift spreads weight over neighbouring locations, the head emits \\(\\gamma\_t \\geq 1\\) and sharpens the result as \\(w\_t(i) = \\tilde{w}\_t(i)^{\\gamma\_t} / \\sum\_j \\tilde{w}\_t(j)^{\\gamma\_t}\\).

The combined system can work in three modes: a content weighting used directly, a content weighting that is then shifted to a neighbour, and a previous weighting that is rotated without any content lookup. The third mode lets a head step through a sequence of addresses at a fixed distance at each time step.

## Feedforward or recurrent controller

A recurrent controller such as LSTM keeps its own hidden state, which can complement the memory matrix in the way processor registers complement RAM. A feedforward controller can imitate a recurrent one by reading and writing the same location at each step, and it is usually easier to interpret.

The drawback of a feedforward controller is that the number of read and write heads limits the computation per step: one read head allows only a unary transform of one memory vector, and two read heads allow binary transforms. A recurrent controller can store earlier read vectors internally, so it does not face this limit.

## Copy task: learning speed and generalisation

Training used eight-bit random vectors followed by a delimiter, with sequence lengths drawn between 1 and 20, and the target was the input without the delimiter. No input is given while the target is produced, so the network must recall the whole sequence from memory alone.

The authors regard the gap as large enough to suggest a qualitative difference in how the models solve the problem.

On longer sequences than seen in training, NTM kept copying as the length grew, while LSTM degraded rapidly beyond length 20. When trained on sequences up to length 20, the NTM was tested on a length 120 sequence, where it made a few local errors and one global error in which a single vector was duplicated.

The authors summarise the learned behaviour as a write phase that moves the head forward one location per input vector, followed by a read phase that returns to the start and emits one vector per step. The algorithm uses content-based addressing to jump to the start of the sequence and location-based shifts to move along it. Read locations match the write locations, which the authors take as evidence that each input is stored at a specific location and read back in the same order.

The memory used in this experiment has 128 locations, and after that the cyclic shifts wrap around and overwrite earlier writes.

## Repeat copy: counting and the end marker

NTM learned the task much faster than LSTM, and both solved the training problem perfectly. The difference between the two only became clear when the networks were tested beyond the training range.

When sequence length and repeat count were both increased, NTM handled longer sequences and more than ten repetitions, while LSTM failed both tests. The NTM did not predict the end marker correctly: it emitted the marker after every repetition beyond the eleventh. The authors attribute this to representing the repeat count numerically, which does not generalise easily beyond a fixed range.

The authors read the solution as an extension of the copy procedure, in which the sequential read is simply repeated as many times as needed. An intermediate memory location appears to redirect the head back to the start of the sequence.

## Associative recall: following a pointer to the next item

Each item is a sequence of three six-bit binary vectors bounded by delimiters, and after several items a query item is shown and the network must produce the item that follows it. Training episodes contained a minimum of 2 and a maximum of 6 items.

NTM with a feedforward controller learned faster than NTM with an LSTM controller, which the authors read as external memory being a more effective way to hold the data structure than LSTM's internal state.

On longer item sequences, NTM with a feedforward controller was nearly perfect for up to 12 items, twice the maximum used in training, and kept an average cost below 1 bit per sequence at 15 items. The longer-sequence comparison covers item counts from 6 to 20 and shows NTM generalising better than LSTM.

The inspection of one test episode shows the controller writing a compressed representation of each item's three time slices at the delimiter. The reads contiguous locations that store the time slices of the target item, and the write weightings show that writes also happen at delimiters.

## Dynamic N-Grams: comparison with a Bayesian estimator

Each training example draws a fresh 6-Gram distribution: a table of 32 probabilities, one for each length-five binary history, drawn from a Beta distribution with both parameters one half. A sequence of 200 bits is drawn from this table, with the first five bits drawn as independent Bernoulli samples with p = 0.5, and the network predicts each next bit as it observes the sequence.

This gives a reference cost against which the NTM and LSTM predictions can be compared.

On a validation set of 1000 sequences of length 200 drawn from the training distribution, NTM achieved a small advantage over LSTM but never reached the optimal cost. Predictions of NTM are almost indistinguishable from the optimal ones in most positions, while the errors appear at a few points where the memory was read from the wrong location.

The authors postulate that the NTM uses writes to keep a count of how often ones and zeros follow each context, and the add vectors look like a distributed counter.

## Priority sort: writes placed by priority

Each input has 20 binary vectors, each with a scalar priority drawn uniformly from \[-1, 1\], and the target is the 16 highest-priority vectors in priority order. The authors limited the output to 16 vectors because they wanted to see whether the NTM would use a binary heap sort of depth 4.

The authors hypothesise that the priorities set the relative location of each write, and a linear function of the priority fitted to the observed write locations closely matches them. The read locations are visited in increasing order, so the network traverses the sorted sequence.

Both NTM variants, with feedforward and LSTM controllers, substantially outperformed LSTM on this task. The feedforward variant needed eight parallel read and write heads to perform best, which the authors connect to the difficulty of sorting with unary vector operations.

## Training setup and parameter counts

All experiments were trained with RMSProp in the form described by Graves (2013), with momentum 0.9, and all LSTM networks had three stacked hidden layers. Gradient components were clipped elementwise to the range (-10, 10) during the backward pass.

The number of LSTM parameters grows quadratically with the number of hidden units because of the recurrent connections, whereas the NTM parameter count does not grow with the number of memory locations.

## Limitations

The experiments are described as preliminary and cover the five tasks reported in the paper. Generalisation was tested only up to the lengths and counts noted in each task: a length 120 copy, item sequences up to 15 for recall, and repeat counts beyond ten.

On repeat copy, the NTM could not count its repetitions and did not predict the end marker correctly. On associative recall the LSTM performed better than on copy, which the authors connect to the shorter sequences and to the larger LSTM network having more memory capacity.

On dynamic N-Grams the NTM never reached the cost of the optimal estimator. The priority sort feedforward variant depended on eight heads, and the authors did not test whether fewer heads would suffice with a different controller.

## Paper info

- **Authors**: Alex Graves, Greg Wayne, Ivo Danihelka
- **Venue**: arXiv · Preprint
- **First published**: 2014-10-20
- **Added**: 2026-11-12
- **Updated**: 2014-12-10
- **Version**: v2
- **arXiv**: 1410.5401
- **Category**: cs.NE
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/1410.5401v2) · [PDF ↗](https://arxiv.org/pdf/1410.5401v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Alex Graves, Greg Wayne, Ivo Danihelka (2014). Neural Turing Machines. arXiv:1410.5401v2. https://arxiv.org/abs/1410.5401v2

**BibTeX**

```
@misc{graves2014neural,
  title = {{Neural Turing Machines}},
  author = {Alex Graves and Greg Wayne and Ivo Danihelka},
  year = {2014},
  eprint = {1410.5401},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1410.5401v2}
}
```

## Other paper commentary

- [All-Optical Machine Learning Using Diffractive Deep Neural Networks](https://refivu.com/en/papers/818615cb-8c22-447c-a0a6-77954575b98a-all-optical-machine-learning-using-diffractive-deep-neural.md)
- [Paying More Attention to Attention: Improving the Performance of Convolutional Neural Networks via Attention Transfer](https://refivu.com/en/papers/9fd3c7c3-a01f-4f1c-84a7-72570c46bdd4-paying-more-attention-to-attention-improving-the-performance.md)
- [CvT: Introducing Convolutions to Vision Transformers](https://refivu.com/en/papers/4d0c84cf-05bb-4f74-822e-0b8fbc635203-cvt-introducing-convolutions-to-vision-transformers.md)
- [Methods for Interpreting and Understanding Deep Neural Networks](https://refivu.com/en/papers/beae0f88-5d82-43a5-a727-e94025cff266-methods-for-interpreting-and-understanding-deep-neural.md)
- [Learning Multi-Domain Convolutional Neural Networks for Visual Tracking](https://refivu.com/en/papers/5b8779fe-6230-4bef-9d38-11634c64c64a-learning-multi-domain-convolutional-neural-networks-for.md)
- [Learning Efficient Convolutional Networks through Network Slimming](https://refivu.com/en/papers/d840aead-455e-4eaf-8f2c-7e4bff390606-learning-efficient-convolutional-networks-through-network.md)
- [A Review on Deep Learning Techniques Applied to Semantic Segmentation](https://refivu.com/en/papers/8ae0e955-3ed5-48a2-b638-f9e5d7aa334e-a-review-on-deep-learning-techniques-applied-to.md)
- [Thumbs up? Sentiment Classification using Machine Learning Techniques](https://refivu.com/en/papers/099cc783-1d4a-4899-b08c-ce3886810f1a-thumbs-up-sentiment-classification-using-machine-learning.md)
- [Run, Don't Walk: Chasing Higher FLOPS for Faster Neural Networks](https://refivu.com/en/papers/dffa1011-36cd-44c9-b21a-c10cc00af851-run-don-t-walk-chasing-higher-flops-for.md)
- [An All-in-One Network for Dehazing and Beyond](https://refivu.com/en/papers/03c50633-ad40-4af4-b88a-e6cb99e97639-an-all-in-one-network-for-dehazing-and.md)
- [SmoothGrad: removing noise by adding noise](https://refivu.com/en/papers/ab609ae7-27e3-4473-bc00-e8bc5f0e8a91-smoothgrad-removing-noise-by-adding-noise.md)
- [Fine-Grained Visual Classification of Aircraft](https://refivu.com/en/papers/3543af61-7a52-4fb1-ac78-312fdda48697-fine-grained-visual-classification-of-aircraft.md)

## Terms in this paper

- [cosine similarity](https://refivu.com/en/terms/3baa01a8760d-cosine-similarity)
- [RMSProp](https://refivu.com/en/terms/a1984fdf7b1d-rmsprop)
- [long short-term memory](https://refivu.com/en/terms/1eac61fff4b1-long-short-term-memory)
- [Neural Turing Machine](https://refivu.com/en/terms/c07a6e2b64ab-neural-turing-machine)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/98345e56-0459-409a-bde9-37eadc2f2d0a-neural-turing-machines)
