changeset 90:3ff05538259c

mnist example
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:23:35 -0800
parents 800f3938ebaa
children d603ee579c3e
files tvii/mnist.py
diffstat 1 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tvii/mnist.py	Sun Dec 17 14:23:35 2017 -0800
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+"""
+https://www.tensorflow.org/get_started/mnist/beginners
+
+Exercise to reader:  compare + contrast to
+https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/examples/tutorials/mnist/mnist_softmax.py
+"""
+
+import sys
+import tensorflow as tf
+from .cli import CLIParser as Parser
+from tensorflow.examples.tutorials.mnist import input_data
+mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
+
+def main(args=sys.argv[1:]):
+    """CLI"""
+
+    parser = Parser(description=__doc__)
+    options = parser.parse_args(args)
+
+    # 28*28 = 784
+    x = tf.placeholder(tf.float32, [None, 784])
+
+    # A Variable is a modifiable tensor that lives in TensorFlow's
+    # graph of interacting operations.
+    # It can be used and even modified by the computation.
+    W = tf.Variable(tf.zeros([784, 10]))
+    b = tf.Variable(tf.zeros([10])) # 0,1,2,34,5,6,7,8,9
+
+    # implement model
+    y = tf.nn.softmax(tf.matmul(x, W) + b)
+
+    # implement cross-entropy
+    y_ = tf.placeholder(tf.float32, [None, 10])
+    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
+
+    # define training curriculum
+    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
+
+    # launch the model
+    sess = tf.InteractiveSession()
+    tf.global_variables_initializer().run()
+
+    # train the model
+    for _ in range(1000):
+        batch_xs, batch_ys = mnist.train.next_batch(100)
+        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
+
+if __name__ == '__main__':
+    main()