forked from p-koo/deepomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit.py
322 lines (263 loc) · 12.2 KB
/
fit.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
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
import sys
import numpy as np
__all__ = [
"train_minibatch",
"train_decay_learning_rate",
"train_anneal_batch_size",
"train_anneal_learning_rate"
]
def train_minibatch(sess, nntrainer, data, batch_size=128, num_epochs=500,
patience=False, verbose=1, shuffle=True, save_all=False, save_epochs=False):
"""Train a model with mini-batch stochastic gradient descent.
Early stopping is applied if value
Monitor cross-validation data and test data as options.
Save all epochs as an option.
"""
for epoch in range(num_epochs):
if verbose >= 1:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
else:
if epoch % 10 == 0:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
# training set
nntrainer.train_epoch(sess, data['train'],
batch_size=batch_size,
verbose=verbose,
shuffle=shuffle)
# test current model with cross-validation data and store results
if save_all:
# save training metrics
nntrainer.test_model(sess, data['train'],
name="train",
batch_size=batch_size,
verbose=verbose)
# save test metrics
if 'test' in data.keys():
nntrainer.test_model(sess, data['test'],
name="test",
batch_size=batch_size,
verbose=verbose)
# test current model with cross-validation data and store results
if 'valid' in data.keys():
# save cross-validcation metrics
loss, _, _ = nntrainer.test_model(sess, data['valid'],
name="valid",
batch_size=batch_size,
verbose=verbose)
# save model
nntrainer.save_model(sess)
# check for early stopping
if patience:
status = nntrainer.early_stopping(loss, patience)
if not status:
break
# save model parameters for each epoch
if save_epochs:
nntrainer.save_model(sess, str(epoch))
def train_decay_learning_rate(sess, nntrainer, data, learning_rate=0.01, decay_rate=0.9, batch_size=128,
num_epochs=500, patience=10, verbose=1, shuffle=True, save_all=False, save_epochs=False):
"""Train a model with cross-validation data and test data"""
nntrainer.nnmodel.feed_dict['learning_rate'] = learning_rate
for epoch in range(num_epochs):
if verbose >= 1:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
else:
if epoch % 10 == 0:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
nntrainer.nnmodel.feed_dict['learning_rate'] *= decay_rate
nntrainer.update_feed_dict(nntrainer.nnmodel.placeholders, nntrainer.nnmodel.feed_dict)
# training set
nntrainer.train_epoch(sess, data['train'],
batch_size=batch_size,
verbose=verbose,
shuffle=shuffle)
# test current model with cross-validation data and store results
if save_all:
# save training metrics
nntrainer.test_model(sess, data['train'],
name="train",
batch_size=batch_size,
verbose=verbose)
# save test metrics
if 'test' in data.keys():
nntrainer.test_model(sess, data['test'],
name="test",
batch_size=batch_size,
verbose=verbose)
# test current model with cross-validation data and store results
if 'valid' in data.keys():
# save cross-validcation metrics
loss, _, _ = nntrainer.test_model(sess, data['valid'],
name="valid",
batch_size=batch_size,
verbose=verbose)
# save model
nntrainer.save_model(sess)
# check for early stopping
if patience:
status = nntrainer.early_stopping(loss, patience)
if not status:
break
# save model parameters for each epoch
if save_epochs:
nntrainer.save_model(sess, str(epoch))
def train_anneal_batch_size(sess, nntrainer, data, batch_schedule, num_epochs=500,
patience=10, verbose=1, shuffle=True, save_all=False, save_epochs=False):
"""Train a model with cross-validation data and test data
batch_schedule = { 0: 50,
20: 100,
40: 500,
55: 1000,
50: 1500,
65: 2000
}
"""
# train model
for epoch in range(num_epochs):
if verbose == 1:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
# change learning rate if on schedule
if epoch in batch_schedule.keys():
batch_size = batch_schedule[epoch]
# training set
nntrainer.train_epoch(sess, data['train'],
batch_size=batch_size,
verbose=verbose,
shuffle=shuffle)
# test current model with cross-validation data and store results
if save_all:
# save training metrics
nntrainer.test_model(sess, data['valid'],
name="valid",
batch_size=batch_size,
verbose=verbose)
# save test metrics
if 'test' in data.keys():
nntrainer.test_model(sess, data['test'],
name="test",
batch_size=batch_size,
verbose=verbose)
# test current model with cross-validation data and store results
if 'valid' in data.keys():
# save cross-validcation metrics
loss, _, _ = nntrainer.test_model(sess, data['valid'],
name="valid",
batch_size=batch_size,
verbose=verbose)
# save model
nntrainer.save_model(sess)
# check for early stopping
if patience:
status = nntrainer.early_stopping(loss, patience)
if not status:
break
# save model parameters for each epoch
if save_epochs:
nntrainer.save_model(sess, str(epoch))
def train_anneal_learning_rate(sess, nntrainer, data, learning_rate_schedule, batch_size=128,
num_epochs=500, patience=10, verbose=1, shuffle=True, save_all=False,
save_epochs=False):
"""Train a model with cross-validation data and test data
learning_rate_schedule = { 0: 0.001
2: 0.01,
5: 0.001,
15: 0.0001
}
"""
# train model
for epoch in range(num_epochs):
if verbose == 1:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
# change learning rate if on schedule
if epoch in learning_rate_schedule:
nntrainer.placeholders['learning_rate'] = np.float32(learning_rate_schedule[epoch])
# training set
nntrainer.train_epoch(sess, data['train'],
batch_size=batch_size,
verbose=verbose,
shuffle=shuffle)
if save_all:
# save training metrics
nntrainer.test_model(sess, data['train'],
name="train",
batch_size=batch_size,
verbose=verbose)
# save test metrics
if 'test' in data.keys():
nntrainer.test_model(sess, data['test'],
name="test",
batch_size=batch_size,
verbose=verbose)
# test current model with cross-validation data and store results
if 'valid' in data.keys():
# save cross-validcation metrics
loss, _, _ = nntrainer.test_model(sess, data['valid'],
name="valid",
batch_size=batch_size,
verbose=verbose)
# save model
nntrainer.save_model(sess)
# check for early stopping
if patience:
status = nntrainer.early_stopping(loss, patience)
if not status:
break
# save model parameters for each epoch
if save_epochs:
nntrainer.save_model(sess, str(epoch))
def train_kl_annealing(sess, nntrainer, data, annealing_rate=None, batch_size=128,
num_epochs=500, patience=10, verbose=1, shuffle=True, save_all=False,
save_epochs=False):
"""Train a model with cross-validation data and test data
learning_rate_schedule = { 0: 0.001
2: 0.01,
5: 0.001,
15: 0.0001
}
"""
# train model
for epoch in range(num_epochs):
if verbose >= 1:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
else:
if epoch % 10 == 0:
sys.stdout.write("\rEpoch %d out of %d \n" % (epoch + 1, num_epochs))
if annealing_rate:
weight = 1 - np.exp(-epoch * annealing_rate)
else:
weight = 1
nntrainer.update_feed_dict('KL_weight', weight)
# training set
nntrainer.train_epoch(sess, data['train'],
batch_size=batch_size,
verbose=verbose,
shuffle=shuffle)
if save_all:
# save training metrics
nntrainer.test_model(sess, data['train'],
name="train",
batch_size=batch_size,
verbose=verbose)
# save test metrics
if 'test' in data.keys():
nntrainer.test_model(sess, data['test'],
name="test",
batch_size=batch_size,
verbose=verbose)
# test current model with cross-validation data and store results
if 'valid' in data.keys():
# save cross-validcation metrics
loss, _, _ = nntrainer.test_model(sess, data['valid'],
name="valid",
batch_size=batch_size,
verbose=verbose)
# save model
nntrainer.save_model(sess)
# check for early stopping
if patience:
status = nntrainer.early_stopping(loss, patience)
if not status:
break
# save model parameters for each epoch
if save_epochs:
nntrainer.save_model(sess, str(epoch))