Relational Recurrent Neural Networks
Split an LSTM's memory into slots and let the slots attend to each other at every step. The network gets much better at comparing things it saw at different times.
Start here (no maths needed)
A computer that keeps its memories in separate boxes, and lets those boxes talk to each other, gets much better at comparing things it saw at different times.
Think of it like this
Think of a detective's notebook. Each clue gets its own page. When a new clue arrives, the detective flips back through the other pages and asks, "Does this connect to anything I already know?" A normal memory program stuffs all clues onto one messy page. This program keeps a few separate pages, and at every step each page checks the others and the newest clue.
How it works
- The program has a few memory slots, like pages in a notebook.
- Something new arrives, for example a point on a map.
- Each slot looks at the other slots and the new thing, and pays most attention to the ones that matter.
- Each slot decides how much old memory to keep and how much new information to write.
- This repeats at every step, so the program can later answer questions that need comparing things from different times.
Why robots care
Objects leave a robot's camera view and come back. A robot tidying a room might see a toy, turn away, then see a box. To decide which toy is furthest from the box, it has to remember things it can no longer see and compare them.
Try it
- Have a friend read out 6 places in your town, one at a time. Then they ask: "Which one is second furthest from school?" Notice how you have to remember them all and compare.
- In the demo further down, move the Reference vector m slider to pick the starting point, and the Rank n slider to choose "1st furthest", "2nd furthest" and so on. The highlighted line is the answer. Press New sequence for a fresh set of points.
Words to know
- Memory slot
- One separate box where the program stores part of what it has seen.
- Attention
- A way for each slot to choose which other slots to listen to most.
- Recurrent
- Working step by step and carrying memory forward from one step to the next.
- Gate
- A dial that decides how much old memory to keep and how much new to add.
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
Memory networks are good at storing and retrieving. This paper asks whether they can also relate what they've stored. It builds the Relational Memory Core (RMC): a matrix of memory slots that runs multi-head self-attention over itself plus the new input at each time step, then updates through LSTM-style gates. On a toy task built to need comparisons across time, LSTM and DNC baselines stayed under 30% accuracy while the RMC reached 91%. It also improved program evaluation, a partially observed Pac-Man game, and word-level language modelling.
- Memory:
Nslots, each a vector. Weights are shared across slots, so adding slots adds no parameters. - Interaction: each slot queries every slot and the new input with dot-product attention. Information moves between slots based on content.
- Recurrence: the attended result goes through a row-wise MLP and then input and forget gates, like an LSTM cell state per slot.
- Language modelling: test perplexity 31.6 on WikiText-103 vs 34.3 for their tuned LSTM. Also better on Project Gutenberg and GigaWord.
- Honest caveat: the authors say they can't prove the gains come from "relational reasoning" as such. They treat it as better function, not a proven mechanism.
2. The core idea
Let M be the memory matrix (one row per slot) and x the new input. One attention step:
Q = M·W_q
K = [M; x]·W_k
V = [M; x]·W_v
M̃ = softmax(Q·Kᵀ / √d_k) · V # same shape as M
[M; x]: the memory with the input stacked on as one extra row. Keys and values come from both, queries only from memory, so the output keepsM's shape.softmax(Q·Kᵀ/√d_k): how strongly each slot reads from each slot and from the input. This is the attention from Day 13.- Several heads run in parallel with their own projections, and their outputs are concatenated.
M̃ is a proposed update. It's passed through a row-wise MLP g_ψ (with residual connections) and then gated into memory:
m_{i,t} = σ(f_{i,t} + b̃_f) ∘ m_{i,t−1} + σ(i_{i,t}) ∘ g_ψ(m̃_{i,t})
h_{i,t} = σ(o_{i,t}) ∘ tanh(m_{i,t})
f, i, o are the usual forget, input and output gate pre-activations computed from x_t and the previous output. So each slot behaves like an LSTM cell state from Day 2, but its candidate update comes from attention over all slots. The authors note that in practice they didn't need the output gate.
3. Deep dive
Three ways to hold memory
- LSTM: one vector holds everything. Easy to train, but it's hard to keep separate facts apart.
- Slot memories like the NTM (Day 17) and DNC: separate rows, read and written by addressing. Facts stay apart, but rows don't interact once written.
- RMC: separate rows and they talk to each other every step. It's the Relation Network idea from Day 19, with attention in place of a pairwise MLP, inside a recurrent loop.
Attention within a step, not over the whole past
A Transformer attends over every past token, so its memory and cost grow with the sequence. The RMC attends only over its N slots and the current input. Cost per step stays fixed, whatever the sequence length. The paper's discussion points out that growing buffers are a problem for online RL, where the stream never ends. Module 05 covers the same trade-off at modern scale: fixed-size recurrent state vs a full attention cache.
Slots versus slot size
Because weights are shared across rows, you can trade many small slots against a few big ones without changing the parameter count. The paper found the best ratio depends on the task. The N-th farthest task wanted many slots. Language modelling used a single large slot. Program evaluation did well with 4 slots and 4 heads.
The N-th farthest task
The model sees eight random 16-dimensional vectors, one per step, each with a label. Then it must answer: "which vector is the n-th farthest from vector m?" It has to remember all eight, compute every distance to m (which may arrive last), and sort those distances. The demo is the same task in 2D.
Interactive: the N-th farthest question
Eight labelled vectors. Pick the reference m and the rank n. Lines show distances to m; the answer is highlighted.
4. Why it matters for robotics
- Robots rarely see everything at once. Objects leave the camera view and come back. The Pac-Man task with a 5×5 viewport is a toy version: the RMC scored about 677 vs an LSTM's 550, because it had to remember ghosts and eaten food outside its view.
- Fixed cost per step suits control loops. A recurrent core with a bounded memory runs at the same speed at minute one and hour two. That's useful for long-running agents where a growing attention cache isn't affordable.
- Slot memories are a step toward per-object memory. Tracking several objects and comparing them over time is the shape of many manipulation and navigation problems. Note the RMC doesn't guarantee one slot per object. Later object-centric models push harder on that.
- Keep expectations calibrated. Language-modelling gains were modest (5–12% relative perplexity), and large Transformers later took over most of these tasks. The lasting lesson is the design pattern, not this exact cell.
5. How to study it
- Read sections 3 and 4 of the paper, and appendix A.1 for the N-th farthest setup (about 1.5 h). Sketch Figure 1 from memory.
- In the 30u30 Day 20 folder, read
implementation.pyand trace one step: input stacked onto memory, attention, MLP, gates. - Do the exercises in order: dot-product attention, gating, one RMC step, the N-th farthest data generator, then training.
- Run
train_minimal.pyand compare against an LSTM baseline on N-th farthest. - Stretch: sweep the number of slots and heads at a fixed total memory size, and see how accuracy changes.
6. Self-check
Why are queries computed from M only, but keys and values from [M; x]?
So the output has exactly one row per memory slot. Each slot can read from other slots and the new input, but the input doesn't get a row of its own in the next memory.
Why doesn't adding memory slots add parameters?
The projection, MLP and gate weights are shared across rows. More rows means more storage and more compute, not more weights.
How does the RMC differ from a Transformer layer over the sequence?
A Transformer attends over all past positions, so cost grows with length. The RMC attends only over its fixed set of slots and the current input, then carries the slots forward recurrently.
Why is N-th farthest hard for a plain LSTM?
It must keep eight vectors separately, then compare distances to one of them, possibly seen last. Packing all of that into one vector and sorting implicitly is hard. The LSTM stayed under 30%.
What does the RMC borrow from the LSTM?
The gating: a forget gate on the old slot value and an input gate on the new candidate, applied per slot. That keeps training stable over long sequences.
7. Further reading
- The original paper, with appendices on BoxWorld and per-task settings
- 30u30 Day 20: implementation, notebook, cheatsheet, exercises
- Next: Day 21, Neural Message Passing for Quantum Chemistry