forked from tensorlayer/TensorLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_mnist.py
More file actions
488 lines (412 loc) · 22.1 KB
/
Copy pathtutorial_mnist.py
File metadata and controls
488 lines (412 loc) · 22.1 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
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN.
- Multi-layer perceptron (MNIST) - Classification task, see tutorial_mnist_simple.py
https://github.com/zsdonghao/tensorlayer/blob/master/example/tutorial_mnist_simple.py
- Multi-layer perceptron (MNIST) - Classification using Iterator, see:
method1 : https://github.com/zsdonghao/tensorlayer/blob/master/example/tutorial_mlp_dropout1.py
method2 : https://github.com/zsdonghao/tensorlayer/blob/master/example/tutorial_mlp_dropout2.py
"""
import time
import tensorflow as tf
import tensorlayer as tl
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))
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_')
# Note: the softmax is implemented internally in tl.cost.cross_entropy(y, y_)
# to speed up computation, so we use identity in the last layer.
# see tf.nn.sparse_softmax_cross_entropy_with_logits()
if model == 'relu':
net = tl.layers.InputLayer(x, name='input')
net = tl.layers.DropoutLayer(net, keep=0.8, name='drop1')
net = tl.layers.DenseLayer(net, n_units=800, act=tf.nn.relu, name='relu1')
net = tl.layers.DropoutLayer(net, keep=0.5, name='drop2')
net = tl.layers.DenseLayer(net, n_units=800, act=tf.nn.relu, name='relu2')
net = tl.layers.DropoutLayer(net, keep=0.5, name='drop3')
net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, name='output')
elif model == 'dropconnect':
net = tl.layers.InputLayer(x, name='input')
net = tl.layers.DropconnectDenseLayer(net, keep=0.8, n_units=800, act=tf.nn.relu, name='dropconnect1')
net = tl.layers.DropconnectDenseLayer(net, keep=0.5, n_units=800, act=tf.nn.relu, name='dropconnect2')
net = tl.layers.DropconnectDenseLayer(net, keep=0.5, n_units=10, act=tf.identity, name='output')
# To print all attributes of a Layer.
# attrs = vars(net)
# print(', '.join("%s: %s\n" % item for item in attrs.items()))
# print(net.all_drop) # {'drop1': 0.8, 'drop2': 0.5, 'drop3': 0.5}
y = net.outputs
cost = tl.cost.cross_entropy(y, y_, name='xentropy')
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# y_op = tf.argmax(tf.nn.softmax(y), 1)
# You can add more penalty to the cost function as follow.
# cost = cost + tl.cost.maxnorm_regularizer(1.0)(net.all_params[0]) + tl.cost.maxnorm_regularizer(1.0)(net.all_params[2])
# cost = cost + tl.cost.lo_regularizer(0.0001)(net.all_params[0]) + tl.cost.lo_regularizer(0.0001)(net.all_params[2])
# cost = cost + tl.cost.maxnorm_o_regularizer(0.001)(net.all_params[0]) + tl.cost.maxnorm_o_regularizer(0.001)(net.all_params[2])
# train
n_epoch = 100
batch_size = 128
learning_rate = 0.0001
print_freq = 5
train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)
tl.layers.initialize_global_variables(sess)
net.print_params()
net.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(net.all_drop) # enable dropout or dropconnect 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))
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(net.all_drop) # disable noise 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(net.all_drop) # disable noise 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:
# # You can visualize the weight of 1st hidden layer as follow.
# tl.vis.draw_weights(net.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([net.all_params[0]] , name='w1'+str(epoch+1)+'.npz')
# except: # pylint: disable=bare-except
# print("You should change vis.draw_weights(), 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(net.all_drop) # disable noise 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))
# Add ops to save and restore all the variables, including variables for training.
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(net.all_params, name='model.npz')
# You can only save one parameter as follow.
# tl.files.save_npz([net.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, net)
# 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))
sess = tf.InteractiveSession()
# placeholder
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
print("Build net")
if model == 'relu':
net = tl.layers.InputLayer(x, name='input')
net = tl.layers.DropoutLayer(net, keep=0.5, name='denoising1') # if drop some inputs, it is denoise AE
net = tl.layers.DenseLayer(net, n_units=196, act=tf.nn.relu, name='relu1')
recon_layer1 = tl.layers.ReconLayer(net, 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
net = tl.layers.InputLayer(x, name='input')
net = tl.layers.DropoutLayer(net, keep=0.5, name='denoising1')
net = tl.layers.DenseLayer(net, n_units=196, act=tf.nn.sigmoid, name='sigmoid1')
recon_layer1 = tl.layers.ReconLayer(net, x_recon=x, n_units=784, act=tf.nn.sigmoid, name='recon_layer1')
## ready to train
tl.layers.initialize_global_variables(sess)
## print all params
print("All net Params")
net.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))
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 net
print("\nBuild net")
net = tl.layers.InputLayer(x, name='input')
# denoise layer for AE
net = tl.layers.DropoutLayer(net, keep=0.5, name='denoising1')
# 1st layer
net = tl.layers.DropoutLayer(net, keep=0.8, name='drop1')
net = tl.layers.DenseLayer(net, n_units=800, act=act, name=model + '1')
x_recon1 = net.outputs
recon_layer1 = tl.layers.ReconLayer(net, x_recon=x, n_units=784, act=act_recon, name='recon_layer1')
# 2nd layer
net = tl.layers.DropoutLayer(net, keep=0.5, name='drop2')
net = tl.layers.DenseLayer(net, n_units=800, act=act, name=model + '2')
recon_layer2 = tl.layers.ReconLayer(net, x_recon=x_recon1, n_units=800, act=act_recon, name='recon_layer2')
# 3rd layer
net = tl.layers.DropoutLayer(net, keep=0.5, name='drop3')
net = tl.layers.DenseLayer(net, 10, act=tf.identity, name='output')
# Define fine-tune process
y = net.outputs
cost = tl.cost.cross_entropy(y, y_, name='cost')
n_epoch = 200
batch_size = 128
learning_rate = 0.0001
print_freq = 10
train_params = net.all_params
# train_op = tf.train.GradientDescentOptimizer(0.5).minimize(cost)
train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost, var_list=train_params)
# Initialize all variables including weights, biases and the variables in train_op
tl.layers.initialize_global_variables(sess)
# Pre-train
print("\nAll net Params before pre-train")
net.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=100, 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=100, batch_size=128, print_freq=10,
save=False
)
print("\nAll net Params after pre-train")
net.print_params()
# Fine-tune
print("\nFine-tune net")
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(net.all_drop) # enable noise layers
feed_dict[tl.layers.LayersConfig.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(net.all_drop) # disable noise 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(net.all_drop) # disable noise 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.vis.draw_weights(net.all_params[0].eval(), second=10, saveable=True, shape=[28, 28], name='w1_' + str(epoch + 1), fig_idx=2012)
# except: # pylint: disable=bare-except
# print("You should change vis.draw_weights(), 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(net.all_drop) # disable noise 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
- For simplified CNN layer see "Convolutional layer (Simplified)"
in read the docs website.
"""
X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))
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])
net = tl.layers.InputLayer(x, name='input')
## Professional conv API for tensorflow expert
# net = tl.layers.Conv2dLayer(net,
# act = tf.nn.relu,
# shape = [5, 5, 1, 32], # 32 features for each 5x5 patch
# strides=[1, 1, 1, 1],
# padding='SAME',
# name ='cnn1') # output: (?, 28, 28, 32)
# net = tl.layers.PoolLayer(net,
# ksize=[1, 2, 2, 1],
# strides=[1, 2, 2, 1],
# padding='SAME',
# pool = tf.nn.max_pool,
# name ='pool1',) # output: (?, 14, 14, 32)
# net = tl.layers.Conv2dLayer(net,
# act = tf.nn.relu,
# shape = [5, 5, 32, 64], # 64 features for each 5x5 patch
# strides=[1, 1, 1, 1],
# padding='SAME',
# name ='cnn2') # output: (?, 14, 14, 64)
# net = tl.layers.PoolLayer(net,
# ksize=[1, 2, 2, 1],
# strides=[1, 2, 2, 1],
# padding='SAME',
# pool = tf.nn.max_pool,
# name ='pool2',) # output: (?, 7, 7, 64)
## Simplified conv API (the same with the above layers)
net = tl.layers.Conv2d(net, 32, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', name='cnn1')
net = tl.layers.MaxPool2d(net, (2, 2), (2, 2), padding='SAME', name='pool1')
net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', name='cnn2')
net = tl.layers.MaxPool2d(net, (2, 2), (2, 2), padding='SAME', name='pool2')
## end of conv
net = tl.layers.FlattenLayer(net, name='flatten')
net = tl.layers.DropoutLayer(net, keep=0.5, name='drop1')
net = tl.layers.DenseLayer(net, 256, act=tf.nn.relu, name='relu1')
net = tl.layers.DropoutLayer(net, keep=0.5, name='drop2')
net = tl.layers.DenseLayer(net, 10, act=tf.identity, name='output')
y = net.outputs
cost = tl.cost.cross_entropy(y, y_, 'cost')
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 = net.all_params
train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost, var_list=train_params)
tl.layers.initialize_global_variables(sess)
net.print_params()
net.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(net.all_drop) # enable noise 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(net.all_drop) # disable noise 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(net.all_drop) # disable noise 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.vis.CNN2d(net.all_params[0].eval(), second=10, saveable=True, name='cnn1_' + str(epoch + 1), fig_idx=2012)
# except: # pylint: disable=bare-except
# print("You should change vis.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(net.all_drop) # disable noise 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()
# 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()