GraphSAGE

Also known as: Graph sample and aggregate

On this page 7

GraphSAGE (Graph sample and aggregate) is an inductive graph neural network framework that computes a node’s embedding by sampling a fixed number of neighbors and aggregating their features with a learned function. In graph learning, it is a landmark method because it produces embeddings for nodes that were never seen during training, which earlier transductive approaches could not do.

Introduced by Hamilton, Ying and Leskovec in 2017, GraphSAGE learns aggregation functions rather than one vector per node. This design made graph neural networks practical on large, evolving graphs such as social networks, recommendation graphs and citation networks with millions of nodes.

How GraphSAGE samples and aggregates neighbors

GraphSAGE builds each node representation in K layers of neighborhood aggregation, where K is typically 2. At each layer, a node collects the current representations of a sampled subset of its neighbors, combines them with an aggregator, concatenates the result with its own representation and passes it through a weight matrix and a nonlinearity.

  1. Sample a fixed number of neighbors for each target node, for example 25 at the first hop and 10 at the second.
  2. Aggregate the neighbors’ representations from the previous layer into a single vector.
  3. Concatenate the aggregated vector with the node’s own previous representation.
  4. Transform the result with a learned weight matrix and an activation such as ReLU.
  5. Normalize the output vector to unit length before the next layer or the final task.

Because the sample size is fixed, the computation per node is bounded regardless of how many neighbors a node actually has. With sample sizes S1 and S2, a two-layer model touches at most S1 times S2 nodes per target, which keeps memory and runtime predictable on graphs with heavy-tailed degree distributions.

Aggregator functions

The aggregator determines how neighbor information is combined, and it must work on an unordered set of neighbors. The original paper proposed three aggregators and a GCN-style variant, each with a different balance of expressiveness and cost.

AggregatorOperationStrengthWeakness
Mean aggregatorElementwise mean of neighbor vectorsSimple and fastCannot count neighbors
GCN aggregatorMean including the node itselfClose to graph convolutionNo separate self representation
LSTM aggregatorLSTM over a random neighbor orderHigh capacityNot truly permutation invariant
Pooling aggregatorPer-neighbor MLP, then elementwise maxStrong empirical resultsMore parameters
Aggregators proposed for GraphSAGE

The pooling aggregator often performed best in the original experiments, while the mean aggregator remains the common default in libraries such as PyTorch Geometric and DGL because of its speed. Later analysis based on the Weisfeiler-Lehman test showed that mean and max aggregators are strictly less expressive than sum aggregation, which motivated the graph isomorphism network.

Inductive versus transductive learning

GraphSAGE is inductive: it learns a function that maps a node’s features and neighborhood to an embedding, so it generalizes to new nodes and even to entirely new graphs with the same feature space. Transductive methods learn embeddings only for the nodes present at training time and must be retrained when the graph changes.

MethodLearning settingUses node featuresScales via
GraphSAGEInductiveYesNeighbor sampling
Graph convolutional networkOriginally transductiveYesFull-graph propagation
Graph attention networkInductiveYesAttention over all neighbors
DeepWalk and node2vecTransductiveNoRandom walks
Cluster-GCNInductiveYesSubgraph partitioning
GraphSAGE compared with related graph methods

The inductive property requires informative node features. When nodes have no attributes, GraphSAGE falls back on structural features such as degree, and its advantage over random walk embeddings becomes smaller.

Training objectives and configuration

GraphSAGE supports both supervised and unsupervised training. In the supervised setting, the final embeddings feed a classifier trained with cross-entropy for node classification. The unsupervised objective encourages nearby nodes to have similar embeddings and distant nodes to differ, using random-walk co-occurrence and negative sampling.

The table below lists the main hyperparameters and their typical ranges in practice.

HyperparameterTypical rangeEffect
Number of layers K2 to 3Receptive field in hops
Neighbor sample sizes5 to 25 per hopAccuracy versus memory and speed
Hidden dimension64 to 512Model capacity
Negative samples (unsupervised)5 to 20Strength of the contrastive signal
Dropout0.0 to 0.5Regularization
Key GraphSAGE hyperparameters

Increasing depth beyond two or three layers rarely helps, because the sampled neighborhood grows exponentially and representations suffer from over-smoothing. Larger sample sizes reduce the variance of the neighbor estimate but raise memory use roughly in proportion to the product of the sizes.

Strengths, limitations and common mistakes

GraphSAGE remains a strong, well-understood baseline for large-scale node classification and link prediction. Its main limitations come from sampling and from the expressiveness of its aggregators.

  • Sampling variance: random neighbor subsets make embeddings slightly stochastic unless inference uses full neighborhoods or fixed seeds.
  • Limited expressiveness: mean and max aggregators cannot distinguish some different neighborhood structures.
  • Neighborhood explosion: multi-hop sampling still grows quickly with depth.
  • Heterophily: performance drops when connected nodes tend to belong to different classes.
  • Feature leakage: in link prediction, test edges must be removed from the graph used for message passing.

A common mistake is to evaluate an inductive model transductively, with test nodes present in the training graph, which overstates its ability to handle new nodes. Another is to ignore edge types and directions; heterogeneous variants or relational graph convolutional networks are better suited to multi-relational data.

The idea in brief

GraphSAGE helps a computer understand items that are connected to each other, such as people in a social network. It describes each item by looking at a few of its neighbors. It does not check every neighbor. It picks a small random group, which keeps the work fast.

Think of meeting a new person at a party. You learn much about them by talking to a few of their friends. GraphSAGE works the same way. Because it learns a way of asking, not a fixed answer for each person, it can describe newcomers right away without starting over.

A practical scenario

An online marketplace wants to flag fraudulent seller accounts within hours of registration. Its graph links sellers, buyers, devices and payment instruments, with about 30 million nodes and several hundred million edges. Each node carries features such as account age, transaction counts and device attributes.

The data science team trains a two-layer GraphSAGE model with a mean aggregator and neighbor sample sizes of 20 and 10, using historical fraud labels. When a new seller registers, the model computes an embedding from the seller’s devices and early transactions without retraining. Investigators receive a ranked list each morning, and the share of confirmed fraud among reviewed accounts rises noticeably compared with the earlier rule-based system, because shared devices and payment links expose fraud rings that individual features missed.