dgl.DGLGraph.out_edges
- DGLGraph.out_edges(u, form='uv', etype=None)[source]
返回给定节点的出边。
- Parameters:
u (节点 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和2。
>>> g.out_edges(torch.tensor([1, 2])) (tensor([1, 1]), tensor([2, 3]))
为
form
指定一个不同的值。>>> g.out_edges(torch.tensor([1, 2]), form='all') (tensor([1, 1]), tensor([2, 3]), tensor([2, 3]))
对于具有多种边类型的图,需要在查询中指定边类型。
>>> 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.out_edges(torch.tensor([1, 2]), etype='follows') (tensor([1]), tensor([2]))