dgl.DGLGraph.num_src_nodes

DGLGraph.num_src_nodes(ntype=None)[source]

返回图中源节点的数量。

If the graph can further divide its node types into two subsets A and B where all the edeges are from nodes of types in A to nodes of types in B, we call this graph a uni-bipartite graph and the nodes in A being the source nodes and the ones in B being the destination nodes. If the graph is not uni-bipartite, the source and destination nodes are just the entire set of nodes in the graph.

Parameters:

ntype (str, 可选) – 源节点类型名称。如果提供,则返回该源节点类型的节点数量。如果未提供(默认),则返回所有源节点类型的节点总数。

Returns:

节点数量

Return type:

int

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

为查询创建一个同构图。

>>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2])))
>>> g.num_src_nodes()
3

创建一个包含两种源节点类型的异构图——‘developer’和‘user’。

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

查询节点数量。

>>> g.num_src_nodes('developer')
2
>>> g.num_src_nodes('user')
5
>>> g.num_src_nodes()
7