property DGLGraph.nodes

Return a node view

One can use it for:

  1. Getting the node IDs for a single node type.

  2. Setting/getting features for all nodes of a single node type.


The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a homogeneous graph and a heterogeneous graph of two node types.

>>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2])))
>>> hg = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
...     ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6]))
... })

Get the node IDs of the homogeneous graph.

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

Get the node IDs of the heterogeneous graph. With multiple node types introduced, one needs to specify the node type for query.

>>> hg.nodes('user')
tensor([0, 1, 2, 3, 4])

Set and get a feature β€˜h’ for all nodes of a single type in the heterogeneous graph.

>>> hg.nodes['user'].data['h'] = torch.ones(5, 1)
>>> hg.nodes['user'].data['h']
tensor([[1.], [1.], [1.], [1.], [1.]])

To set node features for a graph with a single node type, use DGLGraph.ndata().

See also