dgl.softmax_edges

dgl.softmax_edges(graph, feat, *, etype=None)[source]

Perform graph-wise softmax on the edge features.

For each edge \(e\in\mathcal{E}\) and its feature \(x_e\), calculate its normalized feature as follows:

\[z_e = \frac{\exp(x_e)}{\sum_{e'\in\mathcal{E}}\exp(x_{e'})}\]

If the graph is a batch of multiple graphs, each graph computes softmax independently. The result tensor has the same shape as the original edge feature.

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

Result tensor.

Return type

Tensor

Examples

>>> 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 over one graph:

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

Softmax over a batched graph:

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

See also

softmax_nodes()