dgl.DGLGraph.remove_edges

DGLGraph.remove_edges(eids, etype=None, store_ids=False)[source]

移除具有指定边类型的多条边

节点不会被移除。移除边后,剩余的边将使用从 0 开始的连续整数重新索引,并保留其相对顺序。

相应地,被移除边的特征也将被移除。

参数:
  • eids (int, tensor, numpy.ndarray, list) – 要移除边的 ID。

  • etype (strtuple of str, 可选) – 要移除边的类型。如果图中只有一种边类型,则可以省略。

  • store_ids (bool, 可选) – 如果为 True,则会在结果图的 ndataedata 中分别以名称 dgl.NIDdgl.EID 存储提取的节点和边的原始 ID。

说明

此函数会保留批量信息。

示例

>>> import dgl
>>> import torch

同构图或具有单一边类型的异构图

>>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
>>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1)
>>> g.remove_edges(torch.tensor([0, 1]))
>>> g
Graph(num_nodes=3, num_edges=1,
    ndata_schemes={}
    edata_schemes={'he': Scheme(shape=(1,), dtype=torch.float32)})
>>> g.edges('all')
(tensor([2]), tensor([2]), tensor([0]))
>>> g.edata['he']
tensor([[2.]])

从批量图中移除边会保留批量信息。

>>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
>>> g2 = dgl.graph((torch.tensor([1, 2, 3]), torch.tensor([1, 3, 4])))
>>> bg = dgl.batch([g, g2])
>>> bg.batch_num_edges()
tensor([3, 3])
>>> bg.remove_edges([1, 4])
>>> bg.batch_num_edges()
tensor([2, 2])

具有多种边类型的异构图

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]),
...                                 torch.tensor([0, 0, 1, 1])),
...     ('developer', 'develops', 'game'): (torch.tensor([0, 1]),
...                                         torch.tensor([0, 1]))
...     })
>>> g.remove_edges(torch.tensor([0, 1]))
DGLError: Edge type name must be specified
if there are more than one edge types.
>>> g.remove_edges(torch.tensor([0, 1]), 'plays')
>>> g.edges('all', etype='plays')
(tensor([0, 1]), tensor([0, 0]), tensor([0, 1]))