torch_geometric.nn.conv.RGATConv

class RGATConv(in_channels: int, out_channels: int, num_relations: int, num_bases: Optional[int] = None, num_blocks: Optional[int] = None, mod: Optional[str] = None, attention_mechanism: str = 'across-relation', attention_mode: str = 'additive-self-attention', heads: int = 1, dim: int = 1, concat: bool = True, negative_slope: float = 0.2, dropout: float = 0.0, edge_dim: Optional[int] = None, bias: bool = True, **kwargs)[source]

Bases: MessagePassing

关系图注意力操作符来自“关系图注意力网络”论文。

在这里,注意力对数 \(\mathbf{a}^{(r)}_{i,j}\) 是为每个关系类型 \(r\) 计算的,借助于查询和键核,

\[\mathbf{q}^{(r)}_i = \mathbf{W}_1^{(r)}\mathbf{x}_{i} \cdot \mathbf{Q}^{(r)} \quad \textrm{and} \quad \mathbf{k}^{(r)}_i = \mathbf{W}_1^{(r)}\mathbf{x}_{i} \cdot \mathbf{K}^{(r)}.\]

已经提出了两种方案来计算每种关系类型 \(r\) 的注意力对数 \(\mathbf{a}^{(r)}_{i,j}\)

加性注意力

\[\mathbf{a}^{(r)}_{i,j} = \mathrm{LeakyReLU}(\mathbf{q}^{(r)}_i + \mathbf{k}^{(r)}_j)\]

乘法注意力

\[\mathbf{a}^{(r)}_{i,j} = \mathbf{q}^{(r)}_i \cdot \mathbf{k}^{(r)}_j.\]

如果图具有多维边特征 \(\mathbf{e}^{(r)}_{i,j}\),则每个关系类型 \(r\) 的注意力分数 \(\mathbf{a}^{(r)}_{i,j}\) 计算如下

\[\mathbf{a}^{(r)}_{i,j} = \mathrm{LeakyReLU}(\mathbf{q}^{(r)}_i + \mathbf{k}^{(r)}_j + \mathbf{W}_2^{(r)}\mathbf{e}^{(r)}_{i,j})\]

\[\mathbf{a}^{(r)}_{i,j} = \mathbf{q}^{(r)}_i \cdot \mathbf{k}^{(r)}_j \cdot \mathbf{W}_2^{(r)} \mathbf{e}^{(r)}_{i,j},\]

分别地。 对于每种关系类型 \(r\) 的注意力系数 \(\alpha^{(r)}_{i,j}\) 通过两种不同的注意力机制获得: 关系内 注意力机制

\[\alpha^{(r)}_{i,j} = \frac{\exp(\mathbf{a}^{(r)}_{i,j})} {\sum_{k \in \mathcal{N}_r(i)} \exp(\mathbf{a}^{(r)}_{i,k})}\]

跨关系注意力机制

\[\alpha^{(r)}_{i,j} = \frac{\exp(\mathbf{a}^{(r)}_{i,j})} {\sum_{r^{\prime} \in \mathcal{R}} \sum_{k \in \mathcal{N}_{r^{\prime}}(i)} \exp(\mathbf{a}^{(r^{\prime})}_{i,k})}\]

其中 \(\mathcal{R}\) 表示关系的集合, 边的类型。 边的类型需要是一个一维的 torch.long 张量,它 为每条边存储一个关系标识符 \(\in \{ 0, \ldots, |\mathcal{R}| - 1\}\)

为了增强基于注意力的图神经网络的区分能力,该层进一步实现了四种不同的基数保持选项,如“通过基数保持改进图神经网络中的注意力机制”论文中所提出的:

