forked from isakvals/AEV-PLIG
-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
173 lines (128 loc) · 6.36 KB
/
training.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
import torch
import random
import time
import torch.nn as nn
from torch_geometric.loader import DataLoader
from helpers import rmse, pearson, model_dict
from utils import GraphDataset, init_weights
import os
import pandas as pd
import argparse
import numpy as np
import pickle
def predict(model, device, loader, y_scaler=None):
model.eval()
total_preds = torch.Tensor()
total_labels = torch.Tensor()
print('Make prediction for {} samples...'.format(len(loader.dataset)))
with torch.no_grad():
for data in loader:
data = data.to(device)
output = model(data)
total_preds = torch.cat((total_preds, output.cpu()), 0)
total_labels = torch.cat((total_labels, data.y.view(-1, 1).cpu()), 0)
return y_scaler.inverse_transform(total_labels.numpy().flatten().reshape(-1,1)).flatten(), y_scaler.inverse_transform(total_preds.detach().numpy().flatten().reshape(-1,1)).flatten()
def train(model, device, train_loader, optimizer, epoch, loss_fn):
log_interval = 100
model.train()
total_loss = 0.0
for batch_idx, data in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, data.y.view(-1, 1).to(device))
loss.backward()
optimizer.step()
total_loss += (loss.item()*len(data.y))
if batch_idx % log_interval == 0:
print('Train epoch: {} [{}/{} ({:.0f}%)]'.format(epoch,
batch_idx * len(data.y),
len(train_loader.dataset),
100. * batch_idx / len(train_loader)))
print("Loss for epoch {}: {:.4f}".format(epoch, total_loss/len(train_loader.dataset)))
return total_loss/len(train_loader.dataset)
def _train(model, device, loss_fn, train_loader, valid_loader, optimizer, n_epochs, y_scaler, model_output_dir, model_file_name):
best_pc = -1.1
pcs = []
for epoch in range(n_epochs):
_ = train(model, device, train_loader, optimizer, epoch + 1, loss_fn)
G, P = predict(model, device, valid_loader, y_scaler)
current_pc = pearson(G, P)
pcs.append(current_pc)
low = np.maximum(epoch-7,0)
avg_pc = np.mean(pcs[low:epoch+1])
if(avg_pc > best_pc):
torch.save(model.state_dict(), os.path.join(model_output_dir, model_file_name))
best_pc = avg_pc
print('The current validation set Pearson correlation:', current_pc)
return
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='GATv2Net')
parser.add_argument('--dataset', type=str, default='pdbbind_U_bindingnet_ligsim90')
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--epochs', type=int, default=200)
parser.add_argument('--hidden_dim', type=int, default=256)
parser.add_argument('--head', type=int, default=3)
parser.add_argument('--lr', type=float, default=0.00012291937615434127)
parser.add_argument('--activation_function', type=str, default='leaky_relu')
args = parser.parse_args()
return args
def train_NN(args):
modeling = model_dict[args.model]
model_st = modeling.__name__
batch_size = args.batch_size
LR = args.lr
n_epochs = args.epochs
print('Train for {} epochs: '.format(n_epochs))
dataset = args.dataset
print('Running dataset {} on model {}.'.format(dataset, model_st))
timestr = time.strftime("%Y%m%d-%H%M%S")
model_output_dir = os.path.join("output", "trained_models")
train_data = GraphDataset(root='data', dataset=dataset+'_train', y_scaler=None)
valid_data = GraphDataset(root='data', dataset=dataset+'_valid', y_scaler=train_data.y_scaler)
test_data = GraphDataset(root='data', dataset=dataset+'_test', y_scaler=train_data.y_scaler)
seeds = [100, 123, 15, 257, 2, 2012, 3752, 350, 843, 621]
for i,seed in enumerate(seeds):
random.seed(seed)
torch.manual_seed(int(seed))
model_file_name = timestr + '_model_' + model_st + '_' + dataset + '_' + str(i) + '.model'
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
valid_loader = DataLoader(valid_data, batch_size=batch_size, shuffle=False)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False)
if(torch.cuda.is_available()):
print("GPU is available")
device = torch.device("cuda")
else:
device = torch.device("cpu")
print('Device state:', device)
model = modeling(node_feature_dim=train_data.num_node_features, edge_feature_dim=train_data.num_edge_features, config=args)
model.apply(init_weights)
print("The number of node features is ", train_data.num_node_features)
print("The number of edge features is ", train_data.num_edge_features)
weight_decay = 0
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LR, weight_decay=weight_decay)
model.to(device)
_train(model, device, loss_fn, train_loader, valid_loader, optimizer, n_epochs, train_data.y_scaler, model_output_dir, model_file_name)
model.load_state_dict(torch.load(os.path.join(model_output_dir, model_file_name)))
G_test, P_test = predict(model, device, test_loader, train_data.y_scaler)
if(i == 0):
df_test = pd.DataFrame(data=G_test, index=range(len(G_test)), columns=['truth'])
col = 'preds_' + str(i)
df_test[col] = P_test
df_test['preds'] = df_test.iloc[:,1:].mean(axis=1)
scaler_file = timestr + '_model_' + model_st + '_' + dataset + '.pickle'
with open(scaler_file,'wb') as f:
pickle.dump(train_data.y_scaler, f)
test_preds = np.array(df_test['preds'])
test_truth = np.array(df_test['truth'])
test_ens_pc = pearson(test_truth, test_preds)
test_ens_rmse = rmse(test_truth, test_preds)
print("Ensemble test PC:", test_ens_pc)
print("Ensemble test RMSE:", test_ens_rmse)
if __name__ == "__main__":
start_time = time.time()
args = parse_args()
train_NN(args)
print("Total time is %s seconds" % (time.time() - start_time))