Code Architecture — End-To-End Memory Networks

Overview

The notebook generates bAbI-style stories, builds a MemN2N with adjacent weight tying, position or bag-of-words sentence encoding and temporal encoding, trains it with the paper’s recipe (Section 4.2), compares 3 hops with 1 hop and PE with bag-of-words, adds a word-level LSTM baseline, and checks whether the attention lands on the supporting sentences. Everything runs on the CPU.

The bugs that were fixed

  • The cost was averaged over the batch; the paper sums it (“cost is not averaged over a batch”), so the earlier steps were 32 times smaller.
  • Only the first question of each story was used, and questions could ask about objects never mentioned in the story.
  • Position encoding used a fixed sentence length J instead of each sentence’s own number of words.

Section-by-Section Breakdown

1. Setup & Imports (cell 2)

  • Imports PyTorch, NumPy and Matplotlib; saves figures as JPEG at dpi 80; registers the atexit hook that saves the executed notebook on Kaggle.
  • Seeds Python, NumPy and PyTorch with SEED = 42, turns on deterministic algorithms, uses the CPU.

2. bAbI-style data (cell 4)

  • generate_story(seed): 4 actors, 3 objects, 5 rooms; 4 starting moves and 10–20 random actions; after each action a question is asked with probability 0.6, using only the sentences written so far.
  • actor questions have one supporting sentence; actor+object questions have two (the last sentence about the object and the move that says where it is).

3. Vocabulary and tensors (cell 6)

  • Index 0 is the null symbol; vocabulary of 28 including it.
  • Each question becomes a story tensor (50, 6) with the most recent sentence first, a question tensor (6,) and the answer index. Supporting-sentence indices are converted to the same most-recent-first order.
  • 1,000 training (8,979 questions), 100 validation (932) and 200 test stories (1,841).

4. MemN2N (cell 8)

  • position_encoding: l_kj = (1 − j/J) − (k/d)(1 − 2j/J) with each sentence’s own J.
  • MemN2N(hops, d=20, encoding): K + 1 embeddings (A¹, C¹ = A², …, C^K), K + 1 temporal matrices T of size 50 × 20; the null embedding is kept at zero.
  • forward: u = B q with B = A¹; at each hop the scores uᵀm_i over non-empty memories, softmax (or no softmax during linear start), read o = Σ p_i c_i, u ← u + o; logits = u · (C^K)ᵀ.

5. Training (cell 10)

  • add_random_noise: inserts about 10% empty memories (RN).
  • train_memn2n: manual SGD; linear start at learning rate 0.005 until the validation loss stops decreasing, then softmax and learning rate 0.01, halved every 25 epochs; 100 epochs; batch 32 with summed cross-entropy; gradient norm clipped at 40; asserts the loss is finite.
  • Trains the main model (PE, 3 hops) and prints loss and validation accuracy every 20 epochs.

6. Training curves (cell 12)

  • Training loss and validation accuracy per epoch.

7. Comparison (cell 14)

  • The same training for PE with 1 hop and bag-of-words with 3 hops.
  • LSTMQA: embeddings of size 32, one LSTM layer of 64 units over all visible sentences word by word (oldest first) and the question; Adam, learning rate 10⁻³, 30 epochs.
  • Test accuracy per question kind for all four models.

8. Attention check (cell 16)

  • For each test question, the fraction of supporting sentences that are the most-attended memory at some hop; a heatmap of the three hops’ attention for one actor+object question with at least 10 sentences.

9. Summary (cell 17)

  • What the saved run shows, using only the printed numbers.

Differences from the Paper

  Paper Notebook
Data 20 bAbI tasks, 1k and 10k examples Generated stories with two question kinds
Embedding size 20 for bAbI (per task) 20
Validation 10% of the training set 100 separate stories
Language modelling Penn Treebank and Text8 Not implemented
Layer-wise tying, joint training Explored Adjacent tying only, one task
LSTM baseline Results from Weston et al. (2015) Word-level LSTM trained here
Seeds — One seed