\[ \begin{align}\begin{aligned}\text{additive:}~~~\mathbf{x}^{{\prime}(r)}_i &= \sum_{j \in \mathcal{N}_r(i)} \alpha^{(r)}_{i,j} \mathbf{x}^{(r)}_j + \mathcal{W} \odot \sum_{j \in \mathcal{N}_r(i)} \mathbf{x}^{(r)}_j\\\text{scaled:}~~~\mathbf{x}^{{\prime}(r)}_i &= \psi(|\mathcal{N}_r(i)|) \odot \sum_{j \in \mathcal{N}_r(i)} \alpha^{(r)}_{i,j} \mathbf{x}^{(r)}_j\\\text{f-additive:}~~~\mathbf{x}^{{\prime}(r)}_i &= \sum_{j \in \mathcal{N}_r(i)} (\alpha^{(r)}_{i,j} + 1) \cdot \mathbf{x}^{(r)}_j\\\text{f-scaled:}~~~\mathbf{x}^{{\prime}(r)}_i &= |\mathcal{N}_r(i)| \odot \sum_{j \in \mathcal{N}_r(i)} \alpha^{(r)}_{i,j} \mathbf{x}^{(r)}_j\end{aligned}\end{align} \]
  • 如果 attention_mode="additive-self-attention" 并且 concat=True,该层将为每个节点输出 heads * out_channels 个特征。

  • 如果 attention_mode="multiplicative-self-attention" 并且 concat=True,该层将为每个节点输出 heads * dim * out_channels 个特征。

  • 如果 attention_mode="additive-self-attention" 并且 concat=False,该层会为每个节点输出 out_channels 个特征。

  • 如果 attention_mode="multiplicative-self-attention" 并且 concat=False,该层会为每个节点输出 dim * out_channels 个特征。

请确保如果使用此层的多个实例,请相应地设置下一层的in_channels参数。

注意

有关使用 RGATConv 的示例,请参见 examples/rgat.py

Parameters:
  • in_channels (int) – Size of each input sample.

  • out_channels (int) – Size of each output sample.

  • num_relations (int) – 关系的数量。

  • num_bases (int, optional) – 如果设置,该层将使用 基分解正则化方案,其中 num_bases 表示要使用的基的数量。(默认值:None

  • num_blocks (int, optional) – 如果设置,该层将使用块对角分解正则化方案,其中 num_blocks 表示要使用的块数。 (默认: None)

  • mod (str, optional) – 使用的基数保持选项。 ("additive", "scaled", "f-additive", "f-scaled", None). (默认: None)

  • attention_mechanism (str, 可选) – 使用的注意力机制 ("within-relation", "across-relation"). (默认: "across-relation")

  • attention_mode (str, optional) – 计算注意力得分的模式。 ("additive-self-attention", "multiplicative-self-attention"). (默认: "additive-self-attention")

  • heads (int, optional) – Number of multi-head-attentions. (default: 1)

  • dim (int) – 查询和键内核的维度数。 (默认值: 1)

  • concat (bool, 可选) – 如果设置为 False,多头注意力将被平均而不是连接。 (默认: True)

  • negative_slope (float, optional) – LeakyReLU 负斜率的倾斜角度。(默认值:0.2

  • dropout (float, optional) – 归一化注意力系数的丢弃概率,在训练期间使每个节点暴露于随机采样的邻域中。(默认值:0

  • edge_dim (int, optional) – 边的特征维度(如果有的话)。(默认值: None)

  • bias (bool, 可选) – 如果设置为 False,该层将不会学习一个加性偏置。(默认值:True

  • **kwargs (optional) – Additional arguments of torch_geometric.nn.conv.MessagePassing.

forward(x: Tensor, edge_index: Union[Tensor, SparseTensor], edge_type: Optional[Tensor] = None, edge_attr: Optional[Tensor] = None, size: Optional[Tuple[int, int]] = None, return_attention_weights=None)[source]

运行模块的前向传播。

Parameters:
  • x (torch.Tensor) – 输入节点特征。 可以是一个 [num_nodes, in_channels] 节点特征 矩阵,或者一个可选的一维节点索引张量(在 这种情况下,输入特征被视为可训练的节点 嵌入)。

  • edge_index (torch.Tensor or SparseTensor) – The edge indices.

  • edge_type (torch.Tensor, optional) – 一维关系类型/索引,用于edge_index中的每条边。 在edge_index类型为torch_sparse.SparseTensortorch.sparse.Tensor时,应仅为None。(默认值:None

  • edge_attr (torch.Tensor, optional) – The edge features. (default: None)

  • size ((int, int), optional) – 邻接矩阵的形状。 (default: None)

  • return_attention_weights (bool, optional) – 如果设置为 True, 将额外返回元组 (edge_index, attention_weights),其中包含计算得到的每条边的注意力权重。(默认值:None

reset_parameters()[source]

重置模块的所有可学习参数。