Code Architecture — Pointer Networks
Overview
The notebook implements a Pointer Network in PyTorch: an LSTM encoder, an LSTM decoder and additive attention whose softmax over input positions is the output distribution. It trains the model on random planar convex hulls (5–50 points), evaluates it on held-out point sets of 5–50, 55–65 and 75–85 points with greedy decoding, plots predictions and attention, and trains a second model to sort lists of numbers.
The bug that was fixed
The earlier version set the end-of-sequence (EOS) target to n, the number of points in each example. The model appends its EOS position after the max_n positions of the padded batch, so for every example with n < max_n the target pointed at a masked padding position whose logit was −∞. The cross-entropy was therefore infinite, the printed training loss was inf in every epoch, and the model never learned to stop (sorting exact-match accuracy 0.00%). collate_fn and sort_collate_fn now set the EOS target to max_n, and training asserts that the loss is finite.
Section-by-Section Breakdown
1. Setup & Imports (cell 2)
- Imports
torch,numpyandmatplotlib; saves figures as JPEG at dpi 80; registers theatexithook that saves the executed notebook on Kaggle. - Seeds Python, NumPy and PyTorch with
SEED = 42and turns on deterministic algorithms.
2. Convex hull data (cell 4)
convex_hull_indices: Andrew’s monotone chain, counter-clockwise, rotated to start at the hull point with the lowest index (Section 3.1 of the paper).generate_dataset(n_samples, min_points, max_points): points uniform in the unit square, a random size per example. Shows one 20-point example.
3. Dataset & DataLoader (cell 6)
ConvexHullDatasetreturns points(n, 2), target = hull indices + EOS, andn.collate_fnpads points tomax_n, builds the padding mask (1 = padding), pads targets with −1 and sets the EOS target tomax_n.- 50,000 training examples and 500 test examples with 5–50 points; 100 examples each with 55–65 and 75–85 points.
4. Pointer Network (cell 8)
PointerNet(input_dim, hidden_dim=128): LSTM encoder and LSTM decoder (both 128 units), attentionu_j = vᵀ tanh(W_s s_t + W_e e_j).- A zero vector is appended to the encoder outputs as the EOS position; padding positions are masked to −∞ before the softmax.
- The decoder’s first input is a zero vector; afterwards it is the coordinates of the chosen point (teacher forcing in training), looked up in one indexed operation. The decoder starts from the encoder’s final state.
predict: greedy decoding until every example has pointed at EOS ormax_output_lensteps.
5. Training (cell 10)
- Cross-entropy at each decoder step over real target steps (EOS included), averaged; Adam with learning rate 10⁻³; gradient-norm clipping at 2.0 (as in Section 4.1); batch 128; 50 epochs; teacher forcing throughout.
- Prints the loss every 5 epochs, asserts that all losses are finite, and plots the loss curve.
6. Evaluation (cell 12)
evaluate_model: sequence accuracy (prediction equals the true hull up to the starting point) and point accuracy (fraction of true hull points that appear anywhere in the prediction), for the three test sets.
7. Predictions (cell 14)
- For 3 test examples: true and predicted hulls over the points, and the order in which points were chosen.
- The printed
Matchline compares the sets of indices only (order and repeats are ignored), so it is more lenient than the sequence accuracy in Section 6.
8. Attention heatmaps (cell 16)
- Softmax of the pointer logits under teacher forcing for 2 test examples.
9. Sorting (cell 18)
- 20,000 training and 500 test lists of 5–30 distinct values from {0, 0.001, …, 0.999}; target = indices in ascending order + EOS.
PointerNet(input_dim=1, hidden_dim=128), 30 epochs with the same training function; exact-match accuracy with greedy decoding; one example; loss curve.
10. Summary (cell 19)
- What the saved run shows, using only the printed numbers: finite loss (0.2411 after 50 epochs), exact-hull accuracy 43.40% / 20.00% / 14.00% on the three test sets, the repeated-point mistakes, and the failed sorting bonus (5.20%).
- Added after the Kaggle run; the code cells and their outputs are exactly those of Kaggle version 4.
Differences from the Paper
| Paper | Notebook | |
|---|---|---|
| Hidden units | 256 or 512 | 128 |
| Training examples | 1M | 50,000 (hulls), 20,000 (sorting) |
| Optimiser | SGD, learning rate 1.0, uniform init in [−0.08, 0.08] | Adam, learning rate 10⁻³, PyTorch default init |
| Decoding | Beam search | Greedy |
| Test sizes | n = 5 to 500 | 5–50, 55–65, 75–85 |
| Tasks | Convex hull, Delaunay, TSP | Convex hull, sorting |
| Metrics | Accuracy and area coverage | Sequence accuracy and point accuracy |