Search

Search papers and lessons

Pointer Networks

Oriol Vinyals, Meire Fortunato, Navdeep Jaitly · 2015

Pointer Networks use attention to point at an input position as the output, so the answer can be as long as the input and no fixed output list is needed.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Architectures
  • Probability
  • ReadReady to readNot yet

TL;DR

Standard sequence models choose each output from a fixed list of words, so their output list must be the same size every time. A pointer network instead uses attention to point at one position in its input, and the output is that position. Because the list of choices is the input itself, the output can be as long as the input needs. The authors train such networks from examples alone to produce approximate solutions to convex hulls, Delaunay triangulations and small travelling salesman tours.

Builds on: Pointer Network, Recurrent Neural Network, Long Short-Term Memory, Attention Mechanism · Leads to: sequence-to-sequence models for variable-size outputs

Why it mattered

The paper opened a way to apply neural networks to problems whose answer is a selection of input items, such as a route through cities or a list of boundary points. Before it, models that read a sequence and wrote a sequence needed a fixed output vocabulary, which made problems like sorting variable-sized lists or routing awkward. The idea of using an attention distribution directly as the output has since been reused in other models that copy or select from their input. The arXiv version appeared in June 2015, and Semantic Scholar counts about 3,900 citations for it. The authors called their approach a "purely data driven" way to get approximate answers to problems that are hard to solve exactly.

The problem

A sequence-to-sequence model, built from two recurrent networks (an encoder and a decoder), estimates the probability of an output sequence one element at a time. Each element comes from a softmax over a fixed set of outputs. The paper points out the flaw: for a problem where the answer is a set of input positions, the number of outputs depends on the input length. A model trained for 5 cities cannot answer for 10 without retraining, because its output layer has 5 slots.

A second flaw is that the encoder compresses the whole input into one vector. The attention model of Bahdanau and colleagues lets the decoder look back at every encoder state, which helps a lot. It still blends those states into a context vector, so its output dictionary is still fixed. The paper asks for a model that keeps the attention and drops the fixed output list. Its three geometric tasks were chosen because the exact answers are known, so the model's output can be scored.

How it works

Take a tiny input of four points and trace one output step. The attention scores below are made up for illustration. They are not from the paper.

flowchart LR
  A["Input points P1 to Pn"] --> B["Encoder LSTM gives states e1 to en"]
  B --> C["Decoder LSTM gives state d_i at output step i"]
  C --> D["Score each input: u_j from e_j and d_i"]
  D --> E["Softmax over the n inputs gives probabilities"]
  E --> F["Output the index with the highest probability as a pointer"]
  F --> G["Feed the chosen point back into the decoder"]
  1. Read the input. Four points P1, P2, P3, P4 go into an encoder LSTM one at a time. The encoder leaves a state e_j for each point. Check: there are four states, one per input point, so the model keeps track of all four.

  2. Start the decoder. The decoder LSTM starts from the encoder's final state and receives a start symbol. At each output step i it produces a decoder state d_i. Check: d_i summarises what has been output so far.

  3. Score every input position. For each input position j, compute u_j = v^T tanh(W1 e_j + W2 d_i), where v, W1 and W2 are learned. Suppose the four scores are u = [2.0, 0.5, -1.0, 0.0]. Check: there is one score per input, so the number of scores follows the input length.

  4. Turn scores into a pointer distribution. Softmax gives exp(u_j) divided by the sum of all exp(u_k). The exponentials are 7.39, 1.65, 0.37 and 1.00, which sum to 10.41, so the probabilities are about 0.71, 0.16, 0.04 and 0.10. Check: the four probabilities should add up to 1; after rounding to two places they add up to 1.01, which is rounding error.

  5. Point. The output is the input position with the largest probability, here position 1, which is P1. The network does not output coordinates or words; it outputs a position. Check: because the answer is a position, it always refers to a real input.

  6. Feed back and repeat. The chosen point's vector is fed as the next decoder input, and steps 2 to 5 repeat until the network points at an end symbol. For a convex hull, the output is the list of boundary points in order, starting from the lowest-index point and going counter-clockwise, as the paper specifies.

