分析:
看 TensorFlow 也有一段时间了,准备按照 GitHub 上的教程,敲出来,顺便整理一下思路。
RNN部分
  1. 定义参数,包括数据相关,训练相关。
  2. 定义模型,损失函数,优化函数。
  3. 训练,准备数据,输入数据,输出结果。

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*- import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib import rnn mnist=input_data.read_data_sets("./data",one_hot=True) training_rate=0.001
training_iters=100000
batch_size=128
display_step=10 n_input=28
n_steps=28
n_hidden=128
n_classes=10 x=tf.placeholder("float",[None,n_steps,n_input])
y=tf.placeholder("float",[None,n_classes]) weights={'out':tf.Variable(tf.random_normal([n_hidden,n_classes]))}
biases={'out':tf.Variable(tf.random_normal([n_classes]))} def RNN(x,weights,biases):
x=tf.unstack(x,n_steps,1)
lstm_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
outputs,states=rnn.static_rnn(lstm_cell,x,dtype=tf.float32)
return tf.matmul(outputs[-1],weights['out'])+biases['out'] pred=RNN(x,weights,biases)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=training_rate).minimize(cost) correct_pred=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accuaracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32)) init=tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
step=1
while step*batch_size<training_iters:
batch_x,batch_y=mnist.train.next_batch(batch_size)
batch_x=batch_x.reshape(batch_size,n_steps,n_input)
sess.run(optimizer,feed_dict={x:batch_x,y:batch_y})
if step%display_step==0:
acc=sess.run(accuaracy,feed_dict={x:batch_x,y:batch_y})
loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
print("Iter " + str(step * batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step+=1

输出:

/anaconda/bin/python2.7 /Users/xxxx/PycharmProjects/TF_3/tf_rnn.py
Extracting ./data/train-images-idx3-ubyte.gz
Extracting ./data/train-labels-idx1-ubyte.gz
Extracting ./data/t10k-images-idx3-ubyte.gz
Extracting ./data/t10k-labels-idx1-ubyte.gz
2017-07-15 16:41:15.125981: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn’t compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-15 16:41:15.125994: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn’t compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-15 16:41:15.125997: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn’t compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-15 16:41:15.126002: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn’t compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Iter 1280, Minibatch Loss= 1.842738, Training Accuracy= 0.33594
Iter 2560, Minibatch Loss= 1.489123, Training Accuracy= 0.50000
Iter 3840, Minibatch Loss= 1.300060, Training Accuracy= 0.57812
Iter 5120, Minibatch Loss= 1.244872, Training Accuracy= 0.62500
Iter 6400, Minibatch Loss= 0.947143, Training Accuracy= 0.71094
Iter 7680, Minibatch Loss= 0.709695, Training Accuracy= 0.75781
Iter 8960, Minibatch Loss= 0.799844, Training Accuracy= 0.76562
Iter 10240, Minibatch Loss= 0.594611, Training Accuracy= 0.83594
Iter 11520, Minibatch Loss= 0.529350, Training Accuracy= 0.82031
Iter 12800, Minibatch Loss= 0.624426, Training Accuracy= 0.82031
Iter 14080, Minibatch Loss= 0.481889, Training Accuracy= 0.82812
Iter 15360, Minibatch Loss= 0.449692, Training Accuracy= 0.84375
Iter 16640, Minibatch Loss= 0.418820, Training Accuracy= 0.85938
Iter 17920, Minibatch Loss= 0.412161, Training Accuracy= 0.85156
Iter 19200, Minibatch Loss= 0.256099, Training Accuracy= 0.90625
Iter 20480, Minibatch Loss= 0.227309, Training Accuracy= 0.90625
Iter 21760, Minibatch Loss= 0.431014, Training Accuracy= 0.85938
Iter 23040, Minibatch Loss= 0.377097, Training Accuracy= 0.87500
Iter 24320, Minibatch Loss= 0.268153, Training Accuracy= 0.89844
Iter 25600, Minibatch Loss= 0.170557, Training Accuracy= 0.95312
Iter 26880, Minibatch Loss= 0.286947, Training Accuracy= 0.91406
Iter 28160, Minibatch Loss= 0.189623, Training Accuracy= 0.94531
Iter 29440, Minibatch Loss= 0.228949, Training Accuracy= 0.95312
Iter 30720, Minibatch Loss= 0.157198, Training Accuracy= 0.94531
Iter 32000, Minibatch Loss= 0.205744, Training Accuracy= 0.93750
Iter 33280, Minibatch Loss= 0.195218, Training Accuracy= 0.92188
Iter 34560, Minibatch Loss= 0.177956, Training Accuracy= 0.92969
Iter 35840, Minibatch Loss= 0.131563, Training Accuracy= 0.96875
Iter 37120, Minibatch Loss= 0.215156, Training Accuracy= 0.92969
Iter 38400, Minibatch Loss= 0.232274, Training Accuracy= 0.94531
Iter 39680, Minibatch Loss= 0.324053, Training Accuracy= 0.91406
Iter 40960, Minibatch Loss= 0.196385, Training Accuracy= 0.93750
Iter 42240, Minibatch Loss= 0.151221, Training Accuracy= 0.95312
Iter 43520, Minibatch Loss= 0.242021, Training Accuracy= 0.95312
Iter 44800, Minibatch Loss= 0.304008, Training Accuracy= 0.90625
Iter 46080, Minibatch Loss= 0.185177, Training Accuracy= 0.93750
Iter 47360, Minibatch Loss= 0.190960, Training Accuracy= 0.94531
Iter 48640, Minibatch Loss= 0.141995, Training Accuracy= 0.94531
Iter 49920, Minibatch Loss= 0.199995, Training Accuracy= 0.94531
Iter 51200, Minibatch Loss= 0.193773, Training Accuracy= 0.92188
Iter 52480, Minibatch Loss= 0.151757, Training Accuracy= 0.94531
Iter 53760, Minibatch Loss= 0.153755, Training Accuracy= 0.94531
Iter 55040, Minibatch Loss= 0.141472, Training Accuracy= 0.93750
Iter 56320, Minibatch Loss= 0.168057, Training Accuracy= 0.96094
Iter 57600, Minibatch Loss= 0.135691, Training Accuracy= 0.96094
Iter 58880, Minibatch Loss= 0.097003, Training Accuracy= 0.97656
Iter 60160, Minibatch Loss= 0.274090, Training Accuracy= 0.92188
Iter 61440, Minibatch Loss= 0.147230, Training Accuracy= 0.95312
Iter 62720, Minibatch Loss= 0.106019, Training Accuracy= 0.96094
Iter 64000, Minibatch Loss= 0.101133, Training Accuracy= 0.97656
Iter 65280, Minibatch Loss= 0.169548, Training Accuracy= 0.93750
Iter 66560, Minibatch Loss= 0.101966, Training Accuracy= 0.96094
Iter 67840, Minibatch Loss= 0.106501, Training Accuracy= 0.96875
Iter 69120, Minibatch Loss= 0.082817, Training Accuracy= 0.96875
Iter 70400, Minibatch Loss= 0.192926, Training Accuracy= 0.96094
Iter 71680, Minibatch Loss= 0.086935, Training Accuracy= 0.96875
Iter 72960, Minibatch Loss= 0.052052, Training Accuracy= 0.98438
Iter 74240, Minibatch Loss= 0.129968, Training Accuracy= 0.95312
Iter 75520, Minibatch Loss= 0.058070, Training Accuracy= 0.99219
Iter 76800, Minibatch Loss= 0.089518, Training Accuracy= 0.96875
Iter 78080, Minibatch Loss= 0.106092, Training Accuracy= 0.98438
Iter 79360, Minibatch Loss= 0.223101, Training Accuracy= 0.92188
Iter 80640, Minibatch Loss= 0.069419, Training Accuracy= 0.97656
Iter 81920, Minibatch Loss= 0.050585, Training Accuracy= 0.99219
Iter 83200, Minibatch Loss= 0.048002, Training Accuracy= 0.98438
Iter 84480, Minibatch Loss= 0.094293, Training Accuracy= 0.96875
Iter 85760, Minibatch Loss= 0.152253, Training Accuracy= 0.96094
Iter 87040, Minibatch Loss= 0.085382, Training Accuracy= 0.97656
Iter 88320, Minibatch Loss= 0.147018, Training Accuracy= 0.95312
Iter 89600, Minibatch Loss= 0.099780, Training Accuracy= 0.96094
Iter 90880, Minibatch Loss= 0.118362, Training Accuracy= 0.93750
Iter 92160, Minibatch Loss= 0.110498, Training Accuracy= 0.96094
Iter 93440, Minibatch Loss= 0.077664, Training Accuracy= 0.98438
Iter 94720, Minibatch Loss= 0.070865, Training Accuracy= 0.96094
Iter 96000, Minibatch Loss= 0.156309, Training Accuracy= 0.94531
Iter 97280, Minibatch Loss= 0.116825, Training Accuracy= 0.94531
Iter 98560, Minibatch Loss= 0.099852, Training Accuracy= 0.96875
Iter 99840, Minibatch Loss= 0.116358, Training Accuracy= 0.96875 Process finished with exit code 0

原文链接:http://www.tensorflownews.com/2017/07/15/tensorflow-rnn-turorial-mnist-code/

TensorFlow RNN 教程和代码的更多相关文章

  1. tensorflow rnn 最简单实现代码

    tensorflow rnn 最简单实现代码 #!/usr/bin/env python # -*- coding: utf-8 -*- import tensorflow as tf from te ...

  2. Tensorflow快餐教程(1) - 30行代码搞定手写识别

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/details ...

  3. Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例

    紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...

  4. TensorFlow 安装教程

    1.准备好Anaconda环境 tensorflow是属于很高层的应用.高层应用的一个比较大的麻烦就是需要依赖的底层的东西很多,如果底层依赖没有弄好的话,高层应用是没法玩转的. 在极客学院有关tens ...

  5. (转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)

    干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码) 该博客来源自:https://mp.weixin.qq.com/s?__biz=MzA4NzE1NzYyMw==& ...

  6. TensorFlow (RNN)深度学习 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 序列标注问题 源码下载

    http://blog.csdn.net/scotfield_msn/article/details/60339415 在TensorFlow (RNN)深度学习下 双向LSTM(BiLSTM)+CR ...

  7. 云栖社区 Tensorflow快餐教程

    云栖社区 Tensorflow快餐教程(1) - 30行代码搞定手写识别:https://yq.aliyun.com/articles/582122云栖社区 Tensorflow快餐教程(2) - 标 ...

  8. Tensorflow学习教程------过拟合

    Tensorflow学习教程------过拟合   回归:过拟合情况 / 分类过拟合 防止过拟合的方法有三种: 1 增加数据集 2 添加正则项 3 Dropout,意思就是训练的时候隐层神经元每次随机 ...

  9. windows下用pycharm安装tensorflow简易教程

    https://blog.csdn.net/heros_never_die/article/details/79760616 最近开始学习深度学习的相关知识,准备实战一下,看了一些关于tensorfl ...

随机推荐

  1. Vue.observable()使用方法

    前言 随着组件的细化,就会遇到多组件状态共享的情况, Vuex当然可以解决这类问题,不过就像 Vuex官方文档所说的,如果应用不够大,为避免代码繁琐冗余,最好不要使用它,今天我们介绍的是 vue.js ...

  2. 整合 KAFKA+Flink 实例(第一部分,趟坑记录)

    2017年后,一大波网络喧嚣,说流式处理如何牛叉,如何高大上,抱歉,工作满负荷,没空玩那个: 今年疫情隔离在家,无聊,开始学习 KAFKA+Flink ,目前的打算是用爬虫抓取网页数据,传递到Kafk ...

  3. python类变量与构造函数的使用

    类变量:可在类的所有实例之间共享的变量 实例类对象:类的实例是调用类对象来创建的.如:par = Parent(),par就是类Parent的一个实例类对象. 实例变量(成员变量):同一个类对象可以创 ...

  4. Docker: Error response from daemon: Get.........unauthorized: incorrect username or password

    今天在Centos中使用docker拉取redis镜像时报Error response from daemon: Get https://registry-1.docker.io/v2/library ...

  5. UBB代码

    UBB代码是HTML(标准通用标记语言下的一个应用)的一个变种,是Ultimate Bulletin Board (国外的一个BBS程序)采用的一种特殊的TAG.您也许已经对它很熟悉了.UBB代码很简 ...

  6. 数据结构-ST表

    数据结构-ST表 不可修改,在线查询的 RMQ 问题. 其中 \(f[i][j]\) 表示 \(i\sim i+(1<<j)-1\) 这段的 RMQ 值. 时间复杂度 \(O(n\log ...

  7. python之三元表达式,列表|字典推导式,函数对象

    #### 三元表达式: 就是if....else...的语法糖 # -- 1) 只能解决if...else...结构,其他if分支结构都不管 # -- 2)一个分支提供一个结果: 如果一个分支提供了多 ...

  8. js中排序方法

    有些代码一两个月都会忘了,有空多做下总结,记录下来,等需要用到的时候可以来翻翻总结的博客.写技术博客,对自己是一种总结,对别人,是一种参考. 1.sort()方法 var ar1=[2,4,6,8,1 ...

  9. Docker 使用杂记 - 最佳实践尝试 - 实战

    目录 Docker 使用杂记 - 最佳实践尝试 - 实战 Docker简介 项目背景 内在原因 外在原因 基础镜像 需求 镜像维护者 工作文件夹 文件 ADD COPY 宗卷 命令 入口点 Docke ...

  10. Microsoft Visual Studio 修改语言包

    需求内容: 更改 Microsoft Visual Studio 界面的语言包(将中文改为英文) 解决方案: https://docs.microsoft.com/zh-cn/visualstudio ...