dgl.DGLGraph.apply_nodes

DGLGraph.apply_nodes(func='default', v='__ALL__', inplace=False)[source]

Apply the function on the nodes to update their features.

If None is provided for func, nothing will happen.

Parameters:
  • func (callable or None, optional) – Apply function on the nodes. The function should be a Node UDF.
  • v (int, iterable of int, tensor, optional) – The node (ids) on which to apply func. The default value is all the nodes.
  • inplace (bool, optional) – If True, update will be done in place, but autograd will break.

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(3)
>>> g.ndata['x'] = th.ones(3, 1)
>>> # Increment the node feature by 1.
>>> def increment_feature(nodes): return {'x': nodes.data['x'] + 1}
>>> g.apply_nodes(func=increment_feature, v=[0, 2]) # Apply func to nodes 0, 2
>>> g.ndata
{'x': tensor([[2.],
              [1.],
              [2.]])}