import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data as mnist_data print("Tensorflow version " + tf.__version__) tf.set_random_seed(0) # Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels) mnist = mnist_data.read_data_sets("data", one_hot=True, reshape=False, validation_size=0) # input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch X = tf.placeholder(tf.float32, [None, 28, 28, 1]) # correct answers will go here Y_ = tf.placeholder(tf.float32, [None, 10]) # weights W[784, 10] 784=28*28 W = tf.Variable(tf.zeros([784, 10])) # biases b[10] b = tf.Variable(tf.zeros([10])) # flatten the images into a single line of pixels # -1 in the shape definition means "the only possible dimension that will preserve the number of elements" XX = tf.reshape(X, [-1, 784]) # The model Y = tf.nn.softmax(tf.matmul(XX, W) + b) cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0 # normalized for batches of 100 images, # *10 because "mean" included an unwanted division by 10 # accuracy of the trained model, between 0 (worst) and 1 (best) correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # training, learning rate = 0.005 train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy) # init init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) def training_step(i): # training on batches of 100 images with 100 labels batch_X, batch_Y = mnist.train.next_batch(100) a, c, bb = sess.run([accuracy, cross_entropy, b], feed_dict={X: batch_X, Y_: batch_Y}) print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c)) a, c, bb = sess.run([accuracy, cross_entropy, b], feed_dict={X: mnist.test.images, Y_: mnist.test.labels}) print(str(i) + ": ********* epoch " + str(i*100//mnist.train.images.shape[0]+1) + " ********* test accuracy:" + str(a) + " test loss: " + str(c)) print(bb) # the backpropagation training step sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y}) for i in range(2): training_step(i)