AIAny. ← Back to AIAny

Lesson notes · One paper · three authors · one idea

Pointer Networks

By the end of this page you can explain — with real numbers — how one small twist on attention lets a single neural network point at its own input, and why that quietly unlocked problems plain sequence-to-sequence models could not touch.

Subject Discrete output sequences Date Jun 9, 2015 Venue NeurIPS 2015 · arXiv 1506.03134 Tasks Convex hull · Delaunay · TSP

Oriol Vinyals · Meire Fortunato · Navdeep Jaitly — Google Brain & University of California, Berkeley. Part of Ilya Sutskever's "30 papers" reading list.

The core idea

Don't summarize the input — point at it.

A standard attention model uses its attention weights to blend the encoder's hidden states into one context vector, then predicts the next word from a fixed vocabulary. Pointer Networks change one line of that recipe: keep the attention weights, but stop blending. Read the weights directly as a probability distribution over the input positions, and emit the position that scores highest.

That one change has an outsized payoff. The output is no longer a token from a fixed dictionary — it is an index back into the input itself. So the answer space can be different on every example: feed in n points and the model can point to any of those n positions, for any n it has never seen. Problems whose valid answers depend on the current input — sorting, convex hulls, routes — suddenly fit.

Attention used to be the glue that summarized the input. Here it becomes the finger that picks pieces of the input out.

PART A

The puzzle plain seq2seq can't solve

What is this thing for, and what shape is the problem?

Step 1 · The black box

Points go in, a fence comes out — as a list of indices.

Here is the whole task in one picture, and it is the example that will follow us through every step. We scatter five points on a sheet of paper and ask for their convex hull — the smallest fence (convex polygon) that wraps all of them, like a rubber band snapped around nails. Four points end up on the fence; one sits strictly inside.

1 2 3 4 5 5 points in POINTER NETWORK ⇒ 1 2 3 4 ⇐ indices of the fence counter-clockwise
Our running example. The output is a sequence of input indices — here {⇒, 1, 2, 3, 4, ⇐}, the fence read counter-clockwise from the lowest index. Point 5 is inside, so it is never named.

Notice what the answer is made of: not new symbols, but the numbers 1 2 3 4 — labels of points we were just handed. The arrows and are only "start" and "stop" markers. Everything in the answer is a finger pointing back at something in the input.

The paper trains the very same architecture, with no task-specific rules, to also produce Delaunay triangulations (carve the points into non-overlapping triangles) and short Travelling Salesman tours (visit every point once and come back). Three classic geometry problems, one model, learned from input–output pairs alone.

Step 2 · Why the obvious approach jams

A fixed output vocabulary can't name a moving target.

We have the task — point at hull vertices. Before the fix, see exactly where the standard recipe breaks.

A 2014-era sequence-to-sequence model (read an input sequence with one network, write an output sequence with another) ends each step the same way: a softmax over a fixed list of output symbols decided before training. For translation that list is the German vocabulary — large, but it never changes from sentence to sentence.

Convex hull breaks that assumption. With 5 input points the answer must choose among 5 labels; with 50 points, 50 labels; with 500, five hundred. The size of the answer space is the input length, and it differs on every single example. A softmax sized once, up front, simply has no slot to point at the 137th input point if it only ever saw 50.

Fixed softmax (frozen at train time) always the same K slots Answer space = how many points came in n = 3 n = 6 n = 7 …
Left: a softmax must commit to a number of output classes before training. Right: the convex-hull answer space grows and shrinks with each input. The two cannot be reconciled.
The blocker, in one line

The paper's words: these methods "require the size of the output dictionary to be fixed a priori," so they "cannot directly apply … to combinatorial problems where the size of the output dictionary depends on the length of the input." That is the wall Pointer Networks walk through.

Step 3 · The shape of the machine

Read all the points, then emit one index at a time.

The blocker is the fixed output list. The fix lives in the decoder's final step — but first, the overall shape both models share.

Like ordinary seq2seq, a Pointer Network has two recurrent halves (the paper uses single-layer LSTMs — a kind of recurrent cell that carries memory across steps). The encoder reads the input points one by one, feeding in each point as its raw (x, y) coordinates, and leaves behind one hidden state ej per point. The decoder then writes the answer, producing one hidden state di per output step.

At every output step the decoder looks back over all the encoder states and scores each input position. In a plain attention model those scores would be turned into one blended summary. The whole twist — coming in Part B — is what the Pointer Network does with the scores instead.

Encoder reads the points e₁ e₂ e₃ e₄ e₅ (x₁,y₁) (x₂,y₂) (x₃,y₃) (x₄,y₄) (x₅,y₅) Decoder writes one index per step d₁ d₂ d₃ step 1 points → e₁ (index 1) 1 2 3 Same skeleton as seq2seq — the difference is one step at the very end.
Encoder hidden states e1…e5 (one per point) on the left; decoder states d1, d2, … on the right. Each decoder step reaches back to score every encoder state. What it does with those scores is the subject of Part B.
PART B

