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
- TensorFlow实战(一)手写数字识别
- TensorFlow实战-mnist手写数字识别(卷积神经网络)
- TensorFlow实战—mnist手写数字识别
- 基于Tensorflow的MNIST手写数字识别(一)
- TensorFlow代码实现(一)[MNIST手写数字识别]
- tensorflow入门-mnist手写数字识别(一)
- tensorflow识别手写数字
- Tensorflow手写数字识别
- Tensorflow实战(一)(MNIST手写字体识别TensorFlow实现)
- tensorflow识别手写数字(2)
- Tensorflow实战——利用Softmax Regression识别手写数字
- TensorFlow实战:手写数字识别之K近邻
- Tensorflow实战之用softmax Regression识别手写数字
- [TensorFlow实战] 构建LeNet实现手写数字识别
- 《TensorfFlow实战》读书笔记(三) —— Tensorflow 实现 Softmax Regression 识别手写数字
- tensorflow-mnist手写数字识别
- TensorFlow实现识别手写数字
- TensorFlow学习笔记(一):手写数字识别之softmax回归
- MYSQL explain详解
- MyBatis 分别结合 SQL Server、MySQL、ORACLE进行数据库分页
- 关于DBCP为什么加载了配置文件 直接一个getConnection就获得了连接对象呢 ?
- springboot跨域
- 大数据和云计算技术周报(第4期):NoSQL特辑
- TensorFlow实战(一)手写数字识别
- request payload和form data
- 线程练习题
- 洛谷1273 有线电视网
- CentOS下mysql数据库常用命令总结
- PHP使用3种方法实现数据采集
- CSS中的样式层叠机制Cascade
- Java通过jdbc访问SQLServer数据库
- C/C++ 第八周串和数组 (一)对称矩阵压缩存储的实现与应用 项目3—(1)