dgl.DGLGraph.has_nodes

DGLGraph.has_nodes(vid, ntype=None)[source]

返回图表是否包含给定的节点。

Parameters:
  • vid (节点 ID(s)) –

    节点 ID。允许的节点 ID 格式有:

    • int: 单个节点的 ID。

    • Int Tensor: 每个元素都是一个节点 ID。张量必须与图的设备类型和 ID 数据类型相同。

    • iterable[int]: 每个元素都是一个节点 ID。

  • ntype (str, optional) – The node type name. Can be omitted if there is only one type of nodes in the graph.

Returns:

一个布尔标志的张量,如果节点在图中,则每个元素为True。 如果输入是单个节点,则返回一个布尔值。

Return type:

bool or bool Tensor

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

创建一个包含两种节点类型的图——‘用户’和‘游戏’。

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

查询节点。

>>> g.has_nodes(0, 'user')
True
>>> g.has_nodes(3, 'game')
False
>>> g.has_nodes(torch.tensor([3, 0, 1]), 'game')
tensor([False,  True,  True])