Graph sampling limits how much of a graph a GNN processes at once. When aggregating across every relevant node or edge becomes impractical, a sampler selects a subset for computation. In node sampling, that subset consists of nodes; neighborhood sampling selects neighbors around input nodes for message passing. section 7.7.2 section 7.9
GraphSAGE makes the idea concrete. Rather than aggregate messages from every neighbor, it draws a random sample of a node’s neighborhood. At each layer, it aggregates sampled neighbor embeddings, combines them with the central node’s representation, applies learned weights and an activation, then normalizes the result. A mean is one possible aggregation function. section 3.4.6
For mini-batch training in PyTorch Geometric (PyG):
- Choose the training input nodes, batch size, and number of neighbors to sample at each layer. section 7.7.2
- Create a NeighborLoader and iterate over its batches. Move each batch’s features, labels, and adjacency information to the device before the model’s forward and backward passes. section 7.7.2
- Tune batch size and sampled-neighbor count against memory use and computation. Repeated appearances of sampled nodes can cause redundant work. section 7.6.1
Planning for a growing geographical graph
Suppose nodes represent geographical areas, with features such as land value and zoning, and edges represent proximity or road connections. As the graph grows toward millions of nodes, GraphSAGE’s neighborhood sampling and mini-batching offer a way to train without processing the entire graph structure for each computation. section 7.6.1 section 3.1.2
Sampling is also a data-access choice. With remote storage, a GraphStore supplies graph structure, a FeatureStore supplies node features, and a sampler produces subgraphs from input nodes for a loader. Custom samplers can implement node-level or edge-level sampling. Database connections, sampler correctness, and I/O latency then become part of the training problem. PyG also offers samplers for different needs; their available functionality can vary by version. section 7.9 section 7.9.1 section 7.7.2
Listing 7.3 Loader object using remote backend
loader = NodeLoader(
data=(feature_store, graph_store),
node_sampler=node_sampler,
batch_size=20,
input_nodes='paper',
)
for batch in loader:
<training loop>
The remote-backend loader listing shows how a feature store, graph store, node sampler, batch size, and input nodes fit into one training pipeline. section 7.9
