跳到主要内容

图网络 — 没有网格的数据怎么做卷积

这一章讲三件事: 图怎么用邻接矩阵表示、卷积怎么被重定义到图上; 从「能跑」到「能用」缺的两块(自环、归一化); 消息传递怎么把边上的信息(键级、交互强度)也学进去。 主走查是一个「1」在 5 节点环图上绕圈——信息沿图流动的最小完整样本。

1. 顶层全景

图像卷积(第 05 章) 图卷积(本章)
───────────────── ────────────
滤波器在规则网格上滑动 没有网格,只有"谁连着谁"
邻居 = 上下左右的固定几个 邻居 = 邻接矩阵里为 1 的那些
重数固定 → 可以用核扫 数量不定 → 改为"聚合邻居信息"

x ──Conv──▶ 特征图 节点属性 ──沿边聚合──▶ 新节点属性

图说:图像是图的特例(网格图)。图卷积是卷积的推广:
把"空间位置关系"换成"连接关系",把"滑窗"换成"沿边收信息"[^1]。

2. 核心原理

2.1 图与它的矩阵

图 = 节点 + 边;边不只是连线,常常自带信息——化学键的强度、两个人联系的多寡1。 一张图最常用的数值载体是邻接矩阵 A:第 i 行第 j 列填 1 表示「从 i 到 j 有边」, 否则 02。有向图的 A 可以不对称;无向图的 A 必须对称(两边都记)3。 节点自己还能挂属性(原子类型、人的年龄),存成向量。

2.2 主走查:一个「1」在环图上绕一圈

原书的玩具:5 个节点首尾相连成一个有向环(0→1→2→3→4→0), 属性向量初始为 [1, 0, 0, 0, 0]——只有 0 号节点有「货」4。 最简图卷积的定义:对每条边 i→j,把 i 的属性值搬到 j 处累加。 实现上就一行矩阵乘:新属性 = A @ 属性(有向图要用 A 的转置,无向图直接 A)5

初始: [1, 0, 0, 0, 0]
卷积 1 次: [0, 1, 0, 0, 0] ← "1" 沿边走到 1 号
卷积 2 次: [0, 0, 1, 0, 0]
卷积 3 次: [0, 0, 0, 1, 0]
卷积 4 次: [0, 0, 0, 0, 1]
卷积 5 次: [1, 0, 0, 0, 0] ← 绕环一圈,回到原点[^7]

这串数字是原书实测输出。它把三件事一次性演示到位: 信息严格沿边流动(没边的格子永远是 0)、每卷一次走一步(走的步数=层数)、 拓扑决定命运(是环,所以会转回来;换成链,「1」会一路流走不回头)。 「层数=信息能传播的跳数」这条对应关系,是后面所有图网络架构的底层直觉。

2.3 从玩具到能用的层:transform → propagate → update

真正的图卷积层分三步走6:

transform: 每个节点的属性过一个线性层(学"该提取什么")
propagate: A @ 属性 ——沿边把邻居的(变换后的)属性聚合过来
update: 聚合结果过一个非线性(如 ReLU),得出新属性

对照玩具版:玩具只有 propagate,真层把「学什么」(transform)和「再加工」(update) 也交给了可学习参数——卷积核在图上的等价物,就是 transform 那个线性层。

2.4 两处必改:自环与归一化

照搬玩具版到真实分子图上,立刻暴露两个缺陷7:

缺陷一:自己的信息丢了。 聚合只收邻居的,节点原有的属性没有参与—— 而「这个原子本身是什么」往往是判断的核心。修法:自环(self-loop)—— 给每个节点加一条指向自己的边,实现上就是把邻接矩阵对角线填 1(A + 单位矩阵)8

缺陷二:社交红人会淹没安静的人。 邻居多的节点(度大)聚合到的总量天然大, 数值失衡。修法:归一化——每个节点的聚合值除以它的度(更精确的写法是两侧 同乘「度⁻¹/²」的对角矩阵,即对称归一化),让「每个邻居的声音」音量一致9

这两处修正合起来,正是图卷积网络(GCN)的标准配方;本书的工程实现 (add_self_loops、normalize 两个方法)与论文一一对应10

