liveBook · Manning

Batching

As explained in Graph Neural Networks in Action

Mini-batch training is a model-training technique that processes subsets of examples or graph nodes in separate batches rather than the entire dataset at once.

Training on an entire graph at once can strain memory. A graph attention network (GAT), for example, calculates attention scores for every head and edge, while autograd retains tensors needed for training. Mini-batching loads smaller portions of the graph into the training loop instead of using the whole graph as one batch. section 4.4.1

Batching and sampling are related but distinct. Batching divides training data into smaller groups of nodes or edges; sampling chooses what goes into each group. A group may form a connected subgraph, but need not. Processing one group at a time reduces the need to hold the entire graph in memory during an epoch. section 7.7.1

A practical node-batching workflow is:

  • Choose the training nodes and a batch size. In PyTorch Geometric, NeighborLoader can use input_nodes=data.train_mask and batch_size=128. section 4.4.1
  • Set num_neighbors for each GNN layer to control neighborhood sampling. The GAT example uses [1000]*2 for two layers. section 4.4.1
  • Iterate over the loader in the training loop, move each sampled batch to the device, then run the model, loss calculation, backward pass, and optimizer step. section 4.4.1
Listing 4.6 Setting up NeighborLoader for GAT
from torch_geometric.loader import NeighborLoader

batch_size = 128
loader = NeighborLoader(
    data,
    num_neighbors=[1000]*2,   #1
    batch_size=batch_size,   #2
    input_nodes=data.train_mask)

sampled_data = next(iter(loader))
print(f'Checking that batch size is \
{batch_size}: {batch_size == \
sampled_data.batch_size}')
print(f'Percentage fraud in batch: \
{100*sampled_data.y.sum()/\
len(sampled_data.y):.4f}%')
sampled_data

The NeighborLoader setup shows the three inputs that determine this example’s batches: training nodes, batch size, and neighbors sampled across layers. section 4.4.1

Choosing batches for a growing geographic graph

For a graph of geographic areas connected by proximity, roads, or development patterns, batch size and sampled-neighbor count offer tuning controls as the graph grows. GraphSAGE uses neighborhood sampling and mini-batching to limit its dependence on the full graph size; a GCN can also use PyG mini-batches rather than store the entire adjacency matrix. section 7.6.1

Smaller batches are not a free substitute for full-graph training. Sampling can omit neighbors needed for message passing or introduce bias, affecting performance and generalizability. Samplers also differ in epoch and convergence time, so test candidates on limited data. Batching can be combined with other scale measures, including sparse representations, parallel computing, and remote backends. section 7.7.1 section 7.11

How Graph Neural Networks in Action covers it

How deep it goes
Distinguishes batching from sampling and implements NeighborLoader in a GAT loop, while leaving sampler choice to experimentation.
Examples
Uses a 128-node NeighborLoader GAT batch and a growing geographic graph to illustrate configuration and scalability choices.
What it assumes
Builds on graph nodes, edges, message passing, and training loops while defining the distinction between batching and sampling.
What you can do after
Apply it via PyTorch Geometric NeighborLoader to train a GAT in batches and test sampling choices against memory and training-time constraints.
How it connects
Links neighborhood sampling and message passing to GraphSAGE, GCN, GAT, and other graph-scaling methods.

Also explained in

Compare how 15 books explain batching →

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.