The math

The sequence-to-sequence starting point. Given input points P = {P1, ..., Pn} and an output index sequence C = {C1, ..., Cm}, the baseline models the probability with the chain rule:

p(CP∣P;θ)=∏i=1m(P)pθ(Ci∣C1,…,Ci−1,P;θ)p(\mathcal{C}^{\mathcal{P}}|\mathcal{P};\theta)=\prod_{i=1}^{m(\mathcal{P})}p_{\theta}(C_{i}|C_{1},\ldots,C_{i-1},\mathcal{P};\theta)

Training maximises the sum of log p over the training pairs. The paper's baseline uses a fixed-size softmax for every C_i, which is the flaw described above.

Step 2 in symbols: the attention scores

The attention model of Bahdanau et al. scores each encoder state against the current decoder state:

uji=vTtanh⁡(W1ej+W2di),j∈(1,…,n)u^{i}_{j}=v^{T}\tanh(W_{1}e_{j}+W_{2}d_{i}), \quad j\in(1,\ldots,n)

Here v is a vector and W1, W2 are learned matrices. The paper uses the same hidden size at the encoder and decoder, so the matrices are square. The usual attention then takes a = softmax(u) and blends the encoder states into a context vector. Pointer networks skip the blend.

Step 3 in symbols: the pointer

The Ptr-Net defines the output distribution directly from the scores:

p(Ci∣C1,…,Ci−1,P)=softmax(ui)p(C_{i}|C_{1},\ldots,C_{i-1},\mathcal{P})=\text{softmax}(u^{i})

The vector u^i has length n, the number of input points, so the output dictionary has the same size as the input. The paper sets C_i to the index j with the highest probability (or to a beam search over several such choices), so C_i is always one of the input positions. To condition on the previous output, the decoder receives the input point P_{C_{i-1}} as its next input.

Step 4 in symbols: the cost

For each output step the model computes n scores, so inference costs O(n^2) for an output of length about n. The exact convex hull takes O(n log n). The paper notes that the model has about O(n) computation per output step, and that its attention adds computational capacity to close that gap.

Step 5 in symbols: the TSP objective

For the travelling salesman problem, the output is a permutation of the city indices, and the paper uses beam search to keep only valid tours. The quality of a tour is its total length. The paper's Table 2 compares tour lengths, so lower is better.

Results

Convex hull (Table 1). The Ptr-Net was trained on points uniformly sampled in the unit square. The metrics were accuracy (whether the output is the same polygon as the true hull) and area coverage (how much of the true hull the output covers). For 50 points, the LSTM with no attention scored 1.9% with a failed area measure, the attention model 38.9% with 99.7% area, and the Ptr-Net 72.6% with 99.9% area. A single Ptr-Net trained on sizes 5 to 50 scored 92.0% at 5 points and 87.0% at 10, 69.6% at 50, 50.3% at 100, 22.1% at 200 and 1.3% at 500. The area stayed at 99.2% or higher even at 500 points, so the outputs were close to the true hull even when they were not exactly right.

Delaunay triangulation. Accuracy was 80.7% at 5 points with 93.0% of triangles correct, 22.6% at 10 points with 81.3% of triangles correct, and no fully correct triangulation at 50 points, where 52.8% of triangles were right.

Travelling salesman problem (Table 2). The planar symmetric TSP was tested on tour lengths. At 5 cities the Ptr-Net trained on optimal data matched the optimal tour (2.12), and at 10 cities it gave 2.87, the optimum. At 20 cities it reached 3.88 against an optimal 3.83. At 25 cities, a model trained on sizes 5 to 20 gave 4.30 against the Christofides-based A3 result of 4.24. At 40 cities it gave 5.91 against A3's 5.23, and at 50 it gave 7.66 against 5.79. The authors note that the model trained on the weakest heuristic (A1) outperformed that heuristic at 50 cities. Without beam search constraints to keep tours valid, at least 10% of instances with more than 20 cities gave an invalid tour.

