dgl.softmax_nodes
- dgl.softmax_nodes(graph, feat, *, ntype=None)[source]
对节点特征执行图级别的softmax。
对于每个节点 \(v\in\mathcal{V}\) 及其特征 \(x_v\),按如下方式计算其归一化特征:
\[z_v = \frac{\exp(x_v)}{\sum_{u\in\mathcal{V}}\exp(x_u)}\]如果图是一批多个图,每个图独立计算softmax。结果张量与原始节点特征具有相同的形状。
- Parameters:
- Returns:
结果张量。
- Return type:
张量
示例
>>> 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])
另请参阅