# GraphSAGE

GraphSAGE（Graph Sample and Aggregate，图采样与聚合）是一个用于在大规模图上学习节点嵌入的归纳式框架，由斯坦福大学的威廉·L·汉密尔顿（William L. Hamilton）、应锐（Rex Ying）和[尤雷·莱斯科韦茨](https://www.edgepedia.org/zh/jure-leskovec)（Jure Leskovec）在2017年NIPS论文《大规模图上的归纳式表示学习》（Inductive Representation Learning on Large Graphs）中提出。它不为每个节点训练单独的嵌入，而是学习一个函数，通过采样并聚合节点局部邻域的特征来生成嵌入，因此可以为训练中从未见过的节点和图生成嵌入。<sup>[1](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 斯坦福大学威廉·L·汉密尔顿、应锐和尤雷·莱斯科韦茨的2017年NIPS论文<sup>[1](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)</sup> |
| 核心思想 | 学习对采样邻域特征的聚合函数，而非逐节点嵌入，从而实现对未见节点的归纳式泛化<sup>[1](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)</sup> |
| 与前人方法的对比 | 早期大多数节点嵌入方法是直推式的：它们要求训练时所有节点都在场，且不能自然泛化到未见节点<sup>[2](https://arxiv.org/abs/1706.02216)</sup> |
| 基准测试 | 在三个归纳式节点分类基准上超越了强基线：引文数据、Reddit帖子数据，以及一个多图蛋白质-蛋白质相互作用数据集<sup>[1](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)</sup> |
| 典型扇出 | 实践中GraphSAGE在第一层采样25个邻居，第二层采样10个（D1 = 25，D2 = 10）<sup>[3](https://horizon.documentation.ird.fr/exl-doc/pleins_textes/2024-02/010086177.pdf)</sup> |
| 库实现 | PyTorch Geometric中的SAGEConv算子和DGL中的SAGEConv实现了该方法；GraphSAGE风格的采样是主要图库中小批量训练的基础<sup>[4](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.nn.conv.SAGEConv.html)</sup><sup> • </sup><sup>[5](https://multigrid.ai/learn/graph-sampling-strategies-training)</sup> |

## 归纳式嵌入与直推式嵌入

低维节点嵌入支撑着从内容推荐到蛋白质功能识别等各种任务，但GraphSAGE之前可用的方法所训练的嵌入与训练时见到的特定节点绑定。这类方法本质上是直推式的：当新节点加入一个演进中的信息图时，不重新训练就无法生成它们的嵌入。<sup>[2](https://arxiv.org/abs/1706.02216)</sup>

GraphSAGE通过学习一个归纳式聚合函数来解决这个问题，该映射的形式为

\[ f\left(\mathbf{x}_v, \{\mathbf{x}_u : u \in \mathcal{N}(v)\}\right) \]

它接收节点自身的特征及其邻居的特征，生成一个嵌入。由于该函数作用于特征而非学习到的逐节点参数，它适用于任何特征可用的节点，包括训练后新增的节点。<sup>[6](https://anvithpothula.github.io/graph-neural-networks-textbook/chapters/07-gnn-design-space/)</sup> 该框架利用节点特征信息（如文本属性），为聚合函数提供所需的原材料。<sup>[1](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)</sup>

## 采样与聚合

方法的名称描述了它的两个操作。在每一层，对节点的邻域进行采样，将采样到的特征聚合后与节点自身的表示相结合。在PyTorch Geometric中以SAGEConv实现的算子，使用默认的均值聚合器，计算

\[ \mathbf{x}^{\prime}_i = \mathbf{W}_1 \mathbf{x}_i + \mathbf{W}_2 \cdot \mathrm{mean}_{j \in \mathcal{N}(i)} \, \mathbf{x}_j \]

其中 \( \mathbf{W}_1 \) 变换节点自身的特征，\( \mathbf{W}_2 \) 变换聚合后的邻域均值。<sup>[4](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.nn.conv.SAGEConv.html)</sup>

**为什么需要采样。** L层模型的感受野是L跳邻域，在平均度为d的图上大约包含 \( d^{L} \) 个节点。现实世界的图往往是无标度的，因此取全部邻居进行消息聚合可能需要覆盖图很大一部分的输入特征，代价过高。<sup>[5](https://multigrid.ai/learn/graph-sampling-strategies-training)</sup><sup> • </sup><sup>[7](https://www.dgl.ai/dgl_docs/stochastic_training/neighbor_sampling_overview.html)</sup> GraphSAGE的解决办法是聚合固定大小的邻居随机样本，而非全部邻居。<sup>[8](https://scalablebook.icsgen-ai.org/part-3-distributed-ml/module-13-distributed-graph-ml/section-13.6.html)</sup>

这种采样并非仅仅是一种不得不容忍的近似。对于均值聚合器，逐节点均匀采样是无偏的：采样邻居上的平均值以真实邻域均值为期望，但这通常并不能使采样训练目标的期望等于全邻域目标。<sup>[10](https://cs.stanford.edu/~jure/pubs/graphsage-nips17.pdf)</sup> 更高的扇出会降低估计的方差，当扇出接近度时趋近于全聚合；扇出是实践者在训练不稳定时调节的旋钮。<sup>[8](https://scalablebook.icsgen-ai.org/part-3-distributed-ml/module-13-distributed-graph-ml/section-13.6.html)</sup> 常用的第一层25个邻居、第二层10个邻居的设置，是为了在现有方法之上提供统计显著的增益而选定的。<sup>[3](https://horizon.documentation.ird.fr/exl-doc/pleins_textes/2024-02/010086177.pdf)</sup>

**采样如何重塑计算。** 在DGL等框架中，邻居采样为每个GNN层生成一个二分消息流图：一侧是目标节点，另一侧是其输入所需的全部源节点。包括SAGEConv在内的一些模块需要目标节点上一层的特征来计算输出，因此目标节点本身总是被包含在源节点中。<sup>[7](https://www.dgl.ai/dgl_docs/stochastic_training/neighbor_sampling_overview.html)</sup>

## 基准测试与评估

原论文在三个归纳式节点分类基准上评估了GraphSAGE。其中两个测试了在演进信息图中对未见节点的泛化，使用引文数据和Reddit帖子数据对训练时不存在的节点进行分类。第三个测试了对完全未见图的泛化，使用一个蛋白质-蛋白质相互作用的多图数据集。GraphSAGE在三个基准上都超越了强基线。<sup>[1](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)</sup>

后续工作继续改进采样步骤本身。一种数据驱动的节点采样方法取代了均匀采样，在三个归纳式节点分类数据集上提升了GraphSAGE基线的准确率，在该设定下超越了图神经网络的近期变体。<sup>[9](https://arxiv.org/abs/1904.12935)</sup> 另一条路线GraphSAINT（ICLR 2020）通过随机节点、随机边或随机游走直接采样子图，并用由每个节点和边的采样概率导出的归一化系数对产生的偏差进行解析校正。<sup>[5](https://multigrid.ai/learn/graph-sampling-strategies-training)</sup>

## 遗产

GraphSAGE成为可扩展图神经网络训练的模板：聚合固定大小邻居随机样本的模式是主要图库中小批量训练的基础，SAGEConv算子至今仍是PyTorch Geometric和DGL中的标准层。<sup>[8](https://scalablebook.icsgen-ai.org/part-3-distributed-ml/module-13-distributed-graph-ml/section-13.6.html)</sup><sup> • </sup><sup>[4](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.nn.conv.SAGEConv.html)</sup> 在分布式GNN系统中，小批量邻域采样是在单机无法容纳的大图上进行训练的标准方式，每个采样种子顶点使用有限数量的采样邻居。<sup>[3](https://horizon.documentation.ird.fr/exl-doc/pleins_textes/2024-02/010086177.pdf)</sup>

## References

1. [William L. Hamilton, Rex Ying, Jure Leskovec (2017). Inductive Representation Learning on Large Graphs. NIPS 2017.](https://proceedings.neurips.cc/paper/2017/hash/5dd9db5e033da9c6fb5ba83c7a7ebea9-Abstract.html)
2. [Inductive Representation Learning on Large Graphs, arXiv:1706.02216](https://arxiv.org/abs/1706.02216)
3. [48th International Conference on Very Large Data Bases: proceedings (distributed GNN training)](https://horizon.documentation.ird.fr/exl-doc/pleins_textes/2024-02/010086177.pdf)
4. [torch_geometric.nn.conv.SAGEConv, PyTorch Geometric documentation](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.nn.conv.SAGEConv.html)
5. [Graph Sampling Strategies for Graphs Too Large to Fit in Memory, Multigrid](https://multigrid.ai/learn/graph-sampling-strategies-training)
6. [Chapter 7: GNN Design Space: GraphSAGE and GAT](https://anvithpothula.github.io/graph-neural-networks-textbook/chapters/07-gnn-design-space/)
7. [Neighbor Sampling Overview, DGL 2.5 documentation](https://www.dgl.ai/dgl_docs/stochastic_training/neighbor_sampling_overview.html)
8. [Section 13.6: Distributed Neighbor Sampling, Scaling Out AI](https://scalablebook.icsgen-ai.org/part-3-distributed-ml/module-13-distributed-graph-ml/section-13.6.html)
9. [Advancing GraphSAGE with A Data-Driven Node Sampling, arXiv:1904.12935](https://arxiv.org/abs/1904.12935)
10. [cs.stanford.edu](https://cs.stanford.edu/~jure/pubs/graphsage-nips17.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Neural network architectures › Graph neural network architectures

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/graphsage

English original: https://www.edgepedia.org/graphsage
