TL;DR
A pointer network is a encoder-decoder model whose output at each step is a choice of one position in the input. Instead of picking a word from a fixed vocabulary, it uses attention scores as a probability distribution over the input items, and the chosen item is the output. This solves tasks where the answer is a list of input positions whose length depends on the input, such as the order of points on a convex hull.
Builds on: Encoder-Decoder Architecture, Attention Mechanism, Long Short-Term Memory · Leads to: Pointer Networks (the 2015 paper)
Where it's used
Routing and delivery. A courier must visit a set of stops and return to the depot. The answer is an ordering of the stops, and the number of stops changes from day to day. A pointer network can output the next stop by pointing at one of the unvisited stops at each step.
Computational geometry. Given a set of points, the convex hull is the smallest convex polygon enclosing them. Its vertices, listed in order, are a list of input positions. The paper tests the model on this problem, along with Delaunay triangulation and a small planar travelling salesman problem.
Copying and sorting. Sorting a list of numbers can mean outputting input positions in sorted order. Later models use the same idea to copy a word from the source text into a translation or summary when the word is rare or is a name. The copy step is a pointer, while the rest of the model writes ordinary words.
How it works
Suppose we want to sort four numbers by outputting their positions. The input is , and the target output is the position order , because the smallest value is at position 3, then position 1, and so on.
- Encode. An LSTM reads the four numbers one at a time and produces one hidden state per input, . Check: there are four states, one per input position.
- Start the decoder. A second LSTM starts from the encoder's final state and reads a start token. Its state is . This state summarises what has been output so far, which is nothing yet.
- Score every input position. For each position , compute a score that compares with . Suppose the scores are . Check: four scores, one per input.
- Turn scores into probabilities. Apply softmax over the four scores. The result is about . Check: the four numbers add to 1, and the largest is at position 3.
- Point. Output the position with the highest probability, which is 3. The output is the index 3, not the number 0.1, though the value at that position is the smallest.
- Feed the pointed item back. The decoder reads the input at position 3 as its next input, updates its state to , and repeats steps 3 to 5. A trained model should now point at position 1, the next smallest value. The decoder must not choose position 3 again, so the example masks it out, which is how repeated outputs are prevented here.
- Stop. When the decoder points at an end token, or has produced four pointers, the output is .
flowchart LR I["Inputs: 0.3, 0.9, 0.1, 0.6"] --> E["LSTM encoder: e1 to e4"] E --> D["Decoder state d1"] D --> S["Scores u_j for each position j"] S --> P["Softmax over the 4 positions"] P --> O["Point at position 3"] O -->|"feed input 3 back, mask position 3"| D
The diagram shows one loop of steps 3 to 6. Each pass through the loop produces one pointer.
The key difference from a normal sequence model is step 5. The output list is made of positions, and the probability distribution in step 4 has as many entries as there are inputs. For a list of length 4 there are 4 choices, and for a list of length 500 there are 500 choices, with the same model.
The math
Let the input be , a sequence of vectors, and let the output be , a sequence of indices, each between 1 and . The encoder produces states , and the decoder produces states .
The standard sequence-to-sequence model defines the probability of the output with the chain rule, . Its output distribution has a fixed size, so it cannot point at inputs of varying length.
Step 3 in symbols (the pointer scores). For output step and input position , the pointer network computes
where , and are learned. This is the additive form of attention from Bahdanau et al. The vector scores how well position fits the decoder's current state.
Step 4 in symbols (the output distribution). The pointer distribution is
so the output is a probability over the input positions. The softmax is over the input length, so its size changes with .
Step 5 in symbols (the key difference from attention). In standard attention, the scores are turned into weights and used to blend the encoder states, , which becomes a context vector. A pointer network does not blend. It uses the same directly as the output distribution, and the chosen index is the answer.
Step 6 in symbols (the input fed back). The chosen input is copied as the decoder's next input, so the model conditions on the actual item it pointed at.
Cost. Each output step scores all positions, so an output of length costs about score computations. For outputs that are about as long as the input, this is work in big-O terms. That is much cheaper than exhaustive search for problems such as sorting, but more than the of the classical exact convex hull algorithms.
Worked examples
Three inputs get scores at one output step.
- Exponentiate: , , .
- Sum: .
- Probabilities: , , .
- Check: .
- The highest probability is at position 3, so the pointer selects position 3.
Two inputs tie at a score of 0.5, and the third scores . Find the pointer probabilities.
- Exponentiate: for the first two, and for the third.
- Sum: .
- Probabilities: for each tied position, and for the third.
- Check: .
- The model cannot choose between the two tied positions from the scores alone. Any rule that breaks the tie, such as the lower index, is a design decision outside the softmax.
A courier has visited stop 1 and must choose the next stop from stops 2, 3 and 4. The pointer scores are for stops 2, 3 and 4. Stop 1 is excluded from the choice.
- Exponentiate: , , .
- Sum: .
- Probabilities: stop 2 gets , stop 3 gets , stop 4 gets .
- Check: , which is 1 up to rounding.
- The pointer selects stop 3, the most probable.
Try it: Suppose stop 3 has already been visited, so it is also excluded. Using the same scores for stops 2 and 4, what is the probability of choosing stop 2? Answer: , so the probability of stop 2 is , and stop 4 gets .
Common mistakes and tradeoffs
It is tempting to think a pointer network outputs the values it points at. In fact it outputs the position. In the sorting example, the answer is 3, not 0.1. A downstream step must look up the value if it is needed.
It is tempting to think the model handles any length correctly once trained. In fact the paper shows that generalisation has limits. On the convex hull task, a model trained on lengths 5 to 50 reached 69.6% accuracy at length 50. At length 500 its accuracy fell to 1.3%, although its area coverage stayed at 99.2%. The model can extrapolate in length, but exact accuracy falls quickly.
It is tempting to think the pointers always give the best answer. In fact the paper's travelling salesman results are approximate. For 10 cities the pointer network reached a tour length of 2.88, against 2.87 for the optimal tour. For 50 cities, after training on 5 to 20 cities, it reached 7.66, which is worse than algorithm A1 at 6.46 in the same table.
When to use it. Use a pointer network when the output is a sequence of positions in the input, the output length depends on the input, and you have many input-output examples.
When not to use it. If the output words come from a fixed vocabulary, a normal decoder is simpler. If an exact, fast algorithm exists, such as the convex-hull algorithms the paper cites, use it, since the paper notes that exact solutions are available. If the output must always be valid, such as a valid tour, the decoder needs an extra constraint, as the paper applies in its beam search for the travelling salesman problem.
Alternatives. Copy mechanisms mix a pointer with a normal word distribution, which suits text where some outputs are copied and some are generated.
History
Sequence-to-sequence models with LSTMs (Sutskever, Vinyals and Le, 2014) mapped one sequence to another with a fixed output vocabulary, which made them strong at translation. Bahdanau, Cho and Bengio (2014) added attention so that the decoder could look back over all encoder states, which improved translation of long sentences.
Vinyals, Fortunato and Jaitly (2015) noticed that attention already produced a distribution over input positions, and that this distribution could be the output. Their paper introduced the pointer network and tested it on convex hulls, Delaunay triangulations and planar travelling salesman tours. Its main claim was that the output dictionary could change size with the input, something the earlier models could not do directly.
Recap
- A pointer network uses attention scores over the input as its output distribution, so each output is a position in the input.
- The output vocabulary has the same size as the input, which lets one model handle inputs of different lengths.
- It does not blend encoder states into a context vector. The chosen index is the answer, and the chosen input is fed back.
- Length generalisation is real but limited: accuracy falls as the input grows well past the training lengths.
- Read next: the Pointer Networks paper for the experiments, and Bahdanau attention for the scoring function it reuses.
Check yourself
QuestionIn one sentence, how does a pointer network's output differ from a normal sequence-to-sequence decoder's output?Show answer
A normal decoder chooses from a fixed vocabulary, while a pointer network chooses one position among the input items, using a softmax over the input length.
QuestionThree inputs get scores . What probability does the middle position get?Show answer
Exponentiate: , , . The sum is . The middle probability is .
QuestionWhy does a pointer network need the chosen input to be fed back into the decoder, and what would go wrong without it?Show answer
The decoder needs to know which item it has already chosen, so it does not repeat it and so the next choice depends on the sequence so far. Without the feedback, each step would see only the decoder's own state, and the model could output the same position repeatedly or ignore the structure of the partial answer.
QuestionA team wants to route a delivery van through 40 stops, and they have a fast exact solver for their small instances. Should they use a pointer network? Give a reason for your answer.Show answer
Probably not as the main method. The paper shows that pointer networks give approximate tours that can be worse than classical heuristics when generalising to larger sizes, and an exact or heuristic solver is often better for a fixed problem. A pointer network may be worth testing if the problem has many variations that a single learned model could cover.
Sources
- Vinyals, Fortunato and Jaitly, "Pointer Networks", arXiv:1506.03134 (2015). https://arxiv.org/abs/1506.03134
- Bahdanau, Cho and Bengio, "Neural machine translation by jointly learning to align and translate", arXiv:1409.0473 (2014). https://arxiv.org/abs/1409.0473
- Travelling salesman problem, Wikipedia. https://en.wikipedia.org/wiki/Travelling_salesman_problem