dgl.DGLGraph.batch_num_nodes

DGLGraph.batch_num_nodes(ntype=None)[source]

Return the number of nodes for each graph in the batch with the specified node type.

Parameters:

ntype (str, optional) – The node type for query. If the graph has multiple node types, one must specify the argument. Otherwise, it can be omitted. If the graph is not a batched one, it will return a list of length 1 that holds the number of nodes in the graph.

Returns:

The number of nodes with the specified type for each graph in the batch. The i-th element of it is the number of nodes with the specified type for the i-th graph.

Return type:

Tensor

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Query for homogeneous graphs.

>>> g1 = dgl.graph((torch.tensor([0, 1, 2]), torch.tensor([1, 2, 3])))
>>> g1.batch_num_nodes()
tensor([4])
>>> g2 = dgl.graph((torch.tensor([0, 0, 0, 1]), torch.tensor([0, 1, 2, 0])))
>>> bg = dgl.batch([g1, g2])
>>> bg.batch_num_nodes()
tensor([4, 3])

Query for heterogeneous graphs.

>>> hg1 = dgl.heterograph({
...       ('user', 'plays', 'game') : (torch.tensor([0, 1]), torch.tensor([0, 0]))})
>>> hg2 = dgl.heterograph({
...       ('user', 'plays', 'game') : (torch.tensor([0, 0]), torch.tensor([1, 0]))})
>>> bg = dgl.batch([hg1, hg2])
>>> bg.batch_num_nodes('user')
tensor([2, 1])