diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..67fdd3d0d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +# https://docs.travis-ci.com/user/languages/python/ +language: python +python: + - "2.7" + - "3.6" +install: + - pip install tensorflow + - pip install . +script: + # units test + # https://docs.pytest.org/en/latest/ + # TODO: make it work + # - pytest + + # smoke tests + - python example/tutorial_mnist_tiny.py diff --git a/example/tutorial_mnist_tiny.py b/example/tutorial_mnist_tiny.py new file mode 100644 index 000000000..b7891a55d --- /dev/null +++ b/example/tutorial_mnist_tiny.py @@ -0,0 +1,60 @@ +#! /usr/bin/python +# -*- coding: utf-8 -*- + + + +import tensorflow as tf +import tensorlayer as tl + +sess = tf.InteractiveSession() + +# prepare data +X_train, y_train, X_val, y_val, X_test, y_test = \ + tl.files.load_mnist_dataset(shape=(-1,784)) +# define placeholder +x = tf.placeholder(tf.float32, shape=[None, 784], name='x') +y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_') + +# define the network +network = tl.layers.InputLayer(x, name='input') +network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1') +network = tl.layers.DenseLayer(network, 800, tf.nn.relu, name='relu1') +network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2') +network = tl.layers.DenseLayer(network, 800, tf.nn.relu, name='relu2') +network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3') +# the softmax is implemented internally in tl.cost.cross_entropy(y, y_) to +# speed up computation, so we use identity here. +# see tf.nn.sparse_softmax_cross_entropy_with_logits() +network = tl.layers.DenseLayer(network, n_units=10, + act=tf.identity, name='output') + +# define cost function and metric. +y = network.outputs +cost = tl.cost.cross_entropy(y, y_, name='cost') +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) + +# define the optimizer +train_params = network.all_params +train_op = tf.train.AdamOptimizer(learning_rate=0.0001 + ).minimize(cost, var_list=train_params) + +# initialize all variables in the session +tl.layers.initialize_global_variables(sess) + +# print network information +network.print_params() +network.print_layers() + +# train the network +tl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_, + acc=acc, batch_size=500, n_epoch=5, print_freq=5, + X_val=X_val, y_val=y_val, eval_train=False) + +# evaluation +tl.utils.test(sess, network, acc, X_test, y_test, x, y_, batch_size=None, cost=cost) + +# save the network to .npz file +tl.files.save_npz(network.all_params , name='model.npz') +sess.close()