-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrerundiehl.py
More file actions
327 lines (251 loc) · 11 KB
/
rerundiehl.py
File metadata and controls
327 lines (251 loc) · 11 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import torch
from torch_geometric.nn import GCNConv, SAGEConv
import torch.nn.functional as F
from torch_geometric.nn import global_mean_pool
from torch_geometric.nn import BatchNorm
from torch_geometric.data import Data
from torch_geometric.nn.pool import EdgePooling
class GCNDiehlq1(torch.nn.Module):
archName = "GCN Diehl Q1"
def __init__(self, node_features, task_type_node, num_classes, dataset_name, device, batch_norm=False):
super().__init__()
self.n_epochs = 200
self.num_classes = num_classes
self.device = device
self.hid_channel = 128
self.edge_act = EdgePooling.compute_edge_score_softmax
if dataset_name == "PROTEIN":
self.hid_channel = 64
self.batch_size = 128
if dataset_name == "REDDIT-BINARY" or dataset_name == "REDDIT-MULTI-12K":
#self.edge_act = EdgePooling.compute_edge_score_tanh
#self.batch_size = 1
a = 1
self.learningrate = 0.01
self.lrhalving = True
self.halvinginterval = 50
self.optimizertype = torch.optim.Adam
self.batch_norm = batch_norm
if self.num_classes == 2: #binary
self.num_classes = 1
self.task_type_node = task_type_node
dropout=0.0 #The authors say they use drop out but not what rate, so we use the default value of 0.5? As suggested by the dropout paper. set to 0.2 as i think 0.5 is too much
dropout_pool=dropout
self.dropout = torch.nn.Dropout(p=dropout)
self.conv1 = SAGEConv(node_features, self.hid_channel)
if batch_norm:
self.batchnorm1 = BatchNorm(self.hid_channel)
self.conv2 = SAGEConv(self.hid_channel, self.hid_channel)
if batch_norm:
self.batchnorm2 = BatchNorm(self.hid_channel)
self.lin1 = torch.nn.Linear(self.hid_channel*2, self.hid_channel)
self.pool1 = EdgePooling(self.hid_channel, dropout=dropout_pool, edge_score_method=self.edge_act)
self.conv3 = SAGEConv(self.hid_channel, self.hid_channel)
if batch_norm:
self.batchnorm3 = BatchNorm(self.hid_channel)
self.conv4 = SAGEConv(self.hid_channel, self.hid_channel)
if batch_norm:
self.batchnorm4 = BatchNorm(self.hid_channel)
self.lin2 = torch.nn.Linear(self.hid_channel*2, self.hid_channel)
self.pool2 = EdgePooling(self.hid_channel, dropout=dropout_pool, edge_score_method=self.edge_act)
self.conv5 = SAGEConv(self.hid_channel, self.hid_channel)
if batch_norm:
self.batchnorm5 = BatchNorm(self.hid_channel)
self.conv6 = SAGEConv(self.hid_channel, self.hid_channel)
if batch_norm:
self.batchnorm6 = BatchNorm(self.hid_channel)
self.lin3 = torch.nn.Linear(self.hid_channel*2, self.hid_channel)
self.pool3 = EdgePooling(self.hid_channel, dropout=dropout_pool, edge_score_method=self.edge_act)
self.fc1 = torch.nn.Linear(self.hid_channel, self.hid_channel)
self.fc2 = torch.nn.Linear(self.hid_channel, self.num_classes)
def forward(self, data):
batch = data[3]
data = Data(x=data[0], edge_index=data[1].t().contiguous())
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
if self.batch_norm:
x = self.batchnorm1(x)
copy_x = x.clone()
x = self.conv2(x, edge_index)
x = F.relu(x)
if self.batch_norm:
x = self.batchnorm2(x)
x = self.lin1(torch.concat((x, copy_x), dim=1))
x = self.dropout(x)
block1_copy, block1_batch = x.clone(), batch.clone()
x, edge_index, batch, unpool1 = self.pool1(x, edge_index.long(), batch)
x = self.conv3(x, edge_index)
x = F.relu(x)
if self.batch_norm:
x = self.batchnorm3(x)
copy_x = x.clone()
x = self.conv4(x, edge_index)
x = F.relu(x)
if self.batch_norm:
x = self.batchnorm4(x)
x = self.lin2(torch.concat((x, copy_x), dim=1))
x = self.dropout(x)
block2_copy, block2_batch = x.clone(), batch.clone()
x, edge_index, batch, unpool2 = self.pool2(x, edge_index.long(), batch)
x = self.conv5(x, edge_index)
x = F.relu(x)
if self.batch_norm:
x = self.batchnorm5(x)
copy_x = x.clone()
x = self.conv6(x, edge_index)
x = F.relu(x)
if self.batch_norm:
x = self.batchnorm6(x)
x = self.lin3(torch.concat((x, copy_x), dim=1))
x = self.dropout(x)
block3_copy, block3_batch = x.clone(), batch.clone()
x, edge_index, batch, unpool3 = self.pool3(x, edge_index.long(), batch)
x = torch.cat((block1_copy, block2_copy, block3_copy, x))
batch = torch.cat((block1_batch, block2_batch, block3_batch, batch))
x = global_mean_pool(x, batch)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc2(x)
if self.num_classes == 1: #binary
x = torch.sigmoid(x)
return torch.flatten(x)
else:
return torch.nn.functional.log_softmax(x, dim=1)
# return torch.nn.functional.softmax(x, dim=1)
class GCNDiehlq2(torch.nn.Module):
archName = "GCN Diehl Q2"
def __init__(self, node_features, task_type_node, num_classes, dataset_name, device):
super().__init__()
self.n_epochs = 200
self.num_classes = num_classes
self.device = device
self.hid_channel = 128
self.edge_act = EdgePooling.compute_edge_score_softmax
if dataset_name == "PROTEIN":
self.hid_channel = 64
self.batch_size = 128
if dataset_name == "REDDIT-BINARY" or dataset_name == "REDDIT-MULTI-12K":
#self.edge_act = EdgePooling.compute_edge_score_tanh
#self.batch_size = 1
self.batch_size = 128
# self.batch_size = 32
self.learningrate = 0.01
self.lrhalving = True
self.halvinginterval = 50
self.optimizertype = torch.optim.Adam
if self.num_classes == 2: #binary
self.num_classes = 1
self.task_type_node = task_type_node
dropout=0.0
#dropout=0.5 #The authors say they use drop out but not what rate, so we use the default value of 0.5? As suggested by the dropout paper. set to 0.2 as i think 0.5 is too much
dropout_pool=dropout
self.dropout = torch.nn.Dropout(p=dropout)
self.conv1 = SAGEConv(node_features, self.hid_channel)
self.conv2 = SAGEConv(self.hid_channel, self.hid_channel)
#self.pool1 = EdgePooling(self.hid_channel, dropout=dropout_pool, edge_score_method=self.edge_act)
self.conv3 = SAGEConv(self.hid_channel, self.hid_channel)
self.conv4 = SAGEConv(self.hid_channel, self.hid_channel)
#self.pool2 = EdgePooling(self.hid_channel, dropout=dropout_pool, edge_score_method=self.edge_act)
self.conv5 = SAGEConv(self.hid_channel, self.hid_channel)
self.fc1 = torch.nn.Linear(self.hid_channel, self.hid_channel)
self.fc2 = torch.nn.Linear(self.hid_channel, self.num_classes)
def forward(self, data):
batch = data[3]
data = Data(x=data[0], edge_index=data[1].t().contiguous())
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
#x, edge_index, batch, unpool1 = self.pool1(x, edge_index.long(), batch)
x = self.conv3(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x = self.conv4(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
#x, edge_index, batch, unpool2 = self.pool2(x, edge_index.long(), batch)
x = self.conv5(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x = global_mean_pool(x, batch)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc2(x)
if self.num_classes == 1: #binary
x = torch.sigmoid(x)
return torch.flatten(x)
else:
return torch.nn.functional.log_softmax(x, dim=1)
class GCNDiehl(torch.nn.Module):
archName = "GCN Diehl from scratch"
def __init__(self, node_features, task_type_node, num_classes, dataset_name, device):
super().__init__()
self.n_epochs = 200
self.num_classes = num_classes
self.device = device
self.task_type_node = task_type_node
self.poolLayer = EdgePooling
self.convLayer = SAGEConv
self.hid_channel = 128
#self.batch_size = 128 #Batch size (minibtaching) does not seem to work
self.batch_size = 1
self.learningrate = 0.001
self.lrhalving = True
self.halvinginterval = 50
dropout=0.0
dropout_pool=dropout
if dataset_name == "REDDIT-MULTI-12K":
#self.batch_size = 128
#self.learningrate = 0.0005
self.hid_channel = 256
self.batch_size = 1
self.learningrate = 0.00025
if self.num_classes == 2: #binary
self.num_classes = 1
self.dropout = torch.nn.Dropout(p=dropout)
self.conv1 = self.convLayer(node_features, self.hid_channel)
self.conv2 = self.convLayer(self.hid_channel, self.hid_channel)
self.pool1 = self.poolLayer(self.hid_channel, dropout=dropout_pool)
self.conv3 = self.convLayer(self.hid_channel, self.hid_channel)
self.conv4 = self.convLayer(self.hid_channel, self.hid_channel)
self.pool2 = self.poolLayer(self.hid_channel, dropout=dropout_pool)
self.conv5 = self.convLayer(self.hid_channel, self.hid_channel)
self.fc1 = torch.nn.Linear(self.hid_channel, self.hid_channel)
self.fc2 = torch.nn.Linear(self.hid_channel, self.num_classes)
def forward(self, data):
batch = data[3]
data = Data(x=data[0], edge_index=data[1].t().contiguous())
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x, edge_index, batch, unpool1 = self.pool1(x, edge_index.long(), batch)
x = self.conv3(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x = self.conv4(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x, edge_index, batch, unpool2 = self.pool2(x, edge_index.long(), batch)
x = self.conv5(x, edge_index)
x = F.relu(x)
x = self.dropout(x)
x = global_mean_pool(x, batch)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc2(x)
x = torch.sigmoid(x)
if self.num_classes == 1: #binary
return torch.flatten(x)
else:
return x