MNIST手写识别(一)

通过TensorFlow实现Softmax Regression,识别MNIST数据集,最终正确率92%左右。
## 通过TensorFlow实现Softmax Regression,实现手写识别

# 加载mnist数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)

# 查看mnist数据集的情况(训练集55000个样本,测试集10000个,验证集5000个)
print(mnist.train.images.shape,mnist.train.labels.shape)
print(mnist.test.images.shape,mnist.test.labels.shape)
print(mnist.validation.images.shape,mnist.validation.labels.shape)

# 载入tensorflow库,并创建一个新的InteractiveSession
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32,[None,784]) #None代表不限条数的输入

# 给softmax Regression模型中的weights和biases创建Variable对象
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

# 实现softmax Regression算法,即公式:y = softmax(Wx + b)
y = tf.nn.softmax(tf.matmul(x,W)+b)

# 定义损失函数cross_entropy
y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))

# 定义优化算法,采用随机梯度下降SGD
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #学习率0.5,优化目标设定为cross_entropy


# 使用TensorFlow的全局参数初始化器,并执行它的run方法
tf.global_variables_initializer().run()
# 开始训练
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    train_step.run({x: batch_xs, y_: batch_ys})


# 对准确率进行验证
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
#将correct_prediction输出的bool值转化为float32
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
posted @ 2018-05-30 16:24  yucen  阅读(250)  评论(0编辑  收藏  举报