liveBook · Manning

Pointer Network

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A pointer network is a sequence-to-sequence neural architecture that outputs positions or elements from its input sequence through attention-based pointers.

A pointer network (Ptr-Net) is a sequence-to-sequence model that builds an output by selecting elements from its input. Its attention mechanism acts as a pointer: at each output step, it chooses an input position rather than a category from a fixed output dictionary. This is useful when the available choices change with the length of the input, as they do in sorting, the travelling salesman problem, and convex hull construction. section 11.3.3 section 11.10

Selecting points for a convex hull

Imagine four indexed input points, [1, 2, 3, 4]. A pointer network can output [1, 4, 2]: three input positions selected in an order, with point 3 left out. For a convex hull, the selected points form the polygon around the point set. The output need not contain every input point. section 11.3.3

Figure 11.12 Pointer network (Prt-Net) estimating the output sequence [1 4 2] from the input data points [1 2 3 4]

The diagram traces the example output [1, 4, 2] back to four input positions, making the pointer interpretation visible. section 11.3.3

For the convex hull task, an encoder represents the input sequence of point coordinates. A decoder uses content-based attention over those inputs; a softmax assigns each input position a probability. At each step, the network selects a point for the output sequence. The probability distribution has one choice per input position, so its size changes with the number of points. section 11.9

A supervised convex hull experiment puts the idea into practice:

  • Generate training and validation sets of random 2D points with varying numbers of points per sample. section 11.9
  • Train a PyTorch ConvexNet on labelled point sequences using Adam and negative log-likelihood loss; track loss and masked pointer accuracy. section 11.9
  • Test across different point counts, measure hull overlap, and plot a predicted hull beside a SciPy ConvexHull result. Accuracy varies with point count, so inspect more than one input size. section 11.9
Listing 11.6 Solving a convex hull problem using pointer networks
import numpy as np
import torch
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
  
from ptrnets.Data import display_points_with_hull, cyclic_permute, 
 Scatter2DDataset,Disp_results
from ptrnets.ptr_net import ConvexNet, AverageMeter, masked_accuracy,
 calculate_hull_overlap
  
min_samples = 5
max_samples = 50
n_rows_train = 100000
n_rows_val = 1000
  
torch.random.manual_seed(231)
train_dataset = Scatter2DDataset(n_rows_train, min_samples, max_samples)
val_dataset = Scatter2DDataset(n_rows_val, min_samples, max_samples)

The Python listing begins the reproducible workflow: imports, point-count limits, and training and validation datasets. section 11.9

Pointer networks are one end-to-end approach to neural combinatorial optimization: they learn to produce a solution directly from a problem instance. A predicted sequence still needs evaluation. In the convex hull experiment, pointer accuracy and hull overlap provide different checks, and performance changes across tested input sizes. section 11.5 section 11.9

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains encoder, decoder, attention probabilities, and a supervised training workflow, but does not derive the network mathematically.
Examples
Uses a four-position pointer sequence and a PyTorch convex hull experiment with variable-sized point sets and SciPy comparisons.
What it assumes
Introduces sequence-to-sequence models and the output-dictionary problem before explaining pointers; the implementation uses PyTorch training components.
What you can do after
Apply it via PyTorch ConvexNet to train and evaluate a supervised convex hull predictor on varying point counts.
How it connects
Links attention and sequence-to-sequence models to convex hulls, travelling salesman problems, and neural combinatorial optimization.

Also explained in

Compare how 2 books explain Pointer Network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.