Foundations to Frontier
Tests
E. Deep Learning › E14 Graph Neural Networks

GraphSAGE

GraphSAGE (Hamilton, Ying and Leskovec, NeurIPS 2017) made graph neural networks inductive and scalable. Instead of learning an embedding per node, it learns aggregator functions that combine a node's own features with an aggregate (mean, max-pool or LSTM) of its neighbours'. These functions apply to nodes and graphs never seen in training. To keep cost bounded on huge graphs, each layer samples a fixed number of neighbours, so a node's computation touches at most 1 + S₁ + S₁S₂ nodes. This page covers inductive learning, the aggregators and neighbour sampling. In the exercises you implement a GraphSAGE layer, then train on one community graph and test on a new, unseen one with different neighbour-sample sizes. Without neighbours, accuracy was 0.53. With all neighbours it was 0.93, and with 10, 5 and 1 sampled neighbours 0.89, 0.84 and 0.70. Fewer samples mean noisier aggregates.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learn how to aggregate, not who is who

Some graph methods learn a fixed vector for every node, which is useless for a node added tomorrow. GraphSAGE instead learns a recipe: “take my own features, average a sample of my neighbours’ features, and combine them.” The recipe works on any node of any graph, including new ones. Sampling a fixed number of neighbours keeps the work per node bounded even in graphs with billions of edges. The demo shows how far a node’s information reaches through the layers.

Graph neural network lab
01234567891011
After 2 rounds, node 0 has received information from 6 of 12 nodes: its 2-hop neighbourhood. A GNN with k layers cannot see further.
Why engineers care

Real graphs grow and change: new users, new parts, new sensors. Inductive, sampled GNNs like GraphSAGE handle them and scale to web-size graphs (PinSage at Pinterest).

📝 Quick check (Level 0)
0/3 answered
  1. 1. GraphSAGE learns…
  2. 2. It can handle…
  3. 3. Sampling neighbours keeps…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Layer: h_i’ = σ([h_i ‖ AGG of h_j over j in S(i)] W), followed by L2 normalisation of h_i’.
    • ‖ is concatenation, so W has 2·d_in rows (8 → 16 needs 256 weights).
    • S(i) is a sample of i’s neighbours.
  • Aggregators:
    • mean (close to a GCN);
    • max-pooling (an elementwise max over transformed neighbours);
    • LSTM over a random ordering of neighbours.
  • Sampling: with S₁ = 25 and S₂ = 10, one output depends on at most 1 + 25 + 250 = 276 nodes. Without sampling, an average degree of 50 gives 1 + 50 + 2500 + 125,000 nodes over 3 hops.
  • Inductive use: the learned W apply unchanged to new nodes and graphs; only features and edges are needed.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Nodes touched with S₁ = 25, S₂ = 10:
  2. 2. GraphSAGE combines self and neighbours by…
  3. 3. Which is a GraphSAGE aggregator?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

GraphSAGE in code

Exercise 1.

  • The SAGE layer gave unit-length outputs.
  • Max aggregation picked elementwise maxima of the neighbours.
  • Sampling with replacement stayed within the neighbour list, and the computation size was 276 for samples of 25 and 10.

Practice.

  • In PyTorch Geometric, use SAGEConv(in, out, aggr="mean") with NeighborLoader(data, num_neighbors=[25, 10], batch_size=512) for mini-batches.
  • Unsupervised training uses a link-prediction loss with negative sampling.
  • For production (PinSage), use importance-based neighbour selection with random walks and hard negatives.
from torch_geometric.loader import NeighborLoader
loader = NeighborLoader(data, num_neighbors=[25, 10], batch_size=512, input_nodes=data.train_mask)
for batch in loader:                          # each batch: seed nodes plus their sampled 2-hop neighbourhood
    out = model(batch.x, batch.edge_index)[:batch.batch_size]
    loss = F.cross_entropy(out, batch.y[:batch.batch_size])
🐍 Exercise 1: a GraphSAGE layer
Implement a GraphSAGE layer with mean or max aggregation, neighbour sampling and the computation size.
📝 Quick check (Level 2)
0/3 answered
  1. 1. PyG mini-batching with sampled neighbourhoods:
  2. 2. Unsupervised GraphSAGE uses…
  3. 3. PinSage selects neighbours by…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Inductive versus transductive.

  • Transductive methods (DeepWalk, node2vec, embedding tables) learn a vector per node and must retrain for new nodes.
  • GraphSAGE’s parameters are shared across nodes and depend only on features and local structure, so they generalise to new nodes and graphs drawn from a similar distribution.

Sampling as estimation.

  • The mean over a sample of S neighbours is an unbiased estimate of the full neighbourhood mean, with variance shrinking like 1/S.
  • Fewer samples mean cheaper but noisier aggregates. The noise can be partly absorbed during training (it acts like dropout), but it costs accuracy at small S.
  • Variance-reduction methods (control variates, layer-wise and subgraph sampling such as FastGCN, Cluster-GCN and GraphSAINT) improve this trade-off.

Expressiveness. Mean and max aggregators cannot distinguish some neighbourhoods (for example, multisets with the same mean). Sum aggregation with an MLP is more expressive (GIN, a later page).

📝 Quick check (Level 3)
0/3 answered
  1. 1. node2vec is…
  2. 2. Variance of a sampled mean shrinks like…
  3. 3. A subgraph-sampling training method:

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What does neighbour sampling cost in accuracy?

History.

  • W. L. Hamilton, R. Ying and J. Leskovec, “Inductive representation learning on large graphs” (NeurIPS 2017).
  • R. Ying et al., “Graph convolutional neural networks for web-scale recommender systems” (PinSage, KDD 2018).

Exercise 2.

  • Graphs: two independent 3-community graphs (300 nodes each, average degree about 7) with noisy 8-dimensional features from shared class prototypes.
  • Training: a 2-layer GraphSAGE-mean model on graph A, with fresh neighbour samples at every step.
  • Test: the new graph B (inductive), using the same sample size S at test time. A model without neighbour information is the baseline.
neighbours sampled S accuracy on the unseen graph
none (features only) 0.530
1 0.700
2 0.730
5 0.837
10 0.893
all 0.933
  • The learned aggregator transfers: trained on one graph, it classified an entirely new graph at 0.93, 40 points above using features alone.
  • Sampling trades accuracy for cost smoothly: accuracy rose steadily with S. Even one sampled neighbour per layer added 17 points, and 10 samples recovered most of the full-neighbourhood accuracy.
  • Small samples are noisy: a single neighbour’s features are a poor estimate of the neighbourhood mean.
  • Seeds: two more graph pairs gave the same ordering (1 → all: 0.69 → 0.92 and 0.72 → 0.93; features alone 0.65 and 0.63).
🐍 Exercise 2 (research): what does neighbour sampling cost in accuracy?
Train GraphSAGE on one graph with different neighbour-sample sizes and test on an unseen graph.

GraphSAGE in AI, robotics and aerospace

  • Recommendation and search: web-scale graphs of users, items and documents (PinSage).
  • Changing networks: new sensors, vehicles or components join a graph without retraining from scratch.
  • Design lesson: if the graph will grow, choose an inductive model, and tune sample sizes against a latency budget.

References

  1. W. L. Hamilton, R. Ying and J. Leskovec (2017). “Inductive representation learning on large graphs.” NeurIPS.
  2. R. Ying et al. (2018). “Graph convolutional neural networks for web-scale recommender systems.” Proc. KDD.
  3. H. Zeng et al. (2020). “GraphSAINT: graph sampling based inductive learning method.” ICLR.