TensorFlow RNN 教程和代码
分析:
- 定义参数,包括数据相关,训练相关。
- 定义模型,损失函数,优化函数。
- 训练,准备数据,输入数据,输出结果。
代码:
#!/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 教程和代码的更多相关文章
- tensorflow rnn 最简单实现代码
tensorflow rnn 最简单实现代码 #!/usr/bin/env python # -*- coding: utf-8 -*- import tensorflow as tf from te ...
- Tensorflow快餐教程(1) - 30行代码搞定手写识别
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/details ...
- Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例
紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...
- TensorFlow 安装教程
1.准备好Anaconda环境 tensorflow是属于很高层的应用.高层应用的一个比较大的麻烦就是需要依赖的底层的东西很多,如果底层依赖没有弄好的话,高层应用是没法玩转的. 在极客学院有关tens ...
- (转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)
干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码) 该博客来源自:https://mp.weixin.qq.com/s?__biz=MzA4NzE1NzYyMw==& ...
- TensorFlow (RNN)深度学习 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 序列标注问题 源码下载
http://blog.csdn.net/scotfield_msn/article/details/60339415 在TensorFlow (RNN)深度学习下 双向LSTM(BiLSTM)+CR ...
- 云栖社区 Tensorflow快餐教程
云栖社区 Tensorflow快餐教程(1) - 30行代码搞定手写识别:https://yq.aliyun.com/articles/582122云栖社区 Tensorflow快餐教程(2) - 标 ...
- Tensorflow学习教程------过拟合
Tensorflow学习教程------过拟合 回归:过拟合情况 / 分类过拟合 防止过拟合的方法有三种: 1 增加数据集 2 添加正则项 3 Dropout,意思就是训练的时候隐层神经元每次随机 ...
- windows下用pycharm安装tensorflow简易教程
https://blog.csdn.net/heros_never_die/article/details/79760616 最近开始学习深度学习的相关知识,准备实战一下,看了一些关于tensorfl ...
随机推荐
- hexo+github搭建自己的博客
之前很早就想用hexo弄一个自己独立的博客了,在博客园也写了很多的博客,不过不喜欢博客园的风格.不过今天,终于折腾成功了,用hexo搭建了一个在github写的博客,开心,后面会将自己以前的博客慢慢迁 ...
- C++走向远洋——40(第九周,深复制体验)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- c#百度IP定位API使用方法
c#百度IP定位API使用方法 1.先建立一个收集信息的实体类 IPModel.cs: using System; using System.Collections.Generic; using Sy ...
- 用table类型布局一个新闻网页
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- FC及BFC
1.什么是FC 2.BFC块级格式化上下文(Block formatting context) Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很多个 Box 组成的.元素的类 ...
- Java 读取Word中的脚注、尾注
本文介绍读取Word中的脚注及尾注的方法,添加脚注.尾注可以参考这篇文章. 注:本文使用了Word类库(Free Spire.Doc for Java 免费版)来读取,获取该类库可通过官网下载,并解压 ...
- Linux apache开启虚拟主机伪静态.htaccess
打开apache配置文件 /etc/httpd/conf/httpd.conf 查找“#LoadModule rewrite_module modules/mod_rewrite.so” 去掉前面的# ...
- Latex数学符号对应表
1. 希腊字母 字母 实现 字母 实现 $\alpha$ \alpha $\Alpha$ \Alpha $\beta$ \beta $\Beta$ \Beta $\gamma$ \gamma $\Ga ...
- Simulink仿真入门到精通(十八) TLC语言
TLC(Target Language Compiler)是一种为转换为目标语言而存在的额解释性语言,其目的就是将模型中编译出来的rtw文件转换为目标代码(C/C++等).与M语言类似,既可以写成脚本 ...
- Element UI中的上传文件功能
上传文件给后台: <el-upload style="display:inline-block" :limit=" class="upload-demo& ...