Skip to content

Wikipedia 图神经网络核心概念 (Wikipedia Graph Neural Network Concepts)

来源: Wikipedia (英文维基百科)
编译时间: 2026-06-01
状态: 中英对照编译
基于: Graph neural network, Graph convolutional network, Message passing 等条目
关联: 深度学习概念, 强化学习概念


📚 目录 (Table of Contents)

  1. 图神经网络基础
  2. 图卷积网络
  3. 图注意力网络
  4. 消息传递框架
  5. 图自编码器
  6. 应用与挑战

1. 图神经网络基础 (GNN Basics)

英文定义 (English Definition):

A Graph Neural Network (GNN) is a class of neural networks designed to work with graph-structured data. GNNs can learn representations of nodes, edges, and entire graphs while preserving graph structure.

中文翻译 (Chinese Translation):

图神经网络 (GNN) 是一类设计用于处理图结构数据的神经网络。GNN 可以学习节点、边和整个图的表示,同时保留图结构。

图的基本概念 (Basic Graph Concepts):

概念 英文 中文 说明
Node Node/Vertex 节点/顶点 图中的基本单元
Edge Edge 节点之间的连接
Adjacency Matrix Adjacency Matrix 邻接矩阵 表示连接关系的矩阵
Degree Degree 节点的连接数
Path Path 路径 节点间的连接序列
Subgraph Subgraph 子图 图的子集

图的表示 (Graph Representation):

\[ G = (V, E, X) \]

其中: - \(V = \{v_1, v_2, ..., v_n\}\): 节点集合 - \(E \subseteq V \times V\): 边集合 - \(X \in \mathbb{R}^{n \times d}\): 节点特征矩阵

邻接矩阵 (Adjacency Matrix):

\[ A_{ij} = \begin{cases} 1 & \text{if } (v_i, v_j) \in E \\ 0 & \text{otherwise} \end{cases} \]

2. 图卷积网络 (GCN)

英文定义:

A Graph Convolutional Network (GCN) is a type of GNN that applies convolution operations on graph-structured data, extending the convolution operation from regular grids to irregular graphs.

中文翻译:

图卷积网络 (GCN) 是一种 GNN,在图结构数据上应用卷积操作,将卷积操作从规则网格扩展到不规则图。

2.1 GCN 层 (GCN Layer)

图卷积公式 (Graph Convolution Formula):

\[ H^{(l+1)} = \sigma\left(\tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}} H^{(l)} W^{(l)}\right) \]

其中: - \(\tilde{A} = A + I_N\): 带自环的邻接矩阵 - \(\tilde{D}\): \(\tilde{A}\) 的度矩阵 - \(H^{(l)}\): 第 l 层的节点表示 - \(W^{(l)}\): 可学习权重矩阵 - \(\sigma\): 激活函数 (如 ReLU)

Python 实现 (Python Implementation):

import torch
import torch.nn as nn
import torch.nn.functional as F

class GCNLayer(nn.Module):
    def __init__(self, in_features, out_features):
        super(GCNLayer, self).__init__()
        self.linear = nn.Linear(in_features, out_features)

    def forward(self, x, adj_matrix):
        """
        x: 节点特征 [num_nodes, in_features]
        adj_matrix: 邻接矩阵 [num_nodes, num_nodes]
        """
        # 添加自环
        adj_matrix = adj_matrix + torch.eye(adj_matrix.size(0))

        # 计算度矩阵
        degree = adj_matrix.sum(dim=1)
        degree_inv_sqrt = torch.pow(degree, -0.5)
        degree_inv_sqrt[torch.isinf(degree_inv_sqrt)] = 0
        D_inv_sqrt = torch.diag(degree_inv_sqrt)

        # 归一化邻接矩阵
        norm_adj = D_inv_sqrt @ adj_matrix @ D_inv_sqrt

        # 图卷积
        out = norm_adj @ x
        out = self.linear(out)

        return F.relu(out)

class GCN(nn.Module):
    def __init__(self, in_features, hidden_features, out_features, num_layers=2):
        super(GCN, self).__init__()

        self.layers = nn.ModuleList()
        self.layers.append(GCNLayer(in_features, hidden_features))

        for _ in range(num_layers - 2):
            self.layers.append(GCNLayer(hidden_features, hidden_features))

        self.layers.append(GCNLayer(hidden_features, out_features))

    def forward(self, x, adj_matrix):
        for i, layer in enumerate(self.layers):
            x = layer(x, adj_matrix)
            if i < len(self.layers) - 1:
                x = F.dropout(x, p=0.5, training=self.training)

        return x

