In [2]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [11]:
print(mnist.train.images.shape)
print(mnist.train.labels.shape)
print(mnist.validation.images.shape)
print(mnist.validation.labels.shape)
print(mnist.test.images.shape)
print(mnist.test.labels.shape)


(55000, 784)
(55000, 10)
(5000, 784)
(5000, 10)
(10000, 784)
(10000, 10)

In [12]:
import tensorflow as tf

In [13]:
x = tf.placeholder(tf.float32, [None, 784])

In [14]:
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

In [15]:
y = tf.nn.softmax(tf.matmul(x, W) + b)

In [17]:
y_ = tf.placeholder(tf.float32, [None, 10])

In [23]:
#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))

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

In [35]:
sess = tf.InteractiveSession()

In [36]:
tf.global_variables_initializer().run()

In [41]:
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})

In [37]:
a = mnist.train.next_batch(100)

In [40]:
a[0].shape, a[1].shape


Out[40]:
((100, 784), (100, 10))

In [44]:
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

In [46]:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

In [49]:
accuracy


Out[49]:
<tf.Tensor 'Mean_5:0' shape=() dtype=float32>

In [48]:
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))


0.9069