import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data", one_hot=True)
batch_size=200
n_batch=mnist.train.num_examples // batch_size
x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10])
W=tf.Variable(tf.zeros([784,500]))
b=tf.Variable(tf.zeros([1,500]))
Wx_plus_b_L1=tf.matmul(x,W)+b
L1=tf.nn.sigmoid(Wx_plus_b_L1)
W2=tf.Variable(tf.random_normal([500,100]))
b2=tf.Variable(tf.zeros([1,100]))
Wx_plus_b_L2=tf.matmul(L1,W2)+b2
L2=tf.nn.sigmoid(Wx_plus_b_L2)
W3=tf.Variable(tf.truncated_normal([100,10]))
b3=tf.Variable(tf.random_normal([1,10]))
prediction=tf.nn.softmax(tf.matmul(L2,W3)+b3)
loss=tf.reduce_mean(tf.square(y-prediction))
train_step=tf.train.GradientDescentOptimizer(0.25).minimize(loss)
init=tf.global_variables_initializer()
correct_prediction=tf.equal(tf.argmax(y,1), tf.argmax(prediction,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
sess.run(init)
for epoch in range(30):
for batch in range(n_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs, y:batch_ys})
acc=sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels})
print("Iter"+str(epoch)+",Testing Accuracy "+str(acc))
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
Iter0,Testing Accuracy 0.7418
Iter1,Testing Accuracy 0.8122
Iter2,Testing Accuracy 0.8403
Iter3,Testing Accuracy 0.8466
Iter4,Testing Accuracy 0.8496
Iter5,Testing Accuracy 0.9283
Iter6,Testing Accuracy 0.9369
Iter7,Testing Accuracy 0.9412
Iter8,Testing Accuracy 0.945
Iter9,Testing Accuracy 0.945
Iter10,Testing Accuracy 0.9474
Iter11,Testing Accuracy 0.949
Iter12,Testing Accuracy 0.9506
Iter13,Testing Accuracy 0.9512
Iter14,Testing Accuracy 0.9514
Iter15,Testing Accuracy 0.9532
Iter16,Testing Accuracy 0.9528
Iter17,Testing Accuracy 0.9547
Iter18,Testing Accuracy 0.9546
Iter19,Testing Accuracy 0.9556
Iter20,Testing Accuracy 0.9561
Iter21,Testing Accuracy 0.9557
Iter22,Testing Accuracy 0.9571
Iter23,Testing Accuracy 0.9571
Iter24,Testing Accuracy 0.9563
Iter25,Testing Accuracy 0.9579
Iter26,Testing Accuracy 0.9575
Iter27,Testing Accuracy 0.9577
Iter28,Testing Accuracy 0.9577
Iter29,Testing Accuracy 0.9583