liveBook · Manning

GraphSAGE

As explained in Graph Neural Networks in Action

GraphSAGE is a graph neural network method that learns node representations by aggregating sampled neighborhood information.

GraphSAGE learns a representation for each node from its own features and information gathered from nearby nodes. It is useful when relationships help predict a label: in a product co-purchase graph, connected products provide context for predicting a product’s category. Unlike a GCN approach that uses the full neighborhood, GraphSAGE is designed to aggregate randomly sampled neighbors, reducing the work required on large graphs. section 3.1 section 3.4.6

Think of each layer as one round of message passing:

  • Select neighbors and aggregate their representations—for example, with a mean, sum, or max. section 3.4.6
  • Combine the neighborhood result with the central node’s representation, apply learned weights, and produce an updated embedding. The general GraphSAGE procedure also describes activation and normalization steps. section 3.4.6
  • Stack layers to draw on more hops of context. More depth can capture broader patterns, but may make node representations too similar through over-smoothing. section 3.6

In PyTorch Geometric, `SAGEConv` supplies the GraphSAGE layer. A compact classifier stacks two such layers: pass node features `x` and graph connections `edge_index` through the first layer, apply ReLU, then pass the result through the second. Return that result when embeddings are needed for analysis, or apply log-softmax for category prediction. The simple model definition specifies graph layers; it does not itself show a neighbor-sampling data pipeline. Within `SAGEConv`, options such as projection, a central-node weight, and output normalization control parts of the update. section 3.1.2 section 3.4.7

Listing 3.3 GraphSAGE class
class GraphSAGE(torch.nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels):
    super(GraphSAGE, self).__init__()
    self.conv1 = SAGEConv(in_channels, \
    hidden_channels)  #1
    self.conv2 = SAGEConv(hidden_channels, out_channels)

    def forward(self, x, edge_index, \
    return_embeds=False):  #2
        x = self.conv1(x, edge_index)
        x = torch.relu(x)  #3
        x = self.conv2(x, edge_index)
        if return_embeds:  #4
            return x

     return torch.log_softmax(x, dim=1)  #5

The two-layer PyTorch Geometric class shows the inputs, embedding-return option, and classification output in one place. section 3.1.2

Product categories and bundles

The Amazon Products graph connects products bought together. Train and validate GraphSAGE to predict product categories, then compare learned product embeddings to look for potential bundles. The full dataset has about 2.5 million product nodes and 61.9 million edges, so a manageable subgraph makes an initial run practical. How that subgraph is selected matters: an arbitrary slice may omit useful relationships. section 3.1 section 3.3.3 section 7.1.1

Aggregation is a setting to test, not a universal winner. `SAGEConv` can use mean, max, sum, or lists of aggregations; layers can also use different settings. In one product-category experiment, two-layer models with 50% dropout and alternative aggregations outperformed the reported default-mean baseline. Judge changes with classification metrics and a confusion matrix, which can expose misclassifications and class imbalance. Sampling and mini-batching help GraphSAGE scale, but repeated sampled nodes can cause redundant computation, and memory and epoch time still matter. section 3.2.2 section 3.3.3 section 7.6.1

Table 3.7 Two-layer GraphSAGE models using 50% dropout and different aggregation types
GraphSAGE Model
Aggregation Type
F1 Score
Log Loss
Model 1
'max'
0.8674
0.594
Model 2
['max', 'sum', 'mean']
0.8876
0.660
Model 3
[SoftmaxAggregation(), StdAggregation()]
0.8829
0.574
Baseline model
Mean (default)
0.7406
2.1214

Compare F1 score and log loss for the tested aggregation settings and default-mean baseline. section 3.3.3

How Graph Neural Networks in Action covers it

How deep it goes
Works through sampled-neighbor updates and a two-layer implementation, then examines aggregation settings and selected SAGEConv operations.
Examples
Uses an Amazon co-purchase graph, PyTorch Geometric model classes, aggregation comparisons, and a classification confusion matrix.
What it assumes
Assumes basic model-training familiarity while introducing neighborhood aggregation through a graph-based prediction task and a concrete model class.
What you can do after
Apply it via PyTorch Geometric to train a node classifier and evaluate aggregation choices against classification metrics.
How it connects
Connects GraphSAGE to message passing, GCNs, neighborhood aggregation, node classification, and mini-batching.

Also explained in

Compare how 2 books explain GraphSAGE →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.