liveBook · Manning

Batch vector

As explained in Build AI Drug Discovery Pipelines

A batch vector assigns each flattened graph node to the original graph it came from during batched graph processing.

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, batchB

Inspect the custom collate function that constructs separate PyTorch Geometric Batch objects for molecular and protein graph data. section 11.5.5

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Explains flattening, graph-boundary tracking, and separate mean pooling, but does not detail the vector's internal construction.
Examples
Demonstrates molecular and protein batches, custom collation, and pooling through mol_batch in a graph-convolution forward pass.
What it assumes
Assumes basic graph data concepts while defining Data objects, node features, edge indices, and variable-sized graph batching.
What you can do after
Operate PyTorch Geometric batching and pool node representations into one embedding per original graph.
How it connects
Connects graph batching with Data objects, DataLoader, graph convolutional networks, and global mean pooling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.