GraphSAGE: Inductive Learning on Large Graphs

4 minute read

Published:

TL;DR: GraphSAGE (SAmple and aggreGatE) learns to aggregate features from a sampled subset of neighbours. Because it learns the aggregation function (not per-node embeddings), it generalises to new nodes never seen during training, making it inductive.

The Inductive vs. Transductive Distinction

Transductive GNNs (GCN, GAT): as originally formulated, these operate on one fixed graph: the layer is a product with a normalised adjacency \(\hat{A}\) built from the whole training graph. Add a new node tomorrow and \(\hat{A}\) changes, so at minimum you must rebuild it and re-run a full-graph forward pass.

Inductive GNNs (GraphSAGE): learn a function that maps a node’s local neighbourhood to an embedding. Apply this function to any neighbourhood, seen or unseen, to get an embedding.

This matters enormously in practice:

  • Pinterest uses GraphSAGE to embed new pins (items) in real-time as users upload them.
  • Social networks onboard new users continuously, their profiles must be embedded immediately.

The Algorithm

For each node \(v\) at each layer \(k = 1, \dots, K\):

\[ \begin{aligned} \textbf{1. Sample:}\quad & \mathcal{S}_v \sim \operatorname{Uniform}\big(\mathcal{N}(v)\big), \quad \lvert \mathcal{S}_v \rvert = S \\[2pt] \textbf{2. Aggregate:}\quad & a_v^{(k)} = \operatorname{AGGREGATE}_k\big(\{\, h_u^{(k-1)} : u \in \mathcal{S}_v \,\}\big) \\[2pt] \textbf{3. Update:}\quad & h_v^{(k)} = \sigma\Big( W^{(k)} \big[\, h_v^{(k-1)} \,\Vert\, a_v^{(k)} \,\big] \Big) \\[2pt] \textbf{4. Normalise:}\quad & h_v^{(k)} \leftarrow \frac{h_v^{(k)}}{\lVert h_v^{(k)} \rVert_2} \end{aligned} \]

Where:

  • \(K\), the number of layers, equivalently the number of hops each node sees.
  • \(S\), the fixed neighbourhood sample size, a hyperparameter. Note that \(\mathcal{S}_v\) always has exactly \(S\) elements: when \(\lvert \mathcal{N}(v) \rvert < S\) the sample is drawn with replacement, which is what keeps the per-node cost constant.
  • \(\Vert\), concatenation, so \(W^{(k)}\) has twice as many input columns as \(h\) has dimensions.
  • \(\lVert \cdot \rVert_2\), the Euclidean norm; step 4 projects every embedding onto the unit sphere.

The key novelty is step 3: concatenate the node’s own previous representation with the aggregated neighbourhood representation, then apply a shared learned \(W^{(k)}\). This ensures the node retains its own identity while incorporating neighbour information, and because \(W^{(k)}\) does not depend on which node it is applied to, the same layer works for a node that was never seen during training.

Full neighbourhood of v v n1 n2 n3 n4 n5 n6 Too expensive to use all 6! sample S=2 Sampled neighbourhood (S=2) v n2 sampled ✓ n5 sampled ✓ n1 n6 Only 2 neighbours needed per node! AGGREGATE({h_n2, h_n5}) → concat with h_v → W → new h_v
Figure 1: GraphSAGE samples \(S = 2\) neighbours instead of using all 6. The sampled neighbours' features are aggregated, concatenated with v's own features, then transformed via \(W^{(k)}\), the same \(W^{(k)}\) for every node, which is what makes the model inductive.
Why Inductive Learning Matters: GCN and GAT compute embeddings tied to a specific adjacency matrix. Their weight matrices learn "which position in this fixed graph matters." GraphSAGE instead learns "what kind of neighbourhood looks like this?", a transferable pattern. This is the difference between memorising a map vs. learning to navigate any city.

Concrete Example: Embedding a New Node at Inference Time

Suppose we trained GraphSAGE on a product graph. A new product \(P\) is uploaded tonight with features \(h_P = [0.8,\, 0.3,\, 0.1]\) and two existing, similar products as neighbours: \(h_{n_1} = [0.7,\, 0.4,\, 0.2]\) and \(h_{n_2} = [0.6,\, 0.5,\, 0.1]\).

Without retraining, with one layer and sample size \(S = 2\):

\[ \begin{aligned} \textbf{1. Sample:}\quad & \mathcal{S}_P = \{n_1, n_2\} \\[2pt] \textbf{2. Aggregate (mean):}\quad & a_P = \tfrac{1}{2}\big([0.7, 0.4, 0.2] + [0.6, 0.5, 0.1]\big) = [0.65,\, 0.45,\, 0.15] \\[2pt] \textbf{3. Concatenate + transform:}\quad & h_P' = \sigma\big(W \, [0.8,\, 0.3,\, 0.1,\, 0.65,\, 0.45,\, 0.15]^{\top}\big) \\[2pt] \textbf{4. Normalise:}\quad & h_P' \leftarrow h_P' / \lVert h_P' \rVert_2 \end{aligned} \]

The resulting embedding places \(P\) in the correct region of the embedding space relative to existing products, ready for recommendation, all without touching the training set.

Aggregator Choices

GraphSAGE proposes three aggregators (all operating on the sampled set \(\mathcal{S}_v\)):

AggregatorFormulaProperties
Mean\(\frac{1}{\lvert \mathcal{S}_v \rvert}\sum_{u \in \mathcal{S}_v} h_u\)Fast, size-invariant, closest to GCN
Max-pooling\(\max_{u \in \mathcal{S}_v} \sigma(W_{\text{pool}} h_u + b)\), elementwiseCaptures extreme features
LSTMLSTM applied to a random ordering of \(\mathcal{S}_v\)Highest capacity, not permutation-invariant

The LSTM aggregator violates permutation invariance (an LSTM cares about input order), GraphSAGE handles this by applying it to a random permutation of the neighbours, which empirically works well but gives no invariance guarantee.

Because mean and max are not injective over multisets, none of these aggregators reaches the 1-WL expressiveness bound; the GIN post explains why sum is required for that.

Mini-Batch Training

Because GraphSAGE uses neighbourhood sampling, it supports mini-batch training on arbitrarily large graphs:

  1. Sample a batch of target nodes.
  2. Sample their \(K\)-hop neighbourhoods, expanding the computation graph outwards, with a fixed sample size \(S\) per hop, this costs \(O(S^K)\) nodes per target instead of the whole graph.
  3. Compute embeddings bottom-up: 0-hop → 1-hop → … → target nodes.
  4. Update the \(W^{(k)}\) via backprop.

Pinterest’s PinSage builds on exactly this idea to scale to a graph with billions of nodes and edges.

✅ Key Takeaways

  • GraphSAGE is inductive: it learns an aggregation function \(\operatorname{AGGREGATE}_k\) and shared weights \(W^{(k)}\), not per-node embeddings, so it generalises to nodes never seen in training.
  • Neighbourhood sampling of a fixed \(S\) neighbours per node bounds the cost of a \(K\)-layer forward pass at \(O(S^K)\) nodes, which is what enables mini-batch training on billion-scale graphs.
  • Concatenates own representation with the aggregated neighbourhood before the linear transform, preserving node identity, then L2-normalises.
  • The same idea underpins production systems such as Pinterest's PinSage for real-time item embedding.