您的位置:首页 > 其它

tensorflow用Softmax Regression识别MNIST手写数字识别

2018-03-29 14:02 721 查看
<div><br class="Apple-interchange-newline"># 直接加载mnist集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MINIST_DATA/',one_hot=True)
<br></div>
# 直接加载mnist集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MINIST_DATA/',one_hot=True)
c:\program files\python\python36\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Extracting MINIST_DATA/train-images-idx3-ubyte.gz
Extracting MINIST_DATA/train-labels-idx1-ubyte.gz
Extracting MINIST_DATA/t10k-images-idx3-ubyte.gz
Extracting MINIST_DATA/t10k-labels-idx1-ubyte.gz
# 看下mnist的概况 28*28,标签是0-9
# 这里直接把mnist打成列了,损失了空间价值
# 多分类用softmax
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)
(55000, 784) (55000, 10)
(10000, 784) (10000, 10)
(5000, 784) (5000, 10)
# 使用InteractiveSession会将该session注册为默认的session,之后所有的运算都会跑在这个session里面,不同的session之间的数据和运算都是独立的
import tensorflow as tf
sess = tf.InteractiveSession()
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 先用一个x变量占坑,None表示不限输入维度,784表示这个维度是784
# 第二个维度就是shape
x = tf.placeholder(tf.float32,[None,784])
# 给权重weights和偏差biases创建Variable对象,并初始化为0
# 这个Variable会被持久化,也就是会一直保存,不像Tensor会丢失
# tf的model会自动学习合适的w和b值
# 但在网络较深的模型中,初始化就不能这么简单粗暴,否则模型跑不起来
W = tf.Variable(tf.zeros([784,10]))
# 这个在加的时候维度会自动扩充
b = tf.Variable(tf.zeros([10]))
# 输出层
# tf.nn包含大量的网络神经组件
# tf.matmul是矩阵乘法
y = tf.nn.softmax(tf.matmul(x,W)+b)
# 为了训练模型,我们必须定义一个loss function来判断学习精度
# loss越小,分类结果与真实偏差越小
# 定义一个placeholder,输入真实的label,用预测的与它进行某种比较,计算loss
y_ = tf.placeholder(tf.float32,[None,10])
# tf.reduce_sum是求和,而tf.reduce_mean是对每个batch的结果求均值
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
# 既然已经定义好了loss和softmax,就缺一个优化方法了,这里采用随机梯度下降法SGD
# 只要定义好优化方法,tf就可以根据我们的定义的计算图自动求导,并利用反向传播BP进行训练
# 每一轮迭代都会更新参数以减小loss
# 这里设定学习率为0.5,优化目标为上面的cross-entropy
# 如果要换函数名,改个函数名字就可以了
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# 使用全局的参数初始化器,并run
tf.global_variables_initializer().run()
# 迭代地训练,每次随机取100个样本构成mini-batch,并把样本传递给上面设定的placeholder
# 然后用上面设定的train_step来训练,每次只计算一点,既能节省计算资源,也能加快收敛速度
for i in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100)
# 这里的参数就是把batch的训练集和label传给早些时候设定的占位符placeholder
train_step.run({x:batch_xs,y_:batch_ys})
# 上面已经完成训练了,验证一下准确率
# tf.argmax就是从一个Tensor中找到值最大的序号
# 参数前者就是找最大值,后者就是找真实值,比对
# 然后tf.equal比对是否正确
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
# 统计所有样本的accuracy,需要用tf.cast把correct_prdiction的bool转成float32,再平均
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
# 计算准确率,打印结果
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
0.9174
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tensorflow softmax