In [1]:
import tensorflow as tf
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
The above two lines downloaded the mnist dataset. It is the base of handwritten digits.
every image is 28x28. So it is 784 points. So, every image can be represented as a point in 784d array.
we will also use one-hot vectors to signal, what is the number of image.
In [1]: def one_hot(k, n):
...: vector = [0 for _ in range(k)]
...: vector[n] = 1
...: return vector
...:
In [2]: one_hot(10, 3)
Out[2]: [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
In [19]:
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
Out[19]:
we did describe the variables. Now, call the softmax function
In [25]:
y = tf.nn.softmax(tf.matmul(x, W) + b)
tf.nn.softmax?
In [29]:
y_ = tf.placeholder(tf.float32, [None, 10]) # correct answers
In [32]:
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
In [34]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
In [40]:
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})
In [45]:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
print(correct_prediction)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
In [ ]: