torch_geometric.nn.conv.RGCNConv
- class RGCNConv(in_channels: Union[int, Tuple[int, int]], out_channels: int, num_relations: int, num_bases: Optional[int] = None, num_blocks: Optional[int] = None, aggr: str = 'mean', root_weight: bool = True, is_sorted: bool = False, bias: bool = True, **kwargs)[source]
Bases:
MessagePassing来自“使用图卷积网络建模关系数据”论文的关系图卷积操作符。
\[\mathbf{x}^{\prime}_i = \mathbf{\Theta}_{\textrm{root}} \cdot \mathbf{x}_i + \sum_{r \in \mathcal{R}} \sum_{j \in \mathcal{N}_r(i)} \frac{1}{|\mathcal{N}_r(i)|} \mathbf{\Theta}_r \cdot \mathbf{x}_j,\]其中 \(\mathcal{R}\) 表示关系的集合,即 边的类型。 边的类型需要是一个一维的
torch.long张量,它 为每条边存储一个关系标识符 \(\in \{ 0, \ldots, |\mathcal{R}| - 1\}\)。注意
此实现通过迭代每种单独的关系类型,尽可能高效地利用内存。 因此,如果图具有大量关系,可能会导致GPU利用率较低。 作为替代方法,
FastRGCNConv不会迭代每种单独的类型,但可能会消耗大量内存以进行补偿。 我们建议检查这两种实现,看看哪一种适合您的需求。注意
RGCNConv可以使用 动态形状,这意味着中间张量的形状可以在运行时确定。 如果您的设备不支持动态形状,请使用FastRGCNConv代替。- Parameters:
in_channels (int 或 tuple) – 每个输入样本的大小。一个元组 对应于源和目标维度的大小。 如果没有给出输入特征,此参数应 对应于图中节点的数量。
out_channels (int) – Size of each output sample.
num_relations (int) – Number of relations.
num_bases (int, optional) – If set, this layer will use the basis-decomposition regularization scheme where
num_basesdenotes the number of bases to use. (default:None)num_blocks (int, optional) – If set, this layer will use the block-diagonal-decomposition regularization scheme where
num_blocksdenotes the number of blocks to use. (default:None)aggr (str, 可选) – 使用的聚合方案 (
"add","mean","max"). (默认:"mean")root_weight (bool, optional) – If set to
False, the layer will not add transformed root node features to the output. (default:True)is_sorted (bool, 可选) – 如果设置为
True,则假定edge_index已按edge_type排序。这可以避免 内部重新排序数据,从而提高运行时和内存 效率。(默认值:False)bias (bool, optional) – If set to
False, the layer will not learn an additive bias. (default:True)**kwargs (optional) – Additional arguments of
torch_geometric.nn.conv.MessagePassing.
- forward(x: Union[Tensor, None, Tuple[Optional[Tensor], Tensor]], edge_index: Union[Tensor, SparseTensor], edge_type: Optional[Tensor] = None)[source]
运行模块的前向传播。
- Parameters:
x (torch.Tensor 或 tuple, 可选) – 输入节点特征。 可以是一个
[num_nodes, in_channels]节点特征 矩阵,或者一个可选的一维节点索引张量(在 这种情况下,输入特征被视为可训练的节点 嵌入)。 此外,x可以是tuple类型,表示 源节点和目标节点特征。edge_index (torch.Tensor or SparseTensor) – The edge indices.
edge_type (torch.Tensor, optional) – 一维关系类型/索引,用于
edge_index中的每条边。如果edge_index的类型是torch_sparse.SparseTensor,则应为None。(默认值:None)