学习资料 理解深度学习 第 13 章 · 图神经网络
学 习 资 料 · 中 英 对 照

第 13 章 · 图神经网络

Understanding Deep Learning — Graph neural networks

图神经网络
Graph neural networks

Chapter 10 described convolutional networks, which specialize in processing regular arrays of data (e.g., images). Chapter 12 described transformers, which specialize in processing sequences of variable length (e.g., text). This chapter describes graph neural networks. As the name suggests, these are neural architectures that process graphs (i.e., sets of nodes connected by edges).

There are three novel challenges associated with processing graphs. First, their topology is variable, and it is hard to design networks that are both sufficiently expressive and can cope with this variation. Second, graphs may be enormous; a graph representing connections between users of a social network might have a billion nodes. Third, there may only be a single monolithic graph available, so the usual protocol of training with many data examples and testing with new data is not always appropriate.

This chapter starts by presenting real-world examples of graphs. It then describes how to encode these graphs and how to formulate supervised learning problems for graphs. The algorithmic requirements for processing graphs are discussed, and these lead naturally to graph convolutional networks, a particular type of graph neural network.

第 10 章介绍了卷积网络,它专门处理规则的数据数组(例如图像)。第 12 章介绍了 Transformer,它专门处理可变长度的序列(例如文本)。本章介绍图神经网络。顾名思义,这些是处理图(即由边连接的节点集合)的神经网络架构。

处理图时存在三个全新的挑战。首先,图的拓扑结构是可变的,很难设计出既具有足够表达能力又能应对这种变化的网络。其次,图可能极其庞大;一个表示社交网络用户之间连接关系的图可能有十亿个节点。第三,可能只有一个单一的整体图可用,因此使用大量数据样本训练并用新数据测试的通常流程并不总是适用。

本章首先介绍现实世界中的图示例,然后描述如何编码这些图以及如何为图构建监督学习问题。文章还讨论了处理图所需的算法要求,这些要求自然引出了图卷积网络,这是一种特定的图神经网络。

13.1 什么是图?
13.1 What is a graph?

A graph is a very general structure and consists of a set of nodes or vertices, where pairs of nodes are connected by edges or links. Graphs are typically sparse; only a small subset of the possible edges are present.

Some objects in the real world naturally take the form of graphs.

For example, road networks can be considered graphs where the nodes are physical locations, and the edges represent roads between them (figure 13.1a). Chemical molecules are small graphs where the nodes represent atoms, and the edges represent chemical bonds (figure 13.1b). Electrical circuits are graphs where the nodes represent components and junctions, and the edges are electrical connections (figure 13.1c).

图是一种非常通用的结构,由一组节点或顶点组成,其中节点对通过边或链接相连。图通常是稀疏的;只存在可能边的一小部分。

现实世界中的某些对象天然具有图的形式。例如,道路网络可以被视为图,其中节点是物理位置,边表示它们之间的道路(图 13.1a)。化学分子是小型图,其中节点表示原子,边表示化学键(图 13.1b)。电路是图,其中节点表示元件和连接点,边表示电气连接(图 13.1c)。

Furthermore, many datasets can also be represented by graphs, even if this is not their obvious surface form. For example: • Social networks are graphs where nodes are people, and the edges represent friend-

ships between them. • The scientific literature can be viewed as a graph where the nodes are papers, and

此外,许多数据集也可以用图来表示,即使这并非其最直观的表层形式。例如:

• 社交网络是一种图,其中节点是人,边表示他们之间的朋友关系。

图 13.1 现实世界中的图。某些对象,例如 a) 道路网络、 b) 分子和 c) 电路,天然地具有图结构。
图 13.1 现实世界中的图。某些对象,例如 a) 道路网络、 b) 分子和 c) 电路,天然地具有图结构。Figure 13.1 Real-world graphs. molecules, and c) electrical circuits, are naturally structured as graphs.

the edges represent citations. • Wikipedia can be considered a graph where the nodes are articles, and the edges

represent hyperlinks between articles. • Computer programs can be represented as graphs where the nodes are syntax tokens (variables at different points in the program flow), and the edges represent

computations involving these variables. • Geometric point clouds can be represented as graphs. Here, each point is a node

• 科学文献可以被视为一个图,其中节点是论文,边表示引用关系。

• 维基百科可以被视为一个图,其中节点是文章,边表示文章之间的超链接。

• 计算机程序可以表示为图,其中节点是语法词元(程序流程中不同位置的变量),边表示涉及这些变量的计算。

with edges connecting to other nearby points. • Protein interactions in a cell can be expressed as graphs, where the nodes are the

proteins, and there is an edge between two proteins if they interact.

In addition, a set (an unordered list) can be treated as a graph in which every member is a node and connects to every other. An image can be treated as a graph with regular topology, in which each pixel is a node with edges to the adjacent pixels.

• 几何点云可以表示为图。在这里,每个点都是一个节点,并通过边连接到其他邻近的点。 • 细胞中的蛋白质相互作用可以用图来表示,其中节点是蛋白质,如果两个蛋白质相互作用,则它们之间就有一条边。

13.1.1 图的类型
13.1.1 Types of graphs

Graphs can be categorized in various ways. The social network in figure 13.2a contains undirected edges; each pair of individuals with a connection between them have mutually agreed to be friends, so there is no sense that the relationship is directional. In contrast, the citation network in figure 13.2b contains directed edges.

Each paper cites other papers, and this relationship is inherently one-way.

Figure 13.2c depicts a knowledge graph that encodes a set of facts about objects by defining relations between them. Technically, this is a directed heterogeneous multigraph. It is heterogeneous because the nodes can represent different types of entities (e.g., people, countries, companies). It is a multigraph because there can be multiple edges of different types between any two nodes.

图可以按多种方式进行分类。图 13.2a 中的社交网络包含无向边;每对彼此有联系的个人都相互同意成为朋友,因此这种关系没有方向性。相反,图 13.2b 中的引用网络包含有向边。每篇论文引用其他论文,这种关系本质上是单向的。

图 13.2c 描绘了一个知识图谱,它通过定义对象之间的关系来编码关于对象的一组事实。从技术上讲,这是一个有向异质多重图。它之所以是异质的,是因为节点可以表示不同类型的实体(例如人物、国家、公司)。它之所以是多重图,是因为任意两个节点之间可以有多种类型的多条边。

图 13.2 图的类型。 a) 社交网络是无向图;人与人之间的连接是对称的。 b) 引用网络是有向图;一篇出版物引用另一篇出版物,因此关系是非对称的。 c) 知
图 13.2 图的类型。 a) 社交网络是无向图;人与人之间的连接是对称的。 b) 引用网络是有向图;一篇出版物引用另一篇出版物,因此关系是非对称的。 c) 知识图谱是有向异质多重图。节点是异质的,因为它们表示不同的对象类型(人、地点、公司),并且多条边可以表示节点之间的不同关系。 d) 点集可以通过在邻近点之间连边来转换为图。每个节点都关联一个三维空间中的位置,这被称为几何图(改编自 Hu 等, 2022)。 e) 左侧场景可以用层次图表示。房间、桌子和灯的拓扑结构均由图表示。这些图作为节点构成一个更大图,该更大图表示对象邻接关系(改编自 Fernández-Madrigal & González, 2022)。Figure 13.2 Types of graphs. a) A social network is an undirected graph; the connections between people are symmetric. b) A citation network is a directed graph; one publication cites another, so the relationship is asymmetric. knowledge graph is a directed heterogeneous multigraph. The nodes are heterogeneous in that they represent different object types (people, places, companies) and multiple edges may represent different relations between each node. d) A point set can be converted to a graph by forming edges between nearby points. Each node has an associated position in 3D space, and this is termed a geometric graph (adapted from Hu et al., 2022). e) The scene on the left can be represented by a hierarchical graph. The topology of the room, table, and light are all represented by graphs. These graphs form nodes in a larger graph representing object adjacency (adapted from Fernández-Madrigal & González, 2002). c) A
图 13.3 图的表示。 a) 包含六个节点和七条边的示例图。每个节点都有一个长度为五的相关嵌入(棕色向量)。每条边都有一个长度为四的相关嵌入(蓝色向量)。该图
图 13.3 图的表示。 a) 包含六个节点和七条边的示例图。每个节点都有一个长度为五的相关嵌入(棕色向量)。每条边都有一个长度为四的相关嵌入(蓝色向量)。该图可以用三个矩阵表示。 b) 邻接矩阵是一个二值矩阵,其中元素 (m, n) 在节点 m 连接到节点 n 时被设置为 1。 c) 节点数据矩阵 X 包含拼接后的节点嵌入。 d) 边数据矩阵 E 包含边嵌入。Figure 13.3 Graph representation. a) Example graph with six nodes and seven edges. Each node has an associated embedding of length five (brown vectors). Each edge has an associated embedding of length four (blue vectors). This graph can be represented by three matrices. b) The adjacency matrix is a binary matrix where element(m, n) is set to one if node m connects to node n. c) The node data matrix X contains the concatenated node embeddings. d) The edge data matrix E contains the edge embeddings.

The point set representing the airplane in figure 13.2d can be converted into a graph by connecting each point to its K nearest neighbors. The result is a geometricgraph where each point is associated with a position in 3D space. Figure 13.2e represents a hierarchical graph. The table, light, and room are each described by graphs representing the adjacency of their respective components. These three graphs are themselves nodes in another graph that represents the topology of the objects in a larger model.

All types of graphs can be processed using deep learning.

However, this chapter focuses on undirected graphs like the social network in figure 13.2a.

图 13.2d 中表示飞机的点集可以通过将每个点连接到其 K 个最近邻点来转换为图。结果是一个几何图,其中每个点都与三维空间中的一个位置相关联。图 13.2e 表示一个层次图。桌子、灯和房间分别由表示其各自组件邻接关系的图来描述。这三个图本身也是另一个图中的节点,该图表示更大模型中对象的拓扑结构。

所有类型的图都可以使用深度学习进行处理。不过,本章重点讨论无向图,例如图 13.2a 中的社交网络。

13.2 图表示
13.2 Graph representation

In addition to the graph structure itself, information is typically associated with each node. For example, in a social network, each individual might be characterized by a fixedlength vector representing their interests. Sometimes, the edges also have information attached. For example, in the road network example, each edge might be characterized by its length, number of lanes, frequency of accidents, and speed limit. The information at a node is stored in a node embedding, and the information at an edge is stored in an edge embedding.

More formally, a graph consists of a set of N nodes connected by a set of E edges. The graph can be encoded by three matrices A, X, and E, representing the graph structure, node embeddings, and edge embeddings, respectively (figure 13.3). The graph structure is represented by the adjacencymatrix, A. This is an N× N matrix where entry(m, n) is set to one if there is an edge between nodes m and n and

除了图结构本身之外,通常每个节点还会关联信息。例如,在社交网络中,每个人可以用一个表示其兴趣的定长向量来刻画。有时,边也会附带信息。例如,在道路网络示例中,每条边可以用其长度、车道数、事故频率和限速来刻画。节点处的信息存储在节点嵌入中,而边处的信息存储在边嵌入中。

更形式化地说,图由一组 N 节点组成,这些节点通过一组 E 边连接。该图可以用三个矩阵 A、 X 和 E 进行编码,分别表示图结构、节点嵌入和边嵌入(图 13.3)。

图 13.4 邻接矩阵的性质。 a) 示例图。 b) 邻接矩阵 A 的位置(m, n)包含从节点 m 到节点 n 的长度为 1 的游走数量。 c) 平方邻接矩阵
图 13.4 邻接矩阵的性质。 a) 示例图。 b) 邻接矩阵 A 的位置(m, n)包含从节点 m 到节点 n 的长度为 1 的游走数量。 c) 平方邻接矩阵 A 2 的位置(m, n)包含从节点 m 到节点 n 的长度为 2 的游走数量。 d) 表示节点 6 的独热向量,该节点在面板 (a) 中被高亮显示。 e) 当我们在该向量左侧乘以 A 时,结果包含从节点 6 到每个节点的长度为 1 的游走数量;我们可以在 1 步内到达节点 5、 7 和 8。 f) 当我们在该向量左侧乘以 A 2, 时,得到的向量包含从节点 6 到每个节点的长度为 2 的游走数量;我们可以在 2 步内到达节点 2、 3、 4、 5 和 8,并且我们可以以 3 种不同的方式回报至原始节点(经由节点 5、 7 和 8)。图结构由邻接矩阵 A 表示。这是一个 N× N 矩阵,其中元素 (m, n) 在节点 m 和 n 之间存在边时设为 1,并且否则为零。对于无向图,该矩阵始终对称。对于大型稀疏图,可以将其存储为连接列表(m, n)以节省内存。Figure 13.4 Properties of the adjacency matrix. tion (m, n) of the adjacency matrix A contains the number of walks of length one from node m to node n. c) Position(m, n) of the squared adjacency matrix A 2 contains the number of walks of length two from node m to node n. d) One hot vector representing node six, which was highlighted in panel (a). e) When we pre-multiply this vector by A, the result contains the number of walks of length one from node six to each node; we can reach nodes five, seven, and eight in one move. f) When we pre-multiply this vector by A 2, the resulting vector contains the number of walks of length two from node six to each node; we can reach nodes two, three, four, five, and eight in two moves, and we can return to the original node in three different ways (via nodes five, seven, and eight).

zero otherwise. For undirected graphs, this matrix is always symmetric. For large sparse graphs, it can be stored as a list of connections(m, n) to save memory.

The nth node has an associated node embedding x(n) of length D. These embeddings are concatenated and stored in the D×N node data matrix X. Similarly, the eth edge has an associated edge embedding e(e) of length DE. These edge embeddings are collected into the DE× E matrix E. For simplicity, we initially consider graphs that only have node embeddings and return to edge embeddings in section 13.9.

第 nth 个节点具有相应的节点嵌入 x(n),长度为 D。这些嵌入被拼接并存储在 D×N 节点数据矩阵 X 中。类似地,第 eth 条边具有相应的边嵌入 e(e),长度为 DE。这些边嵌入被收集到 DE× E 矩阵 E 中。为简单起见,我们最初只考虑具有节点嵌入的图,并在 13.9 节回到边嵌入。

13.2.1 邻接矩阵的性质
13.2.1 Properties of the adjacency matrix

The adjacency matrix can be used to find the neighbors of a node using linear algebra. Consider encoding the nth node’s position as a one-hot column vector (a vector with only one non-zero entry at position n, which is set to one). When we pre-multiply this vector by the adjacency matrix, it extracts the nth column of the adjacency matrix and returns a vector with ones at the positions of the neighbors (i.e., all the places we can reach in a walk of length one from the nth node). If we repeat this procedure (i.e., pre-multiply by A again), the resulting vector contains the number of walks of length two from node n

邻接矩阵可以借助线性代数来查找节点的邻居。考虑将 nth 节点的位置编码为一个独热列向量(一个仅在位置 n 处有一个非零元素且该元素被设为 1 的向量)。当我们用邻接矩阵左乘这个向量时,它会提取邻接矩阵的 nth 列,并回报一个向量,其中邻居位置为 1 (即,所有我们可以到达的位置在

一条从 nth 节点出发的长度为 1 的游走中)。如果重复此过程(即再次用 A 左乘),所得向量包含从节点 n 出发长度为 2 的游走数量到每一个节点(图 13.4d–f)。

to every node (figures 13.4d–f). In general, if we raise the adjacency matrix to the power of L, the entry at position(m, n) of A L contains the number of unique walks of length L from node m to node n (figures 13.4a–c). This is not the same as the number of unique paths since the walks include routes that visit the same node more than once. Nonetheless, A L still

节点 n (图 13.4a–c)。这与不同路径的数量不同,因为游走包括多次访问同一节点的路线。尽管如此, A L 仍然包含关于图连通性的宝贵信息;位置(m, n)处的非零元素表明,从 m 到 n 的距离必须小于或等于 L。

contains valuable information about the graph connectivity; a non-zero entry at position(m, n) indicates that the distance from m to n must be less than or equal to L.

13.2.2 节点索引的置换
13.2.2 Permutation of node indices

