Neural Turing Machine — Graves (2014)

N×M Matrix · Content + Location Addressing · Read & Write Heads

Overview

Graves 2014

Core idea. External memory is a 2D matrix M of shape N×W (N locations, each a W-dimensional row vector). A controller LSTM emits interface parameters each timestep to position soft attention heads anywhere over the N rows — no constraint to read/write only the top.

Addressing. Two-stage: (1) content addressing — compute cosine similarity between a key vector and every memory row, apply softmax → content weights. (2) location addressing — optionally interpolate with the previous head position, then convolve with a learned shift kernel, then sharpen. Together they allow both associative lookup and sequential scanning.

Reading. The read head returns r = MTw — a weighted sum of memory rows using the attention weights w. This is fully differentiable.

Writing. The write head first erases: M ← M ⊙ (1 − w·eT), then adds: M ← M + w·aT. Erase vector e and add vector a are both output by the controller.

Multiple heads. There can be R read heads and W write heads, each with independent addressing. All reads happen before all writes each timestep.

One Forward Pass

N=4 locations · W=3 dims · single read head + single write head
Memory matrix M — 4 rows × 3 dims
loc d0 d1 d2 content tag
M[0] 0.8 0.2 0.5 ← high attention
M[1] 0.1 0.9 0.3 medium
M[2] 0.6 0.4 0.7 low
M[3] 0.0 0.0 0.0 empty
Read head addressing — content then location
Stage A — Content addressing
key k = [0.7, 0.3, 0.5] (from controller)
M[0] · cos_sim = 0.99 → wc[0] = 0.50
M[1] · cos_sim = 0.56 → wc[1] = 0.01
M[2] · cos_sim = 0.97 → wc[2] = 0.48
M[3] · cos_sim = 0.00  → wc[3] = 0.01
Stage B — Location addressing
interpolate(g=0.8): wg = g·wc + (1−g)·wprev
shift kernel s = [0.1, 0.8, 0.1] (left/stay/right)
w̃ = convolve(wg, s) → redistribute mass
sharpen(γ=3): w = w̃γ / Σw̃γ
Final w[0] = 0.60
Final w[1] = 0.28
Final w[2] = 0.10
Final w[3] = 0.02
Content addressing is associative (similarity lookup). Location addressing enables sequential scanning. β (key strength) sharpens content similarity before softmax.
Read — weighted sum r = MTw
r = 0.60·M[0] + 0.28·M[1] + 0.10·M[2] + 0.02·M[3]
r[d0] = 0.60·0.8 + 0.28·0.1 + 0.10·0.6 + 0.02·0.0 = 0.568
r[d1] = 0.60·0.2 + 0.28·0.9 + 0.10·0.4 + 0.02·0.0 = 0.412
r[d2] = 0.60·0.5 + 0.28·0.3 + 0.10·0.7 + 0.02·0.0 = 0.454
read vector r = [0.568, 0.412, 0.454]
Controller — LSTM with read vector
input = concat([ xt, r1, r2, … ]) — all read vectors appended
ht, ct = LSTM( input, ht-1, ct-1 )
interface = Linear(ht) — emits all head parameters
output = Linear([ ht, r1, … ]) — prediction from h + reads
Write head — erase then add (separate attention ww)
Erase step: M ← M ⊙ (1 − ww eT)
erase vector e = [0.9, 0.5, 0.8] (Sigmoid)
write attn ww[0] = 0.70 (focused on M[0])
M[0] ← M[0] ⊙ (1 − 0.70·e)
     = [0.8,0.2,0.5] ⊙ [0.37,0.65,0.44]
     = [0.296, 0.130, 0.220] ← partially erased
Add step: M ← M + ww aT
add vector a = [0.5, 0.8, 0.3] (Tanh)
ww[0] = 0.70 (same write attn)
M[0] ← erased_M[0] + 0.70·a
     = [0.296,0.130,0.220] + [0.35,0.56,0.21]
     = [0.646, 0.690, 0.430] ← new M[0]
loc d0 d1 d2 ww status
M[0] 0.646 0.690 0.430 0.70 rewritten
M[1] 0.182 0.970 0.312 0.20 lightly touched
M[2] 0.597 0.448 0.679 0.08 almost unchanged
M[3] 0.010 0.016 0.006 0.02 nearly unchanged
Erase then add means write is not idempotent: the order matters. Locations with ww≈0 are untouched. The same 2-stage addressing pipeline applies to the write head independently.

