-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_graph_norm.py
163 lines (144 loc) · 5.92 KB
/
test_graph_norm.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
import cPickle as pickle
import sys
import numpy as np
import torch
import torch.cuda
from rdkit import Chem
from sklearn import metrics
from sklearn.model_selection import train_test_split
from torch import nn
from torch import optim
from torch.utils.data import DataLoader
from models.normed_basic_model import BasicModel
from models.graph_model_wrapper import GraphWrapper
from mol_graph import *
from mol_graph import GraphEncoder
from pre_process.data_loader import GraphDataSet, collate_2d_graphs, collate_2d_tensors
from pre_process.load_dataset import load_classification_dataset
import tqdm
def count_model_params(model):
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
return sum([np.prod(p.size()) for p in model_parameters])
def save_model(model, model_name, model_att, model_metrics):
# type: (nn.Module, dict) -> None
torch.save(model.state_dict(), 'basic_model' + str(model_name) + '.state_dict')
with open('basic_model_attributes.pickle', 'wb') as out_file:
pickle.dump(model_att, out_file)
with open('basic_model_' + str(model_name) + '_stats.pickle', 'wb') as out_file:
pickle.dump(model_metrics, out_file)
def test_model(model, dataset):
model.eval()
labels = []
true_labels = []
with torch.no_grad():
for batch in tqdm.tqdm(dataset):
labels = labels + model(batch).max(dim=-1)[1].cpu().data.numpy().tolist()
true_labels = true_labels + batch['labels'].cpu().data.numpy().tolist()
return (
metrics.accuracy_score(true_labels, labels),
metrics.precision_score(true_labels, labels, average='weighted'),
metrics.recall_score(true_labels, labels, average='weighted')
)
seed = 317
torch.manual_seed(seed)
data_file = sys.argv[1]
mgf = MolGraphFactory(Mol2DGraph.TYPE, AtomFeatures(), BondFeatures())
try:
file_data = np.load(data_file+'.npz')
data = file_data['data']
for graph in data:
graph.mask = np.ones(graph.afm.shape[0], dtype=np.float32).reshape(graph.afm.shape[0], 1)
graph.afm = graph.afm.astype(np.float32)
graph.bfm = graph.bfm.astype(np.float32)
graph.adj = graph.adj.astype(np.float32)
graph.label = long(graph.label)
no_labels = int(file_data['no_labels'])
all_labels = file_data['all_labels']
file_data.close()
except IOError:
data, no_labels, all_labels = load_classification_dataset(data_file+'.csv',
'InChI', Chem.MolFromInchi, mgf, 'target')
graph_encoder = GraphEncoder()
with open('basic_model_graph_encoder.pickle', 'wb') as out:
pickle.dump(graph_encoder, out)
np.savez_compressed(data_file, data=data, no_labels=no_labels, all_labels=all_labels)
model_attributes = {
'afm': data[0].afm.shape[-1],
'bfm': data[0].bfm.shape[-1],
'mfm': 2*data[0].afm.shape[-1],
'adj': data[0].adj.shape[-1],
'out': 4*data[0].afm.shape[-1],
'classification_output': no_labels
}
model = nn.Sequential(
GraphWrapper(BasicModel(model_attributes['afm'], model_attributes['bfm'], model_attributes['mfm'],
model_attributes['adj'], model_attributes['out'])),
nn.BatchNorm1d(model_attributes['out']),
nn.Linear(model_attributes['out'], model_attributes['classification_output'])
)
model.float() # convert to half precision
# for layer in model.modules():
# if isinstance(layer, nn.BatchNorm1d):
# layer.float()
print "Model has: {} parameters".format(count_model_params(model))
if torch.cuda.is_available():
model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
model.train()
train, test, train_labels, test_labels = train_test_split(data, all_labels, test_size=0.1,
random_state=seed, stratify=all_labels)
del data
del all_labels
del test_labels
train, val = train_test_split(train, test_size=0.1, random_state=seed, stratify=train_labels)
del train_labels
train = GraphDataSet(train)
val = GraphDataSet(val)
test = GraphDataSet(test)
train = DataLoader(train, 16, shuffle=True, collate_fn=collate_2d_graphs)
val = DataLoader(val, 16, shuffle=True, collate_fn=collate_2d_graphs)
test = DataLoader(test, 16, shuffle=True, collate_fn=collate_2d_graphs)
losses = []
epoch_losses = []
break_con = False
for epoch in tqdm.trange(500):
model.train()
epoch_loss = 0
for batch in tqdm.tqdm(train):
model.zero_grad()
loss = criterion(model(batch), batch['labels'])
losses.append(loss.item())
epoch_loss += loss.item()
loss.backward()
optimizer.step()
acc, pre, rec = test_model(model, val)
f1 = 2 * (pre * rec) / (pre + rec)
tqdm.tqdm.write(
"epoch {} training loss: {}, validation acc: {}, pre: {}, rec: {}, F1: {}".format(epoch, epoch_loss, acc,
pre, rec, f1))
if not np.isnan(f1) and f1 > 0.78:
save_model(model, 'epoch_'+str(epoch), model_attributes, {'acc': acc, 'pre': pre, 'rec': rec, 'f1': f1})
# epoch_losses.append(epoch_loss)
# if 0 == (epoch+1) % 50:
# print "epoch: {}, loss: {}".format(epoch, epoch_loss)
# break_con = loss.item() < 0.02
# if break_con:
# break
acc, pre, rec = test_model(model, test)
f1 = 2 * (pre * rec) / (pre + rec)
tqdm.tqdm.write(
"Testing acc: {}, pre: {}, rec: {}, F1: {}".format(epoch, epoch_loss, acc, pre, rec, f1))
# save_model(model, model_attributes)
# model.eval()
# labels = []
# true_labels = []
# for batch in val:
# labels = labels + model(batch).max(dim=-1)[1].cpu().data.numpy().tolist()
# true_labels = true_labels + batch['labels'].cpu().data.numpy().tolist()
#
# print "accuracy: {}, precision: {}, recall: {}".format(
# metrics.accuracy_score(true_labels, labels),
# metrics.precision_score(true_labels, labels, average='micro'),
# metrics.recall_score(true_labels, labels, average='micro')
# )