Node indexing in graphs is arbitrary; permuting the node indices results in a permutation of the columns of the node data matrix X and a permutation of both the rows and columns of the adjacency matrix A. However, the underlying graph is unchanged (figure 13.5). This is in contrast to images, where permuting the pixels creates a different image, and to text, where permuting the words creates a different sentence.

The operation of exchanging node indices can be expressed mathematically by a permutationmatrix, P. This is a matrix where exactly one entry in each row and column take the value one, and the remaining values are zero. When position(m, n) of the permutation matrix is set to one, it indicates that node m will become node n after

图中的节点索引是任意的;对节点索引进行置换会导致节点数据矩阵 X 的列发生置换,以及邻接矩阵 A 的行和列同时发生置换。然而,底层图保持不变(图 13.5)。这与图像不同,因为对像素进行置换会生成不同的图像;也与文本不同,因为对词语进行置换会生成不同的句子。

the permutation. To map from one indexing to another, we use the operations:

where post-multiplying by P permutes the columns and pre-multiplying by P T permutes the rows. It follows that any processing applied to the graph should also be indifferent to these permutations. Otherwise, the result will depend on the choice of node indices.

交换节点索引的操作可以在数学上用一个置换矩阵 P 来表示。该矩阵的每一行和每一列中恰好有一个元素取值为 1,其余元素均为 0。当置换矩阵中位置 (m, n) 被设置为 1 时,表示节点 m 将变成节点 n,在置换后。为了将一种索引映射到另一种索引,我们使用以下操作:

其中,右乘 P 会对列进行置换,左乘 P T 会对行进行置换。因此,对图进行的任何处理也应对这些置换保持不变。否则,结果将取决于节点索引的选择。

13.3 图神经网络、任务与损失函数
13.3 Graph neural networks, tasks, and loss functions

A graph neural network is a model that takes the node embeddings X and the adjacency matrix A as inputs and passes them through a series of K layers. The node embeddings are updated at each layer to create intermediate “hidden” representations H k before finally computing output embeddings H K.

图神经网络是一种模型,它以节点嵌入 X 和邻接矩阵 A 作为输入,并将它们经过一系列 K 层。在每一层中更新节点嵌入,以创建中间“隐藏”表示 H k,最后计算输出嵌入

At the start of this network, each column of the input node embeddings X just contains information about the node itself. At the end, each column of the model output H K includes information about the node and its context within the graph. This is similar to word embeddings passing through a transformer network. These represent words at the start, but represent the word meanings in the context of the sentence at the end.

在该网络的初始阶段,输入节点嵌入 X 的每一列仅包含节点自身的信息。在最终阶段,模型输出 H K 的每一列包含节点及其在图中的上下文信息。这类似于词嵌入经过 Transformer 网络:它们在开始时表示词,而在结束时表示句子上下文中的词义。

图 13.5 节点索引的置换。 a) 示例图, b) 对应的邻接矩阵, c) 节点嵌入。 d) 索引的(任意)顺序已改变的同一图。 e) 邻接矩阵和 f) 节点
图 13.5 节点索引的置换。 a) 示例图, b) 对应的邻接矩阵, c) 节点嵌入。 d) 索引的(任意)顺序已改变的同一图。 e) 邻接矩阵和 f) 节点矩阵现在不同。因此,任何在图上操作的网络层都应当对节点顺序不敏感。Figure 13.5 Permutation of node indices. a) Example graph, b) associated adjacency matrix and c) node embeddings. d) The same graph where the (arbitrary) order of the indices has been changed. matrix are now different. Consequently, any network layer that operates on the graph should be indifferent to the ordering of the nodes.
13.3.1 任务与损失函数
13.3.1 Tasks and loss functions

We defer discussion of graph neural network models until section 13.4 and first describe the types of problems these networks tackle and their associated loss functions. Supervised graph problems usually fall into one of three categories (figure 13.6).

Graph-level tasks:

The network assigns a label or estimates one or more values from the entire graph, exploiting both the structure and node embeddings. For example, we might want to predict the temperature at which a molecule becomes liquid (a regression task) or whether a molecule is poisonous to human beings or not (a classification task).

我们将图神经网络模型的讨论推迟到 13.4 节,首先介绍这些网络所处理的问题类型及其相关损失函数。监督式图问题通常分为三类(图 13.6)。

图级任务:网络利用图结构和节点嵌入,从整个图中给出标签或估计一个或多个值。例如,我们可能希望预测分子变为液态的温度(一个回归任务),或判断分子是否对人类有毒(一个分类任务)。

For graph-level tasks, the output node embeddings are combined (e.g., by averaging), and the resulting vector is mapped via a linear transformation or neural network to a fixed-size vector. For regression, the mismatch between the result and the ground truth values is computed using the least squares loss. For binary classification, the output is passed through a sigmoid function, and the mismatch is calculated using the binary cross-entropy loss. Here, the probability that the graph belongs to class one might be given by:

where the scalar βK and 1 × D vector ωK are learned parameters. Post-multiplying the output embedding matrix H K by the column vector 1 that contains ones has the effect of summing together all the embeddings and subsequently dividing by the number of nodes N computes the average. This is known as meanpooling (see figure 10.11).

对于图级任务,输出的节点嵌入会被组合(例如通过平均),所得向量通过线性变换或神经网络映射为固定大小的向量。对于回归,使用最小二乘损失计算结果与真值之间的差异。对于二分类,输出通过 sigmoid 函数,并使用二分类交叉熵损失计算差异。这里,图属于类别 1 的概率可能由下式给出:

其中,标量 βK 和 1 × D 向量 ωK 是可学习参数。将输出嵌入矩阵 H K 右乘包含全 1 的列向量 1,其效果是将所有嵌入求和,然后除以节点数 N,即计算平均值。这被称为平均池化(见图 10.11)。

图 13.6 图的常见任务。在每种情况下,输入都是由邻接矩阵和节点嵌入表示的图。图神经网络通过将节点嵌入传递经过一系列层来处理节点嵌入。最后一层的节点嵌入包含有
图 13.6 图的常见任务。在每种情况下,输入都是由邻接矩阵和节点嵌入表示的图。图神经网络通过将节点嵌入传递经过一系列层来处理节点嵌入。最后一层的节点嵌入包含有关节点及其在图中的上下文的信息。 a) 图分类。节点嵌入被组合(例如通过平均),然后映射为固定大小的向量,再通过 Softmax 函数生成类别概率。 b) 节点分类。每个节点嵌入单独用作分类依据(青色和橙色表示分配的节点类别)。 c) 边预测。与该边相邻的节点嵌入被组合(例如通过点积)以计算单个数值,再通过 sigmoid 函数映射,以生成缺失边应当存在的概率。Figure 13.6 Common tasks for graphs. In each case, the input is a graph represented by its adjacency matrix and node embeddings. The graph neural network processes the node embeddings by passing them through a series of layers. The node embeddings at the last layer contain information about both the node and its context in the graph. a) Graph classification. The node embeddings are combined (e.g., by averaging) and then mapped to a fixed-size vector that is passed through a softmax function to produce class probabilities. b) Node classification. Each node embedding is used individually as the basis for classification (cyan and orange colors represent assigned node classes). c) Edge prediction. Node embeddings adjacent to the edge are combined (e.g., by taking the dot product) to compute a single number that is mapped via a sigmoid function to produce a probability that a missing edge should be present.

Node-level tasks:

The network assigns a label (classification) or one or more values (regression) to each node of the graph, using both the graph structure and node embeddings. For example, given a graph constructed from a 3D point cloud similar to figure 13.2d, the goal might be to classify the nodes according to whether they belong to the wings or fuselage. Loss functions are defined in the same way as for graph-level tasks, except that now this is done independently at each node n:

节点级任务:网络同时利用图结构和节点嵌入,为图中的每个节点分配一个标签(分类)或一个或多个数值(回归)。例如,给定一个类似于图 13.2d 的由 3D 点云构建的图,目标可能是根据节点属于机翼还是机身对其进行分类。损失函数的定义方式与图级任务相同,只是现在是在每个节点上独立进行 n:

Edge prediction tasks:

The network predicts whether or not there should be an edge between nodes n and m. For example, in the social network setting, the network might predict whether two people know and like each other and suggest that they connect if that is the case. This is a binary classification task where the two node embeddings must be mapped to a single number representing the probability that the edge is present. One possibility is to take the dot product of the node embeddings and pass the result through a sigmoid function to create the probability:

