dgl.softmax_edges

dgl.softmax_edges(graph, feat, *, etype=None)[source]

对边的特征执行图级别的 Softmax 操作。

对于每条边 \(e\in\mathcal{E}\) 及其特征 \(x_e\),按如下方式计算其归一化特征

\[z_e = \frac{\exp(x_e)}{\sum_{e'\in\mathcal{E}}\exp(x_{e'})}\]

如果图是多个图的批次,则每个图独立计算 Softmax。结果张量与原始边的特征形状相同。

参数:
  • graph (DGLGraph.) – 输入图。

  • feat (str) – 边的特征名称。

  • etype (str(str, str, str), 可选) –

    边的类型名称。允许的类型名称格式有

    • (str, str, str) 分别表示源节点类型、边类型和目标节点类型。

    • 或者一个 str 边类型名称,如果该名称可以在图中唯一标识一个三元组格式。

    如果图只有一种边类型,则可以省略。

返回值:

结果张量。

返回类型:

张量

示例

>>> import dgl
>>> import torch as th

创建两个 DGLGraph 对象并初始化它们的边特征。

>>> g1 = dgl.graph(([0, 1], [1, 0]))              # Graph 1
>>> g1.edata['h'] = th.tensor([1., 1.])
>>> g2 = dgl.graph(([0, 1, 0], [1, 2, 2]))        # Graph 2
>>> g2.edata['h'] = th.tensor([1., 1., 1.])

在单个图上执行 Softmax

>>> dgl.softmax_edges(g1, 'h')
tensor([.5000, .5000])

在批处理图上执行 Softmax

>>> bg = dgl.batch([g1, g2])
>>> dgl.softmax_edges(bg, 'h')
tensor([.5000, .5000, .3333, .3333, .3333])

另请参阅

softmax_nodes