forked from robinlingwood/BIMODAL
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtrainer.py
More file actions
390 lines (296 loc) · 17.2 KB
/
Copy pathtrainer.py
File metadata and controls
390 lines (296 loc) · 17.2 KB
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
"""
Implementation of different training methods
"""
import numpy as np
from sklearn.model_selection import KFold, train_test_split, ShuffleSplit
import pandas as pd
import configparser
from fb_rnn import FBRNN
from forward_rnn import ForwardRNN
from nade import NADE
from bimodal import BIMODAL
from one_hot_encoder import SMILESEncoder
from sklearn.utils import shuffle
import os
from helper import clean_molecule, check_model, check_molecules
np.random.seed(1)
class Trainer():
def __init__(self, experiment_name='ForwardRNN'):
self._encoder = SMILESEncoder()
# Read all parameter from the .ini file
self._config = configparser.ConfigParser()
self._config.read('../experiments/' + experiment_name + '.ini')
self._model_type = self._config['MODEL']['model']
self._experiment_name = experiment_name
self._hidden_units = int(self._config['MODEL']['hidden_units'])
self._file_name = '../data/' + self._config['DATA']['data']
self._encoding_size = int(self._config['DATA']['encoding_size'])
self._molecular_size = int(self._config['DATA']['molecular_size'])
self._epochs = int(self._config['TRAINING']['epochs'])
self._n_folds = int(self._config['TRAINING']['n_folds'])
self._learning_rate = float(self._config['TRAINING']['learning_rate'])
self._batch_size = int(self._config['TRAINING']['batch_size'])
self._samples = int(self._config['EVALUATION']['samples'])
self._T = float(self._config['EVALUATION']['temp'])
self._starting_token = self._encoder.encode([self._config['EVALUATION']['starting_token']])
if self._model_type == 'FBRNN':
self._model = FBRNN(self._molecular_size, self._encoding_size,
self._learning_rate, self._hidden_units)
elif self._model_type == 'ForwardRNN':
self._model = ForwardRNN(self._molecular_size, self._encoding_size,
self._learning_rate, self._hidden_units)
elif self._model_type == 'BIMODAL':
self._model = BIMODAL(self._molecular_size, self._encoding_size,
self._learning_rate, self._hidden_units)
elif self._model_type == 'NADE':
self._generation = self._config['MODEL']['generation']
self._missing_token = self._encoder.encode([self._config['TRAINING']['missing_token']])
self._model = NADE(self._molecular_size, self._encoding_size, self._learning_rate,
self._hidden_units, self._generation, self._missing_token)
self._data = self._encoder.encode_from_file(self._file_name)
def complete_run(self, stor_dir='../evaluation/', restart=False):
'''Training without validation on complete data'''
# Create directories
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/models'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/models')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/molecules'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/molecules')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/statistic'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/statistic')
# Compute labels
label = np.argmax(self._data, axis=-1).astype(int)
# Special preprocessing in the case of NADE
if self._model_type == 'NADE' and self._generation == 'random':
# First column stores correct SMILES and second column stores SMILES with missing values
label = np.argmax(self._data[:, 0], axis=-1).astype(int)
aug = self._data.shape[1] - 1
label = np.repeat(label[:, np.newaxis, :], aug, axis=1)
self._data = self._data[:, 1:]
# Build model
self._model.build()
# Store total Statistics
tot_stat = []
# only single fold
fold = 1
# Shuffle data before training (Data reshaped from (N_samples, N_augmentation, molecular_size, encoding_size)
# to (all_SMILES, molecular_size, encoding_size))
self._data, label = shuffle(self._data.reshape(-1, self._molecular_size, self._encoding_size),
label.reshape(-1, self._molecular_size))
for i in range(self._epochs):
print('Fold:', fold)
print('Epoch:', i)
# With restart read existing files
if restart:
tmp_stat_file = pd.read_csv(
stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(fold) + '.csv',
header=None).to_numpy()
# Check if current epoch is successfully completed else continue with normal training
if check_model(self._model_type, self._experiment_name, stor_dir, fold, i) and check_molecules(
self._experiment_name, stor_dir, fold, i) and tmp_stat_file.shape[0] > i:
# Load model
self._model.build(
stor_dir + '/' + self._experiment_name + '/models/model_fold_' + str(fold) + '_epochs_' + str(i))
# Fill statistic and loss list
tot_stat.append(tmp_stat_file[i, 1:].reshape(1, -1).tolist())
continue
# Continue with normal training
else:
restart = False
# Train model
statistic = self._model.train(self._data, label, epochs=1, batch_size=self._batch_size)
tot_stat.append(statistic.tolist())
# Store model
self._model.save(
stor_dir + '/' + self._experiment_name + '/models/model_fold_' + str(fold) + '_epochs_' + str(i))
# Sample new molecules
new_molecules = []
for s in range(self._samples):
mol = self._encoder.decode(self._model.sample(self._starting_token, self._T))
new_molecules.append(clean_molecule(mol[0], self._model_type))
# Store new molecules
new_molecules = np.array(new_molecules)
pd.DataFrame(new_molecules).to_csv(
stor_dir + '/' + self._experiment_name + '/molecules/molecule_fold_' + str(fold) + '_epochs_' + str(
i) + '.csv', header=None)
# Store statistic
store_stat = np.array(tot_stat).reshape(i + 1, -1)
pd.DataFrame(np.array(store_stat)).to_csv(
stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(fold) + '.csv',
header=None)
def single_run(self, stor_dir='../evaluation/', restart=False):
'''Training with validation and store data'''
# Create directories
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/models'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/models')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/molecules'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/molecules')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/statistic'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/statistic')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/validation'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/validation')
# Compute labels
label = np.argmax(self._data, axis=-1).astype(int)
# Special preprocessing in the case of NADE
if (self._model_type == 'NADE' or self._model_type == 'NADE_v2') and self._generation == 'random':
# First column stores correct SMILES and second column stores SMILES with missing values
label = np.argmax(self._data[:, 0], axis=-1).astype(int)
aug = self._data.shape[1] - 1
label = np.repeat(label[:, np.newaxis, :], aug, axis=1)
self._data = self._data[:, 1:]
# Split data into train and test data
train_data, test_data, train_label, test_label = train_test_split(self._data, label, test_size=1. / 5,
random_state=1, shuffle=True)
# Build model
self._model.build()
# Store total Statistics
tot_stat = []
# Store validation loss
tot_loss = []
# only single fold
fold = 1
for i in range(self._epochs):
print('Fold:', fold)
print('Epoch:', i)
if restart:
# Read existing files
tmp_val_file = pd.read_csv(
stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(fold) + '.csv',
header=None).to_numpy()
tmp_stat_file = pd.read_csv(
stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(fold) + '.csv',
header=None).to_numpy()
# Check if current epoch is successfully completed else continue with normal training
if check_model(self._model_type, self._experiment_name, stor_dir, fold, i) and check_molecules(
self._experiment_name, stor_dir, fold, i) and tmp_val_file.shape[0] > i and tmp_stat_file.shape[
0] > i:
# Load model
self._model.build(
stor_dir + '/' + self._experiment_name + '/models/model_fold_' + str(fold) + '_epochs_' + str(i))
# Fill statistic and loss list
tot_stat.append(tmp_stat_file[i, 1:].reshape(1, -1).tolist())
tot_loss.append(tmp_val_file[i, 1])
# Skip this epoch
continue
# Continue with normal training
else:
restart = False
# Train model (Data reshaped from (N_samples, N_augmentation, molecular_size, encoding_size)
# to (all_SMILES, molecular_size, encoding_size))
statistic = self._model.train(train_data.reshape(-1, self._molecular_size, self._encoding_size),
train_label.reshape(-1, self._molecular_size), epochs=1,
batch_size=self._batch_size)
tot_stat.append(statistic.tolist())
# Store model
self._model.save(
stor_dir + '/' + self._experiment_name + '/models/model_fold_' + str(fold) + '_epochs_' + str(i))
# Test model on validation set
tot_loss.append(
self._model.validate(test_data.reshape(-1, self._molecular_size, self._encoding_size),
test_label.reshape(-1, self._molecular_size)))
# Sample new molecules
new_molecules = []
for s in range(self._samples):
mol = self._encoder.decode(self._model.sample(self._starting_token, self._T))
new_molecules.append(clean_molecule(mol[0], self._model_type))
# Store new molecules
new_molecules = np.array(new_molecules)
pd.DataFrame(new_molecules).to_csv(
stor_dir + '/' + self._experiment_name + '/molecules/molecule_fold_' + str(fold) + '_epochs_' + str(
i) + '.csv', header=None)
# Store statistic
store_stat = np.array(tot_stat).reshape(i + 1, -1)
pd.DataFrame(np.array(store_stat)).to_csv(
stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(fold) + '.csv',
header=None)
# Store validation data
pd.DataFrame(np.array(tot_loss).reshape(-1, 1)).to_csv(
stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(fold) + '.csv',
header=None)
def cross_validation(self, stor_dir='../evaluation/', restart=False):
'''Perform cross-validation and store data'''
# Create directories
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/models'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/models')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/molecules'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/molecules')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/statistic'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/statistic')
if not os.path.exists(stor_dir + '/' + self._experiment_name + '/validation'):
os.makedirs(stor_dir + '/' + self._experiment_name + '/validation')
self._kf = KFold(n_splits=self._n_folds, shuffle=True, random_state=2)
# Count iterations
fold = 0
# Compute labels
label = np.argmax(self._data, axis=-1).astype(int)
# Special preprocessing in the case of NADE
if (self._model_type == 'NADE') and self._generation == 'random':
# First column stores correct SMILES and second column stores SMILES with missing values
label = np.argmax(self._data[:, 0], axis=-1).astype(int)
aug = self._data.shape[1] - 1
label = np.repeat(label[:, np.newaxis, :], aug, axis=1)
self._data = self._data[:, 1:]
# Split data into train and test data
for train, test in self._kf.split(self._data):
# Shuffle index within test and train set
np.random.shuffle(train)
np.random.shuffle(test)
fold += 1
self._model.build()
# Store total statistics
tot_stat = []
# Store validation loss
tot_loss = []
for i in range(self._epochs):
print('Fold:', fold)
print('Epoch:', i)
if restart:
tmp_val_file = pd.read_csv(
stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(fold) + '.csv',
header=None).to_numpy()
tmp_stat_file = pd.read_csv(
stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(fold) + '.csv',
header=None).to_numpy()
# Check if current epoch is successfully complete[0]d else continue with normal training
if check_model(self._model_type, self._experiment_name, stor_dir, fold, i) and check_molecules(
self._experiment_name, stor_dir, fold, i) and tmp_val_file.shape[0] > i and tmp_stat_file.shape[
0] > i:
# Load model
self._model.build(
stor_dir + '/' + self._experiment_name + '/models/model_fold_' + str(fold) + '_epochs_' + str(i))
# Fill statistic and loss list
tot_stat.append(tmp_stat_file[i, 1:].reshape(1, -1).tolist())
tot_loss.append(tmp_val_file[i, 1])
# Skip this epoch
continue
else:
restart = False
# Train model (Data reshaped from (N_samples, N_augmentation, molecular_size, encoding_size)
# to (all_SMILES, molecular_size, encoding_size))
statistic = self._model.train(
self._data[train].reshape(-1, self._molecular_size, self._encoding_size),
label[train].reshape(-1, self._molecular_size), epochs=1, batch_size=self._batch_size)
tot_stat.append(statistic.tolist())
# Store model
self._model.save(
stor_dir + '/' + self._experiment_name + '/models/model_fold_' + str(fold) + '_epochs_' + str(i))
# Test model on validation set
tot_loss.append(
self._model.validate(self._data[test].reshape(-1, self._molecular_size, self._encoding_size),
label[test].reshape(-1, self._molecular_size)))
# Sample new molecules
new_molecules = []
for s in range(self._samples):
mol = self._encoder.decode(self._model.sample(self._starting_token, self._T))
new_molecules.append(clean_molecule(mol[0], self._model_type))
# Store new molecules
new_molecules = np.array(new_molecules)
pd.DataFrame(new_molecules).to_csv(
stor_dir + '/' + self._experiment_name + '/molecules/molecule_fold_' + str(fold) + '_epochs_' + str(
i) + '.csv', header=None)
# Store statistic
store_stat = np.array(tot_stat).reshape(i + 1, -1)
pd.DataFrame(np.array(store_stat)).to_csv(
stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(fold) + '.csv', header=None)
# Store validation data
pd.DataFrame(np.array(tot_loss).reshape(-1, 1)).to_csv(
stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(fold) + '.csv', header=None)