-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathres_gcn.py
222 lines (207 loc) · 9.08 KB
/
res_gcn.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from functools import partial
import torch
import torch.nn.functional as F
from torch.nn import Linear, BatchNorm1d
from torch_geometric.nn import global_mean_pool, global_add_pool
from gcn_conv import GCNConv
class ResGCN(torch.nn.Module):
"""GCN with BN and residual connection."""
def __init__(self, dataset, hidden, num_feat_layers=1, num_conv_layers=3,
num_fc_layers=2, gfn=False, collapse=False, residual=False,
res_branch="BNConvReLU", global_pool="sum", dropout=0,
edge_norm=True):
super(ResGCN, self).__init__()
assert num_feat_layers == 1, "more feat layers are not now supported"
self.conv_residual = residual
self.fc_residual = False # no skip-connections for fc layers.
self.res_branch = res_branch
self.collapse = collapse
assert "sum" in global_pool or "mean" in global_pool, global_pool
if "sum" in global_pool:
self.global_pool = global_add_pool
else:
self.global_pool = global_mean_pool
self.dropout = dropout
GConv = partial(GCNConv, edge_norm=edge_norm, gfn=gfn)
if "xg" in dataset[0]: # Utilize graph level features.
self.use_xg = True
self.bn1_xg = BatchNorm1d(dataset[0].xg.size(1))
self.lin1_xg = Linear(dataset[0].xg.size(1), hidden)
self.bn2_xg = BatchNorm1d(hidden)
self.lin2_xg = Linear(hidden, hidden)
else:
self.use_xg = False
hidden_in = dataset.num_features
if collapse:
self.bn_feat = BatchNorm1d(hidden_in)
self.bns_fc = torch.nn.ModuleList()
self.lins = torch.nn.ModuleList()
if "gating" in global_pool:
self.gating = torch.nn.Sequential(
Linear(hidden_in, hidden_in),
torch.nn.ReLU(),
Linear(hidden_in, 1),
torch.nn.Sigmoid())
else:
self.gating = None
for i in range(num_fc_layers - 1):
self.bns_fc.append(BatchNorm1d(hidden_in))
self.lins.append(Linear(hidden_in, hidden))
hidden_in = hidden
self.lin_class = Linear(hidden_in, dataset.num_classes)
else:
self.bn_feat = BatchNorm1d(hidden_in)
feat_gfn = True # set true so GCNConv is feat transform
self.conv_feat = GCNConv(hidden_in, hidden, gfn=feat_gfn)
if "gating" in global_pool:
self.gating = torch.nn.Sequential(
Linear(hidden, hidden),
torch.nn.ReLU(),
Linear(hidden, 1),
torch.nn.Sigmoid())
else:
self.gating = None
self.bns_conv = torch.nn.ModuleList()
self.convs = torch.nn.ModuleList()
if self.res_branch == "resnet":
for i in range(num_conv_layers):
self.bns_conv.append(BatchNorm1d(hidden))
self.convs.append(GCNConv(hidden, hidden, gfn=feat_gfn))
self.bns_conv.append(BatchNorm1d(hidden))
self.convs.append(GConv(hidden, hidden))
self.bns_conv.append(BatchNorm1d(hidden))
self.convs.append(GCNConv(hidden, hidden, gfn=feat_gfn))
else:
for i in range(num_conv_layers):
self.bns_conv.append(BatchNorm1d(hidden))
self.convs.append(GConv(hidden, hidden))
self.bn_hidden = BatchNorm1d(hidden)
self.bns_fc = torch.nn.ModuleList()
self.lins = torch.nn.ModuleList()
for i in range(num_fc_layers - 1):
self.bns_fc.append(BatchNorm1d(hidden))
self.lins.append(Linear(hidden, hidden))
self.lin_class = Linear(hidden, dataset.num_classes)
# BN initialization.
for m in self.modules():
if isinstance(m, (torch.nn.BatchNorm1d)):
torch.nn.init.constant_(m.weight, 1)
torch.nn.init.constant_(m.bias, 0.0001)
def reset_parameters(self):
raise NotImplemented(
"This is prune to bugs (e.g. lead to training on test set in "
"cross validation setting). Create a new model instance instead.")
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
if self.use_xg:
# xg is (batch_size x its feat dim)
xg = self.bn1_xg(data.xg)
xg = F.relu(self.lin1_xg(xg))
xg = self.bn2_xg(xg)
xg = F.relu(self.lin2_xg(xg))
else:
xg = None
if self.collapse:
return self.forward_collapse(x, edge_index, batch, xg)
elif self.res_branch == "BNConvReLU":
return self.forward_BNConvReLU(x, edge_index, batch, xg)
elif self.res_branch == "BNReLUConv":
return self.forward_BNReLUConv(x, edge_index, batch, xg)
elif self.res_branch == "ConvReLUBN":
return self.forward_ConvReLUBN(x, edge_index, batch, xg)
elif self.res_branch == "resnet":
return self.forward_resnet(x, edge_index, batch, xg)
else:
raise ValueError("Unknown res_branch %s" % self.res_branch)
def forward_collapse(self, x, edge_index, batch, xg=None):
x = self.bn_feat(x)
gate = 1 if self.gating is None else self.gating(x)
x = self.global_pool(x * gate, batch)
x = x if xg is None else x + xg
for i, lin in enumerate(self.lins):
x_ = self.bns_fc[i](x)
x_ = F.relu(lin(x_))
x = x + x_ if self.fc_residual else x_
x = self.lin_class(x)
return F.log_softmax(x, dim=-1)
def forward_BNConvReLU(self, x, edge_index, batch, xg=None):
x = self.bn_feat(x)
x = F.relu(self.conv_feat(x, edge_index))
for i, conv in enumerate(self.convs):
x_ = self.bns_conv[i](x)
x_ = F.relu(conv(x_, edge_index))
x = x + x_ if self.conv_residual else x_
gate = 1 if self.gating is None else self.gating(x)
x = self.global_pool(x * gate, batch)
x = x if xg is None else x + xg
for i, lin in enumerate(self.lins):
x_ = self.bns_fc[i](x)
x_ = F.relu(lin(x_))
x = x + x_ if self.fc_residual else x_
x = self.bn_hidden(x)
if self.dropout > 0:
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin_class(x)
return F.log_softmax(x, dim=-1)
def forward_BNReLUConv(self, x, edge_index, batch, xg=None):
x = self.bn_feat(x)
x = self.conv_feat(x, edge_index)
for i, conv in enumerate(self.convs):
x_ = F.relu(self.bns_conv[i](x))
x_ = conv(x_, edge_index)
x = x + x_ if self.conv_residual else x_
x = self.global_pool(x, batch)
x = x if xg is None else x + xg
for i, lin in enumerate(self.lins):
x_ = F.relu(self.bns_fc[i](x))
x_ = lin(x_)
x = x + x_ if self.fc_residual else x_
x = F.relu(self.bn_hidden(x))
if self.dropout > 0:
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin_class(x)
return F.log_softmax(x, dim=-1)
def forward_ConvReLUBN(self, x, edge_index, batch, xg=None):
x = self.bn_feat(x)
x = F.relu(self.conv_feat(x, edge_index))
x = self.bn_hidden(x)
for i, conv in enumerate(self.convs):
x_ = F.relu(conv(x, edge_index))
x_ = self.bns_conv[i](x_)
x = x + x_ if self.conv_residual else x_
x = self.global_pool(x, batch)
x = x if xg is None else x + xg
for i, lin in enumerate(self.lins):
x_ = F.relu(lin(x))
x_ = self.bns_fc[i](x_)
x = x + x_ if self.fc_residual else x_
if self.dropout > 0:
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin_class(x)
return F.log_softmax(x, dim=-1)
def forward_resnet(self, x, edge_index, batch, xg=None):
# this mimics resnet architecture in cv.
x = self.bn_feat(x)
x = self.conv_feat(x, edge_index)
for i in range(len(self.convs) // 3):
x_ = x
x_ = F.relu(self.bns_conv[i*3+0](x_))
x_ = self.convs[i*3+0](x_, edge_index)
x_ = F.relu(self.bns_conv[i*3+1](x_))
x_ = self.convs[i*3+1](x_, edge_index)
x_ = F.relu(self.bns_conv[i*3+2](x_))
x_ = self.convs[i*3+2](x_, edge_index)
x = x + x_
x = self.global_pool(x, batch)
x = x if xg is None else x + xg
for i, lin in enumerate(self.lins):
x_ = F.relu(self.bns_fc[i](x))
x_ = lin(x_)
x = x + x_
x = F.relu(self.bn_hidden(x))
if self.dropout > 0:
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin_class(x)
return F.log_softmax(x, dim=-1)
def __repr__(self):
return self.__class__.__name__