dgl.DGLGraph.batch_num_edges

DGLGraph.batch_num_edges(etype=None)

Return the number of edges for each graph in the batch with the specified edge type.

Parameters

etype (str or tuple of str, optional) – The edge type for query, which can be an edge type (str) or a canonical edge type (3-tuple of str). When an edge type appears in multiple canonical edge types, one must use a canonical edge type. If the graph has multiple edge types, one must specify the argument. Otherwise, it can be omitted.

Returns

The number of edges with the specified type for each graph in the batch. The i-th element of it is the number of edges with the specified type for the i-th graph. If the graph is not a batched one, it will return a list of length 1 that holds the number of edges in the 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_edges()
tensor([3])
>>> g2 = dgl.graph((torch.tensor([0, 0, 0, 1]), torch.tensor([0, 1, 2, 0])))
>>> bg = dgl.batch([g1, g2])
>>> bg.batch_num_edges()
tensor([3, 4])

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_edges('plays')
tensor([2, 2])