TensorFlow小程序(三):简单的MNIST数字识别

一、MNIST简介

MNIST数据集中包含60000张图片作为训练数据,其中55000用于训练,5000个用于验证。10000张图片作为测试数据。图片的大小为28*28。
在这里插入图片描述Tensorflow中操作MNIST数据集:
处理后的每一张图片是一个长度为784的一维数组,这个数组中的元素对应了图片像素矩阵中的每一个数字。因神经网络的输入是一个特征向量,故把一张二维图像的像素矩阵放在一个一维数组中,方便tensorflow将图片的像素矩阵提供给神经网络的输入层。

MNIST数据集的标签是介于0-9的数字,我们要把标签转化为"one-hot vectors"。一个one-hot向量除了某一位数字是1以外,其余维度数字都是0,比如标签0将表示为([1,0,0,0,0,0,0,0,0,0]),标签3将表示为([0,0,0,1,0,0,0,0,0,0])。因此,mnist.train.labels 是一个[60000, 10]的数字矩阵。

二、部分代码解释:

1.print(“Images shape:”, mnist.train.images.shape, “Labels shape:”, mnist.train.labels.shape)
结果为: Images shape: (55000, 784) Labels shape: (55000, 10)

在这里插入图片描述
2.使用mnist.train.next_batch随机取出batch_size个图片及图标

batch_size = 100
xs, ys = mnist.train.next_batch(batch_size)
print(“X shape:”, xs.shape)
print(“Y shape:”, ys.shape)
结果为: X shape: (100, 784) Y shape: (100, 10)

3.tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法可参考https://blog.csdn.net/ZHANGHUIHUIA/article/details/83784943

三、MNIST数字识别简单的小程序:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data   #数据集的获取
    
mnist=input_data.read_data_sets("MNIST_data",one_hot=True) #载入MINIST数据集,MINIST_data表示当前目录下的这个文件夹,也可以改为别的路径  read_data_sets时会默认把图像reshape(展开),若想保留图像的二维结构,可以传入reshape=False
batch_size=100 #每个批次大小
n_batch=mnist.train.num_examples//batch_size   #批次数目  (train.num_examples表示训练的样本数)

x=tf.placeholder(tf.float32,[None,784])  #输入,输出占位符
y=tf.placeholder(tf.float32,[None,10])   #输出一共为10个数字

#创建一个简单的神经网络
w=tf.Variable(tf.zeros([784,10]))  #输入784,输出10
b=tf.Variable(tf.zeros([10]))

prediction=tf.nn.softmax(tf.matmul(x,w)+b) #将数值转换成预测概率,Softmax是将神经网络得到的多个值,进行归一化处理,使得到的值在[0,1]之间,让结果变得可解释。即可以将结果看作是概率,某个类别概率越大,将样本归为该类别的可能性也就越高。

loss=tf.reduce_mean(tf.square(y-prediction))  #求预测的平均误差

train_step=tf.train.GradientDescentOptimizer(0.3).minimize(loss)   #梯度下降法是误差减小,学习率为0.3

init=tf.global_variables_initializer()  #变量初始化

correct_prediction=tf.equal(tf.arg_max(y,1),tf.arg_max(prediction,1)) 
 #arg_max返回一维张量中最大值所在的位置,将预测值与真实值用equal进行是否相等判断,结果为True或False
 # tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法,作用:输出正确的预测结果。

accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) 
#cast将correct_prediction中数据转化为float型,并求平均值,计算正确度  tf.reduce_mean(x, axis) 函数表示求取矩阵或者张量指定维度的平均值。若不指定第二个参数,则在所有元素中取平均值;若指定第二个参数为 0 ,则再第一位元素上取平均值,即每一列求平均值;若指定第二个参数为 1,则在第二维元素上取平均值,即每一行的平均值。
with tf.Session()as sess:
    sess.run(init)
    for epoch in range(20):  #共训练20次
        for batch in range(n_batch):   #训练一次,进行的次数
            batch_x,batch_y=mnist.train.next_batch(batch_size) #取出待训练数字batch_x为图像,batch_y为标签
            sess.run(train_step,feed_dict={x:batch_x,y:batch_y})  #进行训练
            acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})  #计算准确率
        print("lter"+str(epoch)+",acc"+str(acc))

运行结果:
lter0,acc0.8576
lter1,acc0.8817
lter2,acc0.8912
lter3,acc0.8973
lter4,acc0.9019
lter5,acc0.9034
lter6,acc0.9057
lter7,acc0.9072
lter8,acc0.9086
lter9,acc0.9098
lter10,acc0.9106
lter11,acc0.9132
lter12,acc0.9129
lter13,acc0.9145
lter14,acc0.915
lter15,acc0.9157
lter16,acc0.9168
lter17,acc0.9165
lter18,acc0.9175
lter19,acc0.9175

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值