Neural Turing Machines
Give a neural network a memory matrix it can read and write with soft, differentiable pointers, and it can learn simple algorithms like copy and sort from examples.
Start here (no maths needed)
A Neural Turing Machine is a neural network with its own notebook: it learns where to write things down and where to read them back.
Think of it like this
Try to remember a long list in your head and you'll soon lose bits. With a notebook, it's easy. You can find a page by what's on it ("the page with the map") or by where it is ("the next page"). You can rub out a line and write over it. This network gets a notebook like that. The twist is that its pencil is a bit blurry: it mostly writes on one line, with a little on the lines nearby. That blur is what lets it learn by trial and error.
How it works
- A small network called the controller is the "brain". It decides what to do at each step.
- The memory is a grid of rows, like lines in a notebook.
- To choose a row, it can search by content ("find the row that looks like this") or by location ("stay where I was" or "move one row down").
- To read, it takes a blend that is mostly the chosen row. To write, it first rubs out part of the row, then adds new information.
- By training on examples, it learned a real step-by-step method for copying lists, and it mostly worked on lists much longer than any it had practised on.
Why robots care
A robot tidying a house might need to answer "where did I put the screwdriver ten minutes ago?" That's too long ago to keep in its short-term view. A notebook-style memory lets it write down where things are and look them up later, by what they are ("the screwdriver") or by place ("the shelf next to me").
Try it
- Have a friend read out ten words. Write each one on a new line as you hear it. Then go back to the top and read them out in order. You just used the same copy method the network learned: write and move down, jump back to the start, read and move down.
- In the demo further down, set Gate g to 0 and Shift to +1. The focus moves from row 10 to row 11, like turning to the next line. Then set Gate g back to 1 and raise Key strength β to lock onto row 5 by content. Try Sharpen γ to make a blurry focus crisp.
Words to know
- Memory
- A grid of rows the network can read from and write to.
- Controller
- The part of the network that decides what to read and write.
- Head
- The network's "pencil": where it is currently reading or writing.
- Content addressing
- Finding a row by what is written in it.
- Location addressing
- Finding a row by where it is, such as the next one down.
Going deeper: everything below is the full version, with some maths. On a first read, skim the headings and the "Why it matters for robotics" box, and come back for the rest later.
1. Summary
TL;DR
An LSTM's memory is its hidden state, so more memory means more weights. The Neural Turing Machine (NTM) separates the two. A controller network (feed-forward or LSTM) talks to an N×M memory matrix through read and write heads. Each head holds a weighting over memory rows: a soft pointer. Because every operation is a weighted sum, the whole machine trains by gradient descent. On copy, repeat copy, associative recall, n-gram prediction and priority sort, NTMs learned faster than LSTMs and generalised to longer inputs than they were trained on.
- Read: a weighted average of memory rows.
- Write: erase then add, both scaled by the head's weighting, like LSTM forget and input gates applied to memory.
- Addressing: four stages. Content lookup by cosine similarity, a gate to keep the old focus, a circular shift, and a sharpening step.
- Copy result: trained on sequences of 8-bit vectors of length 1–20, the NTM copied much longer ones (the paper shows length 120, with a few local errors). LSTM degraded quickly past the training range.
- The experiments used a 128×20 memory. A feed-forward NTM for copy had about 17k parameters.
2. The core idea
Memory M_t has N rows of width M. A head's weighting w_t has N entries, each in [0, 1], summing to 1.
Read: r_t = Σ_i w_t(i) · M_t(i)
Erase: M̃_t(i) = M_{t−1}(i) ⊙ (1 − w_t(i) · e_t) # e_t ∈ (0,1)^M
Add: M_t(i) = M̃_t(i) + w_t(i) · a_t
1 Content: w^c(i) = softmax_i( β_t · cos(k_t, M_t(i)) )
2 Gate: w^g = g_t · w^c + (1 − g_t) · w_{t−1}
3 Shift: w̃(i) = Σ_j w^g(j) · s_t(i − j) # indices mod N
4 Sharpen: w(i) = w̃(i)^γ_t / Σ_j w̃(j)^γ_t # γ_t ≥ 1
k_tis a key the controller emits,β_tits strength. High β makes the content match nearly one-hot.g_t∈ (0, 1) chooses between "find by content" (g = 1) and "stay where I was" (g = 0).s_tis a distribution over allowed shifts, for example −1, 0, +1. Convolution moves the focus, which lets the head iterate through memory like a pointer increment.- A blurry shift spreads the focus a little more each step.
γ_tsharpens it back. e_tanda_tare erase and add vectors. A cell is fully cleared only when both the weighting and the erase entry are 1.
3. Deep dive
Content vs location addressing
Content addressing finds memory by what's in it: "the row that looks like this key". It's good for lookup and recall. Location addressing finds memory by where it is: "the row after the last one". It's needed when values are arbitrary, like variables in a program. The pipeline gives three modes: pure content lookup, content lookup then shift (jump to a neighbour of a found item), and pure iteration from the previous position. Try them:
Interactive: the four addressing stages
16 memory rows with random contents. The key is a noisy copy of row 5. The previous focus wt−1 is on row 10. Each strip is one stage; darker means more weight. Set g = 0 and shift = +1 to watch a pointer increment. The shift slider mixes the two nearest integer shifts, as in the paper's fractional-shift variant.
What the NTM actually learned
On copy, the authors inspected the heads. The learned procedure matches what a programmer would write: during input, write each vector and move the write head one row forward. At the delimiter, jump the read head back to the start by content, then read and move forward one row at a time. That combination of a content jump and location shifts is why it generalises: the loop doesn't care how long the sequence is. The paper notes that past 128 vectors the shifts wrap around the 128-row memory and start overwriting earlier rows, a limit of memory size rather than of the learned procedure.
The line from NTMs to today
- Attention is soft addressing. The content step is softmax over similarities, the same shape as attention in Day 15 and Day 13. A Transformer's KV cache is a growing memory that is read by content but never overwritten.
- Erase-and-add came back as fast-weight and linear-attention updates. Kimi K3's KDA layers keep a fixed-size state matrix and update it with decay, erase and write steps (Module 05). The trade-off is the same one the NTM framed: fixed memory size, learned control over what to overwrite.
- Successor: the Differentiable Neural Computer (Graves et al., 2016) added memory allocation and links between writes to fix NTM issues like overlapping writes.
- NTMs were notoriously hard to train stably, which is part of why simpler attention won in practice.
4. Why it matters for robotics
- Long-horizon tasks need memory beyond the context window. "Where did I put the screwdriver ten minutes ago?" doesn't fit in a few seconds of frames. External memory, written selectively and read by content, is one design for that, and the same idea underlies episodic memory modules in embodied agents.
- Content vs location maps onto spatial memory. Look up "the object that looks like a mug" (content) versus "the cell next to where I am" (location). Neural maps and learned SLAM-like memories for navigation use both kinds of addressing.
- Fixed memory size matters on hardware. An onboard policy can't let a KV cache grow forever. A bounded memory with learned erase and write keeps compute and RAM constant over an hour-long task.
- Learning procedures, not just mappings. Copy and sort are toy versions of step-by-step behaviour such as following a recorded route back. The generalisation-to-longer-inputs test is a good one for any robot policy that should work on longer tasks than it was trained on.
5. How to study it
- Read section 3 of the paper (reading, writing, addressing), then the copy task in section 4.1. Skim the other tasks.
- Work through equations 5–9 by hand on a memory of 4 rows. Confirm the weighting still sums to 1 after every stage.
- In the 30u30 Day 17 folder, read
implementation.py, then runtrain_minimal.pyon the copy task. Usevisualization.pyto plot read and write weightings over time: you should see two diagonal lines. - Do the exercises: addressing, shift, memory, controller, sharpening.
- Test generalisation: train on short sequences, evaluate on lengths 2–5× longer, and compare with a plain LSTM.
6. Self-check
What problem does the NTM's separation of controller and memory solve?
In an LSTM, more memory means a bigger hidden state and quadratically more weights. In an NTM the memory matrix can grow without adding controller parameters.
Why are reads and writes "blurry"?
To keep them differentiable. A hard pointer to one row has no gradient with respect to the address. A weighting over all rows does.
What do g = 0 and shift = +1 do together?
They ignore content, take the previous focus, and move it one row forward: a pointer increment.
Why is a sharpening step needed?
Convolving with a soft shift distribution spreads the focus a bit every step. Over many steps the pointer would dissolve. Raising weights to γ ≥ 1 and renormalising refocuses it.
When does a write fully clear a memory cell?
Only when both the head's weight on that row and the erase vector's entry are 1. Otherwise the old value is only partly removed.
7. Further reading
- The original paper (arXiv)
- 30u30 Day 17: implementation, notebook, cheatsheet, exercises
- Next: Day 18, Pointer Networks
- Course link: Module 10, RLM harnesses: external, addressable memory again, with a Python REPL as the controller