Limits. The paper uses the same small LSTM (256 or 512 units) for every task, trained with stochastic gradient descent on 1 million example pairs, with no search over architectures. Generalisation to lengths beyond training works for convex hulls by about a factor of 10, while for TSP the paper calls the results virtually perfect at 25 cities (a tour of 4.30 against 4.24 for the Christofides-based A3) and good at 30, but says they break down at 40 and beyond. The approximate solutions are learned, not guaranteed, and the authors describe the TSP results as small scale (up to 50 cities). Errors on convex hulls mostly come from aligned points, which are a common source of errors for convex hull algorithms in general.

Reading guide

The paper's sections, in order, with the lessons each one needs:

  • §1 Introduction: Recurrent Neural Network for the sequence models it builds on, sequence-to-sequence for the paradigm it extends, and Bahdanau attention for the content-based mechanism it reuses.
  • §2.1 Sequence-to-sequence: Long Short-Term Memory for the LSTM cell, Encoder-Decoder Architecture for the two-part design, and Beam Search for how the best output is searched.
  • §2.2 Input attention: Attention Mechanism and Softmax for the score-and-normalise step.
  • §2.3 Ptr-Net: Pointer Network, which is the idea this paper introduces.
  • §3 Datasets: Convex Hull for the first task, and the Delaunay triangulation and travelling salesman definitions are given in the text. NP-hardness explains why exact TSP answers are costly for larger inputs.
  • §4 Results: the tables need only the metrics defined in §4.2 to §4.4, and the exact baselines (Held-Karp and the Christofides-based A3) are named in §3.3.
  • §5 Conclusions: no new prerequisites; it names sorting, where outputs are chosen from the inputs, as future work, and Sorting Algorithm gives the problem background.

Check yourself

QuestionWhy can a sequence-to-sequence model with a fixed softmax not answer a convex hull problem for a new input size without retraining?Show answer

Its softmax has one output slot for each symbol in a fixed dictionary. The convex hull answer is a list of input indices, and the number of possible indices equals the number of input points. A model trained with 5 slots cannot choose among 10 points, so the dictionary size is baked into training. A pointer network avoids this because its softmax runs over the input positions themselves.

QuestionThe scores for a four-point input are u = [2.0, 0.5, -1.0, 0.0]. Which position is the pointer, and what is its probability to two decimal places?Show answer

Position 1, which holds the largest score of 2.0. Its probability is exp(2.0) divided by the sum of the four exponentials. The exponentials are 7.39, 1.65, 0.37 and 1.00, which sum to 10.41. So the probability is 7.39 / 10.41, about 0.71.

QuestionIn Table 2, the Ptr-Net trained on sizes 5 to 20 reaches 3.88 for 20 cities and the optimal tour is 3.83. By how much does the Ptr-Net tour exceed the optimal, and as a percentage of the optimal?Show answer

The excess is 3.88 minus 3.83, which is 0.05. As a percentage of the optimal, that is 0.05 divided by 3.83, about 1.3%.

QuestionThe paper reports that a single Ptr-Net on convex hulls scored 1.3% accuracy at 500 points but 99.2% area coverage. Why are both numbers reported, and what does the gap mean?Show answer

Accuracy asks whether the output is exactly the same polygon as the true hull, which is strict. Area coverage asks how much of the true hull the output polygon covers, which is looser. The gap means that the outputs were usually close to the true hull, with small errors that break an exact match, rather than being far off.

QuestionWhy did the authors use beam search with a validity constraint for the TSP but not for convex hulls and Delaunay triangulation?Show answer

For TSP the output must be a valid tour that visits each city exactly once. Without the constraint, the network sometimes repeated a city or skipped one, and the paper says at least 10% of instances with more than 20 cities gave no valid tour. For the convex hull and Delaunay tasks the paper left the decoder unconstrained, and it applied the constraint only to TSP.

Sources

Read the full paper

Oriol Vinyals, Meire Fortunato, Navdeep Jaitly, 2015. arXiv:1506.03134