2.2 GCN 变体 (GCN Variants)

变体 英文 中文 特点
GCN Graph Convolutional Network 图卷积网络 谱图卷积近似
GraphSAGE Graph Sample and Aggregate 图采样与聚合 邻居采样
GAT Graph Attention Network 图注意力网络 注意力机制
ChebNet Chebyshev Network 切比雪夫网络 切比雪夫多项式

3. 图注意力网络 (GAT)

英文定义:

A Graph Attention Network (GAT) applies attention mechanisms to graph-structured data, allowing each node to attend to its neighbors with different weights.

中文翻译:

图注意力网络 (GAT) 将注意力机制应用于图结构数据,允许每个节点以不同权重关注其邻居。

3.1 注意力机制 (Attention Mechanism)

注意力系数计算 (Attention Coefficient Calculation):

\[ e_{ij} = \text{LeakyReLU}\left(\vec{a}^T [Wh_i \| Wh_j]\right) \]
\[ \alpha_{ij} = \frac{\exp(e_{ij})}{\sum_{k \in \mathcal{N}(i)} \exp(e_{ik})} \]

其中: - \(h_i, h_j\): 节点 i 和 j 的特征 - \(W\): 可学习权重矩阵 - \(\vec{a}\): 注意力向量 - \(\|\): 拼接操作 - \(\mathcal{N}(i)\): 节点 i 的邻居

节点更新 (Node Update):

