Ubuntu python3 TensorFlow实例:使用RNN算法实现对MINST-data数字集识别,最终识别准确率达96.875%

PS:小白一个,初级阶段,从调试到实现,step by step.

由于没能及时保留原著作者文章来源,对此深表歉意!!!

附录作者GitHub链接,以示尊重。

aymericdamien/TensorFlow-Examples: TensorFlow Tutorial and Examples for Beginners with Latest APIs  https://github.com/aymericdamien/TensorFlow-Examples

python文件列表:

2:RNN.py

3:tensorboard.py

数据集:MNIST_data

实现过程:

RNN.py

"""
This code is a modified version of the code from this link:
https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/recurrent_network.py
His code is a very good one for RNN beginners. Feel free to check it out.
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data# set random seed for comparing the two result calculations
tf.set_random_seed(1)# this is data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)# hyperparameters
lr = 0.001      #learning rate
training_iters = 100000
batch_size = 128n_inputs = 28   # MNIST data input (img shape: 28*28)
n_steps = 28    # time steps
n_hidden_units = 128   # neurons in hidden layer
n_classes = 10      # MNIST classes (0-9 digits)# tf Graph input
x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_classes])# Define weights
weights = {# (28, 128)'in': tf.Variable(tf.random_normal([n_inputs, n_hidden_units])),# (128, 10)'out': tf.Variable(tf.random_normal([n_hidden_units, n_classes]))
}
biases = {# (128, )'in': tf.Variable(tf.constant(0.1, shape=[n_hidden_units, ])),# (10, )'out': tf.Variable(tf.constant(0.1, shape=[n_classes, ]))
}def RNN(X, weights, biases):# hidden layer for input to cell######################################### transpose the inputs shape from# X ==> (128 batch * 28 steps, 28 inputs)X = tf.reshape(X, [-1, n_inputs])# into hidden# X_in = (128 batch * 28 steps, 128 hidden)X_in = tf.matmul(X, weights['in']) + biases['in']# X_in ==> (128 batch, 28 steps, 128 hidden)X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units])# cell########################################### basic LSTM Cell.if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)else:cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units)# lstm cell is divided into two parts (c_state, h_state)init_state = cell.zero_state(batch_size, dtype=tf.float32)# You have 2 options for following step.# 1: tf.nn.rnn(cell, inputs);# 2: tf.nn.dynamic_rnn(cell, inputs).# If use option 1, you have to modified the shape of X_in, go and check out this:# https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/recurrent_network.py# In here, we go for option 2.# dynamic_rnn receive Tensor (batch, steps, inputs) or (steps, batch, inputs) as X_in.# Make sure the time_major is changed accordingly.outputs, final_state = tf.nn.dynamic_rnn(cell, X_in, initial_state=init_state, time_major=False)# hidden layer for output as the final results############################################## results = tf.matmul(final_state[1], weights['out']) + biases['out']# # or# unpack to list [(batch, outputs)..] * stepsif int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:outputs = tf.unpack(tf.transpose(outputs, [1, 0, 2]))    # states is the last outputselse:outputs = tf.unstack(tf.transpose(outputs, [1,0,2]))results = tf.matmul(outputs[-1], weights['out']) + biases['out']    # shape = (128, 10)return resultspred = RNN(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
train_op = tf.train.AdamOptimizer(lr).minimize(cost)correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))with tf.Session() as sess:# tf.initialize_all_variables() no long valid from# 2017-03-02 if using tensorflow >= 0.12if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:init = tf.initialize_all_variables()else:init = tf.global_variables_initializer()sess.run(init)step = 0while step * batch_size < training_iters:batch_xs, batch_ys = mnist.train.next_batch(batch_size)batch_xs = batch_xs.reshape([batch_size, n_steps, n_inputs])sess.run([train_op], feed_dict={x: batch_xs,y: batch_ys,})if step % 20 == 0:print(sess.run(accuracy, feed_dict={x: batch_xs,y: batch_ys,}))step += 1

Ubuntu下终端输入:

tensorboard --logdir logs

卷积与训练过程效果展示:

《TensorFlow实例一 MINIST手写字体识别》相关推荐

  1. ComeFuture英伽学院——2020年 全国大学生英语竞赛【C类初赛真题解析】(持续更新)

    视频:ComeFuture英伽学院--2019年 全国大学生英语竞赛[C类初赛真题解析]大小作文--详细解析 课件:[课件]2019年大学生英语竞赛C类初赛.pdf 视频:2020年全国大学生英语竞赛 ...

  2. ComeFuture英伽学院——2019年 全国大学生英语竞赛【C类初赛真题解析】大小作文——详细解析

    视频:ComeFuture英伽学院--2019年 全国大学生英语竞赛[C类初赛真题解析]大小作文--详细解析 课件:[课件]2019年大学生英语竞赛C类初赛.pdf 视频:2020年全国大学生英语竞赛 ...

  3. 信息学奥赛真题解析(玩具谜题)

    玩具谜题(2016年信息学奥赛提高组真题) 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业.有一天, 这些玩具小人把小南的眼镜藏了起来.小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的 ...

  4. 信息学奥赛之初赛 第1轮 讲解(01-08课)

    信息学奥赛之初赛讲解 01 计算机概述 系统基本结构 信息学奥赛之初赛讲解 01 计算机概述 系统基本结构_哔哩哔哩_bilibili 信息学奥赛之初赛讲解 02 软件系统 计算机语言 进制转换 信息 ...

  5. 信息学奥赛一本通习题答案(五)

    最近在给小学生做C++的入门培训,用的教程是信息学奥赛一本通,刷题网址 http://ybt.ssoier.cn:8088/index.php 现将部分习题的答案放在博客上,希望能给其他有需要的人带来 ...

  6. 信息学奥赛一本通习题答案(三)

    最近在给小学生做C++的入门培训,用的教程是信息学奥赛一本通,刷题网址 http://ybt.ssoier.cn:8088/index.php 现将部分习题的答案放在博客上,希望能给其他有需要的人带来 ...

  7. 信息学奥赛一本通 提高篇 第六部分 数学基础 相关的真题

    第1章   快速幂 1875:[13NOIP提高组]转圈游戏 信息学奥赛一本通(C++版)在线评测系统 第2 章  素数 第 3 章  约数 第 4 章  同余问题 第 5 章  矩阵乘法 第 6 章 ...

  8. 信息学奥赛一本通题目代码(非题库)

    为了完善自己学c++,很多人都去读相关文献,就比如<信息学奥赛一本通>,可又对题目无从下手,从今天开始,我将把书上的题目一 一的解析下来,可以做参考,如果有错,可以告诉我,将在下次解析里重 ...

  9. 信息学奥赛一本通(C++版) 刷题 记录

    总目录详见:https://blog.csdn.net/mrcrack/article/details/86501716 信息学奥赛一本通(C++版) 刷题 记录 http://ybt.ssoier. ...

  10. 最近公共祖先三种算法详解 + 模板题 建议新手收藏 例题: 信息学奥赛一本通 祖孙询问 距离

    首先什么是最近公共祖先?? 如图:红色节点的祖先为红色的1, 2, 3. 绿色节点的祖先为绿色的1, 2, 3, 4. 他们的最近公共祖先即他们最先相交的地方,如在上图中黄色的点就是他们的最近公共祖先 ...

最新文章

  1. Camera框架初探
  2. hdu 4417 Super Mario 划分树+二分
  3. C/C++指针与内存管理
  4. java实现断点续传_Android 强升逻辑和实现
  5. EF批量插入太慢?那是你的姿势不对
  6. 使用jdbc执行SQL实现登录查询2-避免SQL注入版
  7. elipse调试linux内核,debug eclipse cdt + qemu虚拟机调试linux内核
  8. The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, forc
  9. 依靠信用卡周转生活的人多吗?
  10. springboot事务管理
  11. redmi airdots左右耳不串联怎么办_小米Redmi AirDots蓝牙耳机只能单边连接不能串联的解决办法...
  12. 【开源电机驱动】H桥基础知识
  13. ImageMagick (Magick++ for C++) configuration in Visual Studio 2012
  14. CocosCreator如何制作微信小游戏
  15. 视频编码中CBR和VBR的区别
  16. mendeley引用参考文献不显示_ubuntu下使用mendeley插入参考文献
  17. UML介绍及怎么看UML图
  18. 忠告,男人、女人各100条
  19. reac-hook的使用
  20. 东北大学和南邮的计算机,考研南邮跟东北大学通信与信息系统

热门文章

  1. 山西2019数据结构专升本_山西省2017年专升本数据结构真题
  2. 独家 | 首个5G远程手术成功,一文了解5G应用方向
  3. python django 图片管理系统
  4. 经纬度坐标转化为XYZ坐标的理解
  5. android 蓝牙速率测试软件,安卓蓝牙4.0开发测试 一个测试 APP - 下载 - 搜珍网
  6. 图像解析——(java)识别车牌步骤——更新中
  7. ApiFox 基本使用教程(浅尝辄止,非广)
  8. 计算100以内所有奇数的和以及所有偶数的和
  9. [NE-B]脑电波(三)
  10. 百度UNIT聊天API实现聊天对话