dgl.DGLGraph.apply_nodesΒΆ

DGLGraph.apply_nodes(func, v='__ALL__', ntype=None)[source]ΒΆ

Update the features of the specified nodes by the provided function.

Parameters
  • func (callable) – The function to update node features. It must be a User-defined Functions.

  • v (node IDs) –

    The node IDs. The allowed formats are:

    • int: A single node.

    • Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.

    • iterable[int]: Each element is a node ID.

    If not given (default), use all the nodes in the graph.

  • ntype (str, optional) – The node type name. Can be omitted if there is only one type of nodes in the graph.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Homogeneous graph

>>> g = dgl.graph(([0, 1, 2, 3], [1, 2, 3, 4]))
>>> g.ndata['h'] = torch.ones(5, 2)
>>> g.apply_nodes(lambda nodes: {'x' : nodes.data['h'] * 2})
>>> g.ndata['x']
tensor([[2., 2.],
        [2., 2.],
        [2., 2.],
        [2., 2.],
        [2., 2.]])

Heterogeneous graph

>>> g = dgl.heterograph({('user', 'follows', 'user'): ([0, 1], [1, 2])})
>>> g.nodes['user'].data['h'] = torch.ones(3, 5)
>>> g.apply_nodes(lambda nodes: {'h': nodes.data['h'] * 2}, ntype='user')
>>> g.nodes['user'].data['h']
tensor([[2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.],
        [2., 2., 2., 2., 2.]])

See also

apply_edges