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