dgl.DGLGraph.group_apply_edges

DGLGraph.group_apply_edges(group_by, func, edges='__ALL__', inplace=False)[source]
Group the edges by nodes and apply the function on the grouped edges to
update their features.
Parameters:
  • group_by (str) – Specify how to group edges. Expected to be either ‘src’ or ‘dst’
  • func (callable) – Apply function on the edge. The function should be an Edge UDF. The input of Edge UDF should be (bucket_size, degrees, *feature_shape), and return the dict with values of the same shapes.
  • edges (valid edges type, optional) – Edges on which to group and apply func. See send() for valid edges type. Default is all the edges.
  • inplace (bool, optional) – If True, update will be done in place, but autograd will break.

Notes

On multigraphs, if \(u\) and \(v\) are specified, then all the edges between \(u\) and \(v\) will be updated.

Examples

Note

Here we use pytorch syntax for demo. The general idea applies to other frameworks with minor syntax change (e.g. replace torch.tensor with mxnet.ndarray).

>>> import torch as th
>>> g = dgl.DGLGraph()
>>> g.add_nodes(4)
>>> g.add_edges(0, [1, 2, 3])
>>> g.add_edges(1, [2, 3])
>>> g.add_edges(2, [2, 3])
>>> g.edata['feat'] = th.randn((g.number_of_edges(), 1))
>>> # Softmax over the out edges of each node
>>> # Second dimension of edges.data is the degree dimension
>>> def softmax_feat(edges): return {'norm_feat': th.softmax(edges.data['feat'], dim=1)}
>>> g.group_apply_edges(func=softmax_feat, group_by='src') # Apply func to the first edge.
>>> u, v, eid = g.out_edges(1, form='all')
>>> in_feat = g.edata['feat'][eid]
>>> out_feat = g.edata['norm_feat'][eid]
>>> print(out_feat - th.softmax(in_feat, 0))
    tensor([[0.],
    [0.]])

See also

apply_edges()