Unverified Commit a0512717 authored by Hongzhi (Steve), Chen's avatar Hongzhi (Steve), Chen Committed by GitHub
Browse files

[Misc] Fix indent for AddSelfLoop. (#5843)


Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-28-63.ap-northeast-1.compute.internal>
parent 15419685
...@@ -510,53 +510,53 @@ class LaplacianPE(LapPE): ...@@ -510,53 +510,53 @@ class LaplacianPE(LapPE):
class AddSelfLoop(BaseTransform): class AddSelfLoop(BaseTransform):
r"""Add self-loops for each node in the graph and return a new graph. r"""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 For heterogeneous graphs, self-loops are added only for edge types with same
source and destination node types. source and destination node types.
Parameters Parameters
---------- ----------
allow_duplicate : bool, optional allow_duplicate : bool, optional
If False, it will first remove self-loops to prevent duplicate self-loops. If False, it will first remove self-loops to prevent duplicate self-loops.
new_etypes : bool, optional new_etypes : bool, optional
If True, it will add an edge type 'self' per node type, which holds self-loops. If True, it will add an edge type 'self' per node type, which holds self-loops.
edge_feat_names : list[str], optional edge_feat_names : list[str], optional
The names of the self-loop features to apply `fill_data`. If None, it 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. will apply `fill_data` to all self-loop features. Default: None.
fill_data : int, float or str, optional fill_data : int, float or str, optional
The value to fill the self-loop features. Default: 1. 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 * If ``fill_data`` is ``int`` or ``float``, self-loop features will be directly given by
``fill_data``. ``fill_data``.
* if ``fill_data`` is ``str``, self-loop features will be generated by aggregating the * 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: features of the incoming edges of the corresponding nodes. The supported aggregation are:
``'mean'``, ``'sum'``, ``'max'``, ``'min'``. ``'mean'``, ``'sum'``, ``'max'``, ``'min'``.
Example Example
------- -------
>>> import dgl >>> import dgl
>>> from dgl import AddSelfLoop >>> from dgl import AddSelfLoop
Case1: Add self-loops for a homogeneous graph Case1: Add self-loops for a homogeneous graph
>>> transform = AddSelfLoop(fill_data='sum') >>> transform = AddSelfLoop(fill_data='sum')
>>> g = dgl.graph(([0, 0, 2], [2, 1, 0])) >>> g = dgl.graph(([0, 0, 2], [2, 1, 0]))
>>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1) >>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1)
>>> new_g = transform(g) >>> new_g = transform(g)
>>> print(new_g.edges()) >>> print(new_g.edges())
(tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2])) (tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2]))
>>> print(new_g.edata('he')) >>> print(new_g.edata('he'))
tensor([[0.], tensor([[0.],
[1.], [1.],
[2.], [2.],
[2.], [2.],
[1.], [1.],
[0.]]) [0.]])
Case2: Add self-loops for a heterogeneous graph Case2: Add self-loops for a heterogeneous graph
>>> transform = AddSelfLoop(fill_data='sum') >>> transform = AddSelfLoop(fill_data='sum')
>>> g = dgl.heterograph({ >>> g = dgl.heterograph({
... ('user', 'follows', 'user'): (torch.tensor([1, 2]), ... ('user', 'follows', 'user'): (torch.tensor([1, 2]),
... torch.tensor([0, 1])), ... torch.tensor([0, 1])),
... ('user', 'plays', 'game'): (torch.tensor([0, 1]), ... ('user', 'plays', 'game'): (torch.tensor([0, 1]),
...@@ -564,23 +564,23 @@ class AddSelfLoop(BaseTransform): ...@@ -564,23 +564,23 @@ class AddSelfLoop(BaseTransform):
>>> g.edata['feat'] = {('user', 'follows', 'user'): torch.randn(2, 5), >>> g.edata['feat'] = {('user', 'follows', 'user'): torch.randn(2, 5),
... ('user', 'plays', 'game'): torch.randn(2, 5)} ... ('user', 'plays', 'game'): torch.randn(2, 5)}
>>> g.edata['feat1'] = {('user', 'follows', 'user'): torch.randn(2, 15), >>> g.edata['feat1'] = {('user', 'follows', 'user'): torch.randn(2, 15),
... ('user', 'plays', 'game'): torch.randn(2, 15)} ... ('user', 'plays', 'game'): torch.randn(2, 15)}
>>> new_g = transform(g) >>> new_g = transform(g)
>>> print(new_g.edges(etype='plays')) >>> print(new_g.edges(etype='plays'))
(tensor([0, 1]), tensor([0, 1])) (tensor([0, 1]), tensor([0, 1]))
>>> print(new_g.edges(etype='follows')) >>> print(new_g.edges(etype='follows'))
(tensor([1, 2]), tensor([0, 1])) (tensor([1, 2]), tensor([0, 1]))
>>> print(new_g.edata['feat'][('user', 'follows', 'user')].shape) >>> print(new_g.edata['feat'][('user', 'follows', 'user')].shape)
torch.Size([5, 5]) torch.Size([5, 5])
Case3: Add self-etypes for a heterogeneous graph Case3: Add self-etypes for a heterogeneous graph
>>> transform = AddSelfLoop(new_etypes=True) >>> transform = AddSelfLoop(new_etypes=True)
>>> new_g = transform(g) >>> new_g = transform(g)
>>> print(new_g.edges(etype='follows')) >>> print(new_g.edges(etype='follows'))
(tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2])) (tensor([1, 0, 1, 2]), tensor([2, 0, 1, 2]))
>>> print(new_g.edges(etype=('game', 'self', 'game'))) >>> print(new_g.edges(etype=('game', 'self', 'game')))
(tensor([0, 1]), tensor([0, 1])) (tensor([0, 1]), tensor([0, 1]))
""" """
def __init__( def __init__(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment