dgl.remove_nodes

dgl.remove_nodes(g, nids, ntype=None, store_ids=False)[source]

移除指定的节点并返回一个新图。

同时删除特征。连接这些节点的边也将被移除。移除后,DGL会重新标记剩余的节点和边,ID从0开始。

Parameters:
  • nids (int, Tensor, iterable[int]) – 要移除的节点。

  • ntype (str, optional) – 要移除的节点类型。如果图中只有一种节点类型,则可以省略。

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

Returns:

删除节点后的图形。

Return type:

DGLGraph

注释

This function discards the batch information. Please use dgl.DGLGraph.set_batch_num_nodes() and dgl.DGLGraph.set_batch_num_edges() on the transformed graph to maintain the information.

示例

>>> import dgl
>>> import torch

同构图

>>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
>>> g.ndata['hv'] = torch.arange(3).float().reshape(-1, 1)
>>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1)
>>> g = dgl.remove_nodes(g, torch.tensor([0, 1]))
>>> g
Graph(num_nodes=1, num_edges=1,
    ndata_schemes={'hv': Scheme(shape=(1,), dtype=torch.float32)}
    edata_schemes={'he': Scheme(shape=(1,), dtype=torch.float32)})
>>> g.ndata['hv']
tensor([[2.]])
>>> g.edata['he']
tensor([[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 = dgl.remove_nodes(g, torch.tensor([0, 1]), ntype='game')
>>> g.num_nodes('user')
3
>>> g.num_nodes('game')
0
>>> g.num_edges('plays')
0