边预测任务:网络预测节点 n 和 m 之间是否应该存在一条边。例如,在社交网络场景中,网络可能预测两个人是否互相认识且喜欢对方,并在确实如此时建议他们建立联系。这是一个二分类任务,其中两个节点嵌入必须映射到一个表示边存在的概率的数值。一种可能是计算节点嵌入的点积,并将结果通过 sigmoid 函数以生成概率:

13.4 图卷积网络
13.4 Graph convolutional networks

There are many types of graph neural networks, but here we focus on spatial-based convolutional graph neural networks, or GCNs for short. These models are convolutional in that they update each node by aggregating information from nearby nodes. As such, they induce a relational inductive bias (i.e., a bias toward prioritizing information from neighbors). They are spatial-based because they use the original graph structure. This contrasts with spectral-based methods, which apply convolutions in the Fourier domain.

Each layer of the GCN is a function F[•] with parameters Φ that takes the node embeddings and adjacency matrix and outputs new node embeddings. The network can hence be written as:

图神经网络有很多种,但这里我们重点关注空间型卷积图神经网络,简称 GCN。这些模型之所以是卷积的,是因为它们通过聚合邻近节点的信息来更新每个节点。因此,它们引入了一种关系归纳式偏差(即优先考虑来自邻居节点的信息的偏差)。它们基于空间,因为它们使用原始图结构。这与基于频谱的方法形成对比,后者在傅里叶域中应用卷积。

GCN 的每一层都是一个函数 F[•],带有参数 Φ,它接收节点嵌入和邻接矩阵,并输出新的节点嵌入。因此,该网络可以写为:

where X is the input, A is the adjacency matrix, H k contains the modified node embeddings at the kth layer, and ϕk denotes the parameters that map from layer k to layer k+1.

其中, X 是输入, A 是邻接矩阵, H k 包含第 kth 层修改后的节点嵌入,而 ϕk 表示从第 k 层映射到第 k+1 层的参数。

13.4.1 等变性与不变性
13.4.1 Equivariance and invariance

We noted before that the indexing of the nodes in the graph is arbitrary, and any permutation of the node indices does not change the graph. It is hence imperative that any model respects this property. It follows that each layer must be equivariant (see section 10.1) with respect to permutations of the node indices. In other words, if we permute the node indices, the node embeddings at each stage will be permuted in the same way. In mathematical terms, if P is a permutation matrix, then we must have: For node classification and edge prediction tasks, the output should also be equivariant with respect to permutations of the node indices. However, for graph-level tasks, the final layer aggregates information from across the graph, so the output is invariant

我们此前指出,图中节点的索引是任意的,节点索引的任意置换都不会改变图。因此,任何模型都必须遵循这一性质。由此可知,每一层都必须相对于节点索引的置换具有等变性(见 10.1 节)。换句话说,如果我们对节点索引进行置换,则每个阶段的节点嵌入也会以相同方式置换。用数学语言来说,如果 P 是置换矩阵,则必须满足:对于节点分类和边预测任务,输出也应相对于节点索引的置换保持等变性。然而,对于图级任务,最后一层会聚合来自整个图的信息,因此输出具有不变性

to the node order. In fact, the output layer from equation 13.2 achieves this because:

for any permutation matrix P (see problem 13.6).

相对于节点顺序。事实上,式 13.2 中的输出层之所以能实现这一点,是因为:

对于任意置换矩阵 P (参见问题 13.6)。

This mirrors the case for images, where segmentation should be equivariant to geometric transformations, and image classification should be invariant (figure 10.1). Here, convolutional and pooling layers partially achieve this with respect to translations, but there is no known way to guarantee these properties exactly for more general transformations. However, for graphs, it is possible to define networks that ensure equivariance or invariance to permutations.

这与图像中的情况类似:分割应对几何变换具有等变性,而图像分类应具有不变性(图 10.1)。在这里,卷积层和池化层针对平移部分实现了这一点,但没有已知方法能针对更一般的变换严格保证这些性质。然而,对于图,可以定义确保对置换具有等变性或不变性的网络。

13.4.2 参数共享
13.4.2 Parameter sharing

Chapter 10 argued applying fully connected networks to images isn’t sensible because this requires the network to learn how to recognize an object separately at every image position. Instead, we used convolutional layers that processed every position in the image identically. This reduced the number of parameters and introduced an inductive bias that forced the model to treat every part of the image in the same way.

第 10 章曾指出,将全连接网络应用于图像并不合理,因为这要求网络在图像的每个位置分别学习如何识别一个物体。相反,我们使用了卷积层,以完全相同的方式处理图像中的每个位置。这减少了参数数量,并引入了一种归纳式偏差,迫使模型以相同方式处理图像的每个部分。

The same argument can be made about nodes in a graph. We could learn a model with separate parameters associated with each node. However, now the network must independently learn the meaning of the connections in the graph at each position, and training would require many graphs with the same topology. Instead, we build a model that uses the same parameters at every node, reducing the number of parameters and sharing what the network learns at each node across the entire graph. Recall that a convolution (equation 10.3) updates a variable by taking a weighted sum of information from its neighbors. One way to think of this is that each neighbor sends a message to the variable of interest, which aggregates these messages to form the update. When we considered images, the neighbors were pixels from a fixed-size square region around the current position, so the spatial relationships at each position are the same. However, in a graph, each node may have a different number of neighbors, and there are no consistent relationships; there is no sense that we can weight information

from a node that is “above” the node of interest differently to information from a node that is “below” it.

同样的论点也适用于图中的节点。我们可以学习一个模型,为每个节点关联单独的参数。然而,这样一来,网络就必须在每个位置独立学习图中连接的含义,并且训练将需要许多具有相同拓扑结构的图。相反,我们构建一个在每个节点使用相同参数的模型,减少参数数量,并将网络在每个节点学到的信息在整个图中共享。回想一下,卷积(公式 10.3)通过对来自其邻居的信息取权重和来更新一个变量。一种理解方式是,每个邻居向目标变量发送一条消息,目标变量聚合这些消息以形成更新。当我们考虑图像时,邻居是来自围绕当前位置的固定大小方形区域的像素,因此每个位置的空间关系都相同。然而,在图中,每个节点可能具有不同数量的邻居,并且不存在一致的关系;我们没有理由可以按权重处理信息

,使来自位于目标节点“上方”的节点的信息与来自位于其“下方”的节点的信息有所不同。

图 13.7 简单图 CNN 层。 a) 输入图由结构(体现于图邻接矩阵 A,未显示)和节点嵌入(存储于 X 的列中)组成。 b) 第一个隐藏层中的每个节点通过
图 13.7 简单图 CNN 层。 a) 输入图由结构(体现于图邻接矩阵 A,未显示)和节点嵌入(存储于 X 的列中)组成。 b) 第一个隐藏层中的每个节点通过以下方式更新: (i) 聚合相邻节点以形成单个向量, (ii) 对聚合向量施加线性变换 Ω 0, (iii) 对原始节点施加相同的线性变换 Ω 0, (iv) 将这些结果与偏差 β0 相加,最后 (v) 施加非线性激活函数 a[•] (如 ReLU)。 c) 该过程在后续层中重复进行(但每层使用不同的参数),直到在网络末尾产生最终嵌入。Figure 13.7 Simple Graph CNN layer. a) Input graph consists of structure (embodied in graph adjacency matrix A, not shown) and node embeddings (stored in columns of X). b) Each node in the first hidden layer is updated by (i) aggregating the neighboring nodes to form a single vector, (ii) applying a linear transformation Ω 0 to the aggregated vector, (iii) applying the same linear transformation Ω 0 to the original node, (iv) adding these together with a bias β0, and finally (v) applying a nonlinear activation function a[•] like a ReLU. c) This process is repeated at subsequent layers (but with different parameters for each layer) until we produce the final embeddings at the end of the network.
13.4.3 示例 GCN 层
13.4.3 Example GCN layer

These considerations lead to a simple GCN layer (figure 13.7). At each node n in layer k, we aggregate information from neighboring nodes by summing their node embeddings h •: Then we apply a We can write this more succinctly by noting that post-multiplication of a matrix by a vector returns a weighted sum of its columns. The nth column of the adjacency matrix A contains ones at the positions of the neighbors. Hence, if we collect the node

