TensorFlow读取数据在简单图像识别中的应用

2019-10-21 07:37:04来学伟
现代信息科技 2019年12期

摘  要:本文以MNIST数据库为例,用TensorFlow读取数据集中的数据,并建立一个简单的图像识别模型。同时以TensorFlow为工具,写一个手写体数字识别程序,使用的机器方法是Softmax回归。通过把占位符的值传递给会话,计算后运行梯度下降后,可以检测模型训练的结果,得到预测的标签和实际标签,接下来通过tf.equal函数来比较它们是否相等,并将结果保存到correct_prediction中。最后,用tf.reduce_mean可以计算数组中的所有元素的平均值,相当于得到了模型的预测准确率。该模型识别的准确率超过90%,具有一定的推广价值。

关键词:MNIST数据集;Softmax回归;训练模型

中图分类号:TP181      文献标识码:A 文章编号:2096-4706(2019)12-0098-02

Abstract:This paper takes the MNIST database as an example,uses TensorFlow to read the data in the data set,and establishes a simple image recognition model.At the same time with TensorFlow tool,write a handwritten number recognition program,using Softmax regression. By passing the value of placeholder to the session,the result of model training can be detected after the gradient descent is run after calculation,and the predicted tag and the actual tag can be obtained. Next,the equality function of tf.equal is used to compare whether they are equal,and the result is saved in correct_prediction. Finally,tf.reduce_mean can be used to calculate the average value of all elements in the array,which is equivalent to the prediction accuracy of the model.The recognition accuracy of the modified model is more than 90%,which has a little promotion value.

Keywords:MNIST data set;Softmax regression;training model

0  引  言

拍摄手写的数字而形成的图片以及一些相对应的标记共同组成了MNIST数据集,这种数据集主要包含了两种类型的图像,一种是训练用的图像,还有一种是测试用的图像。在原始的MNIST数据集中,我们用28行28列的矩阵来表示每一张图像。而在TensorFlow中,变量mnist.train.images是训练样本,它的形状为(55000,784)。其中,5000是训练图像的数量,单个图像样本的维数为784,也就是说任何一个图像样本都被一个有784维的向量来表示(28行乘以28列为784维)。

1  Softmax回归

Softmax回归是一个完整的线性的多類分类模型,事实上它是通过Logistic回归直接从模型转化而来的。与之不同的是Logistic回归模型是一种两类分类模型,而Softmax模型则是多类分类模型。在手写数字图像识别问题中,总共有10个不同的类别(即从0到9),我们希望对输入的图像计算它属于每个类别的概率。如属于9的概率为70%,属于1的概率为10%等。最后模型预测的结果就是概率最大的那个类别。……

登录APP查看全文