2.5 消息传递:把边上的信息也学进去

分子的边上还有关键信息:键级(单键/双键/三键)。上面的图卷积只看 A 的 0/1, 边属性进不来11。**消息传递层(message passing)**把三步改造成全文凭:

message: 每条边生成一条"消息" = 拼接(源节点属性, 目标节点属性, 边属性) → 线性层
aggregate: 每个节点把收到的所有消息加总(scatter_add:按目标节点分组求和)[^14]
update: 拼接(自己的属性, 聚合到的消息) → 线性层 → 新属性[^15]

对照关系一句话:图卷积层是消息传递层的特例(消息只有源节点、边权重恒 1 的版本)12

表示结构的数据形式也换了:邻接矩阵换成边索引——两行整数:上行是每条边的起点、下行是终点,每列一条边。

这种表示在大而稀疏(连接数远小于可能上限)的图上省内存,访问边也更直接13

在 ZINC 分子数据集(28 种原子类型,任务=预测 logP,亲脂性指标)上, 消息传递的准确率确实优于同规模的图卷积网络——边属性里的化学信息被兑现了14

2.6 两个大项目:模拟物理、追踪细胞

图网络模拟器(11A):模拟颗粒物质(沙堆这类),传统分子动力学要暴力算每对粒子 之间的相互作用,规模上不去15。图网络模拟器把「粒子」当节点、「邻近/交互」当边, 用消息传递预测每个粒子下一步的位置和速度;训练数据是 SAND 数据集 (1,000 段仿真作训练、各 30 段验证测试)16学出来的动力学代替了写死的物理方程——第 04 章「数字孪生」思想在复杂系统上的升级版。

MAGIK 细胞追踪(11B):细胞追踪=先检测每帧里的细胞,再把跨帧的「同一个」连起来。 难的是连线这一步。MAGIK(2023,作者团队成员 Pineda 发表于 Nature Machine Intelligence) 把「连接」问题建图:节点=各帧检测到的细胞,边=相邻帧间的候选连接, 图神经网络(在「节点加连线」的数据上做学习的网络)判断每条边通不通——用图网络做图上的决策17

验收数字也够硬:在留出的测试图上,连边判断的 F1 约为 0.9918

3. 作者的判断与证据

  • 有证据的:环图的逐次传播输出、ZINC 上消息传递优于图卷积的对比、 颗粒模拟与真实仿真的对照、细胞追踪的轨迹可视化。
  • 结构性判断:三步框架(transform/propagate/update)是各种图网络的公共骨架, Gilmer 等 2017 年的消息传递论文把它确立为统一语言——GCN、图注意力网络都是它的变体19
  • 应用锚点:AlphaFold 用「图+注意力」解蛋白质结构预测,拿下了 2024 年诺贝尔化学奖 (Hassabis 与 Jumper)——原书在 seminal 里把它立为图方法在科学界的标志性成果20

4. 边界与局限

边界说明
层数=传播跳数3 层只见 3 跳邻居;而层数一深又出现过平滑(远处节点长得一样)——层数是双刃剑
度数极端时归一化救不全孤立节点、超级枢纽都要特殊处理(原书代码里对无穷大做了清零)
边索引的内存代价全连接图上边数是节点数的平方,稀疏性是前提
模拟器只在训练分布内守恒学出来的动力学不保证满足真实物理守恒律,外推(在训练数据范围之外作预测)要小心
追踪的「候选边」靠上游MAGIK 只解决连线;检测那一步差,图再准也白搭

5. 可带走的

  1. 图卷积=卷积的推广:空间邻接关系换成连接关系,滑窗换成沿边聚合;
  2. 邻接矩阵 A 是图的数值化身,无向图对称;A @ x 一步传一跳;
  3. 层数=信息传播的跳数,这是图网络深度的真实含义;
  4. 自环补「自己」、归一化平「音量」——从玩具到 GCN 的两处必修;
  5. 消息传递三步:发消息、收消息、改自己;GCN 是它的特例;边属性从 2.5 节起才被兑现;
  6. 边索引取代邻接矩阵是工程标配(省内存、好取边);
  7. 学动力学(模拟器)和学决策(追踪)是图网络的两大用武之地;
  8. AlphaFold 是「图+注意力」路线的诺贝尔级背书。