Differentiable Neural Computer — Graves (2016)

Usage-based Allocation · Temporal Linkage · Freeness Tracking · Multiple R/W Heads

Overview

Graves 2016

Core idea. The DNC extends the NTM with three additional mechanisms: (1) a usage vector u tracking how much each location has been written to, (2) a temporal link matrix L recording the order in which locations were written, and (3) a freeness vector f that releases locations when read-weights dealloc them.

Allocation addressing. Instead of the NTM's shift convolution, the DNC allocates free locations by sorting them by usage. The least-used (most free) location gets the highest allocation weight. This prevents memory fragmentation without needing sequential structure.

Write addressing. A gated blend of content-based address and allocation address: ww = gw·(ga·a + (1−ga)·wc) where gw is write gate and ga is allocation gate.

Temporal links. After each write, the link matrix L updates: L[i,j] is the degree to which location i was written just after j. This enables forward and backward traversal through previously written sequences — the read head can "follow the chain."

Read modes. Each read head blends three orientations: content-based wc, forward link πf·Lwr, and backward link πb·LTwr. Soft mode weights π are output by the controller.

Write before read. Within each timestep the DNC writes first, then reads. This is intentional: the read vector rt is part of the current output yt = Wy[ht; rt], so reading from the already-updated Mt lets the model store a value and immediately reflect it in the same step's output. The temporal link Lt must also be updated before reading so that forward/backward traversal includes the current write. The cost: write and read keys are both derived from the same ht, so the model cannot react to the content it just wrote within the same step.

One Forward Pass

N=4 locations · R=1 read head · W=1 write head · shows allocation + temporal linkage
Persistent state — memory, usage, write order, freeness
Memory M & usage u
loc d0 d1 d2 usage u free ψ
M[0] 0.9 0.1 0.4 0.95 0.05
M[1] 0.3 0.7 0.2 0.70 0.30
M[2] 0.1 0.2 0.1 0.20 0.80
M[3] 0.0 0.0 0.0 0.02 0.98
Temporal link matrix L[i→j] (written i after j?)
L[i\j] j=0 j=1 j=2 j=3
i=0 0.0 0.9 0.0 0.0
i=1 0.9 0.0 0.0 0.0
i=2 0.0 0.0 0.0 0.0
i=3 0.0 0.0 0.0 0.0
L[1,0]=0.9: location 1 was written just after location 0 (write sequence 0→1).
WRITE PHASE
Write addressing — allocation + content blend → ww
Allocation a
Sorted by freeness ψ (least used first)
a[3] = 0.78 (freest)
a[2] = 0.17
a[1] = 0.04
a[0] = 0.01
Content wc
Lookup key from controller
wc[0] = 0.55
wc[1] = 0.30
wc[2] = 0.10
wc[3] = 0.05
Final ww (ga=0.7)
ww = 0.7·a + 0.3·wc
ww[3] = 0.56
ww[0] = 0.17
ww[1] = 0.12
ww[2] = 0.15
ga=1 → pure allocation (write to freest slot). ga=0 → pure content (overwrite a known location). Write gate gw can suppress all writes if ≈0.
Write to memory — erase then add, weighted by ww
// controller emits erase e and write v vectors
e = [0.9, 0.9, 0.9]    (sigmoid → strong erase)
v = [0.5, 0.8, 0.3]    (write vector)
Mt[i] = Mt-1[i] ⊙ (1 − ww[i]·e) + ww[i]·v
Example rows after write
M[3] = [0,0,0]⊙(1−0.56·e) + 0.56·v = [0.28, 0.45, 0.17] ← mostly fresh write
M[0] = [0.9,0.1,0.4]⊙(1−0.17·e) + 0.17·v = [0.85, 0.22, 0.39] ← partially overwritten
M[1] = [0.3,0.7,0.2]⊙(1−0.12·e) + 0.12·v = [0.33, 0.72, 0.21]
M[2] = [0.1,0.2,0.1]⊙(1−0.15·e) + 0.15·v = [0.16, 0.29, 0.13]
Erase happens before add at each location. A location with ww[i]≈0 is untouched; ww[i]≈1 fully replaces it with v.
Temporal link update — records new write sequence (must precede read)
pt = (1 − Σiww[i])·pt-1 + ww,t    — precedence vector
Lt[i,j] = (1−ww[i]−ww[j])·Lt-1[i,j] + ww[i]·pt-1[j]
L[i,j] → 1 means i was most recently written just after j. Updated here so the read head can use Lt.
This makes the DNC's memory behave like a linked list: you can store data in arbitrary slots, then traverse them in write order without knowing the addresses in advance.
Usage & freeness update — after write, before read
ψt = ∏r (1 − fr·wr,t-1)    — free gate × prev read weights
ut = (ut-1 + ww,t − ut-1⊙ww,t) ⊙ ψt
ut is used to compute allocation weights at the next timestep.
Key difference from NTM: there is no explicit "free" action. Freeing happens automatically when a location is read with the free gate open — the model learns when to release slots.
READ PHASE
Read head — 3-mode blend using updated Mt and Lt
Content wr,c
Associative lookup by key against updated Mt
[0.60, 0.25, 0.10, 0.05]
Forward fw = Lt·wr,prev
"What was written after last read?" — uses updated Lt
[0.90, 0.05, 0.03, 0.02]
Backward bw = LtT·wr,prev
"What was written before?" — traverse chain backward
[0.05, 0.90, 0.03, 0.02]
mode π = Softmax([πf, πb, πc]) = [0.5, 0.3, 0.2]
wr = 0.5·fw + 0.3·bw + 0.2·wr,c = [0.585, 0.345, 0.044, 0.026]
r = MtT·wr
πf=1 → follow write order forward (replay). πb=1 → reverse traversal. πc=1 → pure lookup. Reads from the already-updated Mt, so it sees the values just written this step.

