1 import tensorflow.examples.tutorials.mnist.input_data as input_data 2 import tensorflow as tf 3 4 mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) 5 6 x = tf.placeholder(tf.float32,[None, 784]) 7 W = tf.Variable(tf.zeros([784,10])) 8 b = tf.Variable(tf.zeros([10])) 9 10 y= tf.nn.softmax(tf.matmul(x,W) + b) 11 y_ = tf.placeholder("float",[None,10]) 12 cross_entropy = -tf.reduce_sum(y_*tf.log(y)) 13 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 14 15 init = tf.initialize_all_variables() 16 17 sess = tf.Session() 18 sess.run(init) 19 20 for i in range(1000): 21 batch_xs, batch_ys = mnist.train.next_batch(100) 22 sess.run(train_step, feed_dict={x: batch_xs,y_: batch_ys}) 23 24 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 25 accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 26 print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
按照教程,首先跑的是MNIST的历程。按照极客学院的教程,首先使用的是一个传统softmax的方法来实现的机器学习算法,核心是使用梯度下降的方式来进行的。上边是我整理的代码,train阶段的正确率是91%。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:TensorFlow入门——MNIST初探 - Python技术站