6. 原文地图

主题原书章原文位置
图、节点、边与边上信息Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:3(搜「nodes (also known as vertices)」)
图卷积=聚合邻居Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:5(搜「aggregating and weighing」)
邻接矩阵Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:11(搜「adjacency matrix」)
对称性Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:39(搜「symmetric adjacency matrix」)
节点属性Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:41(搜「node attributes」)
环图走查输出Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:74(搜「Convolution 1」)
信息沿边流动的解读Understanding Graph Convolutionstext/82-fm-understanding-graph-convolutions.txt:79(搜「cyclic nature」)
矩阵乘实现Predicting Molecular Properties with Graph Convolutionstext/83-fm-predicting-molecular-properties-with-graph-convo.txt:26(搜「torch.matmul」)
三步结构Predicting Molecular Properties with Graph Convolutionstext/83-fm-predicting-molecular-properties-with-graph-convo.txt:50(搜「transform, propagate, and update」)
两大缺陷Predicting Molecular Properties with Graph Convolutionstext/83-fm-predicting-molecular-properties-with-graph-convo.txt:59(搜「self-loops」) · text/83-fm-predicting-molecular-properties-with-graph-convo.txt:61(搜「has not been normalized」)
自环与对称归一化实现Predicting Molecular Properties with Graph Convolutionstext/83-fm-predicting-molecular-properties-with-graph-convo.txt:68(搜「add_self_loops」) · text/83-fm-predicting-molecular-properties-with-graph-convo.txt:88(搜「symmetric normalization」)
ZINC 与 logPPredicting Molecular Properties with Graph Convolutionstext/83-fm-predicting-molecular-properties-with-graph-convo.txt:106(搜「logP」) · text/83-fm-predicting-molecular-properties-with-graph-convo.txt:160(搜「28 atom types」)
边属性Predicting Molecular Properties with Message Passingtext/84-fm-predicting-molecular-properties-with-message-pas.txt:3(搜「edge attributes」)
GCN 是特例Predicting Molecular Properties with Message Passingtext/84-fm-predicting-molecular-properties-with-message-pas.txt:9(搜「special case of message-passing」)
边索引Predicting Molecular Properties with Message Passingtext/84-fm-predicting-molecular-properties-with-message-pas.txt:11(搜「edge index」)
消息的构成Predicting Molecular Properties with Message Passingtext/84-fm-predicting-molecular-properties-with-message-pas.txt:19(搜「called a message」)
scatter_add 聚合Predicting Molecular Properties with Message Passingtext/84-fm-predicting-molecular-properties-with-message-pas.txt:66(搜「scatter_add」)
update 拼接Predicting Molecular Properties with Message Passingtext/84-fm-predicting-molecular-properties-with-message-pas.txt:91(搜「combining them with aggregated messages」)
图模拟器动机Project 11A: Simulating Complex Physical Phenomenatext/85-fm-project-11a-simulating-complex-physical-phenomen.txt:3(搜「brute-force」) · text/85-fm-project-11a-simulating-complex-physical-phenomen.txt:5(搜「graph network–based simulator」)
SAND 数据Project 11A: Simulating Complex Physical Phenomenatext/85-fm-project-11a-simulating-complex-physical-phenomen.txt:11(搜「1,000 simulations」)
MAGIKProject 11B: Identifying Cell Trajectoriestext/86-fm-project-11b-identifying-cell-trajectories.txt:5(搜「MAGIK」) · text/86-fm-project-11b-identifying-cell-trajectories.txt:421(搜「F1 score of approximately 0.99」)
图谱系与 AlphaFoldSeminal Works and Further Readingtext/88-fm-seminal-works-and-further-reading.txt:3(搜「The Graph Neural Network Model」) · text/88-fm-seminal-works-and-further-reading.txt:7(搜「Neural Message Passing」) · text/88-fm-seminal-works-and-further-reading.txt:15(搜「AlphaFold」)

