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。结果张量与原始边的特征形状相同。
- 参数:
- 返回值:
结果张量。
- 返回类型:
张量
示例
>>> 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])
另请参阅