liveBook · Manning

NeighborSampler

As explained in Graph Neural Networks in Action

NeighborSampler is a PyTorch Geometric data-loading function or component that samples node neighborhoods and returns subgraph batches.

NeighborSampler is a PyTorch Geometric data-loading component that samples node neighborhoods and returns subgraph batches. These batches approximate full-graph convolution, allowing GNN training on graphs too large for available memory. The trade-off is potentially lower accuracy than full-batch training. section 7.7

A typical neighbor-sampling workflow is:

  • Set the mini-batch size and number of neighbors sampled at each layer. section 7.7.2
  • Create a loader over the input graph and training-node indices. section 7.7.2
  • Iterate through sampled batches, move features, labels, and adjacency data to the device, then run forward, loss, backward, and optimizer steps. section 7.7.2

Scaling a road-network GNN

A mapping team models the US road system as intersections and road segments. NeighborSampler reduces each training batch to locally sampled road neighborhoods, making the large graph more memory-efficient while risking less accurate approximations than full-batch training. section 7.7.2

Listing 7.1 Training loop using NeighborSampler
for batch_size, n_id, adjs in sampler:    #1
  x = data.x[n_id].to(device)        #2
  y = data.y[n_id].squeeze(1).to(device)   #3
  adjs = [adj.to(device) for adj in adjs]  #4

  optimizer.zero_grad()              #5
  out = model(x, adjs)             #6
  loss = F.nll_loss(out, y)         #7
  loss.backward()               #8
. optimizer.step()     #9

Inspect the training-loop listing showing sampled batches, device transfer, model execution, loss calculation, backpropagation, and optimization. section 7.7.2

How Graph Neural Networks in Action covers it

How deep it goes
Explains sampling configuration and a training loop, but defers detailed sampler functionality and PyG-version differences.
Examples
Uses PyTorch Geometric loader snippets, a sampled training loop, and a US road-network comparison.
What it assumes
Assumes graph data, training-node indices, node features, labels, a GNN model, and a standard optimization loop.
What you can do after
Apply it via PyTorch Geometric to train GNNs on graphs constrained by memory.
How it connects
Connects neighbor sampling with GNNs, GCNConv, graph convolution, mini-batch training, and full-batch training.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.