-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
227 lines (199 loc) · 8.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import click as ck
import pandas as pd
import torch as th
import numpy as np
from torch import nn
from torch.nn import functional as F
from torch import optim
from torch.optim.lr_scheduler import MultiStepLR
from sklearn.metrics import roc_curve, auc, matthews_corrcoef
import copy
from torch.utils.data import DataLoader, IterableDataset, TensorDataset
from itertools import cycle
import math
from deepgo.torch_utils import FastTensorDataLoader
from deepgo.utils import Ontology, propagate_annots
from multiprocessing import Pool
from functools import partial
from deepgo.data import load_data, load_normal_forms
from deepgo.models import DeepGOModel
from deepgo.metrics import compute_roc
@ck.command()
@ck.option(
'--data-root', '-dr', default='data',
help='Data folder')
@ck.option(
'--ont', '-ont', default='mf', type=ck.Choice(['mf', 'bp', 'cc']),
help='GO subontology')
@ck.option(
'--model-name', '-m', type=ck.Choice([
'deepgozero', 'deepgozero_plus', 'deepgozero_esm', 'deepgozero_esm_plus']),
default='deepgozero_esm',
help='Prediction model name')
@ck.option(
'--model-id', '-mi', type=int, required=False)
@ck.option(
'--test-data-name', '-td', default='test', type=ck.Choice(['test', 'nextprot', 'valid']),
help='Test data set name')
@ck.option(
'--batch-size', '-bs', default=37,
help='Batch size for training')
@ck.option(
'--epochs', '-ep', default=128,
help='Training epochs')
@ck.option(
'--load', '-ld', is_flag=True, help='Load Model?')
@ck.option(
'--device', '-d', default='cuda:0',
help='Device')
def main(data_root, ont, model_name, model_id, test_data_name, batch_size, epochs, load, device):
"""
This script is used to train DeepGO models
"""
if model_id is not None:
model_name = f'{model_name}_{model_id}'
if model_name.find('plus') != -1:
go_norm_file = f'{data_root}/go-plus.norm'
else:
go_norm_file = f'{data_root}/go.norm'
go_file = f'{data_root}/go.obo'
model_file = f'{data_root}/{ont}/{model_name}.th'
terms_file = f'{data_root}/{ont}/terms.pkl'
out_file = f'{data_root}/{ont}/{test_data_name}_predictions_{model_name}.pkl'
# Load Gene Ontology and Normalized axioms
go = Ontology(go_file, with_rels=True)
# Load the datasets
if model_name.find('esm') != -1:
features_length = 2560
features_column = 'esm2'
else:
features_length = None # Optional in this case
features_column = 'interpros'
test_data_file = f'{test_data_name}_data.pkl'
iprs_dict, terms_dict, train_data, valid_data, test_data, test_df = load_data(
data_root, ont, terms_file, features_length, features_column, test_data_file)
n_terms = len(terms_dict)
if features_column == 'interpros':
features_length = len(iprs_dict)
train_features, train_labels = train_data
valid_features, valid_labels = valid_data
test_features, test_labels = test_data
valid_labels = valid_labels.detach().cpu().numpy()
test_labels = test_labels.detach().cpu().numpy()
# Load normal forms
nf1, nf2, nf3, nf4, relations, zero_classes = load_normal_forms(
go_norm_file, terms_dict)
n_rels = len(relations)
n_zeros = len(zero_classes)
normal_forms = nf1, nf2, nf3, nf4
nf1 = th.LongTensor(nf1).to(device)
nf2 = th.LongTensor(nf2).to(device)
nf3 = th.LongTensor(nf3).to(device)
nf4 = th.LongTensor(nf4).to(device)
normal_forms = nf1, nf2, nf3, nf4
# Create DataLoaders
train_loader = FastTensorDataLoader(
*train_data, batch_size=batch_size, shuffle=True)
valid_loader = FastTensorDataLoader(
*valid_data, batch_size=batch_size, shuffle=False)
test_loader = FastTensorDataLoader(
*test_data, batch_size=batch_size, shuffle=False)
loss_func = nn.BCELoss()
net = DeepGOModel(features_length, n_terms, n_zeros, n_rels, device).to(device)
print(net)
optimizer = th.optim.Adam(net.parameters(), lr=5e-4)
scheduler = MultiStepLR(optimizer, milestones=[5, 20], gamma=0.1)
best_loss = 10000.0
if not load:
print('Training the model')
for epoch in range(epochs):
net.train()
train_loss = 0
train_elloss = 0
train_steps = int(math.ceil(len(train_labels) / batch_size))
with ck.progressbar(length=train_steps, show_pos=True) as bar:
for batch_features, batch_labels in train_loader:
bar.update(1)
batch_features = batch_features.to(device)
batch_labels = batch_labels.to(device)
logits = net(batch_features)
loss = F.binary_cross_entropy(logits, batch_labels)
el_loss = net.el_loss(normal_forms)
total_loss = loss + el_loss
train_loss += loss.detach().item()
train_elloss = el_loss.detach().item()
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
train_loss /= train_steps
print('Validation')
net.eval()
with th.no_grad():
valid_steps = int(math.ceil(len(valid_labels) / batch_size))
valid_loss = 0
preds = []
with ck.progressbar(length=valid_steps, show_pos=True) as bar:
for batch_features, batch_labels in valid_loader:
bar.update(1)
batch_features = batch_features.to(device)
batch_labels = batch_labels.to(device)
logits = net(batch_features)
batch_loss = F.binary_cross_entropy(logits, batch_labels)
valid_loss += batch_loss.detach().item()
preds = np.append(preds, logits.detach().cpu().numpy())
valid_loss /= valid_steps
roc_auc = compute_roc(valid_labels, preds)
print(f'Epoch {epoch}: Loss - {train_loss}, EL Loss: {train_elloss}, Valid loss - {valid_loss}, AUC - {roc_auc}')
print('EL Loss', train_elloss)
if valid_loss < best_loss:
best_loss = valid_loss
print('Saving model')
th.save(net.state_dict(), model_file)
scheduler.step()
# Loading best model
print('Loading the best model')
net.load_state_dict(th.load(model_file))
net.eval()
with th.no_grad():
valid_steps = int(math.ceil(len(valid_labels) / batch_size))
valid_loss = 0
preds = []
with ck.progressbar(length=valid_steps, show_pos=True) as bar:
for batch_features, batch_labels in valid_loader:
bar.update(1)
batch_features = batch_features.to(device)
batch_labels = batch_labels.to(device)
logits = net(batch_features)
batch_loss = F.binary_cross_entropy(logits, batch_labels)
valid_loss += batch_loss.detach().item()
preds = np.append(preds, logits.detach().cpu().numpy())
valid_loss /= valid_steps
with th.no_grad():
test_steps = int(math.ceil(len(test_labels) / batch_size))
test_loss = 0
preds = []
with ck.progressbar(length=test_steps, show_pos=True) as bar:
for batch_features, batch_labels in test_loader:
bar.update(1)
batch_features = batch_features.to(device)
batch_labels = batch_labels.to(device)
logits = net(batch_features)
batch_loss = F.binary_cross_entropy(logits, batch_labels)
test_loss += batch_loss.detach().cpu().item()
preds.append(logits.detach().cpu().numpy())
test_loss /= test_steps
preds = np.concatenate(preds)
roc_auc = compute_roc(test_labels, preds)
print(f'Valid Loss - {valid_loss}, Test Loss - {test_loss}, Test AUC - {roc_auc}')
# Save the performance into a file
with open(f'{data_root}/{ont}/valid_{model_name}.pf', 'w') as f:
f.write(f'Valid Loss - {valid_loss}, Test Loss - {test_loss}, Test AUC - {roc_auc}\n')
# return
preds = list(preds)
# Propagate scores using ontology structure
with Pool(32) as p:
preds = p.map(partial(propagate_annots, go=go, terms_dict=terms_dict), preds)
test_df['preds'] = preds
test_df.to_pickle(out_file)
if __name__ == '__main__':
main()