\[ h_i' = \sigma\left(\sum_{j \in \mathcal{N}(i)} \alpha_{ij} Wh_j\right) \]

多头注意力 (Multi-Head Attention):

\[ h_i' = \bigg\|_{k=1}^K \sigma\left(\sum_{j \in \mathcal{N}(i)} \alpha_{ij}^k W^k h_j\right) \]

其中 \(K\) 是注意力头的数量,\(\|\) 表示拼接。

Python 实现 (Python Implementation):

class GATLayer(nn.Module):
    def __init__(self, in_features, out_features, num_heads=1, dropout=0.6):
        super(GATLayer, self).__init__()
        self.num_heads = num_heads
        self.out_features = out_features

        # 每个头的权重
        self.W = nn.Parameter(torch.empty(size=(in_features, num_heads * out_features)))
        self.a = nn.Parameter(torch.empty(size=(num_heads * out_features * 2, 1)))

        self.leakyrelu = nn.LeakyReLU(0.2)
        self.dropout = nn.Dropout(dropout)

        self._init_parameters()

    def _init_parameters(self):
        nn.init.xavier_uniform_(self.W)
        nn.init.xavier_uniform_(self.a)

    def forward(self, x, adj_matrix):
        # 线性变换
        Wh = torch.matmul(x, self.W)  # [N, num_heads * out_features]

        # 计算注意力系数
        N = x.size(0)
        Wh_i = Wh.unsqueeze(1).expand(-1, N, -1)  # [N, N, num_heads * out_features]
        Wh_j = Wh.unsqueeze(0).expand(N, -1, -1)

        # 拼接并计算注意力
        Wh_ij = torch.cat([Wh_i, Wh_j], dim=-1)
        e = torch.matmul(Wh_ij, self.a).squeeze(-1)  # [N, N, num_heads]

        # Mask 非邻居
        mask = (adj_matrix == 0)
        e = e.masked_fill(mask.unsqueeze(-1), float('-inf'))

        # Softmax
        alpha = F.softmax(self.leakyrelu(e), dim=1)
        alpha = self.dropout(alpha)

        # 加权求和
        Wh = Wh.view(N, self.num_heads, self.out_features)
        alpha = alpha.view(N, N, self.num_heads, 1)

        out = torch.sum(alpha * Wh.unsqueeze(0), dim=1)  # [N, num_heads, out_features]
        out = out.view(N, -1)  # [N, num_heads * out_features]

        return F.elu(out)

4. 消息传递框架 (Message Passing)

英文定义:

Message Passing Neural Networks (MPNNs) provide a unified framework for GNNs, where information is passed between nodes through edges and aggregated at each node.

中文翻译:

消息传递神经网络 (MPNN) 为 GNN 提供了统一框架,其中信息通过边在节点之间传递并在每个节点聚合。

4.1 消息传递范式 (Message Passing Paradigm)

通用公式 (General Formula):

\[ m_{ij}^{(l)} = \phi_m(h_i^{(l)}, h_j^{(l)}, e_{ij}) \quad \text{(消息函数)} \]
\[ m_i^{(l)} = \sum_{j \in \mathcal{N}(i)} m_{ij}^{(l)} \quad \text{(聚合函数)} \]
\[ h_i^{(l+1)} = \phi_u(h_i^{(l)}, m_i^{(l)}) \quad \text{(更新函数)} \]

其中: - \(m_{ij}^{(l)}\): 从节点 j 到节点 i 的消息 - \(m_i^{(l)}\): 聚合的消息 - \(\phi_m, \phi_u\): 消息和更新函数 (通常是神经网络)

消息传递流程图 (Message Passing Flow):

消息传递步骤:

步骤 1: 消息生成
节点 j → 消息 m_ij → 节点 i
步骤 2: 消息聚合
所有邻居消息 → 聚合 → m_i
步骤 3: 节点更新
h_i + m_i → 更新函数 → h_i'

重复 L 层 → 最终节点表示

Python 实现 (Python Implementation):

class MPNNLayer(nn.Module):
    def __init__(self, node_in_dim, node_out_dim, edge_dim=None):
        super(MPNNLayer, self).__init__()

        # 消息函数
        if edge_dim:
            self.message_nn = nn.Sequential(
                nn.Linear(node_in_dim * 2 + edge_dim, node_out_dim),
                nn.ReLU()
            )
        else:
            self.message_nn = nn.Sequential(
                nn.Linear(node_in_dim * 2, node_out_dim),
                nn.ReLU()
            )

        # 更新函数
        self.update_nn = nn.Sequential(
            nn.Linear(node_in_dim + node_out_dim, node_out_dim),
            nn.ReLU()
        )

    def forward(self, node_features, edge_index, edge_features=None):
        """
        node_features: [num_nodes, node_in_dim]
        edge_index: [2, num_edges] (源节点,目标节点)
        edge_features: [num_edges, edge_dim] (可选)
        """
        num_nodes = node_features.size(0)

        # 消息生成
        src_nodes = edge_index[0]
        dst_nodes = edge_index[1]

        src_feat = node_features[src_nodes]
        dst_feat = node_features[dst_nodes]

        if edge_features is not None:
            messages = self.message_nn(torch.cat([src_feat, dst_feat, edge_features], dim=-1))
        else:
            messages = self.message_nn(torch.cat([src_feat, dst_feat], dim=-1))

        # 消息聚合 (求和)
        aggregated = torch.zeros(num_nodes, messages.size(1)).to(node_features.device)
        aggregated.index_add_(0, dst_nodes, messages)

        # 节点更新
        updated = self.update_nn(torch.cat([node_features, aggregated], dim=-1))

        return updated

5. 图自编码器 (Graph Autoencoder)

英文定义:

A Graph Autoencoder is an unsupervised learning model that learns node embeddings by reconstructing the graph structure from encoded representations.

中文翻译:

图自编码器是一种无监督学习模型,通过从编码表示重建图结构来学习节点嵌入。

5.1 图自编码器架构 (Graph Autoencoder Architecture)

图自编码器结构:

输入图 G = (A, X)
┌──────────────┐
│   编码器     │  GCN/GAT
│  Encoder     │  编码节点特征
└──────┬───────┘
  节点嵌入 Z
┌──────────────┐
│   解码器     │  内积/MLP
│  Decoder     │  重建邻接矩阵
└──────┬───────┘
重建的邻接矩阵 Â

损失 = 重建误差 + 正则化

编码器 (Encoder):

\[ Z = \text{GCN}(X, A) \]

解码器 (Decoder):

\[ \hat{A} = \sigma(ZZ^T) \]

损失函数 (Loss Function):

\[ \mathcal{L} = -\mathbb{E}_{q(Z|X,A)}[\log p(A|Z)] + \text{KL}(q(Z|X,A) \| p(Z)) \]

Python 实现 (Python Implementation):

class GraphAutoencoder(nn.Module):
    def __init__(self, in_features, hidden_features, latent_features):
        super(GraphAutoencoder, self).__init__()

        # 编码器 (2 层 GCN)
        self.encoder1 = GCNLayer(in_features, hidden_features)
        self.encoder2 = GCNLayer(hidden_features, latent_features)

        # 解码器 (内积)
        self.decoder = lambda Z: torch.sigmoid(torch.matmul(Z, Z.T))

    def encode(self, x, adj_matrix):
        h = self.encoder1(x, adj_matrix)
        z = self.encoder2(h, adj_matrix)
        return z

    def decode(self, z):
        return self.decoder(z)

    def forward(self, x, adj_matrix):
        z = self.encode(x, adj_matrix)
        adj_reconstructed = self.decode(z)
        return adj_reconstructed

    def loss(self, adj_original, adj_reconstructed, z):
        # 重建损失 (二元交叉熵)
        BCE_loss = F.binary_cross_entropy(adj_reconstructed, adj_original)

        # 正则化 (防止过拟合)
        reg_loss = torch.mean(torch.sum(z ** 2, dim=1))

        return BCE_loss + 0.01 * reg_loss

5.2 图变分自编码器 (Graph Variational Autoencoder, GraphVAE)

英文:

GraphVAE extends the graph autoencoder by learning a probabilistic distribution over the latent space, enabling generation of new graphs.

中文:

GraphVAE 通过在学习潜空间上学习概率分布来扩展图自编码器,能够生成新图。

重参数化技巧 (Reparameterization Trick):

\[ q(Z|X,A) = \mathcal{N}(Z; \mu(X,A), \text{diag}(\sigma^2(X,A))) \]
\[ Z = \mu + \epsilon \odot \sigma, \quad \epsilon \sim \mathcal{N}(0, I) \]

6. 应用与挑战 (Applications and Challenges)

6.1 主要应用 (Main Applications)

应用领域 英文 中文 示例
社交网络 Social Networks 社交网络 用户推荐、社区检测
分子图 Molecular Graphs 分子图 药物发现、性质预测
知识图谱 Knowledge Graphs 知识图谱 链接预测、实体对齐
推荐系统 Recommendation Systems 推荐系统 用户 - 物品图
计算机视觉 Computer Vision 计算机视觉 场景图、点云
自然语言处理 NLP 自然语言处理 依存句法树

6.2 GNN 挑战 (GNN Challenges)

挑战 英文 中文 当前研究方向
过度平滑 Over-smoothing 过度平滑 残差连接、跳跃连接
可扩展性 Scalability 可扩展性 图采样、子图训练
异构图 Heterogeneous Graphs 异构图 多类型节点/边
动态图 Dynamic Graphs 动态图 时间演化建模
可解释性 Interpretability 可解释性 注意力可视化
长程依赖 Long-range Dependencies 长程依赖 图 Transformer

6.3 图 Transformer (Graph Transformer)

英文:

Graph Transformers extend the Transformer architecture to graph-structured data, using self-attention over graph nodes.

中文:

图 Transformer 将 Transformer 架构扩展到图结构数据,在图节点上使用自注意力。

图自注意力 (Graph Self-Attention):

\[ \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + M\right)V \]

其中 \(M\) 是基于图结构的掩码矩阵。


🔑 关键术语对照表 (Glossary)

English 中文 定义
Graph Neural Network 图神经网络 处理图结构数据的神经网络
Node 节点 图中的基本单元
Edge 节点之间的连接
Adjacency matrix 邻接矩阵 表示图连接关系的矩阵
Graph Convolution 图卷积 图上的卷积操作
Graph Attention 图注意力 基于注意力的图聚合
Message Passing 消息传递 节点间信息传递框架
Graph Autoencoder 图自编码器 无监督图表示学习
Over-smoothing 过度平滑 深层 GNN 节点表示趋同
Graph Transformer 图 Transformer 基于 Transformer 的 GNN

编译完成时间: 2026-06-01
来源: Wikipedia Graph neural network, Graph convolutional network, Graph attention network 等条目
关联文档: deep-learning-concepts-zh-en.md


**Wikipedia 图神经网络核心概念 | 中英对照版** [返回顶部](#目录-table-of-contents)