A batch vector records which original graph each node belongs to after PyTorch Geometric combines multiple variable-sized graphs into one disconnected graph. It preserves graph boundaries while allowing nodes and edges to be processed together. section 11.5.5
In batched graph processing, the batch vector enables:
- Concatenating node and edge tensors from graphs with different numbers of atoms or residues. section 11.5.5
- Mapping every flattened node back to its molecule or protein graph. section 11.5.5
- Computing global mean pooling separately for each original graph rather than across the entire batch. section 11.5.2
Pooling a molecular batch
A molecular GNN reads node features, edge connectivity, and the batch vector as mol_batch. After graph convolutions, global_mean_pool(x, mol_batch) produces one graph-level vector per molecule, even though all node representations share one tensor. section 11.5.2
from torch_geometric.data import Batch
def collate(data_list):
batchA = Batch.from_data_list([data[-1] for data in data_list])
batchB = Batch.from_data_list([data[0] for data in data_list])
return batchA, batchBInspect the custom collate function that constructs separate PyTorch Geometric Batch objects for molecular and protein graph data. section 11.5.5
