import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data
# Loading data set MNIST = input_data.read_data_sets ( " MNIST_data " , one_hot = True) # each batch size of the batch_size = 64 # calculates a total number of batches n_batch mnist.train.num_examples // = the batch_size # define two a placeholder X = tf.placeholder (tf.float32, [None, 784 ]) Y = tf.placeholder (tf.float32, [None, 10 ]) # Create a simple neural network W = tf.Variable (tf.zeros ([784,10 ])) B = tf.Variable (tf.zeros ([10 ])) Prediction = tf.nn.softmax (tf.matmul (X, W is) + B) # cross entropy cost function # = tf.losses.softmax_cross_entropy Loss (Y, Prediction) Loss = tf.losses.mean_squared_error (Y, Prediction) # using a gradient descent method # train_step = tf.train.GradientDescentOptimizer (0.2) .minimize (Loss) train_step = tf.train .AdamOptimizer (from 0.001 ) .minimize (Loss) # initialize variables the init = tf.global_variables_initializer () # result is stored in a Boolean list correct_prediction = tf.equal (tf.argmax (y, 1), tf.argmax (prediction, . 1)) # the argmax return to the one-dimensional position of the maximum value of the tensor located # seeking accuracy accuracy = tf.reduce_mean (tf.cast (correct_prediction, tf.float32)) with tf.Session () AS Sess: sess.run (the init ) for Epochin range(21): 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))
Iter 0,Testing Accuracy 0.9106 Iter 1,Testing Accuracy 0.921 Iter 2,Testing Accuracy 0.9261 Iter 3,Testing Accuracy 0.9277 Iter 4,Testing Accuracy 0.9291 Iter 5,Testing Accuracy 0.9315 Iter 6,Testing Accuracy 0.9293 Iter 7,Testing Accuracy 0.9299 Iter 8,Testing Accuracy 0.9298 Iter 9,Testing Accuracy 0.9315 Iter 10,Testing Accuracy 0.9317 Iter 11,Testing Accuracy 0.9329 Iter 12,Testing Accuracy 0.9324 Iter 13,Testing Accuracy 0.9339 Iter 14,Testing Accuracy 0.9321 Iter 15,Testing Accuracy 0.9322 Iter 16,Testing Accuracy 0.934 Iter 17,Testing Accuracy 0.9326 Iter 18,Testing Accuracy 0.9331 Iter 19,Testing Accuracy 0.9334 Iter 20,Testing Accuracy 0.9334