dgl.DGLGraph.edge_ids
- DGLGraph.edge_ids(u, v, return_uv=False, etype=None)[source]
返回给定边的两个端点的边ID。
- Parameters:
u (node IDs) –
The source node IDs of the edges. The allowed formats are:
int
: A single node.Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.
iterable[int]: Each element is a node ID.
v (node IDs) –
The destination node IDs of the edges. The allowed formats are:
int
: A single node.Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.
iterable[int]: Each element is a node ID.
return_uv (bool, optional) – 是否返回源节点和目标节点ID以及边。如果为False(默认值),则假设图是一个简单图,并且从一个节点到另一个节点只有一条边。如果为True,则可能找到从一个节点到另一个节点的多条边。
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:
如果
return_uv=False
,它返回一个张量中的边ID,其中第i个元素是边(u[i], v[i])
的ID。如果
return_uv=True
,它返回一个包含三个一维张量的元组(eu, ev, e)
。e[i]
是从eu[i]
到ev[i]
的边的ID。在这种情况下,它返回从eu[i]
到ev[i]
的所有边(包括平行边)。
- Return type:
张量,或(张量,张量,张量)
注释
如果图是一个简单图,
return_uv=False
,并且某些节点对之间没有边,它将引发错误。如果图是一个多重图,
return_uv=False
,并且在某些节点对之间存在多条边,它将从中返回任意一条。示例
以下示例使用PyTorch后端。
>>> import dgl >>> import torch
创建一个同构图。
>>> g = dgl.graph((torch.tensor([0, 0, 1, 1, 1]), torch.tensor([1, 0, 2, 3, 2])))
查询边的请求。
>>> g.edge_ids(0, 0) 1 >>> g.edge_ids(torch.tensor([1, 0]), torch.tensor([3, 1])) tensor([3, 0])
获取所有节点对的边。
>>> g.edge_ids(torch.tensor([1, 0]), torch.tensor([3, 1]), return_uv=True) (tensor([1, 0]), tensor([3, 1]), tensor([3, 0]))
如果图具有多种边类型,则需要指定边类型。
>>> g = dgl.heterograph({ ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])), ... ('user', 'follows', 'game'): (torch.tensor([0, 1, 2]), torch.tensor([1, 2, 3])), ... ('user', 'plays', 'game'): (torch.tensor([1, 3]), torch.tensor([2, 3])) ... }) >>> g.edge_ids(torch.tensor([1]), torch.tensor([2]), etype='plays') tensor([0])
当边类型存在歧义时,请使用规范的边类型。
>>> g.edge_ids(torch.tensor([0, 1]), torch.tensor([1, 2]), ... etype=('user', 'follows', 'user')) tensor([0, 1]) >>> g.edge_ids(torch.tensor([1, 2]), torch.tensor([2, 3]), ... etype=('user', 'follows', 'game')) tensor([1, 2])