Code Architecture — Memory Networks
Overview
The notebook generates a small version of the paper’s simulated world, builds a Memory Network with the I, G, O and R parts and the write-time features of Section 3.4, trains it with the margin ranking loss of Eqs. 6–8, and compares k = 2 (+time), k = 1 (+time), k = 2 without time and an LSTM baseline. Everything runs on the CPU.
The bug that was fixed
The earlier generator asked each question after the whole story but used facts from any point in it, so some answers contradicted the story, and it had no time features, so the model could not prefer the latest fact. Questions are now asked during the story, each with only the facts written before it, and the model has the pairwise write-time scoring.
Section-by-Section Breakdown
1. Setup & Imports (cell 2)
- Imports PyTorch, NumPy and Matplotlib; saves figures as JPEG at dpi 80; registers the
atexithook that saves the executed notebook on Kaggle. - Seeds Python, NumPy and PyTorch with
SEED = 42, turns on deterministic algorithms, uses the CPU.
2. Simulated-world data (cell 4)
generate_story(seed): 4 actors, 3 objects, 5 rooms; 4 starting moves, then 10–20 random actions (move, pick up, drop). After each action a question is asked with probability 0.6.- Question kinds: actor (“Where is Joe?”, one support: the latest move) and actor+object (“Where is the milk?”, two supports: the latest fact about the object and the move that says where it is).
3. Vocabulary and features (cell 6)
Vocabulary.bow: bag of words over a 27-word vocabulary.- 1,000 training stories (8,979 questions) and 200 test stories (1,841 questions); each example keeps only the facts visible when the question is asked.
4. Memory Network (cell 8)
MemNN(W, embed_dim=100, k, use_time):U_Oof size 100 × (3W + 3) andU_Rof size 100 × 3W.mem_scores: the score a_i of each memory and the weight b of the “older” feature;pair_score(i, j) = a_i − a_j + b·[i older than j].retrieve_one: loops over the memories keeping the winner (with time) or takes the arg max (without).answer: first hop on x, second hop on [x, m_o1] (for k = 2), then ranks the 5 rooms withU_R.ranking_loss_memory: margin loss against 10 sampled negative memories, pairwise in both directions when time is used.
5. Training (cell 10)
- Embedding dimension 100, SGD with learning rate 0.01, margin 0.1, 10 epochs, one question at a time (Section 5.2 settings).
- Loss = first-hop loss + second-hop loss (k = 2) + answer ranking loss. For actor questions the second hop is trained to return the single support again.
- Trains k = 2 (+time), k = 1 (+time) and k = 2 without time with the same seed.
6. Training curves (cell 12)
- Mean loss per epoch for the three models.
7. Evaluation (cell 14)
- Answer accuracy per question kind and overall, and the fraction of questions where the retrieved memories are exactly the true supports.
8. LSTM baseline (cell 16)
LSTMQA: one LSTM layer of 128 units over the visible facts and the question (bags of words), then a linear layer over the 5 rooms; Adam, learning rate 10⁻³, 10 epochs.
9. Memory scores for one question (cell 18)
- Bar plot of the first-hop scores for one actor+object test question with at least 10 facts; prints the time weight b and the retrieved and true supports.
10. Example predictions (cell 20)
- Predictions and retrieved facts of the k = 2 and k = 1 models for four test questions.
11. Summary (cell 21)
- What the saved run shows, using only the printed numbers.
Differences from the Paper
| Paper | Notebook | |
|---|---|---|
| Data | Simulation with difficulty 1 and 5, plus “where was X before” questions, single-word and sentence answers | Generated stories, latest-state questions only, single-word answers |
| Large-scale QA, hashing, unseen words | Sections 3.3, 3.5, 5.1 | Not implemented |
| Write-time features | Question, y and y′ order | Only “y older than y′” varies |
| LSTM baseline | Language model trained with backpropagation through time on answer words | Classifier over the 5 rooms, Adam |
| Seeds | — | One seed |