embeddings into the D × N matrix H k and post-multiply by the adjacency matrix A, the nth column of the result is agg[n, k]. The update for the nodes is now:

这些考虑引出了一种简单的 GCN 层(图 13.7)。在层 k 的每个节点 n 处,我们通过求和相邻节点的节点嵌入 h • 来聚合信息:处的嵌入 h(n)我们可以更简洁地写出这一点,因为注意到矩阵后乘向量的回报是其列的加权和。邻接矩阵 A 的第 nth 列在邻居位置处为 1。因此,如果我们收集节点

将嵌入收集到 D × N 矩阵 H k 中,并用邻接矩阵 A 进行后乘,结果的 nth 列是 agg[n, k]。节点的更新现在为:该层满足设计考量:它对 …… 的置换具有等变性

where 1 is an N ×1 vector containing ones. Here, the nonlinear activation function a[•] is applied independently to every member of its matrix argument. This layer satisfies the design considerations: it is equivariant to permutations of the

node indices, can cope with any number of neighbors, exploits the graph structure to provide a relational inductive bias, and shares parameters throughout the graph.

13.5 示例:图分类
13.5 Example: graph classification

We now combine these ideas to describe a network that classifies molecules as toxic or harmless. The network inputs are the adjacency matrix and node embedding matrix X. The adjacency matrix A ∈RN×N derives from the molecular structure. The columns

of the node embedding matrix X ∈R118×N are one-hot vectors indicating which of the 118 elements of the periodic table are present. In other words, they are vectors of length 118 where every position is zero except for the position corresponding to the relevant element, which is set to one. The node embeddings can be transformed to an arbitrary size D by the first weight matrix Ω 0∈RD×118.

邻接矩阵 A ∈RN×N 源自分子结构。节点嵌入矩阵 X ∈R118×N 的列是独热向量,用于指示元素周期表的 118 种元素中存在哪些元素。换句话说,它们是长度为 118 的向量,其中每个位置都是零,只有对应相关元素的位置被设置为 1。节点嵌入可以 D 通过第一个权重矩阵 Ω 0∈RD×118 变换为任意大小。

The network equations are:

where the network output f[ X , A , Φ ] is a single value that determines the probability that the molecule is toxic (see equation 13.2).

网络方程如下:

其中,网络输出 f[ X , A Φ ] 是一个单一值,用于确定分子有毒的概率(参见方程 13.2)。

13.5.1 使用批次训练
13.5.1 Training with batches

Given I training graphs{ X i, A i} and their labels yi, the parameters Φ ={βk, Ω k}K can be learned using SGD and the binary cross-entropy loss (equation 5.19).

Fully connected networks, convolutional networks, and transformers all exploit the parallelism of modern hardware to process an entire batch of training examples concurrently. To this end, the batch elements are concatenated into a higher-dimensional tensor (section 7.4.2).

给定 I 训练图 { X i, A i} 及其标签 yi,参数 Φ ={βk, Ω k}K

k=0 可以通过 SGD 和二元交叉熵损失(公式 5.19)学习。全连接网络、卷积网络和 Transformer 都利用现代硬件的并行性来并发处理整个训练样本批次。为此,批次元素被拼接为高维张量(第 7.4.2 节)。

However, each graph may have a different number of nodes. Hence, the matrices X i and A i have different sizes, and there is no way to concatenate them into 3D tensors.

Luckily, a simple trick allows us to process the whole batch in parallel. The graphs in the batch are treated as disjoint components of a single large graph. The network can then be run as a single instance of the network equations. The mean pooling is carried out only over the individual graphs to make a single representation per graph that can be fed into the loss function.

然而,每个图可能有不同的节点数。因此,矩阵 X i 和 A i 的尺寸不同,无法将它们拼接为三维张量。

幸运的是,一个简单的技巧使我们能够并行处理整个批次。批次中的图被视为单个大型图的互不相交连通分量。然后,该网络可以作为网络方程的单个实例运行。平均池化仅在每个单独的图上进行,从而为每个图生成一个可输入损失函数的表示。

图 13.8 归纳式与直推式问题。 a) 归纳式设置下的节点分类任务。给定一组 I 个训练图,其中节点标签(橙色和青色)已知。训练后,给定一个测试图,必须为每个
图 13.8 归纳式与直推式问题。 a) 归纳式设置下的节点分类任务。给定一组 I 个训练图,其中节点标签(橙色和青色)已知。训练后,给定一个测试图,必须为每个节点分配标签。 b) 直推式设置下的节点分类。存在一个大型图,其中一些节点具有标签(橙色和青色),另一些未知。我们训练模型以正确预测已知标签,然后检查未知节点上的预测结果。Figure 13.8 Inductive vs. transductive problems. a) Node classification task in the inductive setting. We are given a set of I training graphs, where the node labels (orange and cyan colors) are known. After training, we are given a test graph and must assign labels to each node. b) Node classification in the transductive setting. There is one large graph in which some nodes have labels (orange and cyan colors), and others are unknown. We train the model to predict the known labels correctly and then examine the predictions at the unknown nodes.
13.6 归纳式与直推式模型
13.6 Inductive vs. transductive models

Until this point, all of the models in this book have been inductive: we exploit a training set of labeled data to learn the relation between the inputs and outputs. Then we apply this to new test data. One way to think of this is that we are learning the rule that maps inputs to outputs and then applying it elsewhere. By contrast, a transductive model considers both the labeled and unlabeled data

at the same time. It does not produce a rule but merely a labeling for the unknown outputs. This is sometimes termed semi-supervised learning. It has the advantage that it can use patterns in the unlabeled data to help make its decisions. However, it has the disadvantage that the model needs to be retrained when extra unlabeled data are added.

在此之前,本书中的所有模型都是归纳式的:我们利用带标签数据的训练集来学习输入与输出之间的关系,然后将其应用于新的测试数据。一种理解方式是,我们学习的是将输入映射到输出的规则,然后再将其应用到其他地方。相比之下,一个直推式模型会考虑有标签数据和无标签数据

同时。它不产生规则,只是为未知输出给出标签。这有时被称为半监督学习。它的优点是可以利用无标签数据中的模式来帮助决策。然而,其缺点是当添加额外的无标签数据时,模型需要重新训练。

Both problem types are commonly encountered for graphs (figure 13.8). Sometimes, we have many labeled graphs and learn a mapping between the graph and the labels. For example, we might have many molecules, each labeled according to whether it is toxic to humans. We learn the rule that maps the graph to the toxic/non-toxic label and then apply this rule to new molecules. However, sometimes there is a single monolithic graph. In the graph of scientific paper citations, we might have labels indicating the field (physics, biology, etc.) for some nodes and wish to label the remaining nodes. Here, the training and test data are irrevocably connected.

Graph-level tasks only occur in the inductive setting where there are training and test graphs. However, node-level tasks and edge prediction tasks can occur in either setting. In the transductive case, the loss function minimizes the mismatch between the model output and the ground truth where this is known. New predictions are computed by running the forward pass and retrieving the results where the ground truth is unknown.

这两种问题类型在图中都很常见(图 13.8)。有时,我们有许多带标签的图,并学习图与标签之间的映射。例如,我们可能有许多分子,每个分子都根据其对人类是否有毒进行标记。我们学习将图映射到有毒 / 无毒标签的规则,然后将该规则应用于新分子。然而,有时只有一个整体图。在科学论文引用图中,我们可能有一些节点的领域标签(物理、生物等),并希望为其余节点打上标签。在这里,训练数据和测试数据是不可分割地连接在一起的。

图级任务只出现在存在训练图和测试图的归纳式设置中。然而,节点级任务和边预测任务可以出现在任一设置中。在直推式情况下,损失函数最小化模型输出与真值之间的不匹配(当真值已知时)。通过运行前向传播并获取真值未知处的结果来计算新的预测。

13.7 示例:节点分类
13.7 Example: node classification

As a second example, consider a binary node classification task in a transductive setting. We start with a commercial-sized graph with millions of nodes. Some nodes have ground truth binary labels, and the goal is to label the remaining unlabeled nodes. The body of the network will be the same as in the previous example (equation 13.11) but with a different final layer that produces an output vector of size 1 × N:

