dgl.softmax_nodes

dgl.softmax_nodes(graph, feat, *, ntype=None)[source]

对节点特征执行图范围的 softmax 操作。

对于每个节点 vV 及其特征 xv,按如下方式计算其归一化特征

zv=exp(xv)uVexp(xu)

如果图是多个图组成的批次,则每个图独立计算 softmax。结果张量与原始节点特征具有相同的形状。

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

  • feat (str) – 节点特征名称。

  • ntype (str, optional) – 节点类型名称。如果图中只有一个节点类型,可以省略此参数。

返回值

结果张量。

返回值类型

张量

示例

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

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

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

在一个图上执行 Softmax

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

在一个批量图上执行 Softmax

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

另请参阅

softmax_edges