TensorFlow使用RNN实现手写数字识别
学习,笔记,有时间会加注释以及函数之间的逻辑关系。
# https://www.cnblogs.com/felixwang2/p/9190664.html
# https://www.cnblogs.com/felixwang2/p/9190664.html
# TensorFlow(十二):使用RNN实现手写数字识别 import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data # 载入数据集
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 输入图片是28*28
n_inputs = 28 # 输入一行,一行有28个数据
max_time = 28 # 一共28行
lstm_size = 100 # 隐层单元
n_classes = 10 # 10个分类
batch_size = 50 # 每批次50个样本
n_batch = mnist.train.num_examples // batch_size # 计算一共有多少个批次 # 这里的none表示第一个维度可以是任意的长度
x = tf.placeholder(tf.float32, [None, 784])
# 正确的标签
y = tf.placeholder(tf.float32, [None, 10]) # 初始化权值
weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
# 初始化偏置值
biases = tf.Variable(tf.constant(0.1, shape=[n_classes])) # 定义RNN网络
def RNN(X, weights, biases):
# inputs=[batch_size, max_time, n_inputs]
inputs = tf.reshape(X, [-1, max_time, n_inputs])
# 定义LSTM基本CELL
lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
# final_state[state,batch_size,cell.state_size]
# final_state[0]是cell state
# final_state[1]是hidden_state
# outputs: The RNN output 'Tensor'.
# If time_major == False (default), this will be a `Tensor` shaped:
# `[batch_size, max_time, cell.output_size]`.
# If time_major == True, this will be a `Tensor` shaped:
# `[max_time, batch_size, cell.output_size]`.
# final_state 记录的是最后一次的输出结果
# outputs 记录的是每一次的输出结果 outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, inputs, dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases)
return results # 计算RNN的返回结果
prediction = RNN(x, weights, biases)
# 损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
# 使用AdamOptimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# 结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) # argmax返回一维张量中最大的值所在的位置
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 把correct_prediction变为float32类型
# 初始化
init = tf.global_variables_initializer() gpu_options = tf.GPUOptions(allow_growth=True)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
sess.run(init)
for epoch in range(6):
for batch in range(n_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys}) acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
print("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))
输出
Iter , Testing Accuracy= 0.6694
Iter , Testing Accuracy= 0.714
Iter , Testing Accuracy= 0.7984
Iter , Testing Accuracy= 0.8568
Iter , Testing Accuracy= 0.8863
Iter , Testing Accuracy= 0.9088 Process finished with exit code
TensorFlow使用RNN实现手写数字识别的更多相关文章
- 5 TensorFlow入门笔记之RNN实现手写数字识别
------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...
- 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...
- TensorFlow卷积神经网络实现手写数字识别以及可视化
边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...
- TensorFlow(十二):使用RNN实现手写数字识别
上代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist ...
- keras和tensorflow搭建DNN、CNN、RNN手写数字识别
MNIST手写数字集 MNIST是一个由美国由美国邮政系统开发的手写数字识别数据集.手写内容是0~9,一共有60000个图片样本,我们可以到MNIST官网免费下载,总共4个.gz后缀的压缩文件,该文件 ...
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- 手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)
通过: 手写数字识别 ----卷积神经网络模型官方案例详解(基于Tensorflow,Python) 手写数字识别 ----Softmax回归模型官方案例详解(基于Tensorflow,Pytho ...
- 手写数字识别 ----卷积神经网络模型官方案例注释(基于Tensorflow,Python)
# 手写数字识别 ----卷积神经网络模型 import os import tensorflow as tf #部分注释来源于 # http://www.cnblogs.com/rgvb178/p/ ...
- 手写数字识别 ----Softmax回归模型官方案例注释(基于Tensorflow,Python)
# 手写数字识别 ----Softmax回归模型 # regression import os import tensorflow as tf from tensorflow.examples.tut ...
随机推荐
- 题解【洛谷P1314】[NOIP2011]聪明的质监员
题面 题解 不难发现,\(W\)增大时,\(Y\)值会随之减小. 于是考虑二分\(W\). 如何\(\mathcal{O}(N)check?\) 每一次前缀和记录一下\(1-i\)之间\(w_i \g ...
- 【网站】Kiwi浏览器中文网
2020年1月1日上线 访问地址:http://huangenet.gitee.io/kiwibrowser/
- SpringMVC 配置.html拦截时,返回JSON数据时出现406错误解决方案
[说明]在SpringMVC框架的使用中常常会使用@ResponseBody注解,修饰"处理器"(Controller的方法),这样在处理器在返回完毕后,就不走逻辑视图,而是将返回 ...
- python 百万级别类实例实现节省内存
# 案例: ''' 某网络游戏中,定义了玩家类Player(id,name,status) 每当有一个玩家,就会在服务器创建一个Player实例 当在线人数过多时,将产生大量实例(百万级别),消耗内存 ...
- django 搭建一个投票类网站(一)
写在最前,之前零零散散的看过django,但是由于比较杂,学的云里雾里的,所以就停了一段落,但是我最近找到了一个django的书,是李建编著的django入门与实践,于是,打算照着书上的步骤来写好一个 ...
- hdu1716 排列2
12 21 123 132 213 231 321 312 .... 每次都将后面n-1位进行全排列.递归的出口当起始坐标等于终止坐标时.需要还原. 设计标记数组.因为需要从小到大输出. #def ...
- 极客工具,PDF合并工具
前言 这两天一番花两天的时间,重新用python和python图形化开发工具tkinter,完善了下PDF合并小工具,终于可以发布了. 工具目前基本功能已经完善,后期如果有反馈可以修复部分bug或完善 ...
- layui之laydate
.点击年份马上关闭窗口并且赋值 html代码: <div class="layui-form-item"> <label class="layui-fo ...
- 拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393
//拓扑排序判断是否有环 #include<cstdio> #include<algorithm> #include<string.h> #include<m ...
- NOIP2012 疫情控制 题解(LuoguP1084)
NOIP2012 疫情控制 题解(LuoguP1084) 不难发现,如果一个点向上移动一定能控制更多的点,所以可以二分时间,判断是否可行. 但根节点不能不能控制,存在以当前时间可以走到根节点的点,可使 ...