TensorFlow实战(一)手写数字识别

来源:互联网 发布:医院挂号哪个软件好 编辑:程序博客网 时间:2024/05/19 20:20

TensorFlow实战:

第一章是基础,没什么好看的,跳过出,第二章是说TensorFlow和其他的模块,比如caffe等,caffe以前也说过,比较容易,但是配置比较麻烦(CPU的容易点,gpu比较麻烦)


第三章:简单说一下安装吧,就行在命令行输入(前提是你已经有python)

pip install tensorflow

第一个例子就是手写数字的识别,准确率不是太高,92%左右,具体操作如下。

from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)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)import tensorflow as tfsess = tf.InteractiveSession()x = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x, W) + b)y_ = tf.placeholder(tf.float32, [None, 10])cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)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))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
上面是整体代码:

我们会对其一步一步介绍:


from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

这个命令是从网上直接下载数据,下载完的数据会存储在mnist中。

print(mnist.train.images.shape, mnist.train.labels.shape)
#把数据的测试数据集显示出来,结果是(55000, 784) (55000, 10),表示有55000个测试图片,28*28,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)


  28*28写成784的原因:

答:这里丢弃了图片的二 维结构方面的信息,只 是把一张图片变成一个很长的1维向量。读者可能会问,图片的空间结构信息不是很有价值吗,为什么我们要丢弃呢?因为这个数据集的分类任务比较简单,同时也是我们使用TensorFlow的第一次尝试, 我们 不需要建立一个太复杂的模型 ,所以简化了问题,丢弃空间结构的信息。后面的章节将使用卷积神经网络对空间结构信息进行利用,并取得更高的准确率。我们将图片展开成l维向量时 ,顺序并不重要,只要每一张图片都是用同样的顺序进行展开的 就可以。

import tensorflow as tfsess = tf.InteractiveSession()x = tf.placeholder(tf.float32, [None, 784])
#输入数据的地方
W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))
#TensorFlow的输入变量,这里是初始化的输入参数
y = tf.nn.softmax(tf.matmul(x, W) + b)
#输入x是,输出的量,softmax是多分类的激励函数
y_ = tf.placeholder(tf.float32, [None, 10])
#输入的真实数据的标签。
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
#计算损失函数,这里用的是交叉熵(有些用的是二范数),我其中一篇博客讲了它们的优缺点。
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#优化算法
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))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))


我的实验结果准确率是91.8%



阅读全文
0 0
原创粉丝点击