torch_geometric.nn
- class Sequential(input_args: str, modules: List[Union[Tuple[Callable, str], Callable]])[source]
torch.nn.Sequential容器的扩展,用于定义一个顺序的 GNN 模型。由于GNN操作符接受多个输入参数,
torch_geometric.nn.Sequential还期望有全局输入参数和各个操作符的函数头定义。 如果省略,中间模块将操作其前一个模块的输出:from torch.nn import Linear, ReLU from torch_geometric.nn import Sequential, GCNConv model = Sequential('x, edge_index', [ (GCNConv(in_channels, 64), 'x, edge_index -> x'), ReLU(inplace=True), (GCNConv(64, 64), 'x, edge_index -> x'), ReLU(inplace=True), Linear(64, out_channels), ])
在这里,
'x, edge_index'定义了model的输入参数, 而'x, edge_index -> x'定义了函数头,即 输入 参数 和 返回类型GCNConv。特别是,这也允许创建更复杂的模型,例如利用
JumpingKnowledge:from torch.nn import Linear, ReLU, Dropout from torch_geometric.nn import Sequential, GCNConv, JumpingKnowledge from torch_geometric.nn import global_mean_pool model = Sequential('x, edge_index, batch', [ (Dropout(p=0.5), 'x -> x'), (GCNConv(dataset.num_features, 64), 'x, edge_index -> x1'), ReLU(inplace=True), (GCNConv(64, 64), 'x1, edge_index -> x2'), ReLU(inplace=True), (lambda x1, x2: [x1, x2], 'x1, x2 -> xs'), (JumpingKnowledge("cat", 64, num_layers=2), 'xs -> x'), (global_mean_pool, 'x, batch -> x'), Linear(2 * 64, dataset.num_classes), ])
- class Linear(in_channels: int, out_channels: int, bias: bool = True, weight_initializer: Optional[str] = None, bias_initializer: Optional[str] = None)[source]
对输入数据应用线性变换。
\[\mathbf{x}^{\prime} = \mathbf{x} \mathbf{W}^{\top} + \mathbf{b}\]与
torch.nn.Linear相比,它支持延迟初始化以及可定制的权重和偏置初始化。- Parameters:
in_channels (int) – 每个输入样本的大小。如果给定为
-1,则会延迟初始化。out_channels (int) – Size of each output sample.
bias (bool, optional) – If set to
False, the layer will not learn an additive bias. (default:True)weight_initializer (str, optional) – 权重矩阵的初始化器 (
"glorot","uniform","kaiming_uniform"或None). 如果设置为None,将匹配torch.nn.Linear的默认权重初始化。(默认值:None)bias_initializer (str, optional) – 偏置向量的初始化器 (
"zeros"或None)。 如果设置为None,将匹配torch.nn.Linear的默认偏置初始化。(默认值:None)
- Shapes:
输入: 特征 \((*, F_{in})\)
输出: 特征 \((*, F_{out})\)
- class HeteroLinear(in_channels: int, out_channels: int, num_types: int, is_sorted: bool = False, **kwargs)[source]
根据类型对传入的数据应用单独的线性变换。
对于类型 \(\kappa\),它计算
\[\mathbf{x}^{\prime}_{\kappa} = \mathbf{x}_{\kappa} \mathbf{W}^{\top}_{\kappa} + \mathbf{b}_{\kappa}.\]它支持延迟初始化和可定制的权重和偏置初始化。
- Parameters:
- Shapes:
输入: 特征 \((*, F_{in})\), 类型向量 \((*)\)
output: features \((*, F_{out})\)
- forward(x: Tensor, type_vec: Tensor) Tensor[source]
前向传播。
- Parameters:
x (torch.Tensor) – The input features.
type_vec (torch.Tensor) – A vector that maps each entry to a type.
- Return type:
- class HeteroDictLinear(in_channels: Union[int, Dict[Any, int]], out_channels: int, types: Optional[Any] = None, **kwargs)[source]
对传入的数据字典应用单独的线性变换。
对于键 \(\kappa\),它计算
\[\mathbf{x}^{\prime}_{\kappa} = \mathbf{x}_{\kappa} \mathbf{W}^{\top}_{\kappa} + \mathbf{b}_{\kappa}.\]它支持延迟初始化和可定制的权重和偏置初始化。
- Parameters:
卷积层
聚合操作符
聚合函数在图神经网络的消息传递框架和读出函数中扮演着重要角色。 具体来说,许多文献中的工作(Hamilton et al. (2017), Xu et al. (2018), Corso et al. (2020), Li et al. (2020), Tailor et al. (2021))表明,聚合函数的选择对模型的表示能力和性能有显著影响。 例如,均值聚合捕捉元素的分布(或比例),最大聚合被证明在识别代表性元素方面具有优势,而求和聚合则能够学习图的结构属性(Xu et al. (2018))。 最近的研究还表明,使用多重聚合(Corso et al. (2020), Tailor et al. (2021))和可学习聚合(Li et al. (2020))可能会带来显著的改进。 另一项研究则探讨了基于优化和隐式定义的聚合(Bartunov et al. (2022))。 此外,一个有趣的讨论涉及表示能力(通常通过实现为神经网络的可学习函数获得)与排列不变性的形式属性之间的权衡(Buterez et al. (2022))。
为了促进进一步的实验并统一GNN中聚合的概念,无论是在MessagePassing还是全局读取中,我们已将Aggregation的概念作为PyG中的首要原则。
截至目前,PyG提供了对各种聚合的支持——从较为简单的(例如,mean,max,sum),到高级的(例如,median,var,std),可学习的(例如,SoftmaxAggregation,PowerMeanAggregation,SetTransformerAggregation),以及奇特的(例如,MLPAggregation,LSTMAggregation,SortAggregation,EquilibriumAggregation):
from torch_geometric.nn import aggr
# Simple aggregations:
mean_aggr = aggr.MeanAggregation()
max_aggr = aggr.MaxAggregation()
# Advanced aggregations:
median_aggr = aggr.MedianAggregation()
# Learnable aggregations:
softmax_aggr = aggr.SoftmaxAggregation(learn=True)
powermean_aggr = aggr.PowerMeanAggregation(learn=True)
# Exotic aggregations:
lstm_aggr = aggr.LSTMAggregation(in_channels=..., out_channels=...)
sort_aggr = aggr.SortAggregation(k=4)
然后,我们可以轻松地将这些聚合应用于一批可能大小不同的集合。
为此,一个index向量定义了从输入元素到它们在输出中位置的映射:
# Feature matrix holding 1000 elements with 64 features each:
x = torch.randn(1000, 64)
# Randomly assign elements to 100 sets:
index = torch.randint(0, 100, (1000, ))
output = mean_aggr(x, index) # Output shape: [100, 64]
值得注意的是,所有聚合操作共享同一组前向参数,如torch_geometric.nn.aggr.Aggregation基类中详细描述的那样。
提供的每个聚合都可以在MessagePassing中使用,也可以用于层次/全局池化以获得图级表示:
import torch
from torch_geometric.nn import MessagePassing
class MyConv(MessagePassing):
def __init__(self, ...):
# Use a learnable softmax neighborhood aggregation:
super().__init__(aggr=aggr.SoftmaxAggregation(learn=True))
def forward(self, x, edge_index):
....
class MyGNN(torch.nn.Module)
def __init__(self, ...):
super().__init__()
self.conv = MyConv(...)
# Use a global sort aggregation:
self.global_pool = aggr.SortAggregation(k=4)
self.classifier = torch.nn.Linear(...)
def foward(self, x, edge_index, batch):
x = self.conv(x, edge_index).relu()
x = self.global_pool(x, batch)
x = self.classifier(x)
return x
此外,PyG的聚合包引入了两个新概念:
首先,聚合可以通过查找表从纯字符串解析,遵循class-resolver库的设计原则,例如,只需将"median"传递给MessagePassing模块。
这将自动解析为MedianAggregation类:
class MyConv(MessagePassing):
def __init__(self, ...):
super().__init__(aggr="median")
其次,可以通过MultiAggregation模块将多个聚合组合和堆叠,以增强图神经网络(GNNs)的表示能力(Corso et al. (2020), Tailor et al. (2021)):
class MyConv(MessagePassing):
def __init__(self, ...):
# Combines a set of aggregations and concatenates their results,
# i.e. its output will be `[num_nodes, 3 * out_channels]` here.
# Note that the interface also supports automatic resolution.
super().__init__(aggr=aggr.MultiAggregation(
['mean', 'std', aggr.SoftmaxAggregation(learn=True)]))
重要的是,MultiAggregation 提供了多种选项来组合其底层聚合的输出(例如,使用连接、求和、注意力等)通过其 mode 参数。
默认的 mode 执行连接操作("cat")。
要通过注意力进行组合,我们还需要指定 in_channels、out_channels 和 num_heads:
multi_aggr = aggr.MultiAggregation(
aggrs=['mean', 'std'],
mode='attn',
mode_kwargs=dict(in_channels=64, out_channels=64, num_heads=4),
)
如果聚合以列表形式给出,它们将自动解析为MultiAggregation,例如,aggr=['mean', 'std', 'median']。
最后,我们为SAGEConv层添加了完全支持的自定义聚合功能 — 只需覆盖其aggr参数,并利用聚合的力量在您的GNN中。
注意
你可以在这篇博客文章中了解更多关于torch_geometric.nn.aggr包的信息。
用于实现自定义聚合的抽象基类。 |
|
执行一个或多个聚合器的聚合操作,并组合聚合结果,如"Principal Neighbourhood Aggregation for Graph Nets"和"Adaptive Filters and Aggregator Fusion for Efficient Graph Convolutions"论文中所述。 |
|
一个聚合操作符,用于对一组元素中的特征进行求和。 |
|
一个聚合操作符,用于计算一组元素的特征平均值。 |
|
一个聚合操作符,它在一组元素中取特征方向的最大值。 |
|
一个聚合操作符,它在一组元素中取特征方向的最小值。 |
|
一个聚合操作符,用于在一组元素中乘以特征。 |
|
一个聚合操作符,用于计算一组元素的特征方差。 |
|
一个聚合操作符,用于计算一组元素的特征标准偏差。 |
|
基于温度项的softmax聚合操作符,如"DeeperGCN: All You Need to Train Deeper GCNs"论文中所述。 |
|
基于幂项的幂均值聚合操作符,如"DeeperGCN: All You Need to Train Deeper GCNs"论文中所述。 |
|
一个聚合操作符,返回一组数据的特征中位数。 |
|
一个聚合操作符,返回集合 \(\mathcal{X}\) 的特征方向的 \(q\) 分位数。 |
|
执行LSTM风格的聚合,其中要聚合的元素被解释为一个序列,如"Inductive Representation Learning on Large Graphs"论文中所述。 |
|
执行GRU聚合,其中要聚合的元素被解释为一个序列,如"具有自适应读数的图神经网络"论文中所述。 |
|
基于迭代内容注意力的Set2Set聚合操作符,如"顺序重要:集合的序列到序列"论文中所述。 |
|
结合一个或多个聚合器,并使用一个或多个缩放器转换其输出,如"Principal Neighbourhood Aggregation for Graph Nets"论文中介绍的那样。 |
|
来自"An End-to-End Deep Learning Architecture for Graph Classification"论文的池化操作符,其中节点特征根据其最后一个特征通道按降序排序。 |
|
来自"Accurate Learning of Graph Representations with Graph Multiset Pooling"论文的图多集变换器池化操作符。 |
|
来自"Graph Matching Networks for Learning the Similarity of Graph Structured Objects"论文的软注意力聚合层。 |
|
来自"Equilibrium Aggregation: Encoding Sets via Optimization"论文的平衡聚合层。 |
|
执行MLP聚合,其中要聚合的元素被展平为单个向量表示,然后通过多层感知器(MLP)进行处理,如"具有自适应读数的图神经网络"论文中所述。 |
|
执行深度集合聚合,其中要聚合的元素首先通过多层感知器(MLP)\(\phi_{\mathbf{\Theta}}\)进行转换,求和,然后通过另一个MLP \(\rho_{\mathbf{\Theta}}\)进行转换,如"具有自适应读数的图神经网络"论文中所建议的。 |
|
执行“Set Transformer”聚合,其中要聚合的元素通过多头注意力块进行处理,如“具有自适应读数的图神经网络”论文中所述。 |
|
来自"Learnable Commutative Monoids for Graph Neural Networks"论文的可交换幺半群聚合,其中元素使用具有\(\mathcal{O}(\log |\mathcal{V}|)\)深度的二叉树归约进行聚合。 |
|
执行来自"GNN-VPA: A Variance-Preserving Aggregation Strategy for Graph Neural Networks"论文的方差保持聚合(VPA)。 |
|
执行补丁变换器聚合,其中要聚合的元素通过跨补丁的多头注意力块进行处理,如"Simplifying Temporal Heterogeneous Network for Continuous-Time Link Prediction"论文中所述。 |
归一化层
应用批量归一化处理一批特征,如"批量归一化:通过减少内部协变量偏移加速深度网络训练"论文中所述。 |
|
应用批量归一化处理一批异质特征,如"批量归一化:通过减少内部协变量偏移加速深度网络训练"论文中所述。 |
|
对一批节点特征中的每个单独示例应用实例归一化,如"实例归一化:快速风格化的缺失成分"论文中所述。 |
|
在一批特征中对每个单独的示例应用层归一化,如"Layer Normalization"论文中所述。 |
|
如"Layer Normalization"论文中所述,对一批异质特征中的每个单独示例应用层归一化。 |
|
应用图规范化于单个图,如"GraphNorm: A Principled Approach to Accelerating Graph Neural Network Training"论文中所述。 |
|
对一批节点特征中的每个单独图应用图大小归一化,如"基准测试图神经网络"论文中所述。 |
|
应用节点特征上的对归一化,如"PairNorm: Tackling Oversmoothing in GNNs"论文中所述。 |
|
应用层归一化,通过从输入中减去均值,如"Revisiting 'Over-smoothing' in Deep GCNs"论文中所述。 |
|
应用消息归一化于聚合的消息,如"DeeperGCNs: All You Need to Train Deeper GCNs"论文中所述。 |
|
来自"Towards Deeper Graph Neural Networks with Differentiable Group Normalization"论文的可微分组归一化层,该层通过可学习的软聚类分配对节点特征进行分组归一化。 |
池化层
通过跨节点维度添加节点特征,返回批处理级别的图级输出。 |
|
通过跨节点维度平均节点特征,返回批处理级别的图级输出。 |
|
通过跨节点维度的通道方向最大值返回批处理级别的图级输出。 |
|
一个基类,用于通过 |
|
通过 |
|
通过 |
|
通过 |
|
通过 |
|
\(\mathrm{top}_k\) 池化操作符来自 "Graph U-Nets", "Towards Sparse Hierarchical Graph Classifiers" 和 "Understanding Attention and Generalization in Graph Neural Networks" 论文。 |
|
自注意力池化操作符来自"Self-Attention Graph Pooling"和"Understanding Attention and Generalization in Graph Neural Networks"论文。 |
|
边缘池化操作符来自"Towards Graph Pooling by Edge Contraction"和"Edge Contraction Pooling for Graph Neural Networks"论文。 |
|
来自"基于边的图组件池化"论文的集群池化操作符。 |
|
来自"ASAP: Adaptive Structure Aware Pooling for Learning Hierarchical Graph Representations"论文的自适应结构感知池化操作符。 |
|
基于路径积分的池化操作符,来自"基于路径积分的图神经网络卷积和池化"论文。 |
|
基于内存的池化层来自"基于内存的图网络"论文,该层基于软聚类分配学习粗化的图表示。 |
|
根据 |
|
根据 |
|
根据 |
|
最大池化相邻节点特征,其中 |
|
根据 |
|
平均池化相邻节点的特征,其中 |
|
一种来自"Weighted Graph Cuts without Eigenvectors: A Multilevel Approach"论文的贪心聚类算法,该算法选择一个未标记的顶点并将其与其未标记的邻居之一(最大化其边权重)进行匹配。 |
|
体素网格池化来自,例如,Dynamic Edge-Conditioned Filters in Convolutional Networks on Graphs 论文,它在点云上覆盖一个用户定义大小的规则网格,并将同一体素内的所有点聚类。 |
|
一种采样算法,来自"PointNet++: Deep Hierarchical Feature Learning on Point Sets in a Metric Space"论文,该算法迭代地采样相对于其余点最远的点。 |
|
为 |
|
Computes graph edges to the nearest |
|
为 |
|
Computes graph edges to the nearest approximated |
|
为 |
|
计算到给定距离内所有点的图边。 |
|
为 |
反池化层
来自"PointNet++: Deep Hierarchical Feature Learning on Point Sets in a Metric Space"论文的k-NN插值。 |
Models
KGE 模型
用于实现自定义KGE模型的抽象基类。 |
|
来自"Translating Embeddings for Modeling Multi-Relational Data"论文的TransE模型。 |
|
来自"Complex Embeddings for Simple Link Prediction"论文的ComplEx模型。 |
|
来自"嵌入实体和关系以在知识库中进行学习和推理"论文的DistMult模型。 |
|
来自"RotatE: Knowledge Graph Embedding by Relational Rotation in Complex Space"论文的RotatE模型。 |
编码
- class PositionalEncoding(out_channels: int, base_freq: float = 0.0001, granularity: float = 1.0)[source]
来自“Attention Is All You Need”论文的位置编码方案。
\[ \begin{align}\begin{aligned}PE(x)_{2 \cdot i} &= \sin(x / 10000^{2 \cdot i / d})\\PE(x)_{2 \cdot i + 1} &= \cos(x / 10000^{2 \cdot i / d})\end{aligned}\end{align} \]其中 \(x\) 是位置,\(i\) 是维度。
- Parameters:
- class TemporalEncoding(out_channels: int)[source]
来自“我们真的需要复杂的时间网络模型架构吗?”论文的时间编码函数。
它首先将每个条目映射到一个具有指数递减值的向量, 然后使用余弦函数将所有值投影到范围 \([-1, 1]\)。
\[y_{i} = \cos \left(x \cdot \sqrt{d}^{-(i - 1)/\sqrt{d}} \right)\]其中 \(d\) 定义了输出特征的维度,且 \(1 \leq i \leq d\)。
- Parameters:
out_channels (int) – 每个输出样本的大小 \(d\)。
Functional
来自"Improving Molecular Graph Neural Network Explainability with Orthonormalization and Induced Sparsity"论文的批量表示正交性惩罚。 |
|
密集卷积层
密集池化层
来自"Hierarchical Graph Representation Learning with Differentiable Pooling"论文的可微分池化操作符。 |
|
来自"图神经网络中的谱聚类用于图池化"论文的MinCut池化操作符。 |
|
来自"Graph Clustering with Graph Neural Networks"论文的谱模块化池化操作符。 |
模型转换
- class Transformer(module: Module, input_map: Optional[Dict[str, str]] = None, debug: bool = False)[source]
一个
Transformer逐节点执行 FX 图,对每个节点应用转换,并生成一个新的torch.nn.Module。 它暴露了一个transform()方法,该方法返回转换后的Module。Transformer完全以符号方式工作。Transformer类中的方法可以被重写以自定义转换行为。transform() +-- Iterate over each node in the graph +-- placeholder() +-- get_attr() +-- call_function() +-- call_method() +-- call_module() +-- call_message_passing_module() +-- call_global_pooling_module() +-- output() +-- Erase unused nodes in the graph +-- Iterate over each children module +-- init_submodule()与
torch.fx.Transformer类相比,Transformer暴露了额外的功能:它将
call_module()细分为调用常规torch.nn.Module的节点(call_module()),调用MessagePassing模块的节点(call_message_passing_module()),或调用GlobalPooling模块的节点(call_global_pooling_module())。它允许通过
init_submodule()自定义或初始化新的子模块它允许通过
is_edge_level()推断一个节点返回的是节点级别还是边级别的信息。
- Parameters:
- transform() GraphModule[source]
转换
self.module并返回一个转换后的torch.fx.GraphModule。- Return type:
- to_hetero(module: Module, metadata: Tuple[List[str], List[Tuple[str, str, str]]], aggr: str = 'sum', input_map: Optional[Dict[str, str]] = None, debug: bool = False) GraphModule[source]
将同质的GNN模型转换为其异质等价模型,其中为
metadata[0]中的每个节点类型学习节点表示,并在metadata[1]中的每个边类型之间交换消息,如“使用图卷积网络建模关系数据”论文中所描述。import torch from torch_geometric.nn import SAGEConv, to_hetero class GNN(torch.nn.Module): def __init__(self): super().__init__() self.conv1 = SAGEConv((-1, -1), 32) self.conv2 = SAGEConv((32, 32), 32) def forward(self, x, edge_index): x = self.conv1(x, edge_index).relu() x = self.conv2(x, edge_index).relu() return x model = GNN() node_types = ['paper', 'author'] edge_types = [ ('paper', 'cites', 'paper'), ('paper', 'written_by', 'author'), ('author', 'writes', 'paper'), ] metadata = (node_types, edge_types) model = to_hetero(model, metadata) model(x_dict, edge_index_dict)
其中
x_dict和edge_index_dict表示分别保存每种节点类型和边类型的节点特征和边连接信息的字典。下图显示了左侧同质模型的原始计算图,以及右侧新获得的异质模型的计算图:
通过
to_hetero()转换模型。在这里,每个
MessagePassing实例 \(f_{\theta}^{(\ell)}\) 被复制并存储在一个集合 \(\{ f_{\theta}^{(\ell, r)} : r \in \mathcal{R} \}\) 中(每个关系在 \(\mathcal{R}\) 中有一个实例),并且第 \(\ell\) 层的消息传递是通过以下方式进行的:\[\mathbf{h}^{(\ell)}_v = \bigoplus_{r \in \mathcal{R}} f_{\theta}^{(\ell, r)} ( \mathbf{h}^{(\ell - 1)}_v, \{ \mathbf{h}^{(\ell - 1)}_w : w \in \mathcal{N}^{(r)}(v) \}),\]其中 \(\mathcal{N}^{(r)}(v)\) 表示在关系 \(r \in \mathcal{R}\) 下节点 \(v \in \mathcal{V}\) 的邻域,\(\bigoplus\) 表示用于聚合由不同关系生成的节点嵌入的聚合方案
aggr("sum"、"mean"、"min"、"max"或"mul")。- Parameters:
模块 (torch.nn.Module) – 要转换的同类模型。
metadata (Tuple[List[str], List[Tuple[str, str, str]]]) – The metadata of the heterogeneous graph, i.e. its node and edge types given by a list of strings and a list of string triplets, respectively. See
torch_geometric.data.HeteroData.metadata()for more information.aggr (str, 可选) – 用于分组节点嵌入的聚合方案,这些嵌入由不同关系生成 (
"sum","mean","min","max","mul")。(默认:"sum")input_map (Dict[str, str], optional) – 一个字典,用于保存关于
module.forward输入参数类型的信息。 例如,如果arg是一个节点级别的参数,那么input_map['arg'] = 'node',否则input_map['arg'] = 'edge'。 如果input_map没有进一步指定,将尝试自动确定输入参数的正确类型。 (默认值:None)debug (bool, optional) – If set to
True, will perform transformation in debug mode. (default:False)
- Return type:
- to_hetero_with_bases(module: Module, metadata: Tuple[List[str], List[Tuple[str, str, str]]], num_bases: int, in_channels: Optional[Dict[str, int]] = None, input_map: Optional[Dict[str, str]] = None, debug: bool = False) GraphModule[source]
通过“使用图卷积网络建模关系数据”论文中引入的基分解技术,将同质的GNN模型转换为其异质等价模型。
为此,异构图被映射到一个类型化的同构图中,其中其特征表示被对齐并分组为单一表示。模型内的所有GNN层随后将通过基础分解正则化执行消息传递。这种转换在高度多关系的数据中特别有用,使得参数的数量不再依赖于输入图的关系数量:
import torch from torch_geometric.nn import SAGEConv, to_hetero_with_bases class GNN(torch.nn.Module): def __init__(self): super().__init__() self.conv1 = SAGEConv((16, 16), 32) self.conv2 = SAGEConv((32, 32), 32) def forward(self, x, edge_index): x = self.conv1(x, edge_index).relu() x = self.conv2(x, edge_index).relu() return x model = GNN() node_types = ['paper', 'author'] edge_types = [ ('paper', 'cites', 'paper'), ('paper', 'written_by', 'author'), ('author', 'writes', 'paper'), ] metadata = (node_types, edge_types) model = to_hetero_with_bases(model, metadata, num_bases=3, in_channels={'x': 16}) model(x_dict, edge_index_dict)
其中
x_dict和edge_index_dict表示分别保存每种节点类型和边类型的节点特征和边连接信息的字典。 如果为特定输入参数提供了in_channels,则其异构特征信息首先会与给定的维度对齐。下图左侧显示了同质模型的原始计算图,右侧显示了新获得的正则化异质模型的计算图:
通过
to_hetero_with_bases()转换模型。在这里,每个
MessagePassing实例 \(f_{\theta}^{(\ell)}\)被复制num_bases次并 存储在一个集合\(\{ f_{\theta}^{(\ell, b)} : b \in \{ 1, \ldots, B \} \}\)中(每个基在num_bases中有一个实例),并且在层\(\ell\)中的消息传递是通过\[\mathbf{h}^{(\ell)}_v = \sum_{r \in \mathcal{R}} \sum_{b=1}^B f_{\theta}^{(\ell, b)} ( \mathbf{h}^{(\ell - 1)}_v, \{ a^{(\ell)}_{r, b} \cdot \mathbf{h}^{(\ell - 1)}_w : w \in \mathcal{N}^{(r)}(v) \}),\]其中 \(\mathcal{N}^{(r)}(v)\) 表示在关系 \(r \in \mathcal{R}\) 下 \(v \in \mathcal{V}\) 的邻域。值得注意的是,只有可训练的基础系数 \(a^{(\ell)}_{r, b}\) 依赖于 \(\mathcal{R}\) 中的关系。
- Parameters:
module (torch.nn.Module) – The homogeneous model to transform.
metadata (Tuple[List[str], List[Tuple[str, str, str]]]) – The metadata of the heterogeneous graph, i.e. its node and edge types given by a list of strings and a list of string triplets, respectively. See
torch_geometric.data.HeteroData.metadata()for more information.num_bases (int) – 要使用的基础数量。
in_channels (Dict[str, int], optional) – 一个字典,包含有关
module.forward输入参数的期望输入特征维度的信息。 如果为特定输入参数提供了in_channels,则其异构特征信息首先会与给定的维度对齐。 这允许处理不同类型节点和边特征的不同特征维度。(默认值:None)input_map (Dict[str, str], optional) – A dictionary holding information about the type of input arguments of
module.forward. For example, in caseargis a node-level argument, theninput_map['arg'] = 'node', andinput_map['arg'] = 'edge'otherwise. In caseinput_mapis not further specified, will try to automatically determine the correct type of input arguments. (default:None)debug (bool, optional) – If set to
True, will perform transformation in debug mode. (default:False)
- Return type:
DataParallel 层
- class DataParallel(module, device_ids=None, output_device=None, follow_batch=None, exclude_keys=None)[source]
在模块级别实现数据并行。
这个容器通过将一系列
torch_geometric.data.Data对象分割并将它们复制为torch_geometric.data.Batch对象到每个设备上,来并行化给定module的应用。 在前向传播过程中,模块在每个设备上被复制,每个副本处理一部分输入。 在反向传播过程中,来自每个副本的梯度被汇总到原始模块中。批量大小应大于使用的GPU数量。
并行化的
module必须将其参数和缓冲区放在device_ids[0]上。注意
您需要使用
torch_geometric.loader.DataListLoader来加载这个模块。警告
建议使用
torch.nn.parallel.DistributedDataParallel而不是DataParallel进行多GPU训练。DataParallel通常比DistributedDataParallel慢得多,即使在单台机器上也是如此。 查看 这里 以了解如何将 PyG 与DistributedDataParallel结合使用的示例。- Parameters:
module (Module) – 要并行化的模块。
device_ids (整数列表 或 torch.device) – CUDA 设备。 (默认值:所有设备)
output_device (int 或 torch.device) – 输出的设备位置。 (默认:
device_ids[0])follow_batch (list 或 tuple, 可选) – 为列表中的每个键创建分配批处理向量。(默认值:
None)
模型中心
- class PyGModelHubMixin(model_name: str, dataset_name: str, model_kwargs: Dict)[source]
一个用于保存和加载模型到 Huggingface Model Hub 的 mixin。
from torch_geometric.datasets import Planetoid from torch_geometric.nn import Node2Vec from torch_geometric.nn.model_hub import PyGModelHubMixin # Define your class with the mixin: class N2V(Node2Vec, PyGModelHubMixin): def __init__(self,model_name, dataset_name, model_kwargs): Node2Vec.__init__(self,**model_kwargs) PyGModelHubMixin.__init__(self, model_name, dataset_name, model_kwargs) # Instantiate your model: n2v = N2V(model_name='node2vec', dataset_name='Cora', model_kwargs=dict( edge_index=data.edge_index, embedding_dim=128, walk_length=20, context_size=10, walks_per_node=10, num_negative_samples=1, p=1, q=1, sparse=True)) # Train the model: ... # Push to the HuggingFace hub: repo_id = ... # your repo id n2v.save_pretrained( local_file_path, push_to_hub=True, repo_id=repo_id, ) # Load the model for inference: # The required arguments are the repo id/local folder, and any model # initialisation arguments that are not native python types (e.g # Node2Vec requires the edge_index argument which is not stored in the # model hub). model = N2V.from_pretrained( repo_id, model_name='node2vec', dataset_name='Cora', edge_index=data.edge_index, )
- Parameters:
- save_pretrained(save_directory: Union[str, Path], push_to_hub: bool = False, repo_id: Optional[str] = None, **kwargs)[source]
将训练好的模型保存到本地目录或HuggingFace模型中心。
- classmethod from_pretrained(pretrained_model_name_or_path: str, force_download: bool = False, resume_download: bool = False, proxies: Optional[Dict] = None, token: Optional[Union[str, bool]] = None, cache_dir: Optional[str] = None, local_files_only: bool = False, **model_kwargs) Any[source]
从HuggingFace中心下载并实例化一个模型。
- Parameters:
pretrained_model_name_or_path (str) –
可以是以下之一:
托管在HuggingFace hub中的预训练模型的
model_id。您可以通过在
model_id末尾添加@来加载特定版本的模型。包含保存的模型权重的目录路径。
如果您同时提供了配置
config和状态字典state_dict,则为None。
force_download (bool, optional) – 是否强制 (重新)下载模型权重和配置文件, 覆盖缓存版本(如果存在)。 (默认值:
False)resume_download (bool, optional) – 是否删除未完全接收的文件。如果存在这样的文件,将尝试恢复下载。(默认值:
False)proxies (Dict[str, str], optional) – 一个按协议或端点使用的代理服务器字典,例如,
{'http': 'foo.bar:3128', 'http://host': 'foo.bar:4012'}。 代理服务器在每个请求中使用。(默认值:None)token (str 或 bool, 可选) – 用于远程文件的HTTP承载授权的token。如果设置为
True,将使用运行transformers-cli login时生成的token(存储在huggingface中)。如果你想使用私有模型,这是必需的。(默认值:None)cache_dir (str, optional) – 如果不使用标准缓存,则应缓存下载的模型配置的目录路径。(默认值:
None)local_files_only (bool, 可选) – 是否仅查看本地文件,即不尝试下载模型。 (默认:
False)**model_kwargs – 在初始化期间传递给模型的额外关键字参数。
- Return type:
模型摘要
- summary(model: Module, *args, max_depth: int = 3, leaf_module: Optional[Union[Module, List[Module]]] = 'MessagePassing', **kwargs) str[source]
总结给定的
torch.nn.Module。 总结的信息包括(1)层名称,(2)输入和输出形状,以及(3)参数数量。import torch from torch_geometric.nn import GCN, summary model = GCN(128, 64, num_layers=2, out_channels=32) x = torch.randn(100, 128) edge_index = torch.randint(100, size=(2, 20)) print(summary(model, x, edge_index))
+---------------------+---------------------+--------------+--------+ | Layer | Input Shape | Output Shape | #Param | |---------------------+---------------------+--------------+--------| | GCN | [100, 128], [2, 20] | [100, 32] | 10,336 | | ├─(act)ReLU | [100, 64] | [100, 64] | -- | | ├─(convs)ModuleList | -- | -- | 10,336 | | │ └─(0)GCNConv | [100, 128], [2, 20] | [100, 64] | 8,256 | | │ └─(1)GCNConv | [100, 64], [2, 20] | [100, 32] | 2,080 | +---------------------+---------------------+--------------+--------+
- Parameters:
model (torch.nn.Module) – 要总结的模型。
*args –
model的参数。max_depth (int, optional) – 要显示的嵌套层深度。 任何超过此深度的层将不会显示在摘要中。(默认值:
3)leaf_module (torch.nn.Module 或 [torch.nn.Module], 可选) – 被视为叶子模块的模块,其子模块被排除在摘要之外。 (默认:
MessagePassing)**kwargs –
model的额外参数。
- Return type: