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
