HGTConv
- class dgl.nn.pytorch.conv.HGTConv(in_size, head_size, num_heads, num_ntypes, num_etypes, dropout=0.2, use_norm=False)[source]
Bases:
Module
异构图变换器卷积来自 异构图变换器
给定一个图 \(G(V, E)\) 和输入节点特征 \(H^{(l-1)}\), 它计算新的节点特征如下:
为图中的每条边 \((s, e, t)\) 计算多头注意力分数:
\[\begin{split}Attention(s, e, t) = \text{Softmax}\left(||_{i\in[1,h]}ATT-head^i(s, e, t)\right) \\ ATT-head^i(s, e, t) = \left(K^i(s)W^{ATT}_{\phi(e)}Q^i(t)^{\top}\right)\cdot \frac{\mu_{(\tau(s),\phi(e),\tau(t)}}{\sqrt{d}} \\ K^i(s) = \text{K-Linear}^i_{\tau(s)}(H^{(l-1)}[s]) \\ Q^i(t) = \text{Q-Linear}^i_{\tau(t)}(H^{(l-1)}[t]) \\\end{split}\]计算要在每条边 \((s, e, t)\) 上发送的消息:
\[\begin{split}Message(s, e, t) = ||_{i\in[1, h]} MSG-head^i(s, e, t) \\ MSG-head^i(s, e, t) = \text{M-Linear}^i_{\tau(s)}(H^{(l-1)}[s])W^{MSG}_{\phi(e)} \\\end{split}\]向目标节点 \(t\) 发送消息并聚合:
\[\tilde{H}^{(l)}[t] = \sum_{\forall s\in \mathcal{N}(t)}\left( Attention(s,e,t) \cdot Message(s,e,t)\right)\]计算新的节点特征:
\[H^{(l)}[t]=\text{A-Linear}_{\tau(t)}(\sigma(\tilde(H)^{(l)}[t])) + H^{(l-1)}[t]\]- Parameters:
示例
- forward(g, x, ntype, etype, *, presorted=False)[source]
前向计算。
- Parameters:
g (DGLGraph) – The input graph.
x (torch.Tensor) – 一个2D张量的节点特征。形状:\((|V|, D_{in})\)。
ntype (torch.Tensor) – 一个1D整数张量,表示节点类型。形状:\((|V|,)\)。
etype (torch.Tensor) – 一个1D整数张量,表示边的类型。形状:\((|E|,)\)。
presorted (bool, optional) – 输入图的节点和边是否已按其类型排序。在预排序的图上进行前向传播可能会更快。由
to_homogeneous()
创建的图自动满足此条件。另请参阅reorder_graph()
以手动重新排序节点和边。
- Returns:
新节点特征。形状:\((|V|, D_{head} * N_{head})\)。
- Return type:
torch.Tensor