dgl.DGLGraph.remove_edges

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

移除指定边类型的多条边

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

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

Parameters:
  • eids (int, tensor, numpy.ndarray, list) – 要删除的边的ID。

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

  • store_ids (bool, optional) – If True, it will store the raw IDs of the extracted nodes and edges in the ndata and edata of the resulting graph under name dgl.NID and dgl.EID, respectively.

注释

此函数保留批次信息。

示例

>>> 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]))