-
Notifications
You must be signed in to change notification settings - Fork 25
/
nn.py
executable file
·547 lines (425 loc) · 17.9 KB
/
nn.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Autoencoders training and fine-tuning.
Usage:
nn.py [--whole] [--male] [--threshold] [--leave-site-out] [<derivative> ...]
nn.py (-h | --help)
Options:
-h --help Show this screen
--whole Run model for the whole dataset
--male Run model for male subjects
--threshold Run model for thresholded subjects
--leave-site-out Prepare data using leave-site-out method
derivative Derivatives to process
"""
import os
import numpy as np
import tensorflow as tf
# import tensorflow.compat.v1 as tf
from docopt import docopt
from utils import (load_phenotypes, format_config, hdf5_handler, load_fold,
sparsity_penalty, reset, to_softmax, load_ae_encoder)
from model import ae, nn
def run_autoencoder1(experiment,
X_train, y_train, X_valid, y_valid, X_test, y_test,
model_path, code_size=1000):
"""
Run the first autoencoder.
It takes the original data dimensionality and compresses it into `code_size`
"""
# Hyperparameters
learning_rate = 0.0001
sparse = True # Add sparsity penalty
sparse_p = 0.2
sparse_coeff = 0.5
corruption = 0.7 # Data corruption ratio for denoising
ae_enc = tf.nn.tanh # Tangent hyperbolic
ae_dec = None # Linear activation
training_iters = 700
batch_size = 100
n_classes = 2
if os.path.isfile(model_path) or \
os.path.isfile(model_path + ".meta"):
return
# tf.disable_v2_behavior()
# Create model and add sparsity penalty (if requested)
model = ae(X_train.shape[1], code_size, corruption=corruption, enc=ae_enc, dec=ae_dec)
if sparse:
model["cost"] += sparsity_penalty(model["encode"], sparse_p, sparse_coeff)
# Use GD for optimization of model cost
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(model["cost"])
# Initialize Tensorflow session
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# Define model saver
saver = tf.train.Saver(model["params"], write_version=tf.train.SaverDef.V2)
# Initialize with an absurd cost for model selection
prev_costs = np.array([9999999999] * 3)
for epoch in range(training_iters):
# Break training set into batches
batches = range(int(len(X_train) / batch_size))
costs = np.zeros((len(batches), 3))
for ib in batches:
# Compute start and end of batch from training set data array
from_i = ib * batch_size
to_i = (ib + 1) * batch_size
# Select current batch
batch_xs, batch_ys = X_train[from_i:to_i], y_train[from_i:to_i]
# Run optimization and retrieve training cost
_, cost_train = sess.run(
[optimizer, model["cost"]],
feed_dict={
model["input"]: batch_xs
}
)
# Compute validation cost
cost_valid = sess.run(
model["cost"],
feed_dict={
model["input"]: X_valid
}
)
# Compute test cost
cost_test = sess.run(
model["cost"],
feed_dict={
model["input"]: X_test
}
)
costs[ib] = [cost_train, cost_valid, cost_test]
# Compute the average costs from all batches
costs = costs.mean(axis=0)
cost_train, cost_valid, cost_test = costs
# Pretty print training info
print (
"Exp={experiment}, Model= ae1, Iter={epoch:5d}, Cost={cost_train:.6f} {cost_valid:.6f} {cost_test:.6f}",
{
"experiment": experiment,
"epoch": epoch,
"cost_train": cost_train,
"cost_valid": cost_valid,
"cost_test": cost_test,
}
)
# Save better model if optimization achieves a lower cost
if cost_valid < prev_costs[1]:
print ("Saving better model")
saver.save(sess, model_path)
prev_costs = costs
else:
print
def run_autoencoder2(experiment,
X_train, y_train, X_valid, y_valid, X_test, y_test,
model_path, prev_model_path,
code_size=600, prev_code_size=1000):
"""
Run the second autoencoder.
It takes the dimensionality from first autoencoder and compresses it into the new `code_size`
Firstly, we need to convert original data to the new projection from autoencoder 1.
"""
if os.path.isfile(model_path) or \
os.path.isfile(model_path + ".meta"):
return
# tf.disable_v2_behavior()
# Convert training, validation and test set to the new representation
prev_model = ae(X_train.shape[1], prev_code_size,
corruption=0.0, # Disable corruption for conversion
enc=tf.nn.tanh, dec=None)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
saver = tf.train.Saver(prev_model["params"], write_version=tf.train.SaverDef.V2)
if os.path.isfile(prev_model_path):
saver.restore(sess, prev_model_path)
X_train = sess.run(prev_model["encode"], feed_dict={prev_model["input"]: X_train})
X_valid = sess.run(prev_model["encode"], feed_dict={prev_model["input"]: X_valid})
X_test = sess.run(prev_model["encode"], feed_dict={prev_model["input"]: X_test})
del prev_model
reset()
# Hyperparameters
learning_rate = 0.0001
corruption = 0.9
ae_enc = tf.nn.tanh
ae_dec = None
training_iters = 2000
batch_size = 10
n_classes = 2
# Load model
model = ae(prev_code_size, code_size, corruption=corruption, enc=ae_enc, dec=ae_dec)
# Use GD for optimization of model cost
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(model["cost"])
# Initialize Tensorflow session
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# Define model saver
saver = tf.train.Saver(model["params"], write_version=tf.train.SaverDef.V2)
# Initialize with an absurd cost for model selection
prev_costs = np.array([9999999999] * 3)
# Iterate Epochs
for epoch in range(training_iters):
# Break training set into batches
batches = range(int(len(X_train) / batch_size))
costs = np.zeros((len(batches), 3))
for ib in batches:
# Compute start and end of batch from training set data array
from_i = ib * batch_size
to_i = (ib + 1) * batch_size
# Select current batch
batch_xs, batch_ys = X_train[from_i:to_i], y_train[from_i:to_i]
# Run optimization and retrieve training cost
_, cost_train = sess.run(
[optimizer, model["cost"]],
feed_dict={
model["input"]: batch_xs
}
)
# Compute validation cost
cost_valid = sess.run(
model["cost"],
feed_dict={
model["input"]: X_valid
}
)
# Compute test cost
cost_test = sess.run(
model["cost"],
feed_dict={
model["input"]: X_test
}
)
costs[ib] = [cost_train, cost_valid, cost_test]
# Compute the average costs from all batches
costs = costs.mean(axis=0)
cost_train, cost_valid, cost_test = costs
# Pretty print training info
print (
"Exp={experiment}, Model=ae2, Iter={epoch:5d}, Cost={cost_train:.6f} {cost_valid:.6f} {cost_test:.6f}",
{
"experiment": experiment,
"epoch": epoch,
"cost_train": cost_train,
"cost_valid": cost_valid,
"cost_test": cost_test,
}
)
# Save better model if optimization achieves a lower cost
if cost_valid < prev_costs[1]:
print ("Saving better model")
saver.save(sess, model_path)
prev_costs = costs
else:
print
def run_finetuning(experiment,
X_train, y_train, X_valid, y_valid, X_test, y_test,
model_path, prev_model_1_path, prev_model_2_path,
code_size_1=1000, code_size_2=600):
"""
Run the pre-trained NN for fine-tuning, using first and second autoencoders' weights
"""
# Hyperparameters
learning_rate = 0.0005
dropout_1 = 0.6
dropout_2 = 0.8
initial_momentum = 0.1
final_momentum = 0.9 # Increase momentum along epochs to avoid fluctiations
saturate_momentum = 100
training_iters = 100
start_saving_at = 20
batch_size = 10
n_classes = 2
if os.path.isfile(model_path) or \
os.path.isfile(model_path + ".meta"):
return
# Convert output to one-hot encoding
y_train = np.array([to_softmax(n_classes, y) for y in y_train])
y_valid = np.array([to_softmax(n_classes, y) for y in y_valid])
y_test = np.array([to_softmax(n_classes, y) for y in y_test])
# Load pretrained encoder weights
ae1 = load_ae_encoder(X_train.shape[1], code_size_1, prev_model_1_path)
ae2 = load_ae_encoder(code_size_1, code_size_2, prev_model_2_path)
# Initialize NN model with the encoder weights
model = nn(X_train.shape[1], n_classes, [
{"size": code_size_1, "actv": tf.nn.tanh},
{"size": code_size_2, "actv": tf.nn.tanh},
], [
{"W": ae1["W_enc"], "b": ae1["b_enc"]},
{"W": ae2["W_enc"], "b": ae2["b_enc"]},
])
# Place GD + momentum optimizer
model["momentum"] = tf.placeholder("float32")
optimizer = tf.train.MomentumOptimizer(learning_rate, model["momentum"]).minimize(model["cost"])
# Compute accuracies
correct_prediction = tf.equal(
tf.argmax(model["output"], 1),
tf.argmax(model["expected"], 1)
)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# Initialize Tensorflow session
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# Define model saver
saver = tf.train.Saver(model["params"], write_version=tf.train.SaverDef.V2)
# Initialize with an absurd cost and accuracy for model selection
prev_costs = np.array([9999999999] * 3)
prev_accs = np.array([0.0] * 3)
# Iterate Epochs
for epoch in range(training_iters):
# Break training set into batches
batches = range(int(len(X_train) / batch_size))
costs = np.zeros((len(batches), 3))
accs = np.zeros((len(batches), 3))
# Compute momentum saturation
alpha = float(epoch) / float(saturate_momentum)
if alpha < 0.:
alpha = 0.
if alpha > 1.:
alpha = 1.
momentum = initial_momentum * (1 - alpha) + alpha * final_momentum
for ib in batches:
# Compute start and end of batch from training set data array
from_i = ib * batch_size
to_i = (ib + 1) * batch_size
# Select current batch
batch_xs, batch_ys = X_train[from_i:to_i], y_train[from_i:to_i]
# Run optimization and retrieve training cost and accuracy
_, cost_train, acc_train = sess.run(
[optimizer, model["cost"], accuracy],
feed_dict={
model["input"]: batch_xs,
model["expected"]: batch_ys,
model["dropouts"][0]: dropout_1,
model["dropouts"][1]: dropout_2,
model["momentum"]: momentum,
}
)
# Compute validation cost and accuracy
cost_valid, acc_valid = sess.run(
[model["cost"], accuracy],
feed_dict={
model["input"]: X_valid,
model["expected"]: y_valid,
model["dropouts"][0]: 1.0,
model["dropouts"][1]: 1.0,
}
)
# Compute test cost and accuracy
cost_test, acc_test = sess.run(
[model["cost"], accuracy],
feed_dict={
model["input"]: X_test,
model["expected"]: y_test,
model["dropouts"][0]: 1.0,
model["dropouts"][1]: 1.0,
}
)
costs[ib] = [cost_train, cost_valid, cost_test]
accs[ib] = [acc_train, acc_valid, acc_test]
# Compute the average costs from all batches
costs = costs.mean(axis=0)
cost_train, cost_valid, cost_test = costs
# Compute the average accuracy from all batches
accs = accs.mean(axis=0)
acc_train, acc_valid, acc_test = accs
# Pretty print training info
print (
"Exp={experiment}, Model=mlp, Iter={epoch:5d}, Acc={acc_train:.6f} {acc_valid:.6f} {acc_test:.6f}, Momentum={momentum:.6f}",
{
"experiment": experiment,
"epoch": epoch,
"acc_train": acc_train,
"acc_valid": acc_valid,
"acc_test": acc_test,
"momentum": momentum,
}
)
# Save better model if optimization achieves a lower accuracy
# and avoid initial epochs because of the fluctuations
if acc_valid > prev_accs[1] and epoch > start_saving_at:
print ("Saving better model")
saver.save(sess, model_path)
prev_accs = accs
prev_costs = costs
else:
print
def run_nn(hdf5, experiment, code_size_1, code_size_2):
# tf.disable_v2_behavior()
exp_storage = hdf5["experiments"][experiment]
for fold in exp_storage:
experiment_cv = format_config("{experiment}_{fold}", {
"experiment": experiment,
"fold": fold,
})
X_train, y_train, \
X_valid, y_valid, \
X_test, y_test = load_fold(hdf5["patients"], exp_storage, fold)
ae1_model_path = format_config("./data/models/{experiment}_autoencoder-1.ckpt", {
"experiment": experiment_cv,
})
ae2_model_path = format_config("./data/models/{experiment}_autoencoder-2.ckpt", {
"experiment": experiment_cv,
})
nn_model_path = format_config("./data/models/{experiment}_mlp.ckpt", {
"experiment": experiment_cv,
})
reset()
# Run first autoencoder
run_autoencoder1(experiment_cv,
X_train, y_train, X_valid, y_valid, X_test, y_test,
model_path=ae1_model_path,
code_size=code_size_1)
reset()
# Run second autoencoder
run_autoencoder2(experiment_cv,
X_train, y_train, X_valid, y_valid, X_test, y_test,
model_path=ae2_model_path,
prev_model_path=ae1_model_path,
prev_code_size=code_size_1,
code_size=code_size_2)
reset()
# Run multilayer NN with pre-trained autoencoders
run_finetuning(experiment_cv,
X_train, y_train, X_valid, y_valid, X_test, y_test,
model_path=nn_model_path,
prev_model_1_path=ae1_model_path,
prev_model_2_path=ae2_model_path,
code_size_1=code_size_1,
code_size_2=code_size_2)
if __name__ == "__main__":
reset()
arguments = docopt(__doc__)
pheno_path = "./data/phenotypes/Phenotypic_V1_0b_preprocessed1.csv"
pheno = load_phenotypes(pheno_path)
# hdf5 = hdf5_handler("./data/abide.hdf5", "a")
hdf5 = hdf5_handler(bytes("./data/abide.hdf5",encoding="utf8"), 'a')
valid_derivatives = ["cc200", "aal", "ez", "ho", "tt", "dosenbach160"]
derivatives = [derivative for derivative
in arguments["<derivative>"]
if derivative in valid_derivatives]
experiments = []
for derivative in derivatives:
config = {"derivative": derivative}
if arguments["--whole"]:
experiments += [format_config("{derivative}_whole", config)],
if arguments["--male"]:
experiments += [format_config("{derivative}_male", config)]
if arguments["--threshold"]:
experiments += [format_config("{derivative}_threshold", config)]
if arguments["--leave-site-out"]:
for site in pheno["SITE_ID"].unique():
site_config = {"site": site}
experiments += [
format_config("{derivative}_leavesiteout-{site}",
config, site_config)
]
# First autoencoder bottleneck
code_size_1 = 1000
# Second autoencoder bottleneck
code_size_2 = 600
experiments = sorted(experiments)
for experiment in experiments:
# print(experiment)
run_nn(hdf5, experiment[0], code_size_1, code_size_2)