DGLGraph.apply_edges(func='default', edges='__ALL__', inplace=False)[source]

Apply the function on the edges to update their features.

If None is provided for func, nothing will happen.

  • func (callable, optional) – Apply function on the edge. The function should be an Edge UDF.
  • edges (valid edges type, optional) – Edges on which to 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.


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



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(3)
>>> g.add_edges([0, 1], [1, 2])   # 0 -> 1, 1 -> 2
>>> g.edata['y'] = th.ones(2, 1)
>>> # Doubles the edge feature.
>>> def double_feature(edges): return {'y': edges.data['y'] * 2}
>>> g.apply_edges(func=double_feature, edges=0) # Apply func to the first edge.
>>> g.edata
{'y': tensor([[2.],   # 2 * 1

See also