Part XXII — Graph Neural Networks & Structured Data · Chapter 3

GraphSAGE: inductive learning on graphs

Hook

Last chapter's mean aggregated every neighbor a node has. Fine for a six-node toy graph — but a real social network node can have thousands of neighbors, and a brand-new user shows up after training is already done. What breaks first, and how do you fix it without starting over?

Intuition
01.012.020.033.041.052.0
sampled {0, 2} → mean 1.00 | full neighborhood mean 1.50

Click node 1 — it has three neighbors, but only two get highlighted. That's a fixed-size sample, not the full neighborhood. Compare the two readouts: sampling and using everything don't always agree.

Formalize

GraphSAGE ("SAmple and aggreGatE") changes two things about the previous chapter's GCN (short for Graph Convolutional Network). First, it aggregates a fixed-size random sample of neighbors instead of all of them — bounded work per node, no matter how high the degree gets:

hv(k+1)=AGGREGATE(hv(k),  {hu(k):uSAMPLE(N(v),K)})h_v^{(k+1)} = \text{AGGREGATE}\Big(h_v^{(k)}, \; \{h_u^{(k)} : u \in \text{SAMPLE}(\mathcal{N}(v), K)\}\Big)
  • hv(k)h_v^{(k)} — node vv's feature after kk rounds of message passing.
  • hv(k+1)h_v^{(k+1)} — node vv's updated feature after one more round.
  • hu(k)h_u^{(k)} — a sampled neighbor uu's current feature.
  • N(v)\mathcal{N}(v) — node vv's full neighborhood, before sampling.
  • SAMPLE(N(v),K\mathcal{N}(v), K) — a fixed-size random draw of KK neighbors from vv's full neighborhood.
  • KK — the sample size: how many neighbors get aggregated, regardless of how many actually exist.
  • AGGREGATE — the learned combining function, applied to any neighborhood, seen or unseen.
  1. Learns a function, not a lookup table

    Second — and this is the bigger idea — it learns an aggregation function, not a per-node lookup table.

  2. Transductive methods can't handle unseen nodes

    A method that memorizes one embedding vector per node (like node2vec) is transductive: it has nothing to say about a node it never saw.

  3. Inductive: any neighborhood works immediately

    GraphSAGE's function takes any neighborhood as input, so it's inductive: hand it a brand-new node's neighborhood and it produces an answer immediately.

Play
01.012.020.033.041.052.0
toggle to add a brand-new node

Add the new node. It was never part of the original six-node graph, never "trained on" — but the exact same aggregation function runs on it instantly, because the function only ever looked at neighbor values, never at a fixed table of node identities.

Worked example

With sample size K=2K=2 (always take the two lowest-numbered neighbors, standing in for a random draw):

  1. A low-degree node: sampling changes nothing

    Node 00 has exactly two neighbors, {1,2}\{1,2\} — sampling two out of two keeps everything. Sampled mean =1+2+03=1.0=\frac{1+2+0}{3}=1.0, identical to the full mean.

  2. A high-degree node: sampling genuinely loses information

    Node 11 has three neighbors, {0,2,3}\{0,2,3\}. Sampling keeps only {0,2}\{0,2\}, dropping neighbor 33 (feature 33). Sampled mean =2+1+03=1.0=\frac{2+1+0}{3}=1.0; the true full-neighborhood mean is 2+1+0+34=1.5\frac{2+1+0+3}{4}=1.5 — a real, measurable gap, the price of bounding the work per node.

  3. A node that never existed during training

    A new node with feature 44, connected only to node 00 (feature 11): its aggregate is 4+12=2.5\frac{4+1}{2}=2.5, using the same function as every original node — no new parameters, no retraining pass, just one more function call.

Checkpoint

Click through the nodes until you find the one where the sampled mean and the full-neighborhood mean genuinely disagree — only one of them has more neighbors than the sample size of 2.

01.012.020.033.041.052.0
click a node
Click a node to compare
Summary
hv(k+1)=AGGREGATE(hv(k),{hu(k):uSAMPLE(N(v),K)})h_v^{(k+1)} = \text{AGGREGATE}\Big(h_v^{(k)}, \{h_u^{(k)} : u\in\text{SAMPLE}(\mathcal{N}(v),K)\}\Big)

Sampling trades a little accuracy for a hard cap on cost per node — essential once "neighbor count" can be in the thousands. Being inductive is the deeper payoff: any model built to learn a function of a neighborhood generalizes to graphs it's never seen, the same way a CNN's learned filters generalize to images it's never seen. The next chapter asks whether "sample K neighbors and average them equally" is really the best way to combine them — or whether some neighbors should just count for more.