dgl.DGLGraph.predecessors
- DGLGraph.predecessors(v, etype=None)[source]
返回具有指定边类型的特定节点的前驱节点。
节点
u
是节点v
的前驱节点,如果在图中存在一条类型为etype
的边(u, v)
。- Parameters:
v (int) – 节点ID。如果图有多个边类型,ID对应于边类型的目标类型。
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:
v
的前驱节点,具有指定的边类型。- Return type:
张量
示例
以下示例使用PyTorch后端。
>>> import dgl >>> import torch
创建一个同构图。
>>> g = dgl.graph((torch.tensor([0, 0, 1, 1]), torch.tensor([1, 1, 2, 3])))
查询节点1。
>>> g.predecessors(1) tensor([0, 0])
对于具有多种边类型的图,需要在查询中指定边类型。
>>> 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.predecessors(1, etype='follows') tensor([0])
另请参阅