TF:利用是Softmax回归+GD算法实现MNIST手写数字图片识别(10000张图片测试得到的准确率为92%)

TF:利用是Softmax回归+GD算法实现MNIST手写数字图片识别(10000张图片测试得到的准确率为92%)


设计思路

全部代码

#TF:利用是Softmax回归+GD算法实现手写数字识别(10000张图片测试得到的准确率为92%)
#思路:对输入的图像,计算它属于每个类别的概率,找出最大概率即为预测值
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data  

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)#读入MNIST数据

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])  

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)  

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
print('start training...')

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})  

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))  # 0.9185

相关文章

TF:利用是Softmax回归+GD算法实现MNIST手写数字识别(10000张图片测试得到的准确率为92%)

(0)

相关推荐