Neural RAM — Zaremba & Sutskever (2016)

Discrete Addresses · Hard or Soft Access · Register File · Branching Programs

Overview

Zaremba 2016

Core idea. Neural RAM (nRAM) models a register-based random-access machine. Memory consists of a fixed set of integer-indexed slots. The controller emits a discrete address (argmax or Gumbel-softmax over N locations) rather than a continuous attention distribution — trading differentiability for sharpness.

Registers. A small set of R registers hold working values. Each timestep the controller (1) loads from memory into a register, (2) applies an ALU-like function (add, subtract, copy, compare), and (3) stores a register value back to memory. The program is a sequence of such micro-operations.

Addressing. Content-free — addresses are positional integers, not similarity lookups. The controller directly outputs a distribution over [0…N−1] and selects an address. This means reading a value requires knowing (or learning) where it was stored, not what it contains.

Soft vs hard. Training uses a soft (weighted) relaxation of discrete addressing so gradients flow. At test time, the argmax is used — one location, zero cost. The gap between soft training and hard inference is smaller than in NTM because the distribution is already very sharp.

Use case. Best for learning algorithms that are intrinsically positional: sort by index, random swap, array reversal. Content-based lookup (e.g., find a value equal to a key) is harder without similarity addressing.

One Forward Pass (one micro-operation step)

N=6 memory slots · R=3 registers · instruction: LOAD r2 ← mem[addr] then ADD r0 ← r0 + r2
Current state — memory array + register file
Memory M[0…5]
addr value note
0 12 M[0]
1 7 M[1]
2 23 target
3 0 M[3]
4 5 M[4]
5 0 M[5]
Register file R[0…2]
reg value role
R0 35 accumulator
R1 2 ptr
R2 0 scratch
Address distribution — controller outputs soft address, argmax at inference
addr[0] = 0.02
addr[1] = 0.04
addr[2] = 0.88 ← argmax
addr[3] = 0.03
addr[4] = 0.02
addr[5] = 0.01
Soft (training): read = Σ addr[i]·M[i] = 0.88·23 + … = 20.86

Hard (inference): read = M[argmax] = M[2] = 23 exactly.

