liveBook · Manning

Graph sampling

As explained in Graph Neural Networks in Action

Node sampling is a graph-sampling technique that selects a subset of graph nodes for computation.

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

How Graph Neural Networks in Action covers it

How deep it goes
Explains random-neighbor aggregation and outlines loader configuration, training batches, and remote sampler interfaces without detailing sampling algorithms.
Examples
Uses sampled-neighbor training loops, a geographical development-prediction graph, and a remote NodeLoader configuration.
What it assumes
Builds from graph structure and message passing while using Python and PyTorch Geometric for implementation.
What you can do after
Operate/configure it with PyG’s NeighborLoader, choosing input nodes, batch size, and neighbors per layer for mini-batch training.
How it connects
Connects sampling to GraphSAGE, message passing, mini-batching, neighborhood aggregation, and remote graph storage.

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.