dgl.DGLGraph.in_degrees

DGLGraph.in_degrees(v='__ALL__', etype=None)[source]

返回给定节点的入度。

它计算给定边类型的边的入度。

Parameters:
  • v (节点ID) –

    节点ID。允许的格式有:

    • int: 单个节点。

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

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

    如果未给出,则返回所有节点的入度。

  • etype (str or (str, str, str), optional) –

    The type name of the edges. The allowed type name formats are:

    • (str, str, str) for source node type, edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns:

节点在Tensor中的入度。第i个元素是第i个输入节点的入度。如果v是一个int,则也返回一个int

Return type:

int or Tensor

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

创建一个同构图。

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

查询所有节点。

>>> g.in_degrees()
tensor([0, 2, 1, 1])

查询节点1和2。

>>> g.in_degrees(torch.tensor([1, 2]))
tensor([2, 1])

对于具有多种边类型的图,需要在查询中指定边类型。

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

另请参阅

out_degrees