Gradients flow during training through the weighted sum; inference snaps to one slot.
LOAD + ALU micro-ops
LOAD: R[2] ← M[2]
R[2] = 23
ALU: R[0] ← R[0] + R[2]
R[0] = 35 + 23 = 58
Available ALU ops
COPY rdst ← rsrc
ADD rdst ← ra + rb
INC rdst ← rsrc + 1
DEC rdst ← rsrc − 1
CMP rdst ← (ra == rb)
JZ if rcond==0: goto addr
STORE — write register back to memory at chosen address
store addr distribution → focused on M[0]
M[0] ← R[0] = 58
Memory after step
addr value status
0 58 updated ✓
1 7 unchanged
2 23 unchanged
3 0 unchanged
4 5 unchanged
5 0 unchanged
Program structure — controller unrolled for T steps
for t in 1…T:
  addr_read = softmax(Wr·ht)
  addr_write = softmax(Ww·ht)
  reg_src, reg_dst = softmax(Wreg·ht)
  op = softmax(Wop·ht) ← which ALU op
  regs ← execute(op, regs, M[addr_read])
  M[addr_write] ← regs[reg_dst]
  ht+1 = LSTM(concat(ht, regs, M[addr_read]))
All distributions are learned end-to-end from input/output examples. No explicit program is given — the controller discovers the algorithm.

Lie-Access NTM — Yang & Rush (2016)

Lie Group Manifold · Geometric Addressing · Group-Action Navigation · SO(2) / SE(2)

Overview

Yang & Rush 2016

Core idea. Replace the flat integer address space with a Lie group manifold. Each memory cell is pinned to a point on the manifold (a group element gk ∈ G). The read/write head also lives on the manifold. Moving the head means multiplying its current position by a learned group element δ — a group action, not an index increment.

Lie groups used. Typical choices: SO(2) — the circle of rotations in 2D (parameterised by one angle θ); ℝn — translations (recovers NTM-style shift); SE(2) — rotation + translation (richer 2D plane). The group law encodes the geometry: on SO(2), "move by δ" means θ ← θ + δ mod 2π.

Attention. At each timestep, similarity between head position h and each cell address gk is measured by the group metric d(h, gk) = ‖log(h−1gk)‖. A softmax over −β·d(·) gives attention weights wk. Small d → high weight.

Navigation. The controller outputs a "step" δ ∈ G (via a learned map). The head updates: ht+1 = ht · δ. On SO(2) this is just θt+1 = θt + δ. The model can learn complex traversal patterns — e.g., a zigzag (alternating +90°, −90°) to visit odd-indexed cells before even ones (oddFirst task).

Read & write. Once attention weights are computed from geometry, reading and writing are identical to NTM: r = Σ wk·vk; write via erase+add. The only change is how w is computed — geometry replaces cosine similarity.

One Forward Pass

G = SO(2) · K=8 cells equally spaced on circle · head at θ=45° · step δ=+90°
Memory cells on SO(2) manifold — 8 cells at fixed angular addresses
cell k angle gk value vk (scalar shown) d(h, gk) at h=45°
k=0 0.8 45°
k=1 45° 0.3 0° ← head here
k=2 90° 0.5 45°
k=3 135° 0.9 90°
k=4 180° 0.1 135°
k=5 225° 0.4 180°
k=6 270° 0.6 135°
k=7 315° 0.2 90°
SO(2) circle
h 180°
δ = +90° rotation
h: 45° → 135°
Geometric attention — softmax over −β·d(h, gk), β=0.02
k=1 (45°) d=0° → w = 0.43
k=0 (0°) d=45° → w = 0.18
k=2 (90°) d=45° → w = 0.18
k=3 (135°) d=90° → w = 0.07
k=4…7 (far) → w ≈ 0.01-0.03 each
r = 0.43·v1 + 0.18·v0 + 0.18·v2 + 0.07·v3 + … = [weighted blend]
Increasing β sharpens attention — β→∞ recovers hard nearest-neighbour lookup. β is a learnable parameter or fixed hyperparameter.
Controller — LSTM emits group step δ and write vectors
input = concat([ xt, rt ]) — same as NTM
ht = LSTM( input, ht-1 )
δ = lie_exp( Wδ·ht ) — map ℝdim(𝔤) → G via exponential map
hpos,t+1 = hpos,t · δ — group action: new head position
e, a = Sigmoid(We·ht), Tanh(Wa·ht) — erase, add (NTM-style)
The exponential map lie_exp converts a Lie algebra vector (tangent space) into a group element. For SO(2) this is simply exp(iθ), i.e., a rotation by angle θ. The group element is never stored as Euler angles — it lives on the manifold.
Group action — head moves on manifold, re-compute attention
SO(2) group law: θa · θb = (θa + θb) mod 360°
hpos = 45°,   δ = 90°
hpos,new = (45 + 90) mod 360 = 135°
New closest cell: k=3 at 135° (d=0°)
New attention peak: w[3] → ~0.52
oddFirst zigzag example: cells at 0°=even, 45°=odd, 90°=even, 135°=odd, … The model learns δ=+90° on odd steps and δ=−90° on even steps — a zigzag that visits only odd addresses first, then even. The group structure makes this geometrically regular and easy to learn.
Write — identical to NTM erase+add, attention from geometry
wk = softmax(−β · d(hpos, gk)) — geometric weights
vk ← vk ⊙ (1 − wk·e) — erase
vk ← vk + wk·a — add
The group structure does not constrain what values can be stored — values vk are arbitrary float vectors of dimension W. Only the addressing scheme changes. This means any NTM task can be attempted; the question is whether the group geometry helps or hurts generalization on that task.

