dgl.DGLGraph.in_edges

DGLGraph.in_edges(v, form='uv', etype=None)[source]

返回给定节点的传入边。

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

    节点 ID。允许的格式有:

    • int: 单个节点。

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

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

  • form (str, optional) –

    结果格式,可以是以下之一:

    • 'eid': 返回的结果是一个一维张量 \(EID\),表示所有边的ID。

    • 'uv' (默认): 返回的结果是一个由两个一维张量组成的元组 \((U, V)\),表示所有边的源节点和目标节点。对于每个 \(i\)\((U[i], V[i])\) 形成一条边。

    • 'all': 返回的结果是一个由三个一维张量组成的元组 \((U, V, EID)\),表示所有边的源节点、目标节点和ID。对于每个 \(i\)\((U[i], V[i])\) 形成一条边,其ID为 \(EID[i]\)

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

    The type names 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:

具有指定类型的节点的所有传入边。有关返回结果的描述,请参见form的描述。

Return type:

张量或(张量,张量)或(张量,张量,张量)

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

创建一个同构图。

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

查询节点1和0。

>>> g.in_edges(torch.tensor([1, 0]))
(tensor([0, 0]), tensor([1, 0]))

Specify a different value for form.

>>> g.in_edges(torch.tensor([1, 0]), form='all')
(tensor([0, 0]), tensor([1, 0]), tensor([0, 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_edges(torch.tensor([1, 0]), etype='follows')
(tensor([0]), tensor([1]))

另请参阅

edges, out_edges