dgl.softmax_edges

dgl.softmax_edges(graph, feat)[source]

Apply batch-wise graph-level softmax over all the values of edge field feat in graph.

Parameters:
Returns:

The tensor obtained.

Return type:

tensor

Examples

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

Create two DGLGraph objects and initialize their edge features.

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

Softmax over edge attribute h in a batched graph.

>>> bg = dgl.batch([g1, g2], edge_attrs='h')
>>> dgl.softmax_edges(bg, 'h')
tensor([[0.2689, 0.5000], # [0.2689, 0.7311] = softmax([1., 2.])
        [0.7311, 0.5000], # [0.5000, 0.5000] = softmax([0., 0.])
        [0.0900, 0.3333], # [0.0900, 0.2447, 0.6652] = softmax([1., 2., 3.])
        [0.2447, 0.3333], # [0.3333, 0.3333, 0.3333] = softmax([0., 0., 0.])
        [0.6652, 0.3333]])

Softmax over edge attribute h in a single graph.

>>> dgl.softmax_edges(g1, 'h')
tensor([[0.2689, 0.5000],   # [0.2689, 0.7311] = softmax([1., 2.])
        [0.7311, 0.5000]]), # [0.5000, 0.5000] = softmax([0., 0.])

Notes

If graph is a BatchedDGLGraph object, the softmax is applied at each example in the batch.