-
Notifications
You must be signed in to change notification settings - Fork 43
/
train.py
209 lines (157 loc) · 7.23 KB
/
train.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
import copy
from time import time
import numpy as np
import pandas as pd
import torch
from sklearn.metrics import roc_auc_score, average_precision_score, f1_score, roc_curve, confusion_matrix, \
precision_score, recall_score, auc
from torch import nn
from torch.autograd import Variable
from torch.utils import data
torch.manual_seed(2) # reproducible torch:2 np:3
np.random.seed(3)
from argparse import ArgumentParser
from config import BIN_config_DBPE
from models import BIN_Interaction_Flat
from stream import BIN_Data_Encoder
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
parser = ArgumentParser(description='MolTrans Training.')
parser.add_argument('-b', '--batch-size', default=16, type=int,
metavar='N',
help='mini-batch size (default: 16), this is the total '
'batch size of all GPUs on the current node when '
'using Data Parallel or Distributed Data Parallel')
parser.add_argument('-j', '--workers', default=0, type=int, metavar='N',
help='number of data loading workers (default: 0)')
parser.add_argument('--epochs', default=50, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--task', choices=['biosnap', 'bindingdb', 'davis'],
default='', type=str, metavar='TASK',
help='Task name. Could be biosnap, bindingdb and davis.')
parser.add_argument('--lr', '--learning-rate', default=1e-4, type=float,
metavar='LR', help='initial learning rate', dest='lr')
def get_task(task_name):
if task_name.lower() == 'biosnap':
return './dataset/BIOSNAP/full_data'
elif task_name.lower() == 'bindingdb':
return './dataset/BindingDB'
elif task_name.lower() == 'davis':
return './dataset/DAVIS'
def test(data_generator, model):
y_pred = []
y_label = []
model.eval()
loss_accumulate = 0.0
count = 0.0
for i, (d, p, d_mask, p_mask, label) in enumerate(data_generator):
score = model(d.long().cuda(), p.long().cuda(), d_mask.long().cuda(), p_mask.long().cuda())
m = torch.nn.Sigmoid()
logits = torch.squeeze(m(score))
loss_fct = torch.nn.BCELoss()
label = Variable(torch.from_numpy(np.array(label)).float()).cuda()
loss = loss_fct(logits, label)
loss_accumulate += loss
count += 1
logits = logits.detach().cpu().numpy()
label_ids = label.to('cpu').numpy()
y_label = y_label + label_ids.flatten().tolist()
y_pred = y_pred + logits.flatten().tolist()
loss = loss_accumulate / count
fpr, tpr, thresholds = roc_curve(y_label, y_pred)
precision = tpr / (tpr + fpr)
f1 = 2 * precision * tpr / (tpr + precision + 0.00001)
thred_optim = thresholds[5:][np.argmax(f1[5:])]
print("optimal threshold: " + str(thred_optim))
y_pred_s = [1 if i else 0 for i in (y_pred >= thred_optim)]
auc_k = auc(fpr, tpr)
print("AUROC:" + str(auc_k))
print("AUPRC: " + str(average_precision_score(y_label, y_pred)))
cm1 = confusion_matrix(y_label, y_pred_s)
print('Confusion Matrix : \n', cm1)
print('Recall : ', recall_score(y_label, y_pred_s))
print('Precision : ', precision_score(y_label, y_pred_s))
total1 = sum(sum(cm1))
#####from confusion matrix calculate accuracy
accuracy1 = (cm1[0, 0] + cm1[1, 1]) / total1
print('Accuracy : ', accuracy1)
sensitivity1 = cm1[0, 0] / (cm1[0, 0] + cm1[0, 1])
print('Sensitivity : ', sensitivity1)
specificity1 = cm1[1, 1] / (cm1[1, 0] + cm1[1, 1])
print('Specificity : ', specificity1)
outputs = np.asarray([1 if i else 0 for i in (np.asarray(y_pred) >= 0.5)])
return roc_auc_score(y_label, y_pred), average_precision_score(y_label, y_pred), f1_score(y_label,
outputs), y_pred, loss.item()
def main():
config = BIN_config_DBPE()
args = parser.parse_args()
config['batch_size'] = args.batch_size
loss_history = []
model = BIN_Interaction_Flat(**config)
model = model.cuda()
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model, dim=0)
opt = torch.optim.Adam(model.parameters(), lr=args.lr)
print('--- Data Preparation ---')
params = {'batch_size': args.batch_size,
'shuffle': True,
'num_workers': args.workers,
'drop_last': True}
dataFolder = get_task(args.task)
df_train = pd.read_csv(dataFolder + '/train.csv')
df_val = pd.read_csv(dataFolder + '/val.csv')
df_test = pd.read_csv(dataFolder + '/test.csv')
training_set = BIN_Data_Encoder(df_train.index.values, df_train.Label.values, df_train)
training_generator = data.DataLoader(training_set, **params)
validation_set = BIN_Data_Encoder(df_val.index.values, df_val.Label.values, df_val)
validation_generator = data.DataLoader(validation_set, **params)
testing_set = BIN_Data_Encoder(df_test.index.values, df_test.Label.values, df_test)
testing_generator = data.DataLoader(testing_set, **params)
# early stopping
max_auc = 0
model_max = copy.deepcopy(model)
with torch.set_grad_enabled(False):
auc, auprc, f1, logits, loss = test(testing_generator, model_max)
print('Initial Testing AUROC: ' + str(auc) + ' , AUPRC: ' + str(auprc) + ' , F1: ' + str(
f1) + ' , Test loss: ' + str(loss))
print('--- Go for Training ---')
torch.backends.cudnn.benchmark = True
for epo in range(args.epochs):
model.train()
for i, (d, p, d_mask, p_mask, label) in enumerate(training_generator):
score = model(d.long().cuda(), p.long().cuda(), d_mask.long().cuda(), p_mask.long().cuda())
label = Variable(torch.from_numpy(np.array(label)).float()).cuda()
loss_fct = torch.nn.BCELoss()
m = torch.nn.Sigmoid()
n = torch.squeeze(m(score))
loss = loss_fct(n, label)
loss_history.append(loss)
opt.zero_grad()
loss.backward()
opt.step()
if (i % 1000 == 0):
print('Training at Epoch ' + str(epo + 1) + ' iteration ' + str(i) + ' with loss ' + str(
loss.cpu().detach().numpy()))
# every epoch test
with torch.set_grad_enabled(False):
auc, auprc, f1, logits, loss = test(validation_generator, model)
if auc > max_auc:
model_max = copy.deepcopy(model)
max_auc = auc
print('Validation at Epoch ' + str(epo + 1) + ' , AUROC: ' + str(auc) + ' , AUPRC: ' + str(
auprc) + ' , F1: ' + str(f1))
print('--- Go for Testing ---')
try:
with torch.set_grad_enabled(False):
auc, auprc, f1, logits, loss = test(testing_generator, model_max)
print(
'Testing AUROC: ' + str(auc) + ' , AUPRC: ' + str(auprc) + ' , F1: ' + str(f1) + ' , Test loss: ' + str(
loss))
except:
print('testing failed')
return model_max, loss_history
s = time()
model_max, loss_history = main()
e = time()
print(e - s)