dgl.DGLGraph.dstnodesΒΆ

property DGLGraph.dstnodesΒΆ

Return a node view for destination nodes

If the graph is a uni-bipartite graph (see is_unibipartite() for reference), this is nodes() restricted to destination node types. Otherwise, it is an alias for nodes().

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.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a uni-bipartite graph.

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([0]), torch.tensor([1])),
...     ('developer', 'develops', 'game'): (torch.tensor([1]), torch.tensor([2]))
... })

Get the node IDs for destination node types.

>>> g.dstnodes('game')
tensor([0, 1, 2])

Set/get features for destination node types.

>>> g.dstnodes['game'].data['h'] = torch.ones(3, 1)
>>> g.dstnodes['game'].data['h']
tensor([[1.],
        [1.],
        [1.]])

Create a graph that is not uni-bipartite.

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0]), torch.tensor([1])),
...     ('developer', 'develops', 'game'): (torch.tensor([1]), torch.tensor([2]))
... })

dgl.DGLGraph.dstnodes() falls back to dgl.DGLGraph.nodes() and one can get the node IDs for both source and destination node types.

>>> g.dstnodes('developer')
tensor([0, 1])

One can also set/get features for source node types in this case.

>>> g.dstnodes['developer'].data['h'] = torch.ones(2, 1)
>>> g.dstnodes['developer'].data['h']
tensor([[1.],
        [1.]])

See also

dstdata