dgl.DGLGraph.num_nodesο
- DGLGraph.num_nodes(ntype=None)[source]ο
Return the number of nodes in the graph.
- Parameters:
ntype (str, optional) β The node type name. If given, it returns the number of nodes of the type. If not given (default), it returns the total number of nodes of all types.
- Returns:
The number of nodes.
- Return type:
Examples
The following example uses PyTorch backend.
>>> import dgl >>> import torch
Create a graph with two node types β βuserβ and βgameβ.
>>> g = dgl.heterograph({ ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])), ... ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6])) ... })
Query for the number of nodes.
>>> g.num_nodes('user') 5 >>> g.num_nodes('game') 7 >>> g.num_nodes() 12