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。结果张量与原始边特征具有相同的形状。
- Parameters:
graph (DGLGraph.) – The input graph.
feat (str) – The edge feature name.
etype (str or (str, str, str), optional) –
The type names of the edges. The allowed type name formats are:
(str, str, str)
for source node type, edge type and destination node type.or one
str
edge type name if the name can uniquely identify a triplet format in the graph.
Can be omitted if the graph has only one type of edges.
- Returns:
结果张量。
- Return type:
张量
示例
>>> import dgl >>> import torch as th
Create two
DGLGraph
objects and initialize their edge features.>>> 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])
另请参阅