Graph classification

On this page 7

Graph classification is the supervised task of predicting a single label for an entire graph, rather than for its individual nodes or edges. In graph learning it covers problems where each data point is a separate graph, such as a molecule judged toxic or safe, a protein structure assigned to an enzyme class, or a social network labeled by community type.

A graph classifier must turn a structure of variable size and arbitrary node ordering into a fixed-length representation and then into a class prediction. The answer must stay the same however the nodes are numbered, so every method relies on permutation-invariant operations, from classic graph kernels to modern graph neural networks.

How a graph classification pipeline works

Most current systems follow a three-stage pipeline: node-level encoding, graph-level aggregation and a final classifier. The first stage uses message passing layers, in which every node repeatedly gathers information from its neighbors, so after k layers a node embedding summarizes its k-hop neighborhood.

The second stage applies a readout function that combines all node embeddings into one graph vector. Because the order of nodes carries no meaning, the readout must be a symmetric operation such as a sum, mean or maximum. The third stage is usually a small multilayer perceptron that maps the graph vector to class probabilities trained with cross-entropy loss.

StageTypical componentsOutput
Node encodingGCN, GIN, GAT or GraphSAGE layersOne embedding per node
Pooling and readoutSum, mean, attention, hierarchical poolingOne fixed-length graph vector
Classification headMLP with softmax or sigmoidClass probabilities per graph
Stages of a neural graph classifier

Readout and pooling choices

The readout decides which structural information survives into the graph representation. Sum aggregation preserves the count of patterns and is the most expressive simple choice, while mean and max discard size information that may matter, for example the number of rings in a molecule.

Hierarchical pooling coarsens the graph in several steps instead of collapsing it at once. Methods such as DiffPool learn soft cluster assignments, whereas top-k pooling and SAGPool keep only the highest-scoring nodes. The table compares common options.

MethodMechanismStrengthWeakness
Sum readoutAdds all node embeddingsKeeps multiset informationScale grows with graph size
Mean readoutAverages node embeddingsStable across sizesLoses node count
Attention readoutLearned weights per nodeFocuses on key substructuresExtra parameters, overfitting risk
DiffPoolLearned soft clusteringCaptures hierarchyQuadratic memory cost
Top-k poolingKeeps highest-scoring nodesSparse and fastMay drop useful context
Virtual nodeExtra node linked to all othersGlobal information flowCan dominate message passing
Common readout and pooling methods

Empirical studies often find that simple sum or mean readouts perform about as well as complex pooling once training is carefully tuned, so hierarchical pooling should be justified by validation results, not assumed.

Expressive power and the Weisfeiler-Lehman limit

Standard message passing networks can distinguish two graphs no better than the 1-dimensional Weisfeiler-Lehman test. This color refinement procedure fails on certain non-isomorphic graphs, for example two triangles versus one hexagon, which look locally identical. A graph classifier built on such layers will therefore assign them the same embedding and the same label.

The graph isomorphism network (GIN) reaches this upper bound by combining sum aggregation with an injective MLP update. Going beyond it requires higher-order networks, subgraph-based methods, positional or structural encodings such as Laplacian eigenvectors, or graph transformers that attend across all node pairs.

In practice, the WL limit matters most when labels depend on global properties such as cycles, connectivity or symmetry. Molecular tasks are a good illustration: ring membership and ring size strongly affect chemical behavior, yet plain message passing struggles to count rings. Adding cycle counts or random-walk encodings as node features is a cheap and often effective remedy.

ApproachIdeaCost
Structural encodingsAdd cycle counts or random-walk featuresLow, computed once per graph
Laplacian positional encodingsAttach eigenvector coordinates to nodesModerate, sign ambiguity to handle
Subgraph GNNsEncode many node-marked subgraphsHigh, multiplies graph size
Higher-order GNNsPass messages between node tuplesVery high, rarely scalable
Ways to increase expressive power

Graph kernels versus graph neural networks

Before neural methods, graph classification relied on graph kernels combined with support vector machines. A kernel counts shared substructures, such as walks, shortest paths or Weisfeiler-Lehman subtree patterns, and uses the counts as a similarity measure. Kernels remain strong baselines on small datasets.

AspectGraph kernelsGraph neural networks
FeaturesHand-designed substructure countsLearned end to end
Continuous attributesLimited supportHandled natively
ScalabilityKernel matrix grows quadraticallyMini-batch training on GPUs
Small datasetsOften competitive or betterProne to overfitting
Transfer and pretrainingNot applicablePossible with self-supervision
Graph kernels compared with GNNs

Benchmarks and evaluation pitfalls

Graph classification is evaluated on collections such as the TUDataset benchmarks and the Open Graph Benchmark. Many classic datasets are very small, so a few misclassified graphs change accuracy by several points.

DatasetDomainNumber of graphsMetric
MUTAGMutagenic compounds188Accuracy
PROTEINSProtein structures1,113Accuracy
NCI1Anticancer screening4,110Accuracy
REDDIT-BINARYDiscussion threads2,000Accuracy
ogbg-molhivHIV inhibition41,127ROC-AUC
Widely used graph classification datasets
  • High variance: results on datasets with a few hundred graphs should be reported as mean and deviation over repeated cross-validation.
  • Selection leakage: choosing hyperparameters on test folds inflates reported accuracy and has distorted published comparisons.
  • Weak splits: random molecule splits overestimate generalization; a scaffold split better reflects new chemistry.
  • Missing baselines: structure-agnostic models and WL kernels sometimes match complex GNNs and should always be included.

Explained without jargon

A graph is a set of dots joined by lines. A molecule fits this picture: atoms are dots and chemical bonds are lines. Graph classification looks at the whole drawing at once and gives it one label.

Imagine sorting many stick-figure toys into boxes by shape. The sorter does not care which stick was built first. It only cares how the pieces connect. Computers do the same with graphs, and they learn which connection patterns point to which label.

A typical use case

A pharmaceutical research group screens a library of 200,000 candidate compounds for liver toxicity before laboratory testing. Each compound is represented as a graph with atoms as nodes, carrying element type and charge, and bonds as edges, carrying bond type. The team trains a five-layer GIN with sum readout on about 12,000 compounds with known toxicity labels.

To avoid overly optimistic results, the data are divided with a scaffold split, so test molecules have core structures unseen in training. The model reaches a ROC-AUC of about 0.84 on this split, compared with 0.79 for a WL kernel baseline. Chemists then send only the top 5% of predicted safe compounds to assays, which cuts laboratory workload substantially while keeping most promising candidates in the pipeline.