作为第二个示例,考虑一个直推式设定下的节点二分类任务。我们从具有数百万节点的商业规模图开始。一些节点具有真值二分类标签,目标是为其余未标注节点打上标签。该神经网络的主体将与上一个示例相同(式 13.11),但有一个不同的最终层,用于产生大小为 1 × N 的输出向量:

where the function sig[•] applies the sigmoid function independently to every element of the row vector input. As usual, we use the binary cross-entropy loss, but now only at nodes where we know the ground truth label y. Note that equation 13.12 is just a vectorized version of the node classification loss from equation 13.3.

Training this network raises two problems. First, it is logistically difficult to train a graph neural network of this size. Consider that we must store the node embeddings at every network layer in the forward pass. This will involve both storing and processing a structure several times the size of the entire graph, and this may not be practical. Second, we have only a single graph, so it’s not obvious how to perform stochastic gradient descent. How can we form a batch if there is only a single object?

其中,函数 sig[•] 将 sigmoid 函数独立地应用于行向量输入的每个元素。照常,我们使用二元交叉熵损失,但现在仅在已知真值标签 y 的节点上使用。注意,式 13.12 只是式 13.3 中节点分类损失的向量化版本。

训练该神经网络会带来两个问题。首先,训练如此规模的图神经网络在工程上很困难。考虑到在前向传播中,我们必须存储每一神经网络层的节点嵌入。这将涉及存储和处理一个规模为数倍于整个图的结构,而且这可能不切实际。其次,我们只有一个图,因此如何执行随机梯度下降并不明显。如果只有一个对象,我们如何构造一个批次?

13.7.1 选择批次
13.7.1 Choosing batches

One way to form a batch is to choose a random subset of labeled nodes at each training step. Each node depends on its neighbors in the previous layer. These, in turn, depend

on their neighbors in the layer before, so (similarly to convolutional networks) each node has a receptive field (figure 13.9). The receptive field region is termed the k-hop neighborhood. We can hence perform a gradient descent step using the graph that forms the union of the k-hop neighborhoods of the batch nodes; the remaining inputs do not contribute.

一种形成批次的方法是在每个训练步骤中随机选择标签节点的一个子集。每个节点依赖于上一层中的邻居。这些节点反过来又依赖于

其在上一层中的邻居,因此(类似于卷积网络)每个节点都有一个感受野(图 13.9)。感受野区域被称为 k 跳邻域。因此,我们可以使用由批次节点的 k 跳邻域的并集构成的图执行一次梯度下降步骤;其余输入不贡献。

图 13.9 图神经网络中的感受野。考虑第二隐藏层中的橙色节点(右侧)。它接收来自第一隐藏层中 1 跳邻域内节点的输入(中间阴影区域)。第一隐藏层中的这些节点又
图 13.9 图神经网络中的感受野。考虑第二隐藏层中的橙色节点(右侧)。它接收来自第一隐藏层中 1 跳邻域内节点的输入(中间阴影区域)。第一隐藏层中的这些节点又依次接收其邻居的输入,而第二层中的橙色节点接收 2 跳邻域内所有输入节点的输入(左侧阴影区域)。图中对给定节点有贡献的区域,相当于卷积神经网络中感受野的概念。Figure 13.9 Receptive fields in graph neural networks. Consider the orange node in hidden layer two (right). neighborhood in hidden layer one (shaded region in center). hidden layer one receive inputs from their neighbors in turn, and the orange node in layer two receives inputs from all the input nodes in the 2-hop neighborhood (shaded area on left). The region of the graph that contributes to a given node is equivalent to the notion of a receptive field in convolutional neural networks.

Unfortunately, if there are many layers and the graph is densely connected, every input node may be in the receptive field of every output, and this may not reduce the graph size at all. This is known as the graph expansion problem. Two approaches that tackle this problem are neighborhood sampling and graph partitioning.

Neighborhood sampling: The full graph that feeds into the batch of nodes is sampled, thereby reducing the connections at each network layer (figure 13.10). For example, we might start with the batch nodes and randomly sample a fixed number of their neighbors in the previous layer. Then, we randomly sample a fixed number of their neighbors in

不幸的是,如果层数很多且图连接密集,每个输入节点都可能位于每个输出节点的感受野中,这也许完全无法减小图的规模。这被称为图扩展问题。应对该问题的两种方法是邻域采样和图划分。邻域采样:对馈入该节点批次的完整图进行采样,从而减少每个网络层的连接(图 13.10)。例如,我们可以从节点批次开始,随机采样固定数量的邻居节点在前一层中。然后,我们随机采样固定数量的它们的前一层邻居,依此类推。图仍然会

the layer before, and so on. The graph still increases in size with each layer but in

a much more controlled way.

This is done anew for each batch, so the contributing neighbors differ even if the same batch is drawn twice.

This is also reminiscent of dropout (section 9.3.3) and adds some regularization.

随着每一层而增大,但增长方式要受控得多。每个批次都重新进行采样,因此即使同一批次被抽取两次,参与贡献的邻居也会有所不同。这也让人联想到随机失活(第 9.3.3 节),并增加了一些正则化。

Graph partitioning:

A second approach is to cluster the original graph into disjoint subsets of nodes (i.e., smaller graphs that are not connected to one another) before processing (figure 13.11).

There are standard algorithms to choose these subsets to maximize the number of internal links. These smaller graphs can each be treated as batches, or a random subset of them can be combined to form a batch (reinstating any edges between them from the original graph).

图划分:第二种方法是在处理之前将原始图聚类成互不相交的节点子集(即彼此不连接的较小图)(图 13.11)。已有标准算法可以选择这些子集,以最大化内部链接的数量。这些较小的图可以各自被视为批次,也可以随机选取其中一部分组合成一个批次(恢复原始图中它们之间的任何边)。

图 13.10 邻域采样。 a) 在大图上形成批次的一种方法是,在输出层中选择一组带标签的节点(这里只有第二层中的一个节点,右侧),然后反向追溯以找到 K -
图 13.10 邻域采样。 a) 在大图上形成批次的一种方法是,在输出层中选择一组带标签的节点(这里只有第二层中的一个节点,右侧),然后反向追溯以找到 K - 跳邻域(感受野)中的所有节点。训练该批次只需要这个子图。不幸的是,如果图是密集连接的,这可能会保留图的很大一部分。 b) 一种解决方案是邻域采样。当我们从最后一层反向追溯时,在前一层中选择一组邻居(这里为三个),并在更前一层中选择这些邻居的一组邻居。这限制了用于训练该批次的图的大小。在所有面板中,亮度表示与原始节点的距离。Figure 13.10 Neighborhood sampling. a) One way of forming batches on large graphs is to choose a subset of labeled nodes in the output layer (here, just one node in layer two, right) and then working back to find all of the nodes in the K hop neighborhood (receptive field). Only this sub-graph is needed to train this batch. Unfortunately, if the graph is densely connected, this may retain a large proportion of the graph. b) One solution is neighborhood sampling. As we work back from the final layer, we select a subset of neighbors (here, three) in the layer before and a subset of the neighbors of these in the layer before that. This restricts the size of the graph for training the batch. In all panels, the brightness represents the distance from the original node.

Given one of the above methods to form batches, we can now train the network parameters in the same way as for the inductive setting, dividing the labeled nodes into train, test, and validation sets as desired; we have effectively converted a transductive problem to an inductive one.

To perform inference, we compute predictions for the unknown nodes based on their k-hop neighborhood. Unlike training, this does not require storing the intermediate representations, so it is much more memory efficient.

采用上述形成批次的方法之一后,我们现在可以以与归纳式设置相同的方式训练网络参数,并根据需要将带标签的节点划分为训练集、测试集和验证集;这实际上已将直推式问题转换为归纳式问题。为了进行推理,我们基于未知节点的 k-hop 邻域计算预测。与训练不同,这不需要存储中间表示,因此内存效率要高得多。

