dgl.DGLGraph.srcnodes¶
-
property
DGLGraph.
srcnodes
¶ Return a node view for source nodes
If the graph is a uni-bipartite graph (see
is_unibipartite()
for reference), this isnodes()
restricted to source node types. Otherwise, it is an alias fornodes()
.One can use it for:
Getting the node IDs for a single node type.
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 source node types.
>>> g.srcnodes('user') tensor([0]) >>> g.srcnodes('developer') tensor([0, 1])
Set/get features for source node types.
>>> g.srcnodes['user'].data['h'] = torch.ones(1, 1) >>> g.srcnodes['user'].data['h'] tensor([[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.srcnodes()
falls back todgl.DGLGraph.nodes()
and one can get the node IDs for both source and destination node types.>>> g.srcnodes('game') tensor([0, 1, 2])
One can also set/get features for destination node types in this case.
>>> g.srcnodes['game'].data['h'] = torch.ones(3, 1) >>> g.srcnodes['game'].data['h'] tensor([[1.], [1.], [1.]])
See also