【TensorFlow实战】学习笔记 - TensorFlow实现Softmax Regression识别手写数字

开发环境:Ubuntu 16.04  Python 2.7

# coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
'''
/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
'''
print(mnist.train.images.shape, mnist.train.labels.shape)
# ((55000, 784), (55000, 10))
print mnist.test.images.shape,mnist.test.labels.shape
# (10000, 784) (10000, 10)
print mnist.validation.images.shape,mnist.validation.labels.shape
# (5000, 784) (5000, 10)
import tensorflow as tf
sess = tf.InteractiveSession() # 创建一个新的InteractiveSession,使用这个命令会将这个session注册为默认的session,之后的运算也默认跑在这个session里,不同session之间的数据和运算应该都是相互独立的
x = tf.placeholder(tf.float32,[None,784]) # 创建一个placeholder,即输入数据的地方,第一个参数是数据类型,第二个参数[None,784]是tensor的shape,即数据尺寸,这里none代表不限条数的输入,784代表每条输入是一个784维的向量
'''
给Softmax Regression模型中的weights和biases创建variable对象,这里初始化为0
'''
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) # softmax是tf.nn下面的一个函数,而tf.nn则包含了大量神经网络的组件,tf.matmul是tensorflow中的矩阵乘法函数
'''
TensorFlow可以自动实现forward和backward的计算,只要定义好loss,训练时会自动求导并进行梯度下降,完成对SoftMax Regression模型参数的自动学习
'''
# 定义cross-entropy loss function
y_ = tf.placeholder(tf.float32, [None, 10]) # 真实label
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

'''
有了算法SoftMax Regression的定义和损失函数cross-entropy的定义,只需要再定义一个优化算法即可开始训练,这里采用随机梯度下降SGD
'''
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # 直接调用tf.train.GradientDescentOptimizer,并设置学习速率为0.5,优化目标设定为cross-entropy,得到进行训练的操作train_step
# tf.global_variables_initializer().run() # 使用TensorFlow的全局参数初始化器tf.global_variables_initializer,并直接执行它的run方法。由于python2.7中该初始化器不可用,修改如下
tf.initialize_all_variables().run()
'''
迭代地执行训练操作train_step,这里每次随机从训练集中抽取100条样本构成一个mini-batch,并feed给placeholder,然后调用train_step对这些样本进行训练。使用一小部分样本进行训练称为随机梯度下降,与每次使用全部样本的传统的梯度下降相对应。如果每次训练都使用全部样本,计算量太大,有时也不容易跳出局部最优。因此,对于绝大部分机器学习问题,我们都只使用一小部分数据进行随机梯度下降,这种做法绝大多数时候会比全样本训练的收敛速度快很多。
'''
for i in range(1,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)) # tf.argmax是从一个tensor中寻找最大值的序号,tf.argmax(y,1)就是求各个预测的数字中概率最大的那一个,而tf.argmax(y_,1)则是找样本的真实数字类别。tf.equal方法则用来判断预测的数字类别是否就是正确的类别,最后返回计算类别是否正确的操作correct_prediction。
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # 统计全部样本预测的accuracy,这里需要先用tf.cast将之前correct_prediction输出的bool值转换为float32,再求平均。
print accuracy.eval({x: mnist.test.images, y_: mnist.test.labels})# 将测试数据的特征和label输入评测流程accuracy,计算模型在测试集上的准确率再讲结果打印出来,使用SoftMax Regression对MNIST数据进行分类识别,在测试集上平均准确率可达92%左右

输出的准确率为0.9175。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值