Neighbor sampling is a training technique for graph neural networks in which each node aggregates information from a random, fixed-size subset of its neighbors rather than from its entire neighborhood. In graph learning, it is the standard way to make mini-batch training feasible on graphs with millions or billions of nodes, where full-neighborhood computation would exceed memory.
Popularized by GraphSAGE in 2017, neighbor sampling (also called neighborhood sampling) caps the cost of each training step with a per-layer fanout, such as 25 neighbors in the first hop and 10 in the second. It trades a small amount of estimator variance for a large, predictable reduction in computation and memory.
Why graph neural networks need neighbor sampling
Neighbor sampling exists to solve the neighbor explosion problem. In a message passing neural network with L layers, the representation of one node depends on its entire L-hop neighborhood, and that receptive field grows roughly as the average degree raised to the power L.
On a social graph with an average degree of 100, a three-layer model would touch about one million nodes to compute a single output, and high-degree hub nodes make the problem worse. Full-batch training avoids repeated work but must hold all node activations in GPU memory, which fails beyond a few million nodes.
| Layers | Full neighborhood, degree 50 | Sampled, fanout 10 per layer |
|---|---|---|
| 1 layer | About 50 nodes | At most 10 nodes |
| 2 layers | About 2,500 nodes | At most 100 nodes |
| 3 layers | About 125,000 nodes | At most 1,000 nodes |
Sampling turns an unbounded and highly skewed cost into a fixed upper bound per seed node, which makes batch sizes, memory use and step times predictable.
How node-wise neighbor sampling works
Node-wise neighbor sampling builds a computation subgraph for each mini-batch by sampling outward from the target nodes, then computes representations inward from the outermost hop. The procedure is the same in major libraries such as PyTorch Geometric and DGL.
- Select a mini-batch of seed nodes, for example labeled training nodes.
- For each seed, sample up to k1 neighbors uniformly without replacement.
- For each sampled node, sample up to k2 of its own neighbors, and continue for L hops.
- Assemble the sampled edges into one bipartite block per layer.
- Run the layers from the outermost block inward so each node aggregates only its sampled messages.
With a mean aggregator, the sampled average is an unbiased estimate of the full-neighborhood mean for a single layer. Across stacked nonlinear layers the estimate becomes slightly biased, yet in practice models trained this way reach accuracy close to full-batch training.
A fresh sample is drawn for every mini-batch and every epoch, so each node eventually sees many different subsets of its neighborhood. This repeated resampling acts as a mild regularizer, similar in spirit to dropout on edges, and it partly explains why sampled training sometimes generalizes as well as, or better than, exact aggregation.
Key parameters and sampling variants
The behavior of neighbor sampling is governed mainly by the fanout list, the sampling distribution and whether sampling is node-wise, layer-wise or subgraph-based. The table summarizes the settings practitioners tune most often.
| Parameter | Typical values | Effect |
|---|---|---|
| Fanout per layer | 25 and 10, or 15, 10 and 5 | Larger values lower variance but raise cost |
| Number of layers | 2 or 3 | More hops widen context and multiply cost |
| Sampling distribution | Uniform or importance weighted | Weighting can reduce estimator variance |
| Replacement | Usually without replacement | Avoids duplicate messages from one neighbor |
| Batch size | 512 to several thousand seeds | Trades gradient noise against memory |
Beyond the GraphSAGE style, layer-wise sampling methods such as FastGCN and LADIES sample a fixed set of nodes per layer for the whole batch, and subgraph sampling methods such as Cluster-GCN and GraphSAINT train on small induced subgraphs. Each family addresses a different weakness of node-wise sampling.
Neighbor sampling compared with other scalable training methods
Choosing a scaling method depends on graph size, degree distribution and whether the model must produce inductive embeddings for unseen nodes. Neighbor sampling is the most general choice, but it is not always the most efficient.
| Method | What is sampled | Main advantage | Main drawback |
|---|---|---|---|
| Node-wise neighbor sampling | Neighbors of each node per hop | Simple, inductive, widely supported | Cost still grows with depth |
| Layer-wise sampling | Fixed node set per layer | Avoids exponential growth | Sparse connections between layers |
| Cluster-GCN | Graph partitions | Very low memory per batch | Drops edges between clusters |
| GraphSAINT | Random induced subgraphs | Normalization corrects sampling bias | Requires preprocessing statistics |
| Full-batch training | Nothing | Exact gradients | Memory limits graph size |
Many production systems combine approaches, for example neighbor sampling during training and full-neighborhood computation in a layer-by-layer pass at inference, which removes sampling noise from the final embeddings.
Limitations and common mistakes
Neighbor sampling introduces stochastic noise into both training and, if used there, inference. Practitioners who ignore that noise often misread model quality or produce unstable predictions.
- Sampling at inference without averaging yields different predictions on each run; use full neighborhoods or several samples.
- Fanouts that are too small under-represent informative neighbors, especially in heterophilous graphs.
- Ignoring hub nodes inflates cost, because high-degree nodes appear in many sampled neighborhoods.
- Data loading bottlenecks on the CPU can leave the GPU idle; sampling should run in parallel workers.
- Information leakage occurs in link prediction when the target edge stays in the sampled subgraph.
Deeper models also suffer from over-smoothing and over-squashing, problems that sampling does not solve. Two or three layers remain the practical default for sampled training.
A plain-language summary
Graph neural networks learn about a point in a network by looking at its neighbors. A person in a social network may have thousands of friends. Their friends have thousands more. Looking at everyone quickly becomes impossible.
Neighbor sampling solves this by picking a small random group of neighbors each time. It is like asking ten friends for an opinion instead of polling the whole town. The answer is a little less exact, but it is much faster and still reliable. Repeating the process many times during training balances out the randomness.
An example from practice
An online marketplace wants to detect fraudulent seller accounts using a transaction graph with about 80 million accounts and 1.2 billion edges. Full-batch training of a graph neural network on this graph would need far more memory than any single GPU provides.
The machine learning team trains a two-layer GraphSAGE model with fanouts of 20 and 10, a batch of 1,024 labeled accounts and eight CPU workers preparing sampled subgraphs in parallel. Each training step processes at most about 200,000 sampled nodes, which fits comfortably on one GPU.
For scoring, the team computes embeddings for all accounts with a full-neighborhood, layer-by-layer pass once per day. The result is a fraud model that trains in hours rather than days and flags suspicious sellers whose own features look normal but whose transaction partners do not.