Datum Erratum

To err is human…

Running Tensorflow in Anaconda3 Environment

import tensorflow as tf

Testing that import works

## hello = tf.constant('Hello, TensorFlow!')
## sess = tf.Session()
## print(sess.run(hello))

## a = tf.constant(10)
## b = tf.constant(32)
## print(sess.run(a+b))

Import MNIST Data

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

Define Model

First we define our placeholder variables:

## I suppose this will be where the traning information enters
x = tf.placeholder(tf.float32,[None, 784])

## It's like an initialization
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
## And now our model relationship
y = tf.nn.softmax(tf.matmul(x, W) + b)

Define loss function

## Define another placeholder for correct y
y_ = tf.placeholder(tf.float32,[None, 10])

## cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

Define training method

In [10]:
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

Initialize variables

init = tf.global_variables_initializer()

Define session

sess = tf.Session()
sess.run(init)

Loop through training

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

Determine Accuracy

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
0.9064
%d bloggers like this: