torch_geometric.nn.conv.GATv2Conv
- class GATv2Conv(in_channels: Union[int, Tuple[int, int]], out_channels: int, heads: int = 1, concat: bool = True, negative_slope: float = 0.2, dropout: float = 0.0, add_self_loops: bool = True, edge_dim: Optional[int] = None, fill_value: Union[float, Tensor, str] = 'mean', bias: bool = True, share_weights: bool = False, residual: bool = False, **kwargs)[source]
Bases:
MessagePassing来自“图注意力网络有多专注?”论文的GATv2操作符,它修复了标准
GATConv层的静态注意力问题。由于标准GAT中的线性层是紧接着应用的,因此被关注节点的排名与查询节点无关。相比之下,在GATv2中,每个节点都可以关注任何其他节点。\[\mathbf{x}^{\prime}_i = \sum_{j \in \mathcal{N}(i) \cup \{ i \}} \alpha_{i,j}\mathbf{\Theta}_{t}\mathbf{x}_{j},\]其中注意力系数 \(\alpha_{i,j}\) 的计算方式为
\[\alpha_{i,j} = \frac{ \exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left( \mathbf{\Theta}_{s} \mathbf{x}_i + \mathbf{\Theta}_{t} \mathbf{x}_j \right)\right)} {\sum_{k \in \mathcal{N}(i) \cup \{ i \}} \exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left( \mathbf{\Theta}_{s} \mathbf{x}_i + \mathbf{\Theta}_{t} \mathbf{x}_k \right)\right)}.\]如果图具有多维边特征 \(\mathbf{e}_{i,j}\), 则注意力系数 \(\alpha_{i,j}\) 计算如下:
\[\alpha_{i,j} = \frac{ \exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left( \mathbf{\Theta}_{s} \mathbf{x}_i + \mathbf{\Theta}_{t} \mathbf{x}_j + \mathbf{\Theta}_{e} \mathbf{e}_{i,j} \right)\right)} {\sum_{k \in \mathcal{N}(i) \cup \{ i \}} \exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left( \mathbf{\Theta}_{s} \mathbf{x}_i + \mathbf{\Theta}_{t} \mathbf{x}_k + \mathbf{\Theta}_{e} \mathbf{e}_{i,k}] \right)\right)}.\]- Parameters:
in_channels (int 或 tuple) – 每个输入样本的大小,或
-1以从 forward 方法的第一个输入推导大小。 在二分图的情况下,元组对应于源维度和目标维度的大小。out_channels (int) – Size of each output sample.
heads (int, optional) – Number of multi-head-attentions. (default:
1)concat (bool, optional) – If set to
False, the multi-head attentions are averaged instead of concatenated. (default:True)negative_slope (float, optional) – LeakyReLU angle of the negative slope. (default:
0.2)dropout (float, optional) – Dropout probability of the normalized attention coefficients which exposes each node to a stochastically sampled neighborhood during training. (default:
0)add_self_loops (bool, optional) – If set to
False, will not add self-loops to the input graph. (default:True)edge_dim (int, optional) – Edge feature dimensionality (in case there are any). (default:
None)fill_value (float 或 torch.Tensor 或 str, 可选) – 生成自环边特征的方式 (在
edge_dim != None的情况下)。 如果给定为float或torch.Tensor,自环的边特征将直接由fill_value给出。 如果给定为str,自环的边特征将通过聚合指向特定节点的所有边的特征来计算, 根据一个归约操作。("add","mean","min","max","mul")。(默认:"mean")bias (bool, optional) – If set to
False, the layer will not learn an additive bias. (default:True)share_weights (bool, 可选) – 如果设置为
True,相同的矩阵将应用于每条边的源节点和目标节点,即 \(\mathbf{\Theta}_{s} = \mathbf{\Theta}_{t}\)。 (默认:False)residual (bool, 可选) – 如果设置为
True,该层将添加一个可学习的跳跃连接。(默认值:False)**kwargs (optional) – Additional arguments of
torch_geometric.nn.conv.MessagePassing.
- Shapes:
input: node features \((|\mathcal{V}|, F_{in})\) or \(((|\mathcal{V_s}|, F_{s}), (|\mathcal{V_t}|, F_{t}))\) if bipartite, edge indices \((2, |\mathcal{E}|)\), edge features \((|\mathcal{E}|, D)\) (optional)
输出: 节点特征 \((|\mathcal{V}|, H * F_{out})\) 或 \(((|\mathcal{V}_t|, H * F_{out})\) 如果是二分图。 如果
return_attention_weights=True, 那么 \(((|\mathcal{V}|, H * F_{out}), ((2, |\mathcal{E}|), (|\mathcal{E}|, H)))\) 或 \(((|\mathcal{V_t}|, H * F_{out}), ((2, |\mathcal{E}|), (|\mathcal{E}|, H)))\) 如果是二分图
- forward(x: Union[Tensor, Tuple[Tensor, Tensor]], edge_index: Union[Tensor, SparseTensor], edge_attr: Optional[Tensor] = None, return_attention_weights: Optional[Tensor] = None) Tensor[source]
- forward(x: Union[Tensor, Tuple[Tensor, Tensor]], edge_index: Tensor, edge_attr: Optional[Tensor] = None, return_attention_weights: bool = None) Tuple[Tensor, Tuple[Tensor, Tensor]]
- forward(x: Union[Tensor, Tuple[Tensor, Tensor]], edge_index: SparseTensor, edge_attr: Optional[Tensor] = None, return_attention_weights: bool = None) Tuple[Tensor, SparseTensor]
运行模块的前向传播。
- Parameters:
x (torch.Tensor 或 (torch.Tensor, torch.Tensor)) – 输入的节点特征。
edge_index (torch.Tensor or SparseTensor) – The edge indices.
edge_attr (torch.Tensor, optional) – The edge features. (default:
None)return_attention_weights (bool, optional) – If set to
True, will additionally return the tuple(edge_index, attention_weights), holding the computed attention weights for each edge. (default:None)
- Return type:
Union[Tensor,Tuple[Tensor,Tuple[Tensor,Tensor]],Tuple[Tensor,SparseTensor]]