dgl.DGLGraph.nodes

DGLGraph.nodes

Return a node view that can used to set/get feature data.

Examples

>>> G = dgl.DGLGraph()
>>> G.add_nodes(3)

Get nodes in graph G:

>>> G.nodes()
tensor([0, 1, 2])

Get feature dictionary of all nodes:

>>> G.nodes[:].data
{}

The above can be abbreviated as

>>> G.ndata
{}

Init all 3 nodes with zero vector(len=5)

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.ndata['x'] = th.zeros((3, 5))
>>> G.ndata['x']
{'x' : tensor([[0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.]])}

Use G.nodes to get/set features for some nodes.

>>> G.nodes[[0, 2]].data['x'] = th.ones((2, 5))
>>> G.ndata
{'x' : tensor([[1., 1., 1., 1., 1.],
               [0., 0., 0., 0., 0.],
               [1., 1., 1., 1., 1.]])}