Footnotes

  1. 出处:「Understanding Graph Convolutions」第 3 段(text/82-fm-understanding-graph-convolutions.txt:3,搜「nodes (also known as vertices)」)。

  2. 出处:「Understanding Graph Convolutions」第 11 段(text/82-fm-understanding-graph-convolutions.txt:11,搜「adjacency matrix」)。

  3. 出处:「Understanding Graph Convolutions」第 39 段(text/82-fm-understanding-graph-convolutions.txt:39,搜「symmetric adjacency matrix」)。

  4. 出处:「Understanding Graph Convolutions」第 43-49 段(text/82-fm-understanding-graph-convolutions.txt:43,搜「node_attributes」)。

  5. 出处:「Predicting Molecular Properties with Graph Convolutions」第 26-33 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:26,搜「torch.matmul」)。有向图用转置的结论是练习 11-4。

  6. 出处:「Predicting Molecular Properties with Graph Convolutions」第 11-15 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:11,搜「Transform the attributes of each node」)、第 50 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:50,搜「transform, propagate, and update」)。

  7. 出处:「Predicting Molecular Properties with Graph Convolutions」第 59 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:59,搜「self-loops」)、第 61 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:61,搜「has not been normalized」)。

  8. 出处:「Predicting Molecular Properties with Graph Convolutions」第 68-70 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:68,搜「add_self_loops」)、第 86 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:86,搜「identity matrix」)。

  9. 出处:「Predicting Molecular Properties with Graph Convolutions」第 72-78 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:72,搜「normalize」)、第 88 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:88,搜「symmetric normalization」)。

  10. 出处:「Predicting Molecular Properties with Graph Convolutions」第 68-88 段(text/83-fm-predicting-molecular-properties-with-graph-convo.txt:68,搜「add_self_loops」)。「与 GCN 论文对应」的判断见「Seminal Works and Further Reading」第 9 段(text/88-fm-seminal-works-and-further-reading.txt:9,搜「1609.02907」)。

  11. 出处:「Predicting Molecular Properties with Message Passing」第 3-5 段(text/84-fm-predicting-molecular-properties-with-message-pas.txt:3,搜「edge attributes」)。

  12. 出处:「Predicting Molecular Properties with Message Passing」第 9 段(text/84-fm-predicting-molecular-properties-with-message-pas.txt:9,搜「special case of message-passing」)。

  13. 出处:「Predicting Molecular Properties with Message Passing」第 11-15 段(text/84-fm-predicting-molecular-properties-with-message-pas.txt:11,搜「edge index」)。

  14. 出处:「Predicting Molecular Properties with Message Passing」第 5 段(text/84-fm-predicting-molecular-properties-with-message-pas.txt:5,搜「message-passing layers」)与「Summary」第 9 段(text/87-fm-summary.txt:9,搜「improved your model's performance」)。

  15. 出处:「Project 11A: Simulating Complex Physical Phenomena」第 3 段(text/85-fm-project-11a-simulating-complex-physical-phenomen.txt:3,搜「brute-force」)。

  16. 出处:「Project 11A: Simulating Complex Physical Phenomena」第 5 段(text/85-fm-project-11a-simulating-complex-physical-phenomen.txt:5,搜「graph network–based simulator」)、第 11 段(text/85-fm-project-11a-simulating-complex-physical-phenomen.txt:11,搜「1,000 simulations」)。

  17. 出处:「Project 11B: Identifying Cell Trajectories」第 5 段(text/86-fm-project-11b-identifying-cell-trajectories.txt:5,搜「MAGIK」)。

  18. 出处:「Project 11B: Identifying Cell Trajectories」第 421 段(text/86-fm-project-11b-identifying-cell-trajectories.txt:421,搜「F1 score of approximately 0.99」)。

  19. 出处:「Seminal Works and Further Reading」第 7 段(text/88-fm-seminal-works-and-further-reading.txt:7,搜「Neural Message Passing」)。

  20. 出处:「Seminal Works and Further Reading」第 15 段(text/88-fm-seminal-works-and-further-reading.txt:15,搜「AlphaFold」)。