forked from tensorlayer/TensorLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial_mnist.py
570 lines (493 loc) · 26.4 KB
/
tutorial_mnist.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#! /usr/bin/python
# -*- coding: utf8 -*-
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import set_keep
import time
"""Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN.
This tutorial uses placeholder to control all keeping probabilities,
so we need to set the non-one probabilities during training, and set them to 1
during evaluating and testing.
$ Set keeping probabilities.
>>> feed_dict = {x: X_train_a, y_: y_train_a}
>>> feed_dict.update( network.all_drop )
$ Set all keeping probabilities to 1 for evaluating and testing.
>>> dp_dict = tl.utils.dict_to_one( network.all_drop )
>>> feed_dict = {x: X_train_a, y_: y_train_a}
>>> feed_dict.update(dp_dict)
Alternatively, if you don't want to use placeholder to control them, you can
build different inferences for training, evaluating and testing,
and all inferences share the same model parameters.
(see tutorial_ptb_lstm.py)
tensorflow (0.9.0)
"""
def main_test_layers(model='relu'):
X_train, y_train, X_val, y_val, X_test, y_test = \
tl.files.load_mnist_dataset(shape=(-1,784))
X_train = np.asarray(X_train, dtype=np.float32)
y_train = np.asarray(y_train, dtype=np.int32)
X_val = np.asarray(X_val, dtype=np.float32)
y_val = np.asarray(y_val, dtype=np.int32)
X_test = np.asarray(X_test, dtype=np.float32)
y_test = np.asarray(y_test, dtype=np.int32)
print('X_train.shape', X_train.shape)
print('y_train.shape', y_train.shape)
print('X_val.shape', X_val.shape)
print('y_val.shape', y_val.shape)
print('X_test.shape', X_test.shape)
print('y_test.shape', y_test.shape)
print('X %s y %s' % (X_test.dtype, y_test.dtype))
sess = tf.InteractiveSession()
# placeholder
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int32, shape=[None, ], name='y_')
if model == 'relu':
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
network = tl.layers.DenseLayer(network, n_units=800,
act = tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
network = tl.layers.DenseLayer(network, n_units=800,
act = tf.nn.relu, name='relu2')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
network = tl.layers.DenseLayer(network, n_units=10,
act = tl.activation.identity,
name='output_layer')
elif model == 'dropconnect':
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropconnectDenseLayer(network, keep = 0.8,
n_units=800, act = tf.nn.relu,
name='dropconnect_relu1')
network = tl.layers.DropconnectDenseLayer(network, keep = 0.5,
n_units=800, act = tf.nn.relu,
name='dropconnect_relu2')
network = tl.layers.DropconnectDenseLayer(network, keep = 0.5,
n_units=10,
act = tl.activation.identity,
name='output_layer')
# To print all attributes of a Layer.
# attrs = vars(network)
# print(', '.join("%s: %s\n" % item for item in attrs.items()))
#
# print(network.all_drop) # {'drop1': 0.8, 'drop2': 0.5, 'drop3': 0.5}
# print(drop1, drop2, drop3) # Tensor("Placeholder_2:0", dtype=float32) Tensor("Placeholder_3:0", dtype=float32) Tensor("Placeholder_4:0", dtype=float32)
# exit()
y = network.outputs
y_op = tf.argmax(tf.nn.softmax(y), 1)
cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
# Alternatively, you can use TensorLayer's function to compute cost:
# cost = tl.cost.cross_entropy(y, y_)
# You can add more penalty to the cost function as follow.
# cost = cost + tl.cost.maxnorm_regularizer(1.0)(network.all_params[0]) + tl.cost.maxnorm_regularizer(1.0)(network.all_params[2])
# cost = cost + tl.cost.lo_regularizer(0.0001)(network.all_params[0]) + tl.cost.lo_regularizer(0.0001)(network.all_params[2])
# cost = cost + tl.cost.maxnorm_o_regularizer(0.001)(network.all_params[0]) + tl.cost.maxnorm_o_regularizer(0.001)(network.all_params[2])
params = network.all_params
# train
n_epoch = 200
batch_size = 128
learning_rate = 0.0001
print_freq = 10
train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999,
epsilon=1e-08, use_locking=False).minimize(cost)
sess.run(tf.initialize_all_variables()) # initialize all variables
network.print_params()
network.print_layers()
print(' learning_rate: %f' % learning_rate)
print(' batch_size: %d' % batch_size)
for epoch in range(n_epoch):
start_time = time.time()
for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train,
batch_size, shuffle=True):
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( network.all_drop ) # enable all dropout/dropconnect/denoising layers
sess.run(train_op, feed_dict=feed_dict)
# The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:
# If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.
# If the key is a SparseTensor, the value should be a SparseTensorValue.
if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
feed_dict = {x: X_train, y_: y_train}
feed_dict.update(dp_dict)
print(" train loss: %f" % sess.run(cost, feed_dict=feed_dict))
dp_dict = tl.utils.dict_to_one( network.all_drop )
feed_dict = {x: X_val, y_: y_val}
feed_dict.update(dp_dict)
print(" val loss: %f" % sess.run(cost, feed_dict=feed_dict))
print(" val acc: %f" % np.mean(y_val ==
sess.run(y_op, feed_dict=feed_dict)))
try:
# You can visualize the weight of 1st hidden layer as follow.
tl.visualize.W(network.all_params[0].eval(), second=10,
saveable=True, shape=[28, 28],
name='w1_'+str(epoch+1), fig_idx=2012)
# You can also save the weight of 1st hidden layer to .npz file.
# tl.files.save_npz([network.all_params[0]] , name='w1'+str(epoch+1)+'.npz')
except:
raise Exception("You should change visualize_W(), if you want \
to save the feature images for different dataset")
print('Evaluation')
dp_dict = tl.utils.dict_to_one( network.all_drop )
feed_dict = {x: X_test, y_: y_test}
feed_dict.update(dp_dict)
print(" test loss: %f" % sess.run(cost, feed_dict=feed_dict))
print(" test acc: %f" % np.mean(y_test == sess.run(y_op,
feed_dict=feed_dict)))
# Add ops to save and restore all the variables, including variables for training.
# ref: https://www.tensorflow.org/versions/r0.8/how_tos/variables/index.html
saver = tf.train.Saver()
save_path = saver.save(sess, "model.ckpt")
print("Model saved in file: %s" % save_path)
# You can also save the parameters into .npz file.
tl.files.save_npz(network.all_params , name='model.npz')
# You can only save one parameter as follow.
# tl.files.save_npz([network.all_params[0]] , name='model.npz')
# Then, restore the parameters as follow.
# load_params = tl.files.load_npz(path='', name='model.npz')
# tl.files.assign_params(sess, load_params, network)
# In the end, close TensorFlow session.
sess.close()
def main_test_denoise_AE(model='relu'):
X_train, y_train, X_val, y_val, X_test, y_test = \
tl.files.load_mnist_dataset(shape=(-1,784))
X_train = np.asarray(X_train, dtype=np.float32)
y_train = np.asarray(y_train, dtype=np.int64)
X_val = np.asarray(X_val, dtype=np.float32)
y_val = np.asarray(y_val, dtype=np.int64)
X_test = np.asarray(X_test, dtype=np.float32)
y_test = np.asarray(y_test, dtype=np.int64)
print('X_train.shape', X_train.shape)
print('y_train.shape', y_train.shape)
print('X_val.shape', X_val.shape)
print('y_val.shape', y_val.shape)
print('X_test.shape', X_test.shape)
print('y_test.shape', y_test.shape)
print('X %s y %s' % (X_test.dtype, y_test.dtype))
sess = tf.InteractiveSession()
# placeholder
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')
print("Build Network")
if model == 'relu':
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropoutLayer(network, keep=0.5, name='denoising1') # if drop some inputs, it is denoise AE
network = tl.layers.DenseLayer(network, n_units=196,
act = tf.nn.relu, name='relu1')
recon_layer1 = tl.layers.ReconLayer(network, x_recon=x, n_units=784,
act = tf.nn.softplus, name='recon_layer1')
elif model == 'sigmoid':
# sigmoid - set keep to 1.0, if you want a vanilla Autoencoder
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropoutLayer(network, keep=0.5, name='denoising1')
network = tl.layers.DenseLayer(network, n_units=196,
act=tf.nn.sigmoid, name='sigmoid1')
recon_layer1 = tl.layers.ReconLayer(network, x_recon=x, n_units=784,
act=tf.nn.sigmoid, name='recon_layer1')
## ready to train
sess.run(tf.initialize_all_variables())
## print all params
print("All Network Params")
network.print_params()
## pretrain
print("Pre-train Layer 1")
recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val,
denoise_name='denoising1', n_epoch=200,
batch_size=128, print_freq=10, save=True,
save_name='w1pre_')
# You can also disable denoisong by setting denoise_name=None.
# recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val,
# denoise_name=None, n_epoch=500, batch_size=128,
# print_freq=10, save=True, save_name='w1pre_')
# Add ops to save and restore all the variables.
# ref: https://www.tensorflow.org/versions/r0.8/how_tos/variables/index.html
saver = tf.train.Saver()
# you may want to save the model
save_path = saver.save(sess, "model.ckpt")
print("Model saved in file: %s" % save_path)
sess.close()
def main_test_stacked_denoise_AE(model='relu'):
X_train, y_train, X_val, y_val, X_test, y_test = \
tl.files.load_mnist_dataset(shape=(-1,784))
X_train = np.asarray(X_train, dtype=np.float32)
y_train = np.asarray(y_train, dtype=np.int64)
X_val = np.asarray(X_val, dtype=np.float32)
y_val = np.asarray(y_val, dtype=np.int64)
X_test = np.asarray(X_test, dtype=np.float32)
y_test = np.asarray(y_test, dtype=np.int64)
print('X_train.shape', X_train.shape)
print('y_train.shape', y_train.shape)
print('X_val.shape', X_val.shape)
print('y_val.shape', y_val.shape)
print('X_test.shape', X_test.shape)
print('y_test.shape', y_test.shape)
print('X %s y %s' % (X_test.dtype, y_test.dtype))
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')
if model == 'relu':
act = tf.nn.relu
act_recon = tf.nn.softplus
elif model == 'sigmoid':
act = tf.nn.sigmoid
act_recon = act
# Define network
print("\nBuild Network")
network = tl.layers.InputLayer(x, name='input_layer')
# denoise layer for AE
network = tl.layers.DropoutLayer(network, keep=0.5, name='denoising1')
# 1st layer
network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
network = tl.layers.DenseLayer(network, n_units=800, act = act, name=model+'1')
x_recon1 = network.outputs
recon_layer1 = tl.layers.ReconLayer(network, x_recon=x, n_units=784,
act = act_recon, name='recon_layer1')
# 2nd layer
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
network = tl.layers.DenseLayer(network, n_units=800, act = act, name=model+'2')
recon_layer2 = tl.layers.ReconLayer(network, x_recon=x_recon1, n_units=800,
act = act_recon, name='recon_layer2')
# 3rd layer
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
network = tl.layers.DenseLayer(network, n_units=10,
act = tl.activation.identity,
name='output_layer')
# Define fine-tune process
y = network.outputs
y_op = tf.argmax(tf.nn.softmax(y), 1)
ce = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
cost = ce
n_epoch = 200
batch_size = 128
learning_rate = 0.0001
print_freq = 10
train_params = network.all_params
# train_op = tf.train.GradientDescentOptimizer(0.5).minimize(cost)
train_op = tf.train.AdamOptimizer(learning_rate , beta1=0.9, beta2=0.999,
epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)
# Initialize all variables including weights, biases and the variables in train_op
sess.run(tf.initialize_all_variables())
# Pre-train
print("\nAll Network Params before pre-train")
network.print_params()
print("\nPre-train Layer 1")
recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val,
denoise_name='denoising1', n_epoch=200,
batch_size=128, print_freq=10, save=True,
save_name='w1pre_')
print("\nPre-train Layer 2")
recon_layer2.pretrain(sess, x=x, X_train=X_train, X_val=X_val,
denoise_name='denoising1', n_epoch=200,
batch_size=128, print_freq=10, save=False)
print("\nAll Network Params after pre-train")
network.print_params()
# Fine-tune
print("\nFine-tune Network")
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(' learning_rate: %f' % learning_rate)
print(' batch_size: %d' % batch_size)
for epoch in range(n_epoch):
start_time = time.time()
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( network.all_drop ) # enable all dropout/dropconnect/denoising layers
feed_dict[set_keep['denoising1']] = 1 # disable denoising layer
sess.run(train_op, feed_dict=feed_dict)
if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
train_loss, train_acc, n_batch = 0, 0, 0
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
train_loss += err
train_acc += ac
n_batch += 1
print(" train loss: %f" % (train_loss/ n_batch))
print(" train acc: %f" % (train_acc/ n_batch))
val_loss, val_acc, n_batch = 0, 0, 0
for X_val_a, y_val_a in tl.iterate.minibatches(
X_val, y_val, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
feed_dict = {x: X_val_a, y_: y_val_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
val_loss += err
val_acc += ac
n_batch += 1
print(" val loss: %f" % (val_loss/ n_batch))
print(" val acc: %f" % (val_acc/ n_batch))
try:
# visualize the 1st hidden layer during fine-tune
tl.visualize.W(network.all_params[0].eval(), second=10,
saveable=True, shape=[28, 28],
name='w1_'+str(epoch+1), fig_idx=2012)
except:
raise Exception("# You should change visualize_W(), if you want to save the feature images for different dataset")
print('Evaluation')
test_loss, test_acc, n_batch = 0, 0, 0
for X_test_a, y_test_a in tl.iterate.minibatches(
X_test, y_test, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout layers
feed_dict = {x: X_test_a, y_: y_test_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
test_loss += err
test_acc += ac
n_batch += 1
print(" test loss: %f" % (test_loss/n_batch))
print(" test acc: %f" % (test_acc/n_batch))
# print(" test acc: %f" % np.mean(y_test == sess.run(y_op, feed_dict=feed_dict)))
# Add ops to save and restore all the variables.
# ref: https://www.tensorflow.org/versions/r0.8/how_tos/variables/index.html
saver = tf.train.Saver()
# you may want to save the model
save_path = saver.save(sess, "model.ckpt")
print("Model saved in file: %s" % save_path)
sess.close()
def main_test_cnn_layer():
"""Reimplementation of the TensorFlow official MNIST CNN tutorials:
# https://www.tensorflow.org/versions/r0.8/tutorials/mnist/pros/index.html
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/mnist/convolutional.py
More TensorFlow official CNN tutorials can be found here:
# tutorial_cifar10.py
# https://www.tensorflow.org/versions/master/tutorials/deep_cnn/index.html
"""
X_train, y_train, X_val, y_val, X_test, y_test = \
tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))
X_train = np.asarray(X_train, dtype=np.float32)
y_train = np.asarray(y_train, dtype=np.int64)
X_val = np.asarray(X_val, dtype=np.float32)
y_val = np.asarray(y_val, dtype=np.int64)
X_test = np.asarray(X_test, dtype=np.float32)
y_test = np.asarray(y_test, dtype=np.int64)
print('X_train.shape', X_train.shape)
print('y_train.shape', y_train.shape)
print('X_val.shape', X_val.shape)
print('y_val.shape', y_val.shape)
print('X_test.shape', X_test.shape)
print('y_test.shape', y_test.shape)
print('X %s y %s' % (X_test.dtype, y_test.dtype))
sess = tf.InteractiveSession()
# Define the batchsize at the begin, you can give the batchsize in x and y_
# rather than 'None', this can allow TensorFlow to apply some optimizations
# – especially for convolutional layers.
batch_size = 128
x = tf.placeholder(tf.float32, shape=[batch_size, 28, 28, 1]) # [batch_size, height, width, channels]
y_ = tf.placeholder(tf.int64, shape=[batch_size,])
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [5, 5, 1, 32], # 32 features for each 5x5 patch
strides=[1, 1, 1, 1],
padding='SAME',
name ='cnn_layer1') # output: (?, 28, 28, 32)
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool_layer1',) # output: (?, 14, 14, 32)
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [5, 5, 32, 64], # 64 features for each 5x5 patch
strides=[1, 1, 1, 1],
padding='SAME',
name ='cnn_layer2') # output: (?, 14, 14, 64)
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool_layer2',) # output: (?, 7, 7, 64)
network = tl.layers.FlattenLayer(network, name='flatten_layer') # output: (?, 3136)
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop1') # output: (?, 3136)
network = tl.layers.DenseLayer(network, n_units=256,
act = tf.nn.relu, name='relu1') # output: (?, 256)
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2') # output: (?, 256)
network = tl.layers.DenseLayer(network, n_units=10,
act = tl.activation.identity,
name='output_layer') # output: (?, 10)
y = network.outputs
ce = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
cost = ce
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# train
n_epoch = 200
learning_rate = 0.0001
print_freq = 10
train_params = network.all_params
train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999,
epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)
sess.run(tf.initialize_all_variables())
network.print_params()
network.print_layers()
print(' learning_rate: %f' % learning_rate)
print(' batch_size: %d' % batch_size)
for epoch in range(n_epoch):
start_time = time.time()
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( network.all_drop ) # enable all dropout/dropconnect/denoising layers
sess.run(train_op, feed_dict=feed_dict)
if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
train_loss, train_acc, n_batch = 0, 0, 0
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
train_loss += err; train_acc += ac; n_batch += 1
print(" train loss: %f" % (train_loss/ n_batch))
print(" train acc: %f" % (train_acc/ n_batch))
val_loss, val_acc, n_batch = 0, 0, 0
for X_val_a, y_val_a in tl.iterate.minibatches(
X_val, y_val, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
feed_dict = {x: X_val_a, y_: y_val_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
val_loss += err; val_acc += ac; n_batch += 1
print(" val loss: %f" % (val_loss/ n_batch))
print(" val acc: %f" % (val_acc/ n_batch))
try:
tl.visualize.CNN2d(network.all_params[0].eval(),
second=10, saveable=True,
name='cnn1_'+str(epoch+1), fig_idx=2012)
except:
raise Exception("# You should change visualize.CNN(), if you want to save the feature images for different dataset")
print('Evaluation')
test_loss, test_acc, n_batch = 0, 0, 0
for X_test_a, y_test_a in tl.iterate.minibatches(
X_test, y_test, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
feed_dict = {x: X_test_a, y_: y_test_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
test_loss += err; test_acc += ac; n_batch += 1
print(" test loss: %f" % (test_loss/n_batch))
print(" test acc: %f" % (test_acc/n_batch))
if __name__ == '__main__':
sess = tf.InteractiveSession()
sess = tl.ops.set_gpu_fraction(sess, gpu_fraction = 0.3)
try:
"""Dropout and Dropconnect"""
main_test_layers(model='relu') # model = relu, dropconnect
"""Single Denoising Autoencoder"""
# main_test_denoise_AE(model='sigmoid') # model = relu, sigmoid
"""Stacked Denoising Autoencoder"""
# main_test_stacked_denoise_AE(model='relu') # model = relu, sigmoid
"""CNN"""
# main_test_cnn_layer()
tl.ops.exit_tf(sess) # close sess, tensorboard and nvidia-process
except KeyboardInterrupt:
print('\nKeyboardInterrupt')
tl.ops.exit_tf(sess)