The pointer mechanism

One step changes — and that step is the whole paper.

Step 4 · Blend versus point

Same scores, opposite endings: average them, or pick one.

We have the skeleton and the scores. Here is the fork in the road — the single design choice that separates the two models.

Both models start identically. At decoder step i they compute a relevance score uj for every input position j, then run softmax to turn those scores into weights aj that sum to 1. The split happens in what comes next.

Standard attention — blend

a₁e₁ a₂e₂ a₃e₃ a₄e₄ a₅e₅ context d′ one summary vector

The weights multiply the states and add them up. The output feeds the next layer.

Pointer Network — point

1 2 3 4 5 pick 2 the distribution IS the answer

The same weights are read as a distribution over positions. The argmax is emitted as the output index.

input point chosen / pointed-at interior point

Step 5 · Compute one pointer by hand

Score every point, soften the scores, read off the winner.

"Point at the highest score" is the idea. Now do it for real, on our five points, one decoder step at a time.

We are at decoder step 1. The model has just started writing the fence and needs its first vertex — by the paper's convention, the hull point with the lowest index, which here is point 1. For each input position j the decoder produces a raw score uj (these are illustrative logits; the real ones come from the formula in Step 6). Then softmax turns the five scores into a probability over the five points, and the biggest probability is the pointer.

Try it · point the decoder

Click a point (or a bar) to see the pointer land — Step ▸ walks the whole fence.

1 2 3 4 5

softmax over input positions

point 1
76%
point 2
9%
point 3
5%
point 4
8%
point 5
2%

Decoder step 1 — pointing at the first hull vertex.

What to watch: the pointer is just the tallest bar. As you step, the output grows one index at a time — {⇒, 1, 2, 3, 4, ⇐} — and point 5, sitting inside, is never the winner.

Why does the tallest score win so decisively? Because softmax turns differences into exponentials. Take just two of the scores — say point 1 at 3.2 versus point 2 at 1.1. The weight on point 1 is e3.2 / (e3.2 + e1.1), and a gap of 2.1 already pushes it to 89%. Drag the two scores and feel the lever:

Try it · the softmax lever

Move either score — watch a small lead become a confident pointer.

3.2
1.1
weight 1
89.1%
weight 2
10.9%

What to watch: equal scores split 50/50; a lead of about 2 already buys ~88%. The pointer is sharp because softmax is exponential.

Step 6 · The score, written down

The whole mechanism is two short lines — and one of them is just softmax.

You just produced pointers by hand. Here is the formula that produced the scores you used — and you will see you already did it.

The score uj for input position j at decoder step i is the paper's additive attention (a small learned network that compares two vectors): mix the encoder state ej with the decoder state di through two matrices, squash with tanh, and project to a single number with a vector v.

uij = v tanh( W1 ej + W2 di ), j = 1 … n
p( Ci | C1…Ci−1, P ) = softmax( ui )

v is a vector; W1, W2 are square matrices (encoder and decoder share one hidden size, typically 512).

That is the entire pointer. The first line is the score you read off the bars in Step 5; the second line is the softmax you already ran by hand. Ci is the output at step i — an index into the input, not a token from a vocabulary.

The one-line diff vs. standard attention

Standard attention has the exact same first line. It then adds a third line — d′i = Σj aij ej — blending the states into a context vector. Pointer Networks delete that line and keep the softmax as the output. Same math; the interpretation flips from weighting to selecting.

Step 7 · From one pointer to a sequence

Feed each chosen point back in, and the fence draws itself.

One step gives one index. A convex hull is a list of indices — so we loop, and each choice conditions the next.

After step 1 points at point 1, the model feeds that choice back into the decoder, advances to d2, and scores all positions again — now biased to continue the fence counter-clockwise, so point 2 wins. Then point 3, then point 4. When the scores favour the stop marker , the sequence ends. The hull {⇒, 1, 2, 3, 4, ⇐} has been written entirely out of input indices.

step 1 → 1 step 2 → 2 step 3 → 3 step 4 → 4, then ⇐
Each decoder step emits one index and the chosen point is fed back, conditioning the next. The fence closes when the model points at the stop marker. Point 5 is never chosen — it is inside.

Step 8 · Why it stretches to unseen sizes

Nothing in the pointer is tied to a fixed length.

We have a working loop. The quiet superpower is that the very same loop runs on inputs longer than anything it trained on.

Look back at the formula: it scores each input position with the same little network vtanh(W1ej + W2di), then softmaxes over however many positions there are. Add a sixth point and the loop simply scores six. There is no fixed-size output layer to retrain — the output layer is the input.

So a model trained only on point sets of size 5 to 50 still produces sensible hulls at n = 100, 200, even 500 — far past anything it saw. That is strong evidence the network learned the geometry of "what makes a fence," not a lookup table keyed on length.

