RemoveSelfLoop

class dgl.transforms.RemoveSelfLoop[source]

Bases: dgl.transforms.module.BaseTransform

Remove self-loops for each node in the graph and return a new graph.

For heterogeneous graphs, this operation only applies to edge types with same source and destination node types.

Example

>>> import dgl
>>> from dgl import RemoveSelfLoop

Case1: Remove self-loops for a homogeneous graph

>>> transform = RemoveSelfLoop()
>>> g = dgl.graph(([1, 1], [1, 2]))
>>> new_g = transform(g)
>>> print(new_g.edges())
(tensor([1]), tensor([2]))

Case2: Remove self-loops for a heterogeneous graph

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): ([0, 1], [1, 1]),
...     ('user', 'follows', 'user'): ([1, 2], [2, 2])
... })
>>> new_g = transform(g)
>>> print(new_g.edges(etype='plays'))
(tensor([0, 1]), tensor([1, 1]))
>>> print(new_g.edges(etype='follows'))
(tensor([1]), tensor([2]))