appnp.py 1.51 KB
Newer Older
Aymen Waheb's avatar
Aymen Waheb committed
1
2
3
4
5
6
7
8
"""
APPNP implementation in DGL.
References
----------
Paper: https://arxiv.org/abs/1810.05997
Author's code: https://github.com/klicperajo/ppnp
"""
import torch.nn as nn
9

10
from dgl.nn.pytorch.conv import APPNPConv
11
12


Aymen Waheb's avatar
Aymen Waheb committed
13
class APPNP(nn.Module):
14
15
16
17
18
19
20
21
22
23
24
25
    def __init__(
        self,
        g,
        in_feats,
        hiddens,
        n_classes,
        activation,
        feat_drop,
        edge_drop,
        alpha,
        k,
    ):
Aymen Waheb's avatar
Aymen Waheb committed
26
        super(APPNP, self).__init__()
27
        self.g = g
Aymen Waheb's avatar
Aymen Waheb committed
28
29
30
31
32
33
34
35
36
        self.layers = nn.ModuleList()
        # input layer
        self.layers.append(nn.Linear(in_feats, hiddens[0]))
        # hidden layers
        for i in range(1, len(hiddens)):
            self.layers.append(nn.Linear(hiddens[i - 1], hiddens[i]))
        # output layer
        self.layers.append(nn.Linear(hiddens[-1], n_classes))
        self.activation = activation
37
38
        if feat_drop:
            self.feat_drop = nn.Dropout(feat_drop)
Aymen Waheb's avatar
Aymen Waheb committed
39
        else:
40
            self.feat_drop = lambda x: x
41
        self.propagate = APPNPConv(k, alpha, edge_drop)
42
        self.reset_parameters()
Aymen Waheb's avatar
Aymen Waheb committed
43
44
45
46
47
48
49
50

    def reset_parameters(self):
        for layer in self.layers:
            layer.reset_parameters()

    def forward(self, features):
        # prediction step
        h = features
51
        h = self.feat_drop(h)
Aymen Waheb's avatar
Aymen Waheb committed
52
53
54
        h = self.activation(self.layers[0](h))
        for layer in self.layers[1:-1]:
            h = self.activation(layer(h))
55
56
        h = self.layers[-1](self.feat_drop(h))
        # propagation step
57
        h = self.propagate(self.g, h)
Aymen Waheb's avatar
Aymen Waheb committed
58
        return h