图 13.11 图划分。 a) 输入图。 b) 输入图被划分为更小的子图,采用一种以删除最少边为原则的方法。 c-d) 现在我们可以将这些子图用作批次,在直推式
图 13.11 图划分。 a) 输入图。 b) 输入图被划分为更小的子图,采用一种以删除最少边为原则的方法。 c-d) 现在我们可以将这些子图用作批次,在直推式设置中进行训练,因此这里有四个可能的批次。 e) 或者,我们可以将子图的组合作为批次,并恢复它们之间的边。如果使用子图对,则这里有六个可能的批次。Figure 13.11 Graph partitioning. a) Input graph. b) The input graph is partitioned into smaller subgraphs using a principled method that removes the fewest edges. c-d) We can now use these subgraphs as batches to train in a transductive setting, so here, there are four possible batches. combinations of the subgraphs as batches, reinstating the edges between them. If we use pairs of subgraphs, there would be six possible batches here.
13.8 图卷积网络的层
13.8 Layers for graph convolutional networks

In the previous examples, we combined messages from adjacent nodes by summing them together with the transformed current node. This was accomplished by post-multiplying the node embedding matrix H by the adjacency matrix plus the identity A + I. We now consider different approaches to both (i) the combination of the current embedding with the aggregated neighbors and (ii) the aggregation process itself.

在之前的示例中,我们将相邻节点的消息与变换后的当前节点相加,从而将其合并。这是通过将节点嵌入矩阵 H 右乘以邻接矩阵加单位矩阵 A + I 来实现的。现在我们考虑不同的方法,既包括 (i) 将当前嵌入与聚合后的邻居相结合,也包括 (ii) 聚合过程本身。

13.8.1 组合当前节点与聚合邻居
13.8.1 Combining current node and aggregated neighbors

In the example GCN layer above, we combined the aggregated neighbors HA with the current nodes H by just summing them:

In another variation, the current node is multiplied by a factor of(1 + ϵk) before contributing to the sum, where ϵk is a learned scalar that is different for each layer:

在上述示例 GCN 层中,我们仅通过求和,将聚合后的邻居 HA 与当前节点 H 合并:

在另一种变体中,当前节点在参与求和之前乘以因子 (1 + ϵk),其中 ϵk 是每层各自学习到的标量:

This is known as diagonal enhancement. A related variation applies a different linear transform Ψ k to the current node: where we have defined Ω ′

这被称为对角增强。一种相关的变体将不同的线性变换 Ψ k 应用于当前节点:其中,我们在第三行定义了 Ω ′

13.8.2 残差连接
13.8.2 Residual connections

With residual connections, the aggregated representation from the neighbors is transformed and passed through the activation function before summation or concatenation with the current node. For the latter case, the associated network equations are:

使用残差连接时,来自邻居的聚合表示会先经过变换,并通过激活函数,然后再与当前节点求和或拼接。对于后一种情况,相应的网络方程如下:

13.8.3 均值聚合
13.8.3 Mean aggregation

The above methods aggregate the neighbors by summing the node embeddings. However, it’s possible to combine the embeddings in different ways. Sometimes it’s better to take the average of the neighbors rather than the sum; this can be superior if the embedding information is more important and the structural information less so since the magnitude of the neighborhood contributions will not depend on the number of neighbors: where as before, ne[n] denotes a set containing the indices of the neighbors of the nth node. Equation 13.17 can be computed neatly in matrix form by introducing the diagonal N× N degree matrix D. Each non-zero element of this matrix contains the number

上述方法通过对节点嵌入求和来聚合邻居。不过,也可以以不同方式组合嵌入。有时,取邻居的平均值而不是求和会更好;如果嵌入信息更重要而结构信息相对次要,这可能会更优,因为邻域贡献的大小不会依赖于邻居数量:其中,与前面一样, ne[n] 表示一个集合,其中包含 nth 节点的邻居索引。方程 13.17 可以通过引入对角 N× N 度矩阵 D 以简洁的矩阵形式计算。该矩阵的每个非零元素包含相应节点的邻居数量。由此可知,逆矩阵中的每个对角元素

of neighbors for the associated node. It follows that each diagonal element in the inverse matrix D −1 contains the denominator that we need to compute the average. The new GCN layer can be written as:

矩阵 D −1 包含计算平均值所需的分母。新的 GCN 层可以写成:

13.8.4 Kipf 归一化
13.8.4 Kipf normalization

There are many variations of graph neural networks based on mean aggregation. Sometimes the current node is included with its neighbors in the mean computation rather than treated separately. In Kipf normalization, the sum of the node representations is normalized as:

基于均值聚合的图神经网络有许多变体。有时,当前节点会与邻居节点一起纳入均值计算,而不是单独处理。在 Kipf 归一化中,节点表示的总和为归一化为:

with the logic that information coming from nodes with a very large number of neighbors should be down-weighted since there are many connections and they provide less unique information. This can also be expressed in matrix form using the degree matrix:

其逻辑是:来自邻居数量非常多的节点的信息应当被降低权重,因为连接很多且提供的独特信息较少。这也可以使用度矩阵以矩阵形式表示:

13.8.5 最大池化聚合
13.8.5 Max pooling aggregation

An alternative operation that is also invariant to permutation is computing the maximum of a set of objects. The max pooling aggregation operator is:

where the operator max[•] returns the element-wise maximum of the vectors h m that are neighbors to the current node n.

另一种同样对置换不变的运算是对一组对象计算最大值。最大池化聚合算子为:

其中,算子 max[•] 回报当前节点 n 的邻居向量 h m 的逐元素最大值。

13.8.6 基于注意力的聚合
13.8.6 Aggregation by attention

The aggregation methods discussed so far either weight the contribution of the neighbors equally or in a way that depends on the graph topology. Conversely, in graph attention layers, the weights depend on the data at the nodes. A linear transform is applied to the current node embeddings so that: Then the similarity smn of each transformed node embedding h ′ m to the transformed node embedding h ′

到目前为止讨论的聚合方法要么对邻居的贡献赋予相等的权重,要么根据图的拓扑结构赋予权重。相反,在图注意力层中,权重取决于节点处的数据。对当前节点嵌入施加线性变换,使得:然后,每个变换后的节点嵌入 h ′ m 与变换后的节点嵌入 h ′

图 13.12 图卷积网络、点积注意力和图注意力网络的比较。在每种情况下,该机制都将 N 个大小为 D、存储在一个 D× N 矩阵 X 中的嵌入映射为相同大小的
图 13.12 图卷积网络、点积注意力和图注意力网络的比较。在每种情况下,该机制都将 N 个大小为 D、存储在一个 D× N 矩阵 X 中的嵌入映射为相同大小的输出。 a) 图卷积网络对数据矩阵施加线性变换 X ′= ΩX。然后计算变换后数据的加权和,其中权重基于邻接矩阵。加入偏差 β,并将结果送入激活函数。 b) Transformer 中点积自注意力机制的输出同样是变换后输入的加权和,但这一次权重通过注意力矩阵依赖于数据本身。 c) 图注意力网络结合了这两种机制;权重既由数据计算得出,又基于邻接矩阵。Figure 13.12 Comparison of graph convolutional network, dot product attention, and graph attention network. In each case, the mechanism maps N embeddings of size D stored in a D× N matrix X to an output of the same size. a) The graph convolutional network applies a linear transformation X ′= ΩX to the data matrix. It then computes a weighted sum of the transformed data, where the weighting is based on the adjacency matrix. A bias β is added, and the result is passed through an activation function. b) The outputs of the dot-product selfattention mechanism in the transformer are also weighted sums of the transformed inputs, but this time the weights depend on the data itself via the attention matrix. c) The graph attention network combines both of these mechanisms; the weights are both computed from the data and based on the adjacency matrix.

n is computed by concatenating the pairs, taking a dot product with a column vector ϕk of learned parameters, and applying an activation function:

These variables are stored in an N × N matrix S, where each element represents the similarity of every node to every other. As in dot-product self-attention, the attention weights contributing to each output embedding are normalized to be positive and sum to one using the softmax operation. However, only those values corresponding to the current node and its neighbors should contribute. The attention weights are applied to the transformed embeddings:

n 之间的相似度 smn 通过将配对拼接、与学习参数列向量 ϕk 进行点积,并应用激活函数来计算:

这些变量存储在一个 N × N 矩阵 S 中,其中每个元素表示每个节点与其他每个节点之间的相似度。与点积自注意力一样,贡献给每个输出嵌入的注意力权重通过 Softmax 运算归一化为正数且总和为 1。然而,只有对应当前节点及其邻居的那些值才应贡献。注意力权重被应用于变换后的嵌入:

where a[•] is a second activation function. The function Softmask[•, •] computes the attention values by applying softmax operation separately to each column of its first argument S, but only after setting values where the second argument A + I is zero to negative infinity, so they do not contribute. This ensures that the attention to nonneighboring nodes is zero. This is very similar to the dot-product self-attention computation in transformers (see figure 13.12), except that (i) The keys, queries, and values are all the same, (ii) The measure of similarity is different, and (iii) The attentions are masked so that each node only attends to itself and its neighbors. As in transformers, this system can be extended

其中 a[•] 是第二个激活函数。函数 Softmask[•, •] 通过分别对其第一个参数 S 的每一列应用 Softmax 运算来计算注意力值,但只有在将第二个参数 A + I 为零的位置的值设为负无穷之后才这样做,因此它们不会贡献。这确保了对非邻居节点的注意力为零。这与 Transformer 中的点积自注意力计算非常相似

to use multiple heads that are run in parallel and recombined.

13.9 边图
13.9 Edge graphs

Until now, we have focused on processing node embeddings. These evolve as they are passed through the network so that by the end of the network, they represent both the node and its context in the graph. We now consider the case where the information is associated with the edges of the graph.

It is easy to adapt the machinery for node embeddings to process edge embeddings using the edge graph (also known as the adjoint graph or line graph). This is a complementary graph, in which each edge in the original graph becomes a node, and every two edges with a common node in the original graph create an edge in the new graph (figure 13.13). In general, a graph can be recovered from its edge graph, so it’s possible to swap between these two representations. To process edge embeddings, the graph is translated to its edge graph. Then we use exactly the same techniques, aggregating information at each new node from its neighbors and combining this with the current representation. When both node and edge embeddings are present, we can translate back and forth between the two graphs. Now there are four possible updates (nodes update nodes, nodes update edges, edges update nodes, and edges update edges), and these can be alternated as desired, or with

到目前为止,我们一直专注于处理节点嵌入。这些嵌入在网络中传递时会不断演化,因此在网络末端,它们既表示节点本身,也表示其在图中的上下文。现在,我们考虑信息与图的边相关联的情况。

将用于节点嵌入的机制稍作调整,就可以利用边图(也称为伴随图或线图)来处理边嵌入。这是一种互补图,其中原图中的每条边都变为一个节点,而原图中共享一个节点的两条边则在新图中形成一条边(图 13.13)。一般而言,可以从其边图恢复出原图,因此可以在这两种表示之间进行转换。为了处理边嵌入,需要将图转换为其边图。然后,我们采用完全相同的技术,在每个新节点处从其邻居聚合信息,并将其与当前表示相结合。当同时存在节点嵌入和边嵌入时,我们可以在这两个图之间来回转换。现在,有四种可能的更新(节点更新节点、节点更新边、边更新节点以及边更新边),这些更新可以按需交替进行,或按照

minor modifications, nodes can be updated simultaneously from both nodes and edges.

经过少量修改,节点可以同时从节点和边进行更新。

图 13.13 边图。 a) 具有六个节点的图。 b) 为了创建边图,我们为每条原始边分配一个节点(青色圆圈),并且 c) 如果新节点所表示的边在原始图中连接到
图 13.13 边图。 a) 具有六个节点的图。 b) 为了创建边图,我们为每条原始边分配一个节点(青色圆圈),并且 c) 如果新节点所表示的边在原始图中连接到同一个节点,则连接这些新节点。Figure 13.13 Edge graph. a) Graph with six nodes. b) To create the edge graph, we assign one node for each original edge (cyan circles), and c) connect the new nodes if the edges they represent connect to the same node in the original graph.
13.10 小结
13.10 Summary

Graphs consist of a set of nodes, where pairs of these nodes are connected by edges. Both nodes and edges can have data attached, and these are referred to as node embeddings and edge embeddings, respectively. Many real-world problems can be framed in terms of graphs, where the goal is to establish a property of the entire graph, properties of each node or edge, or the presence of additional edges in the graph.

Graph neural networks are deep learning models that are applied to graphs. Since the node order in graphs is arbitrary, the layers of graph neural networks must be equivariant to permutations of the node indices. Spatial-based convolutional networks are a family of graph neural networks that aggregate information from the neighbors of a node and then use this to update the node embeddings.

One challenge of processing graphs is that they often occur in the transductive setting, where there is only one partially labeled graph rather than sets of training and test graphs. This graph can be extremely large, which adds further challenges in terms of training and has led to sampling and partitioning algorithms. The edge graph has a node for every edge in the original graph. By converting to this representation, graph neural networks can be used to update the edge embeddings.

图由一组节点构成,节点对之间通过边连接。节点和边都可以附加数据,这些数据分别称为节点嵌入和边嵌入。许多现实世界问题都可以用图来表述,其目标是确定整个图的性质、每个节点或边的性质,或图中是否存在额外的边。

图神经网络是应用于图的深度学习模型。由于图中节点的顺序是任意的,图神经网络的层必须对节点索引的置换保持等变。基于空间的卷积网络是图神经网络的一族,它们聚合来自节点邻居的信息,然后利用这些信息更新节点嵌入。

处理图的一个挑战是,它们通常出现在直推式设置中,其中只有一张部分有标签的图,而不是训练图和测试图的集合。这张图可能极其庞大,这给训练带来了更多挑战,并催生了采样和分区算法。边图为原始图中的每条边设置一个节点。通过转换为这种表示,可以使用图神经网络来更新边嵌入。

注释
Notes

Applications:

Graph neural networks:

公式

Spectral methods:

Spatial methods:

Aggregate and combine:

Higher-order convolutional layers:

Residual connections:

Attention in graph neural networks:

图神经网络中的注意力: Veličković 等(2019)开发了图注意力网络(图 13.12c)。他们的表

Permutation invariance:

Edge graphs:

Power of graph neural networks:

Batches:

Regularization and normalization:

Multi-relational graphs:

Hierarchical representations and pooling:

Geometric graphs:

Oversmoothing and suspended animation:

问题
Problems
图 13.14 习题 13.1、 13.3 和 13.8 的图。
图 13.14 习题 13.1、 13.3 和 13.8 的图。Figure 13.14 Graphs for problems 13.1, 13.3, and 13.8.
图 13.15 习题 13.11–13.13 的图。其中, H 是一个 D × N 矩阵,其列中包含 N 个节点嵌入, A 是 N × N 邻接矩阵, β 是偏
图 13.15 习题 13.11–13.13 的图。其中, H 是一个 D × N 矩阵,其列中包含 N 个节点嵌入, A 是 N × N 邻接矩阵, β 是偏差向量, Ω 是权重矩阵。证明该层对节点顺序的置换具有等变性,使得: GraphLayer[ H k, A ] P = GraphLayer[ H k P , P T AP ], 其中 P 是一个 N× N 置换矩阵。习题 13.8 图 13.14 中每个图的度矩阵 D 分别是什么? 习题 13.9 GraphSAGE (Hamilton 等, 2017a)的作者提出了一种池化方法,将节点嵌入与其邻居一Figure 13.15 Graphs for problems 13.11–13.13. where H is a D × N matrix containing the N node embeddings in its columns, A is the N × N adjacency matrix, β is the bias vector, and Ω is the weight matrix. Show that this layer is equivariant to permutations of the node order so that: GraphLayer[ H k, A ] P = GraphLayer[ H k P , P T AP ], where P is an N× N permutation matrix. Problem 13.8 What is the degree matrix D for each graph in figure 13.14? Problem 13.9 The authors of GraphSAGE (Hamilton et al., 2017a) propose a pooling method in which the node embedding is averaged together with its neighbors so that: Show how this operation can be computed simultaneously for all node embeddings in the D×N embedding matrix H using linear algebra. You will need to use both the adjacency matrix A and the degree matrix D. Problem 13.10 ∗ Devise a graph attention mechanism based on dot-product self-attention and draw its mechanism in the style of figure 13.12. Problem 13.11 ∗ Draw the edge graph associated with the graph in figure 13.15a.