Key property

A variable-size output dictionary falls out for free: every output is an index into the current input, so a single trained model handles n = 5 and n = 500 with identical weights. This is exactly what blocked plain seq2seq and Neural Turing Machines on these tasks.

PART C

Does it work — and what did it start?

The numbers, the three tasks, and the lineage.

Step 9 · The scorecard

Pointing beats blending — and the fence is right even when the index list isn't perfect.

We understand the mechanism. Now the evidence: on convex hull at n = 50, all three approaches share the encoder — only the last step differs.

"Accuracy" here is strict: the predicted index sequence must match exactly. "Area" is gentler: how much of the true hull's area the predicted polygon covers. Watch the gap between a plain LSTM, an attention-blended LSTM, and the Pointer Network.

Convex hull · n = 50

Exact-sequence accuracy

LSTM (fixed softmax)
1.9%
LSTM + attention (blend)
38.9%
Ptr-Net (point)
72.6%
0%100%
99.9% area Ptr-Net's hulls cover almost all of the true area even when the exact index list is off — the shape is right.
FAIL The plain LSTM can't even produce a valid polygon to measure area at n = 50.
1M pairs Each model trains on a million input–output examples; single-layer LSTM, 256 or 512 hidden units.
n = 500 A model trained on 5–50 still covers 99.2% of hull area at ten times its longest training size.

Generalization, in one row: train on point sets of size 5–50, test far beyond. Exact accuracy fades as expected — 92% at n = 5, 69.6% at 50, 50.3% at 100, 1.3% at 500 — yet area coverage stays above 99% the whole way. The fence keeps being nearly right long after naming every vertex in order becomes hard.

Step 10 · Three problems, one network

The same pointer learns hulls, triangulations and tours.

One task proved the idea. The paper's real flex is that nothing about the architecture is hull-specific.

Change only the training data — same encoder, same decoder, same pointer — and the network learns three different bits of computational geometry, purely from examples:

Convex hullwrap the points

Output = the fence's vertex indices, counter-clockwise. 72.6% exact / 99.9% area at n = 50.

Delaunaytriangulate

Output = triples of indices, one per triangle. 80.7% exact at n = 5; 93.0% of true triangles found.

Planar TSPshortest tour

Output = a permutation of all indices. At n = 5 it matches the optimal tour length 2.12; near-optimal up to n = 50.

A fair caveat the paper is honest about: as a TSP solver it is not competitive with classical heuristics or later neural methods — its tours drift longer as n grows past the training range. The lasting contribution was never the benchmark numbers. It was the pointer abstraction.

Can you re-explain it now?

Why can't plain seq2seq solve convex hull?

Its output layer is a softmax over a vocabulary fixed before training. Convex hull needs to choose among the input points, and the number of points changes every example — so there is no fixed list of output classes to predict.

What exactly is the "pointer"?

The attention distribution itself. The model scores every input position and softmaxes; instead of blending the states by those weights, it reads the distribution as a choice and emits the argmax position's index.

How does it differ from standard attention, in one line?

Identical scores uj = vtanh(W1ej + W2di). Standard attention then sums ajej into a context vector; Ptr-Net deletes that sum and outputs the softmax. Weighting becomes selecting.

Why does it generalize past its training length?

Because the output dictionary is the input itself. The same small scoring network is applied to however many positions exist, so a model trained on n ≤ 50 still points sensibly at n = 100, 200, even 500 — covering 99%+ of hull area.

What is the running example, end to end?

Five points; four on the hull, one inside. The decoder points one index per step — 1, 2, 3, 4 — counter-clockwise from the lowest index, then stops. Output {⇒, 1, 2, 3, 4, ⇐}. Point 5 is never named.

What's the honest limitation?

As a combinatorial-optimization solver (e.g. TSP) it loses to classical heuristics and later neural methods, and accuracy decays for large n. Its value is the reusable idea of pointing at inputs, not the benchmark scores.

Homework
Re-derive step 5 with your own five points: write down five scores, softmax them by hand, and check the argmax really is a hull vertex and never the interior point. Then imagine adding a sixth point — note that nothing in the formula has to change.

The profound impact

The day attention learned to copy.

Pointing at the input turned out to be useful far beyond geometry. Wherever an output should sometimes be lifted straight from the input, a pointer is the natural tool — and the idea spread fast.

2016 → · summarization

Pointer-generator

Summarizers that copy rare names and numbers verbatim from the source article instead of mangling them — a pointer blended with a vocabulary.

2016 → · sequences

Copy mechanisms

Code generation, table-to-text and dialogue all borrowed copy attention, letting models reproduce exact tokens they were just shown.

beyond · selection heads

Neural combinatorics

Pointer decoders became the standard front-end for learning over sets and routes, seeding a line of neural combinatorial-optimization work.

One deleted line — "stop blending, start pointing" — gave attention a second job it still does in modern systems.