Comparison

(NTM · DNC · Neural RAM · Lie-Access)

Side-by-side property table

Key architectural decisions distinguishing the four matrix-memory (random-access) designs.

Property NTM — Graves 2014 DNC — Graves 2016 Neural RAM — Zaremba 2016 Lie-Access NTM — Yang 2016
Memory repr N×W float matrix
fully soft, all locations active
N×W float matrix
+ usage u, precedence p, link L
N integer-indexed slots
+ R small register file
K cells on Lie group manifold
each pinned to group element gk ∈ G
Read addressing Content (cosine) + Location (shift)
2-stage; soft weighted sum over N
Content + Forward link + Backward link
3-mode blend via π weights
Positional (integer address)
soft during train, hard at test
Geometric — group distance d(h, gk)
softmax(−β·d); head lives on manifold
Write addressing Same 2-stage as read (independent head)
separate key / shift / gate
Allocation (freest slot) + Content blend
ga gates between them
Separate positional address
controller outputs store-addr dist
Same geometric weights as read head
single head (read + write share position)
Write operation Erase vector then add vector
M ← M⊙(1−w·eT) + w·aT
Same erase+add
also updates usage u, link L, precedence p
Hard overwrite or soft blend
M[addr] ← reg value
NTM erase+add
vk ← vk⊙(1−wk·e) + wk·a
Memory management None — model must learn not to clobber
no usage tracking
Explicit usage tracking + freeness gates
free gate on reads; alloc writes to least-used
None — positional, no overlap guard
model learns not to alias
None — cells at fixed manifold addresses
geometry prevents unintended overlap if cells spaced far apart
Temporal traversal Location shift (left/right by 1)
limited to local neighbours
Forward / backward link matrix L
follows write-order chain, not spatial
Implicit — controller increments pointer register
INC/DEC on address register
Group action h ← h·δ on manifold
any group element reachable in one step; zigzag, spiral, etc.
Attention type Always soft — weighted sum over all N
never argmax
Always soft — weighted sum
never argmax
Soft train / hard test
Gumbel-softmax bridges the gap
Soft — geometric softmax
β controls sharpness; β→∞ = hard nearest cell
Best suited for Copy, sort, associative recall
where content lookup matters
Algorithms with structured access patterns
graph traversal, question answering, replay
Positional algorithms, arithmetic
addition, sorting by index, pointer arithmetic
Tasks with geometric structure in access pattern
oddFirst, skip-k, spiral traversal, rotation-invariant tasks
Controller LSTM (or FF)
output = f(ht, read vectors)
LSTM
richer interface vector (usage gates, mode π)
LSTM
also receives register file as input
LSTM
outputs Lie algebra vector → exp map → δ ∈ G
Chomsky class Context-sensitive & beyond
sufficient for Turing-complete tasks
Context-sensitive & beyond
arguably stronger due to link traversal
Context-sensitive & beyond
discrete RAM = Turing machine if T unbounded
Context-sensitive & beyond
same capacity as NTM; group structure aids inductive bias