-
Notifications
You must be signed in to change notification settings - Fork 2
/
PMLB_reg_train_nn.py
477 lines (357 loc) · 15.3 KB
/
PMLB_reg_train_nn.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sb
import pickle
from tqdm import tqdm
import os
from os.path import exists
from pmlb import fetch_data, classification_dataset_names,regression_dataset_names
import torch
import torch.nn as nn
import MFCF.gain_fucntions as gf
import MFCF.MFCF as MFCF
import torch.optim as optim
import torch.nn.functional as F
import networkx as nx
from itertools import permutations,combinations
from skorch import NeuralNetRegressor
import sparselinear.sparselinear as sl
class HNN(nn.Module):
def __init__(self, l1, l2, l3, l4, c1, c2, c3):
super(HNN, self).__init__()
self.sl1 = sl.SparseLinear(l1, l2, connectivity=torch.tensor([c1[1], c1[0]],dtype=torch.int64))
self.fc1 = nn.Linear(l1, 1)
self.sl2 = sl.SparseLinear(l2, l3, connectivity=torch.tensor([c2[1], c2[0]],dtype=torch.int64))
self.fc2 = nn.Linear(l2, 1)
self.c3=c3
if len(self.c3[0]) != 0:
self.sl3 = sl.SparseLinear(l3, l4, connectivity=torch.tensor([c3[1], c3[0]],dtype=torch.int64))
self.fc3 = nn.Linear(l3, 1)
self.fc4 = nn.Linear(l4, 1)
self.read_out = nn.Linear(4, 1)
else:
self.sl3 = None
self.fc3 = nn.Linear(l3, 1)
self.fc4 = None
self.read_out = nn.Linear(3, 1)
def forward(self, x):
x_f1 = F.relu(self.fc1(x))
x_s1 = F.relu(self.sl1(x)) # shape l2
x_f2 = F.relu(self.fc2(x_s1))
x_s2 = F.relu(self.sl2(x_s1))
if len(self.c3[0]) != 0:
x_f3 = F.relu(self.fc3(x_s2))
x_s3 = F.relu(self.sl3(x_s2))
x_f4 = F.relu(self.fc4(x_s3))
x = self.read_out(torch.cat([x_f1, x_f2, x_f3, x_f4], 1))
else:
x_f3 = F.relu(self.fc3(x_s2))
x = self.read_out(torch.cat([x_f1, x_f2, x_f3], 1))
return x
class HNN_adv(nn.Module):
def __init__(self, l1, l2, l3, l4, c1, c2, c3):
super(HNN_adv, self).__init__()
self.sl1 = sl.SparseLinear(l1, l2, connectivity=torch.tensor([c1[1], c1[0]],dtype=torch.int64))
self.fc1 = nn.Linear(l1, 1)
self.sl2 = sl.SparseLinear(l2, l3, connectivity=torch.tensor([c2[1], c2[0]],dtype=torch.int64))
self.fc2 = nn.Linear(l2, 1)
self.c3=c3
if len(self.c3[0]) != 0:
self.sl3 = sl.SparseLinear(l3, l4, connectivity=torch.tensor([c3[1], c3[0]],dtype=torch.int64))
self.fc3 = nn.Linear(l3, 1)
self.fc4 = nn.Linear(l4, 1)
self.read_out_1 = nn.Linear(4, 6)
self.read_out_2 = nn.Linear(6, 1)
else:
self.sl3 = None
self.fc3 = nn.Linear(l3, 1)
self.fc4 = None
self.read_out_1 = nn.Linear(3, 3)
self.read_out_2= nn.Linear(3, 1)
def forward(self, x):
x_f1 = F.relu(self.fc1(x))
x_s1 = F.relu(self.sl1(x)) # shape l2
x_f2 = F.relu(self.fc2(x_s1))
x_s2 = F.relu(self.sl2(x_s1))
if len(self.c3[0]) != 0:
x_f3 = F.relu(self.fc3(x_s2))
x_s3 = F.relu(self.sl3(x_s2))
x_f4 = F.relu(self.fc4(x_s3))
x = F.relu(self.read_out_1(torch.cat([x_f1, x_f2, x_f3, x_f4], 1)))
x= self.read_out_2(x)
else:
x_f3 = F.relu(self.fc3(x_s2))
x = F.relu(self.read_out_1(torch.cat([x_f1, x_f2, x_f3], 1)))
x=self.read_out_2(x)
return x
class MLP(nn.Module):
def __init__(self, l1, l2, l3, l4, c1, c2, c3):
super(MLP, self).__init__()
self.fc1 = nn.Linear(l1, l2)
self.fc2 = nn.Linear(l2, l3)
self.c3=c3
if len(self.c3[0]) != 0:
self.fc3 = nn.Linear(l3, l4)
self.fc4 = nn.Linear(l4, 1)
else:
self.fc3 = nn.Linear(l3, 1)
self.fc4 = None
self.read_out = nn.Linear(1, 1)
def forward(self, x):
x=x.float()
x_f1 = F.relu(self.fc1(x))
x_f2 = F.relu(self.fc2(x_f1))
x_f3 = F.relu(self.fc3(x_f2))
if len(self.c3[0]) != 0:
x_f4 = F.relu(self.fc4(x_f3))
x = self.read_out(x_f4)
else:
x = self.read_out(x_f3)
return x
class MLPres(nn.Module):
def __init__(self, l1, l2, l3, l4, c1, c2, c3):
super(MLPres, self).__init__()
self.sl1 = nn.Linear(l1, l2)
self.fc1 = nn.Linear(l1, 1)
self.sl2 = nn.Linear(l2, l3)
self.fc2 = nn.Linear(l2, 1)
self.c3 = c3
if len(self.c3[0]) != 0:
self.sl3 = nn.Linear(l3, l4)
self.fc3 = nn.Linear(l3, 1)
self.fc4 = nn.Linear(l4, 1)
self.read_out = nn.Linear(4, 1)
else:
self.sl3 = None
self.fc3 = nn.Linear(l3, 1)
self.fc4 = None
self.read_out = nn.Linear(3, 1)
def forward(self, x):
x_f1 = F.relu(self.fc1(x))
x_s1 = F.relu(self.sl1(x)) # shape l2
x_f2 = F.relu(self.fc2(x_s1))
x_s2 = F.relu(self.sl2(x_s1))
if len(self.c3[0]) != 0:
x_f3 = F.relu(self.fc3(x_s2))
x_s3 = F.relu(self.sl3(x_s2))
x_f4 = F.relu(self.fc4(x_s3))
x = self.read_out(torch.cat([x_f1, x_f2, x_f3, x_f4], 1))
else:
x_f3 = F.relu(self.fc3(x_s2))
x = self.read_out(torch.cat([x_f1, x_f2, x_f3], 1))
return x
class HNN_skip(nn.Module):
def __init__(self, l1, l2, l3, l4, c1, c2, c3, d2, d3):
super(HNN_skip, self).__init__()
self.d2 = d2
self.d3 = d3
self.c3 = c3
readout_in = 1
self.sl1 = sl.SparseLinear(l1, l2, connectivity=torch.tensor([c1[1], c1[0]],dtype=torch.int64))
self.sl2 = sl.SparseLinear(l2, l3, connectivity=torch.tensor([c2[1], c2[0]],dtype=torch.int64))
if len(d2) != 0:
self.skip2 = sl.SparseLinear(l2, 1, connectivity=torch.tensor([[0] * len(d2), d2],dtype=torch.int64))
readout_in += 1
if len(self.c3[0]) != 0:
self.sl3 = sl.SparseLinear(l3, l4, connectivity=torch.tensor([c3[1], c3[0]],dtype=torch.int64))
if len(d3) != 0:
self.skip3 = sl.SparseLinear(l3, 1, connectivity=torch.tensor([[0] * len(d3), d3],dtype=torch.int64))
readout_in += 1
self.fc4 = nn.Linear(l4, 1)
self.read_out = nn.Linear(readout_in, 1)
else:
self.sl3=None
self.fc4=None
self.fc3 = nn.Linear(l3, 1)
self.read_out = nn.Linear(readout_in, 1)
def forward(self, x):
x_sk_ls = torch.tensor([])
x_s1 = F.relu(self.sl1(x)) # shape l2
x_s2 = F.relu(self.sl2(x_s1))
if len(self.d2) != 0:
x_sk2 = F.relu(self.skip2(x_s1))
x_sk_ls = torch.cat([x_sk_ls, x_sk2],1)
if len(self.c3[0]) != 0:
x_s3 = F.relu(self.sl3(x_s2))
if len(self.d3) != 0:
x_sk3 = F.relu(self.skip3(x_s2))
x_sk_ls = torch.cat([x_sk_ls, x_sk3],1)
x_f4 = F.relu(self.fc4(x_s3))
x = self.read_out(torch.cat([x_sk_ls, x_f4], 1))
else:
x_f3=F.relu(self.fc3(x_s2))
x = self.read_out(torch.cat([x_sk_ls, x_f3], 1))
# x = self.read_out(x_f4)
return x
def train_model(train_X, test_X, train_y, test_y, dataset_name, model_name, model, params):
path = f'Results/PMLB/prediction/regression_raw/{dataset_name}'
file_path = path + '/' + f'{model_name}.pkl'
if not os.path.isdir(path):
os.mkdir(path)
else:
pass
if not os.path.exists(file_path):
CV = GridSearchCV(model, params,n_jobs=1)
CV.fit(train_X, train_y.reshape(-1,1))
pred_y = CV.best_estimator_.predict(test_X)
with open(file_path, 'wb') as f:
pickle.dump([test_y, pred_y], f)
def model_MLP(clique_1, clique_2, clique_3, clique_4,connection_1,connection_2,connection_3
,disconnection_2,disconnection_3):
hyper_params = {'max_epochs': [50,100,300,500],
'lr':[0.001,0.005,0.01,0.05,0.1],
'optimizer__weight_decay':[0,0.05,0.1,0.2]
}
mlp=MLP(len(clique_1), len(clique_2), len(clique_3), len(clique_4),connection_1,connection_2,connection_3)
est = NeuralNetRegressor(mlp
, optimizer=optim.Adam
, optimizer__weight_decay=0.05
, criterion=nn.MSELoss
, max_epochs=100
, lr=0.001
, verbose=0)
return est, hyper_params
def model_MLPres(clique_1, clique_2, clique_3, clique_4,connection_1,connection_2,connection_3
,disconnection_2,disconnection_3):
hyper_params = {'max_epochs': [50,100,300,500],
'lr':[0.001,0.005,0.01,0.05,0.1],
'optimizer__weight_decay':[0,0.05,0.1,0.2]
}
mlpres=MLPres(len(clique_1), len(clique_2), len(clique_3), len(clique_4),connection_1,connection_2,connection_3)
est = NeuralNetRegressor(mlpres
, optimizer=optim.Adam
, optimizer__weight_decay=0.05
, criterion=nn.MSELoss
, max_epochs=100
, lr=0.001
, verbose=0)
return est, hyper_params
def model_HNN(clique_1, clique_2, clique_3, clique_4,connection_1,connection_2,connection_3,
disconnection_2,disconnection_3):
hyper_params = {'max_epochs': [50,100,300,500],
'lr':[0.001,0.005,0.01,0.05,0.1],
'optimizer__weight_decay':[0,0.05,0.1,0.2]
}
hnn=HNN(len(clique_1), len(clique_2), len(clique_3), len(clique_4),connection_1,connection_2,connection_3)
est = NeuralNetRegressor(hnn
, optimizer=optim.Adam
, optimizer__weight_decay=0.05
, criterion=nn.MSELoss
, max_epochs=100
, lr=0.001
, verbose=0)
return est, hyper_params
def model_HNN_adv(clique_1, clique_2, clique_3, clique_4,connection_1,connection_2,connection_3,
disconnection_2,disconnection_3):
hyper_params = {'max_epochs': [50,100,300,500],
'lr':[0.001,0.005,0.01,0.05,0.1],
'optimizer__weight_decay':[0,0.05,0.1,0.2]
}
hnn_adv=HNN_adv(len(clique_1), len(clique_2), len(clique_3), len(clique_4),connection_1,connection_2,connection_3)
est = NeuralNetRegressor(hnn_adv
, optimizer=optim.Adam
, optimizer__weight_decay=0.05
, criterion=nn.MSELoss
, max_epochs=100
, lr=0.001
, verbose=0)
return est, hyper_params
def model_HNN_skip(clique_1, clique_2, clique_3, clique_4,connection_1,connection_2,connection_3,
disconnection_2,disconnection_3):
hyper_params = {'max_epochs': [50,100,300,500],
'lr':[0.001,0.005,0.01,0.05,0.1],
'optimizer__weight_decay':[0,0.05,0.1,0.2]
}
hnn_skip=HNN_skip(len(clique_1), len(clique_2), len(clique_3), len(clique_4),connection_1,connection_2,connection_3,
disconnection_2,disconnection_3)
est = NeuralNetRegressor(hnn_skip
, optimizer=optim.Adam
, optimizer__weight_decay=0.05
, criterion=nn.MSELoss
, max_epochs=100
, lr=0.001
, verbose=0)
return est, hyper_params
def MFCF_J(X,max_clique_size=4):
'''
sparse J
'''
C = np.cov(X, rowvar=False)
ctl = MFCF.mfcf_control()
ctl['threshold'] = 0.1
ctl['drop_sep'] = 0
ctl['min_clique_size'] =2
ctl['max_clique_size'] = 4
gain_function = gf.sumsquares_gen
cliques, separators, peo, gt = MFCF.mfcf(C, ctl, gain_function)
J = MFCF.logo(C, cliques, separators)
return J
def separating_cliques(G):
clique_1 = []
clique_2 = []
clique_3 = []
clique_4 = []
for clique in nx.enumerate_all_cliques(G):
clique = set(clique)
if len(clique) == 1:
clique_1.append(clique)
elif len(clique) == 2:
clique_2.append(clique)
elif len(clique) == 3:
clique_3.append(clique)
elif len(clique) == 4:
clique_4.append(clique)
return clique_1,clique_2,clique_3,clique_4
def get_connection(clique_last, clique_next):
connection_list = [[], []]
component_mapping = {i: x for i, x in enumerate(clique_last)}
for i, clique in enumerate(clique_next):
component = [set(x) for x in combinations(clique, len(clique) - 1)]
index_next = i
index_last = [list(component_mapping.keys())[list(component_mapping.values()).index(x)] for x in component]
for j in index_last:
connection_list[0].append(j)
connection_list[1].append(i)
return connection_list
def get_disconnection(c_last, c_next):
all_simplex = set(c_last[1])
linked_simplex = set(c_next[0])
disconnected_simplex = list(all_simplex - linked_simplex)
return disconnected_simplex
if __name__ == "__main__":
#1191_BNG_pbc
model_dict={'MLPres':model_MLPres}
n=len(model_dict)*len(regression_dataset_names)
pbar = tqdm(total=n, desc='Back Test Progress', )
for regression_dataset in regression_dataset_names:
#print(regression_dataset)
X, y = fetch_data(regression_dataset, return_X_y=True)
train_X, test_X, train_y, test_y = train_test_split(X, y)
try:
J = MFCF_J(train_X)
err=False
except:
err=True
if not err:
G = nx.from_numpy_array(J)
clique_1, clique_2, clique_3, clique_4 = separating_cliques(G)
connection_1 = get_connection(clique_1, clique_2)
connection_2 = get_connection(clique_2, clique_3)
connection_3 = get_connection(clique_3, clique_4)
disconnection_2 = get_disconnection(connection_1, connection_2)
disconnection_3 = get_disconnection(connection_2, connection_3)
if len(connection_2[0])!=0 and len(connection_3[0])!=0:
for model_name,func in model_dict.items():
model,params=func(clique_1, clique_2, clique_3, clique_4,connection_1,connection_2,connection_3,
disconnection_2,disconnection_3)
try:
train_model(torch.from_numpy(train_X).float(), torch.from_numpy(test_X).float(),
torch.from_numpy(train_y).float(), torch.from_numpy(test_y).float(),
regression_dataset,model_name, model, params)
except Exception as err:
print(regression_dataset)
print(err)
pbar.update(1)