-
Notifications
You must be signed in to change notification settings - Fork 2
/
dgl_model.py
121 lines (111 loc) · 3.88 KB
/
dgl_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#*************************************************************************
# > Filename : dgl_model.py
# > Description : dgl baseline model
#*************************************************************************
import torch.nn as nn
from dgl.nn.pytorch import GraphConv,GINConv,GATConv
# import fastgraph
from common_config import *
from utilis import *
class GCN(nn.Module):
def __init__(self,
in_feats,
n_hidden,
n_classes,
n_layers,
activation,
dropout):
super(GCN, self).__init__()
self.layers = nn.ModuleList()
# input layer
self.layers.append(
GraphConv(in_feats, n_hidden, activation=activation, allow_zero_in_degree=True))
# hidden layers
for _ in range(1, n_layers - 1):
self.layers.append(
GraphConv(n_hidden, n_hidden, activation=activation, allow_zero_in_degree=True))
# output layer
self.layers.append(
GraphConv(n_hidden, n_classes, allow_zero_in_degree=True))
self.dropout = nn.Dropout(p=dropout)
def forward(self, blocks, features):
h = features
for i, layer in enumerate(self.layers):
if i != 0:
h = self.dropout(h)
h = layer(blocks[i], h)
return h
class GAT(nn.Module):
def __init__(self,
in_feats,
n_hidden,
n_classes,
n_layers,
activation,
dropout,
heads=8):
super(GAT, self).__init__()
self.layers = nn.ModuleList()
# input layer
self.layers.append(GATConv(in_feats,n_hidden,num_heads=heads,allow_zero_in_degree=True,bias=False))
# hidden layers
for _ in range(1, n_layers - 1):
self.layers.append(GATConv(n_hidden*heads,n_hidden,num_heads=heads,feat_drop=0,allow_zero_in_degree=True,bias=False))
# output layer
self.layers.append(GATConv(n_hidden*heads,n_classes,num_heads=1,feat_drop=0,allow_zero_in_degree=True,bias=False))
self.dropout = nn.Dropout(p=dropout)
def forward(self, blocks, features):
h = features
for i, layer in enumerate(self.layers):
# if i != 0:
# h = self.dropout(h)
h = layer(blocks[i], h)
h = h.view(h.size(0),-1)
return h
class GIN(nn.Module):
def __init__(self,
in_feats,
n_hidden,
n_classes,
n_layers,
activation,
dropout):
super(GIN, self).__init__()
self.layers = nn.ModuleList()
# input layer
self.layers.append(
GINConv(
nn.Sequential(
nn.Linear(in_feats, n_hidden, bias=False),
# nn.ReLU(),
),
aggregator_type='sum',
learn_eps=False)
)
# hidden layers
for _ in range(1, n_layers - 1):
self.layers.append(
GINConv(nn.Sequential(
nn.Linear(n_hidden, n_hidden, bias=False),
# nn.ReLU(),
),
aggregator_type='sum',
)
)
# output layer
self.layers.append(
GINConv(nn.Sequential(
nn.Linear(n_hidden, n_classes, bias=False),
# nn.ReLU(),
),
aggregator_type='sum',
),
)
self.dropout = nn.Dropout(p=dropout)
def forward(self, blocks, features):
h = features
for i, layer in enumerate(self.layers):
if i != 0:
h = self.dropout(h)
h = layer(blocks[i], h)
return h