Skip to content

Commit

Permalink
add simple mnist
Browse files Browse the repository at this point in the history
  • Loading branch information
zsdonghao committed Aug 9, 2016
1 parent 0cc3412 commit 4cb27e7
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/user/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TensorFlow
-----------

`TensorFlow <https://www.tensorflow.org/versions/master/get_started/os_setup.html>`_ only officially
supports GPU acceleration for Linux at present.
supports GPU acceleration for Linux and Mac OX at present.
If you want to use GPU with Mac OX, you need to compile TensorFlow from source.

.. warning::
Expand Down
Binary file added tensorlayer/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/activation.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/cost.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/files.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/iterate.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/layers.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/nlp.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/ops.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/preprocess.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/rein.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/utils.cpython-34.pyc
Binary file not shown.
Binary file added tensorlayer/__pycache__/visualize.cpython-34.pyc
Binary file not shown.
11 changes: 5 additions & 6 deletions tutorial_mnist.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#! /usr/bin/python
# -*- coding: utf8 -*-


import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import set_keep
import numpy as np
import time

"""Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN.
Expand Down Expand Up @@ -36,11 +35,11 @@ def main_test_layers(model='relu'):
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)
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.int64)
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.int64)
y_test = np.asarray(y_test, dtype=np.int32)

print('X_train.shape', X_train.shape)
print('y_train.shape', y_train.shape)
Expand All @@ -54,7 +53,7 @@ def main_test_layers(model='relu'):

# placeholder
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')
y_ = tf.placeholder(tf.int32, shape=[None, ], name='y_')

if model == 'relu':
network = tl.layers.InputLayer(x, name='input_layer')
Expand Down
122 changes: 122 additions & 0 deletions tutorial_mnist_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#! /usr/bin/python
# -*- coding: utf8 -*-


import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import set_keep
import numpy as np
import time

"""Examples of MLP.
tensorflow (0.9.0)
"""

def main():
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))


n_epoch = 200
batch_size = 500
learning_rate = 0.0001
print_freq = 10
is_val = False

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')

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')

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_))

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)

sess.run(tf.initialize_all_variables())

network.print_params()
network.print_layers()

print(' learning_rate: %f' % learning_rate)
print(' batch_size: %d' % batch_size)

start_time_begin = time.time()
for epoch in range(n_epoch):
start_time = time.time()
loss_ep = 0; n_step = 0
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 dropout
loss, _ = sess.run([cost, train_op], feed_dict=feed_dict)
loss_ep += loss
n_step += 1
loss_ep = loss_ep/ n_step


if is_val:
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 dropout
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)))
else:
print("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep))
print("Total training time: %f" % (time.time() - start_time_begin))

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)))

tl.files.save_npz(network.all_params , name='model.npz')
sess.close()


if __name__ == '__main__':
sess = tf.InteractiveSession()
main()

0 comments on commit 4cb27e7

Please sign in to comment.