AddSelfLoopΒΆ

class dgl.transforms.AddSelfLoop(allow_duplicate=False, new_etypes=False, edge_feat_names=None, fill_data=1.0)[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
  • allow_duplicate (bool, optional) – If False, it will first remove self-loops to prevent duplicate self-loops.

  • new_etypes (bool, optional) – If True, it will add an edge type β€˜self’ per node type, which holds self-loops.

  • edge_feat_names (list[str], optional) – The names of the self-loop features to apply fill_data. If None, it will apply fill_data to all self-loop features. Default: None.

  • fill_data (int, float or str, optional) –

    The value to fill the self-loop features. Default: 1.

    • If fill_data is int or float, self-loop features will be directly given by fill_data.

    • if fill_data is str, self-loop features will be generated by aggregating the features of the incoming edges of the corresponding nodes. The supported aggregation are: 'mean', 'sum', 'max', 'min'.

Example

>>> import dgl
>>> from dgl import AddSelfLoop

Case1: Add self-loops for a homogeneous graph

>>> transform = AddSelfLoop(fill_data='sum')
>>> g = dgl.graph(([0, 0, 2], [2, 1, 0]))
>>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1)
>>> new_g = transform(g)
>>> print(new_g.edges())
(tensor([0, 0, 2, 0, 1, 2]), tensor([2, 1, 0, 0, 1, 2]))
>>> print(new_g.edata('he'))
tensor([[0.],
        [1.],
        [2.],
        [2.],
        [1.],
        [0.]])

Case2: Add self-loops for a heterogeneous graph

>>> transform = AddSelfLoop(fill_data='sum')
>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([1, 2]),
...                                   torch.tensor([0, 1])),
...     ('user', 'plays', 'game'): (torch.tensor([0, 1]),
...                                 torch.tensor([0, 1]))})
>>> g.edata['feat'] = {('user', 'follows', 'user'): torch.randn(2, 5),
...                    ('user', 'plays', 'game'): torch.randn(2, 5)}
>>> g.edata['feat1'] = {('user', 'follows', 'user'): torch.randn(2, 15),
...                     ('user', 'plays', 'game'): torch.randn(2, 15)}
>>> new_g = transform(g)
>>> print(new_g.edges(etype='plays'))
(tensor([0, 1]), tensor([0, 1]))
>>> print(new_g.edges(etype='follows'))
(tensor([1, 2, 0, 1, 2]), tensor([0, 1, 0, 1, 2]))
>>> print(new_g.edata['feat'][('user', 'follows', 'user')].shape)
torch.Size([5, 5])

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, 2, 0, 1, 2]), tensor([0, 1, 0, 1, 2]))
>>> print(new_g.edges(etype=('game', 'self', 'game')))
(tensor([0, 1]), tensor([0, 1]))