dgl.remove_self_loop

dgl.remove_self_loop(g, etype=None)[source]

移除图中每个节点的自环并返回一个新图。

Parameters:
  • g (DGLGraph) – The graph.

  • 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.

注释

如果一个节点有多个自环,移除所有自环。对于没有自环的节点,不做任何操作。

此函数保留批次信息。

示例

>>> import dgl
>>> import torch

同构图

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

异构图

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0, 1, 1, 1, 2]),
...                                   torch.tensor([0, 0, 1, 1, 1])),
...     ('user', 'plays', 'game'): (torch.tensor([0, 1]),
...                                 torch.tensor([0, 1]))
...     })
>>> g = dgl.remove_self_loop(g, etype='follows')
>>> g.num_nodes('user')
3
>>> g.num_nodes('game')
2
>>> g.num_edges('follows')
2
>>> g.num_edges('plays')
2

另请参阅

add_self_loop