学习,笔记,有时间会加注释以及函数之间的逻辑关系。

# 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实现手写数字识别的更多相关文章

  1. 5 TensorFlow入门笔记之RNN实现手写数字识别

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...

  2. 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)

    上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...

  3. TensorFlow卷积神经网络实现手写数字识别以及可视化

    边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...

  4. TensorFlow(十二):使用RNN实现手写数字识别

    上代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist ...

  5. keras和tensorflow搭建DNN、CNN、RNN手写数字识别

    MNIST手写数字集 MNIST是一个由美国由美国邮政系统开发的手写数字识别数据集.手写内容是0~9,一共有60000个图片样本,我们可以到MNIST官网免费下载,总共4个.gz后缀的压缩文件,该文件 ...

  6. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  7. 手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)

    通过: 手写数字识别  ----卷积神经网络模型官方案例详解(基于Tensorflow,Python) 手写数字识别  ----Softmax回归模型官方案例详解(基于Tensorflow,Pytho ...

  8. 手写数字识别 ----卷积神经网络模型官方案例注释(基于Tensorflow,Python)

    # 手写数字识别 ----卷积神经网络模型 import os import tensorflow as tf #部分注释来源于 # http://www.cnblogs.com/rgvb178/p/ ...

  9. 手写数字识别 ----Softmax回归模型官方案例注释(基于Tensorflow,Python)

    # 手写数字识别 ----Softmax回归模型 # regression import os import tensorflow as tf from tensorflow.examples.tut ...

随机推荐

  1. 《深入理解Java虚拟机》读书笔记六

    第七章 虚拟机类加载机制 1.类加载的时机 虚拟机的类加载机制: 虚拟机把描述类的数据从class文件中加载到内存,并对数据进行校验.转换解析和初始化,最终形成了可以被虚拟机直接使用的Java类型,这 ...

  2. C语言各语句的作用

    #include <stdio.h> 在使用标准函数库中的输入输出函数时,编译系统要求程序提供有关的信息(例如对这些输入输出函数的声明),#include<stdio.h>的作 ...

  3. 面试官所问的--Token认证

    写这一篇文章的来源是因为某一天的我被面试官提问:让你设计一个登录页面,你会如何设计? 我当时的脑子只有??? 不就是提交账号.密码给后台就搞定了呢? 不可能那么简单,我弱弱的想,难道要对密码加密?? ...

  4. python property(不动产)方法

    class Test(object): @property def test(self): return 100 @test.setter def test(self): return "修 ...

  5. 剑指offer 面试题52. 两个链表的第一个公共节点

    这题之前leetcode做过,权当复习 首先这题没说是否一定有公共节点,如果代码可能因为这一点造成死循环的,需要提前验证所给两个链表是否有公共节点. 方法1:对于每一个list1的节点,遍历list2 ...

  6. QT5.1+中文乱码问题

    原文连接:https://blog.csdn.net/liyuanbhu/article/details/72596952 QT中规定 QString 的 const char* 构造函数是调用 fr ...

  7. 管理QT的组件

    1.在qt的安装目录找到'%QTROOT%\MaintenanceTool.exe'. 2.点击MaintenanceTool的设置,可以设置默认储存库.临时储存库.用户定义储存库,选择其中的临时储存 ...

  8. sql语句代码规范

    19年年底的时候领导一直强调代码规范化以前写代码的时候很随意后来越来越看自己写的代码难受逐渐的也像规范化走去,今天又学了一招记录分享一下 这张图就是以前写代码的时候正常情况很是杂乱无章 这张就是规范话 ...

  9. MSSQL 打开xp_cmdshell

    sp_configure reconfigure go sp_configure reconfigure go

  10. python3爬取高清壁纸(1)

    这次爬取的目标是:美桌网首页 > 桌面壁纸 > 卡通动漫 类别下的壁纸. 我们先随机选取一个专辑来爬(http://www.win4000.com/wallpaper_detail_545 ...