model.py 1.47 KB
Newer Older
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1
import dgl.function as fn
xnouhz's avatar
xnouhz committed
2
3
4
import torch
import torch.nn as nn
import torch.nn.functional as F
Mufei Li's avatar
Mufei Li committed
5
from dgl.nn import GraphConv, JumpingKnowledge
xnouhz's avatar
xnouhz committed
6

7

xnouhz's avatar
xnouhz committed
8
class JKNet(nn.Module):
9
10
11
    def __init__(
        self, in_dim, hid_dim, out_dim, num_layers=1, mode="cat", dropout=0.0
    ):
xnouhz's avatar
xnouhz committed
12
        super(JKNet, self).__init__()
Mufei Li's avatar
Mufei Li committed
13

xnouhz's avatar
xnouhz committed
14
15
16
17
18
19
20
        self.mode = mode
        self.dropout = nn.Dropout(dropout)
        self.layers = nn.ModuleList()
        self.layers.append(GraphConv(in_dim, hid_dim, activation=F.relu))
        for _ in range(num_layers):
            self.layers.append(GraphConv(hid_dim, hid_dim, activation=F.relu))

21
        if self.mode == "lstm":
Mufei Li's avatar
Mufei Li committed
22
23
24
25
            self.jump = JumpingKnowledge(mode, hid_dim, num_layers)
        else:
            self.jump = JumpingKnowledge(mode)

26
        if self.mode == "cat":
xnouhz's avatar
xnouhz committed
27
28
29
30
31
32
33
34
35
            hid_dim = hid_dim * (num_layers + 1)

        self.output = nn.Linear(hid_dim, out_dim)
        self.reset_params()

    def reset_params(self):
        self.output.reset_parameters()
        for layers in self.layers:
            layers.reset_parameters()
Mufei Li's avatar
Mufei Li committed
36
        self.jump.reset_parameters()
xnouhz's avatar
xnouhz committed
37
38
39
40
41
42

    def forward(self, g, feats):
        feat_lst = []
        for layer in self.layers:
            feats = self.dropout(layer(g, feats))
            feat_lst.append(feats)
Mufei Li's avatar
Mufei Li committed
43

44
45
46
        if self.mode == "lstm":
            self.jump.lstm.flatten_parameters()

47
48
        g.ndata["h"] = self.jump(feat_lst)
        g.update_all(fn.copy_u("h", "m"), fn.sum("m", "h"))
xnouhz's avatar
xnouhz committed
49

50
        return self.output(g.ndata["h"])