-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_FNN_DTI.py
625 lines (509 loc) · 27.5 KB
/
train_FNN_DTI.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import os
from builtins import len
from models import get_model
from evaluation_metrics import prec_rec_f1_acc_mcc, get_list_of_scores
from data_processing import get_test_val_folds_train_data_loader, get_train_test_train_data_loader, get_train_data_loader
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import pandas as pd
from sklearn.metrics import roc_curve
cwd = os.getcwd()
project_file_path = "{}TransferLearning4DTI".format(cwd.split("TransferLearning4DTI")[0])
training_files_path = "{}TransferLearning4DTI/training_files".format(cwd.split("TransferLearning4DTI")[0])
result_files_path = "{}/{}".format(project_file_path, "result_files/")
trained_models_path = "{}/{}".format(project_file_path, "trained_models")
def binary_acc(y_pred, y_test):
y_pred_tag = torch.round(torch.sigmoid(y_pred))
correct_results_sum = (y_pred_tag == y_test).sum().float()
acc = correct_results_sum / y_test.shape[0]
acc = torch.round(acc * 100)
return acc
def multi_acc(y_pred, y_test):
y_pred_softmax = torch.log_softmax(y_pred, dim=1)
_, y_pred_tags = torch.max(y_pred_softmax, dim=1)
correct_pred = (y_pred_tags == y_test).float()
acc = correct_pred.sum() / len(correct_pred)
acc = torch.round(acc * 100)
return acc
def compute_test_loss(model, criterion, data_loader, device, num_classes):
total_count = 0
total_loss = 0.0
all_comp_ids = []
all_tar_ids = []
all_labels = []
predictions = []
woutroundpredictions = []
results_dict = {}
for i, data in enumerate(data_loader):
comp_feature_vectors, labels, compound_ids, target_ids = data
comp_feature_vectors, labels = Variable(comp_feature_vectors).to(
device), Variable(labels).to(device)
all_comp_ids.extend(compound_ids)
all_tar_ids.extend(target_ids)
total_count += comp_feature_vectors.shape[0]
y_pred = model(comp_feature_vectors).to(device)
if num_classes == 2:
y_test_pred = torch.sigmoid(y_pred)
y_pred_tag = torch.round(y_test_pred)
loss_val = criterion(y_pred.squeeze(), labels)
total_loss += float(loss_val.item())
for item in labels:
all_labels.append(float(item.item()))
for item in y_pred_tag:
predictions.append(float(item.item()))
for item in y_test_pred:
woutroundpredictions.append(float(item.item()))
for j in range(len(compound_ids)):
results_dict[compound_ids[j]] = y_test_pred[j].item()
else:
loss_val = criterion(y_pred.squeeze(), labels.long())
total_loss += float(loss_val.item())
for item in labels:
all_labels.append(float(item.item()))
_, y_pred_tag = torch.max(y_pred, dim=1)
for item in y_pred_tag:
predictions.append(float(item.item()))
return total_loss, total_count, all_labels, predictions, all_comp_ids, all_tar_ids, woutroundpredictions
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def get_external_test_results(model, data_loader, device, model_nm):
total_count = 0
all_comp_ids = []
predictions = []
results_dict = {}
for i, data in enumerate(data_loader):
comp_feature_vectors, compound_ids = data
comp_feature_vectors = Variable(comp_feature_vectors).to(
device)
all_comp_ids.extend(compound_ids)
total_count += comp_feature_vectors.shape[0]
if "conv1d" in model_nm:
comp_feature_vectors = comp_feature_vectors[:, :, None]
y_pred = model(comp_feature_vectors).to(device)
y_test_pred = torch.sigmoid(y_pred)
for j in range(len(compound_ids)):
results_dict[compound_ids[j]] = y_test_pred[j].item()
for item in y_test_pred:
predictions.append(float(item.item()))
return results_dict
def save_best_model_predictions(trained_models_path, experiment_name, model, feature):
if not os.path.exists(os.path.join(trained_models_path, experiment_name)):
os.makedirs(os.path.join(trained_models_path, experiment_name))
torch.save(model.state_dict(),
"{}/{}/best_state_dict_{}.pth".format(trained_models_path, experiment_name, feature))
def find_optimal_cutoff(target, predicted):
fpr, tpr, threshold = roc_curve(target, predicted)
i = np.arange(len(tpr))
roc = pd.DataFrame({'tf': pd.Series(tpr-(1-fpr), index=i), 'threshold': pd.Series(threshold, index=i)})
roc_t = roc.iloc[(roc.tf-0).abs().argsort()[:1]]
return list(roc_t['threshold'])
def five_fold_training(target_dataset, source_dataset, comp_feature_list, comp_hidden_lst, learning_rate, batch_size,
model_nm, dropout, experiment_name, n_epoch, subset_flag, tl_flag, freeze_flag, freezing_layers,
subset_size, setting, num_classes):
arguments = [str(argm) for argm in [target_dataset, source_dataset, comp_feature_list, comp_hidden_lst, learning_rate, batch_size,
experiment_name, model_nm, dropout, n_epoch, setting,
subset_flag, tl_flag, freeze_flag, freezing_layers, subset_size, num_classes]]
str_arguments = "-".join(arguments)
print("Arguments:", str_arguments)
torch.manual_seed(101)
np.random.seed(101)
training_dataset_path = "{}/{}".format(training_files_path, target_dataset)
if subset_flag == 0:
exp_path = os.path.join(result_files_path, target_dataset)
else:
exp_path = os.path.join(result_files_path, target_dataset + "/dataSubset" + str(subset_size))
if not os.path.exists(exp_path):
os.makedirs(exp_path)
if not os.path.exists(exp_path + "/scratch"):
os.makedirs(exp_path + "/scratch")
if not os.path.exists(exp_path + "/freeze"):
os.makedirs(exp_path + "/freeze")
if not os.path.exists(exp_path + "/fine-tuned"):
os.makedirs(exp_path + "/fine-tuned")
if tl_flag == 0:
tl = "scratch"
else:
tl = "fine-tuned"
if subset_flag == 0 or (subset_flag == 1 and tl_flag == 0):
best_val_test_result_fl = open("{}/scratch/{}_perf_results-{}.txt".format(exp_path, tl, str_arguments), "w")
elif subset_flag == 1:
if freeze_flag == 1:
best_val_test_result_fl = open("{}/freeze/sub_{}_freeze_{}_perf_results-{}.txt".format(
exp_path, tl, freezing_layers, str_arguments), "w")
else:
best_val_test_result_fl = open(
"{}/fine-tuned/sub_{}_perf_results-{}.txt".format(exp_path, tl, str_arguments), "w")
loader_fold_dict, test_loader, external_data_loader = get_test_val_folds_train_data_loader(training_dataset_path,
comp_feature_list,
batch_size, subset_size,
subset_flag)
num_of_folds = len(loader_fold_dict)
folds = range(num_of_folds)
average_validation_mcc, average_test_mcc = 0, 0
for fold in folds:
print("FOLD:", fold + 1)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
train_loader, valid_loader = loader_fold_dict[fold]
comp_feature_size = 300
if comp_feature_list[0] == "ecfp4":
comp_feature_size = 1024
model = get_model(model_nm, comp_feature_size, comp_hidden_lst, num_classes, dropout)
if tl_flag == 1:
if source_dataset == "multi":
multi_model = get_model(model_nm, comp_feature_size, comp_hidden_lst, 5, dropout)
multi_model.load_state_dict(
torch.load("{}/{}/best_state_dict_{}.pth".format(trained_models_path, source_dataset, comp_feature_list[0]),
map_location=torch.device(device)))
multi_model.layer_out = nn.Linear(comp_hidden_lst[-1], 1)
model = multi_model
else:
model.load_state_dict(
torch.load("{}/{}/best_state_dict_{}.pth".format(trained_models_path, source_dataset, comp_feature_list[0]),
map_location=torch.device(device)))
model.to(device)
if num_classes == 2:
criterion = nn.BCEWithLogitsLoss()
else:
criterion = nn.CrossEntropyLoss()
if freeze_flag == 1:
if "1" in freezing_layers:
model.l1.bias.requires_grad = False
model.l1.weight.requires_grad = False
if "2" in freezing_layers:
model.l2.bias.requires_grad = False
model.l2.weight.requires_grad = False
if "3" in freezing_layers:
model.l3.bias.requires_grad = False
model.l3.weight.requires_grad = False
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-5)
best_model = model
best_val_score_epoch, best_test_score_epoch = 0, 0
best_val_mcc_score, best_test_mcc_score = -10000.0, -10000.0
best_val_test_performance_dict = dict()
best_val_test_performance_dict["MCC"] = 0.0
model.train()
for epoch in range(1, n_epoch + 1):
total_training_loss, total_validation_loss, total_test_loss = 0.0, 0.0, 0.0
epoch_loss = 0
epoch_acc = 0
for i, data in enumerate(train_loader):
# clear gradient DO NOT forget you fool!
optimizer.zero_grad()
comp_feature_vectors, labels, compound_ids, target_ids = data
comp_feature_vectors, labels = Variable(comp_feature_vectors).to(
device), Variable(labels).to(device)
y_pred = model(comp_feature_vectors).to(device)
if num_classes == 2:
loss = criterion(y_pred.squeeze(), labels)
else:
loss = criterion(y_pred.squeeze(), labels.long())
total_training_loss += float(loss.item())
epoch_loss += loss.item()
if num_classes == 2:
acc = binary_acc(y_pred.squeeze(), labels)
else:
acc = multi_acc(y_pred.squeeze(), labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
model.eval()
with torch.no_grad(): # torch.set_grad_enabled(False):
total_val_loss, total_val_count, val_labels, val_predictions, all_val_comp_ids, all_val_tar_ids, woutroundpredictions = \
compute_test_loss(model, criterion, valid_loader, device, num_classes)
total_test_loss, total_test_count, test_labels, test_predictions, all_test_comp_ids, test_tar_ids, woutroundpredictions =\
compute_test_loss(model, criterion, test_loader, device, num_classes)
val_perf_dict = dict()
val_perf_dict["MCC"] = 0.0
val_perf_dict = prec_rec_f1_acc_mcc(val_labels, val_predictions, num_classes)
test_perf_dict = dict()
test_perf_dict["MCC"] = 0.0
test_perf_dict = prec_rec_f1_acc_mcc(test_labels, test_predictions, num_classes)
print(f'Epoch {epoch + 0:03}: | Loss: {total_training_loss / len(train_loader):.5f} | Val_loss: {total_val_loss / len(train_loader):.5f} '
f'| Acc: {epoch_acc / len(train_loader):.3f} | Val_MCC: {val_perf_dict["MCC"]:.4f} '
f'| Test_MCC: {test_perf_dict["MCC"]:.4f}')
if val_perf_dict["MCC"] > best_val_mcc_score:
best_val_mcc_score = val_perf_dict["MCC"]
best_val_performance_dict = val_perf_dict
best_val_score_epoch = epoch
best_model = model
if subset_flag == 0:
save_best_model_predictions(trained_models_path, target_dataset, best_model, comp_feature_list[0])
if test_perf_dict["MCC"] > best_test_mcc_score:
best_test_mcc_score = test_perf_dict["MCC"]
best_test_performance_dict = test_perf_dict
best_test_score_epoch = epoch
if epoch == n_epoch:
print(best_val_performance_dict, "in epoch:", best_val_score_epoch)
print(best_test_performance_dict, "in epoch:", best_test_score_epoch)
average_validation_mcc += best_val_performance_dict["MCC"]
average_test_mcc += best_test_performance_dict["MCC"]
score_list = get_list_of_scores(num_classes)
best_val_test_result_fl.write("FOLD : {}\n".format(fold + 1))
for scr in score_list:
best_val_test_result_fl.write("Val {}:\t{}\n".format(scr, best_val_performance_dict[scr]))
for scr in score_list:
best_val_test_result_fl.write("Test {}:\t{}\n".format(scr, best_test_performance_dict[scr]))
if fold == 4:
average_validation_mcc /= 5
average_test_mcc /= 5
print("average best validation mcc:", average_validation_mcc)
print("average best test mcc:", average_test_mcc)
best_val_test_result_fl.write("Val avg mcc:\t{}\n".format(average_validation_mcc))
best_val_test_result_fl.write("Test avg mcc:\t{}\n".format(average_test_mcc))
best_val_test_result_fl.close()
def training_test(target_dataset, source_dataset, comp_feature_list, comp_hidden_lst, learning_rate, batch_size,
model_nm, dropout, experiment_name, n_epoch, subset_flag, tl_flag, freeze_flag, freezing_layers,
subset_size, setting, num_classes):
arguments = [str(argm) for argm in [target_dataset, source_dataset, comp_feature_list, comp_hidden_lst, learning_rate, batch_size,
experiment_name, model_nm, dropout, n_epoch, setting,
subset_flag, tl_flag, freeze_flag, freezing_layers, subset_size, num_classes]]
str_arguments = "-".join(arguments)
print("Arguments:", str_arguments)
torch.manual_seed(101)
np.random.seed(101)
training_dataset_path = "{}/{}".format(training_files_path, target_dataset)
train_loader, test_loader, external_data_loader = get_train_test_train_data_loader(training_dataset_path, comp_feature_list, batch_size, subset_size,
subset_flag)
if subset_flag == 0:
exp_path = os.path.join(result_files_path, target_dataset)
else:
exp_path = os.path.join(result_files_path, target_dataset + "/dataSubset" + str(subset_size))
if not os.path.exists(exp_path):
os.makedirs(exp_path)
if not os.path.exists(exp_path + "/scratch"):
os.makedirs(exp_path + "/scratch")
if not os.path.exists(exp_path + "/freeze"):
os.makedirs(exp_path + "/freeze")
if not os.path.exists(exp_path + "/fine-tuned"):
os.makedirs(exp_path + "/fine-tuned")
if tl_flag == 0:
tl = "scratch"
else:
tl = "fine-tuned"
if subset_flag == 0 or (subset_flag == 1 and tl_flag == 0):
best_val_test_result_fl = open("{}/scratch/{}_perf_results-{}.txt".format(exp_path, tl, str_arguments), "w")
elif subset_flag == 1:
if freeze_flag == 1:
best_val_test_result_fl = open("{}/freeze/sub_{}_freeze_{}_perf_results-{}.txt".format(
exp_path, tl, freezing_layers, str_arguments), "w")
else:
best_val_test_result_fl = open(
"{}/fine-tuned/sub_{}_perf_results-{}.txt".format(exp_path, tl, str_arguments), "w")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
comp_feature_size = 300
if comp_feature_list[0] == "ecfp4":
comp_feature_size = 1024
model = get_model(model_nm, comp_feature_size, comp_hidden_lst, num_classes, dropout)
if tl_flag == 1:
model.load_state_dict(
torch.load("{}/{}/best_state_dict_{}.pth".format(trained_models_path, source_dataset, comp_feature_list[0]),
map_location=torch.device(device)))
model.to(device)
if num_classes == 2:
criterion = nn.BCEWithLogitsLoss()
else:
criterion = nn.CrossEntropyLoss()
if freeze_flag == 1:
# freeze layers
if "1" in freezing_layers:
model.l1.bias.requires_grad = False
model.l1.weight.requires_grad = False
if "2" in freezing_layers:
model.l2.bias.requires_grad = False
model.l2.weight.requires_grad = False
if "3" in freezing_layers:
model.l3.bias.requires_grad = False
model.l3.weight.requires_grad = False
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-5)
best_val_score_epoch, best_test_score_epoch = 0, 0
best_val_mcc_score, best_test_mcc_score = -10000.0, -10000.0
best_val_test_performance_dict = dict()
best_val_test_performance_dict["MCC"] = 0.0
model.train()
for epoch in range(1, n_epoch + 1):
total_training_loss, total_validation_loss, total_test_loss = 0.0, 0.0, 0.0
epoch_loss = 0
epoch_acc = 0
for i, data in enumerate(train_loader):
# clear gradient DO NOT forget you fool!
optimizer.zero_grad()
comp_feature_vectors, labels, compound_ids, target_ids = data
comp_feature_vectors, labels = Variable(comp_feature_vectors).to(
device), Variable(labels).to(device)
y_pred = model(comp_feature_vectors).to(device)
if num_classes == 2:
loss = criterion(y_pred.squeeze(), labels)
else:
loss = criterion(y_pred.squeeze(), labels.long())
total_training_loss += float(loss.item())
epoch_loss += loss.item()
if num_classes == 2:
acc = binary_acc(y_pred.squeeze(), labels)
else:
acc = multi_acc(y_pred.squeeze(), labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
model.eval()
with torch.no_grad(): # torch.set_grad_enabled(False):
total_test_loss, total_test_count, test_labels, test_predictions, all_test_comp_ids, test_tar_ids, woutroundpredictions =\
compute_test_loss(model, criterion, test_loader, device, num_classes)
if num_classes == 2:
threshold = find_optimal_cutoff(test_labels, woutroundpredictions)
test_perf_dict = dict()
test_perf_dict["MCC"] = 0.0
test_perf_dict = prec_rec_f1_acc_mcc(test_labels, test_predictions, num_classes)
print(f'Epoch {epoch + 0:03}: | Loss: {total_training_loss / len(train_loader):.5f} '
f'| Acc: {epoch_acc / len(train_loader):.3f} '
f'| Test_MCC: {test_perf_dict["MCC"]:.4f}')
if test_perf_dict["MCC"] > best_test_mcc_score:
best_test_mcc_score = test_perf_dict["MCC"]
best_test_performance_dict = test_perf_dict
best_test_score_epoch = epoch
if num_classes == 2:
best_threshold = threshold[0]
all_predictions = woutroundpredictions
fp = 0
if epoch == n_epoch:
for pred in all_predictions:
if pred >= best_threshold:
fp += 1
print(best_test_performance_dict, "in epoch:", best_test_score_epoch)
score_list = get_list_of_scores(num_classes)
for scr in score_list:
best_val_test_result_fl.write("Test {}:\t{}\n".format(scr, best_test_performance_dict[scr]))
best_val_test_result_fl.close()
def training(target_dataset, source_dataset, comp_feature_list, comp_hidden_lst, learning_rate, batch_size,
model_nm, dropout, experiment_name, n_epoch, subset_flag, tl_flag, freeze_flag, freezing_layers,
subset_size, external_file, num_classes):
arguments = [str(argm) for argm in
[target_dataset, source_dataset, comp_feature_list, comp_hidden_lst,
learning_rate, batch_size,
experiment_name, model_nm, dropout, n_epoch, external_file,
subset_flag, tl_flag, freeze_flag, freezing_layers, subset_size, num_classes]]
str_arguments = "-".join(arguments)
print("Arguments:", str_arguments)
torch.manual_seed(101)
np.random.seed(101)
training_dataset_path = "{}/{}".format(training_files_path, target_dataset)
train_loader, external_test_loader = get_train_data_loader(training_dataset_path, comp_feature_list, batch_size, subset_size,
subset_flag, external_file)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
if comp_feature_list[0] == "ecfp4":
comp_feature_size = 1024
elif comp_feature_list[0] == "chemprop":
comp_feature_size = 300
model = get_model(model_nm, comp_feature_size, comp_hidden_lst, num_classes, dropout)
if tl_flag == 1:
model.load_state_dict(torch.load("{}/{}/best_state_dict_{}.pth".format(trained_models_path, source_dataset, comp_feature_list[0]),
map_location=torch.device(device)))
model.to(device)
if num_classes == 2:
criterion = nn.BCEWithLogitsLoss()
else:
criterion = nn.CrossEntropyLoss()
if freeze_flag == 1:
# freeze layers
if "1" in freezing_layers:
model.l1.bias.requires_grad = False
model.l1.weight.requires_grad = False
if "2" in freezing_layers:
model.l2.bias.requires_grad = False
model.l2.weight.requires_grad = False
if "3" in freezing_layers:
model.l3.bias.requires_grad = False
model.l3.weight.requires_grad = False
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-5)
best_loss = 10000
best_val_test_performance_dict = dict()
best_val_test_performance_dict["MCC"] = 0.0
model.train()
for epoch in range(1, n_epoch + 1):
total_training_loss, total_validation_loss, total_test_loss = 0.0, 0.0, 0.0
epoch_loss = 0
epoch_acc = 0
for i, data in enumerate(train_loader):
# clear gradient DO NOT forget you fool!
optimizer.zero_grad()
comp_feature_vectors, labels, compound_ids, target_ids = data
comp_feature_vectors, labels = Variable(comp_feature_vectors).to(
device), Variable(labels).to(device)
y_pred = model(comp_feature_vectors).to(device)
if num_classes == 2:
loss = criterion(y_pred.squeeze(), labels)
else:
loss = criterion(y_pred.squeeze(), labels.long())
total_training_loss += float(loss.item())
epoch_loss += loss.item()
if num_classes == 2:
acc = binary_acc(y_pred.squeeze(), labels)
else:
acc = multi_acc(y_pred.squeeze(), labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
print(f'Epoch {epoch + 0:03}: | Loss: {total_training_loss / len(train_loader):.5f} '
f'| Acc: {epoch_acc / len(train_loader):.3f} ')
if external_file != "-":
model.eval()
with torch.no_grad(): # torch.set_grad_enabled(False):
results_dict = get_external_test_results(model, external_test_loader, device, model_nm)
if epoch == n_epoch:
save_best_model_predictions(trained_models_path, target_dataset, model, comp_feature_list[0])
if external_file != "-":
sorted_dict = dict(sorted(results_dict.items(), key=lambda item: item[1]))
if not os.path.exists("output/"):
os.makedirs("output/")
external_file_path = external_file.split("/")
pred_file_name = external_file_path[len(external_file_path)-1].split(".")[0] + "_preds.txt"
with open("output/" + pred_file_name, 'w') as f:
for drug, pred in sorted_dict.items():
f.write("{:}\t{:.3f}\n".format(drug, float(pred)))
f.close()
def test(target_dataset, source_dataset, comp_feature_list, comp_hidden_lst, learning_rate, batch_size,
model_nm, dropout, experiment_name, n_epoch, subset_flag, tl_flag, freeze_flag, freezing_layers,
subset_size, external_file, num_classes):
arguments = [str(argm) for argm in
[target_dataset, source_dataset, comp_feature_list, comp_hidden_lst,
learning_rate, batch_size,
experiment_name, model_nm, dropout, n_epoch, external_file,
subset_flag, tl_flag, freeze_flag, freezing_layers, subset_size, num_classes]]
str_arguments = "-".join(arguments)
print("Arguments:", str_arguments)
torch.manual_seed(101)
np.random.seed(101)
training_dataset_path = "{}/{}".format(training_files_path, target_dataset)
train_loader, external_test_loader = get_train_data_loader(training_dataset_path, comp_feature_list, batch_size, subset_size,
subset_flag, external_file)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
comp_feature_size = 300
if comp_feature_list[0] == "ecfp4":
comp_feature_size = 1024
model = get_model(model_nm, comp_feature_size, comp_hidden_lst, num_classes, dropout)
if tl_flag == 1:
model.load_state_dict(torch.load("{}/{}/best_state_dict_{}.pth".format(trained_models_path, source_dataset, comp_feature_list[0]),
map_location=torch.device(device)))
model.to(device)
if external_file != "-":
model.eval()
with torch.no_grad(): # torch.set_grad_enabled(False):
results_dict = get_external_test_results(model, external_test_loader, device, model_nm)
sorted_dict = dict(sorted(results_dict.items(), key=lambda item: item[1]))
if not os.path.exists("output/"):
os.makedirs("output/")
external_file_path = external_file.split("/")
pred_file_name = external_file_path[len(external_file_path) - 1].split(".")[0] + "_preds.txt"
with open("output/" + pred_file_name, 'w') as f:
for drug, pred in sorted_dict.items():
f.write("{:}\t{:.3f}\n".format(drug, float(pred)))
f.close()