AddSelfLoop¶
-
class
dgl.transforms.
AddSelfLoop
(allow_duplicate=False, new_etypes=False)[source]¶ Bases:
dgl.transforms.module.BaseTransform
Add self-loops for each node in the graph and return a new graph.
For heterogeneous graphs, self-loops are added only for edge types with same source and destination node types.
- Parameters
Example
>>> import dgl >>> from dgl import AddSelfLoop
Case1: Add self-loops for a homogeneous graph
>>> transform = AddSelfLoop() >>> g = dgl.graph(([1, 1], [1, 2])) >>> new_g = transform(g) >>> print(new_g.edges()) (tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2]))
Case2: Add self-loops for a heterogeneous graph
>>> g = dgl.heterograph({ ... ('user', 'plays', 'game'): ([0], [1]), ... ('user', 'follows', 'user'): ([1], [2]) ... }) >>> new_g = transform(g) >>> print(new_g.edges(etype='plays')) (tensor([0]), tensor([1])) >>> print(new_g.edges(etype='follows')) (tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2]))
Case3: Add self-etypes for a heterogeneous graph
>>> transform = AddSelfLoop(new_etypes=True) >>> new_g = transform(g) >>> print(new_g.edges(etype='follows')) (tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2])) >>> print(new_g.edges(etype=('game', 'self', 'game'))) (tensor([0, 1]), tensor([0, 1]))