import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist = input_data.read_data_sets("F:\TensorflowProject\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.core_rnn_cell.BasicLSTMCell(lstm_size)
lstm_cell = rnn.BasicLSTMCell(lstm_size)
# final_state[0]是cell state
# final_state[1]是hidden_state
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(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()

with tf.Session() 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})

    test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
    print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(test_acc ))

#############运行结果

Extracting F:\TensorflowProject\MNIST_data\train-images-idx3-ubyte.gz
Extracting F:\TensorflowProject\MNIST_data\train-labels-idx1-ubyte.gz
Extracting F:\TensorflowProject\MNIST_data\t10k-images-idx3-ubyte.gz
Extracting F:\TensorflowProject\MNIST_data\t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From <ipython-input-5-efe314a2932e>:40: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating: Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default. See tf.nn.softmax_cross_entropy_with_logits_v2. Iter 0, Testing Accuracy= 0.7372
Iter 1, Testing Accuracy= 0.8334
Iter 2, Testing Accuracy= 0.908
Iter 3, Testing Accuracy= 0.9182
Iter 4, Testing Accuracy= 0.9172
Iter 5, Testing Accuracy= 0.9266 ##############第二次运行
Iter  0  ,Testing Accuracy= 0.744
Iter 1 ,Testing Accuracy= 0.8028
Iter 2 ,Testing Accuracy= 0.8803
Iter 3 ,Testing Accuracy= 0.9115
Iter 4 ,Testing Accuracy= 0.9262
Iter 5 ,Testing Accuracy= 0.9303

Tensorflow递归神经网络学习练习的更多相关文章

  1. 用tensorflow构建神经网络学习简单函数

    目标是学习\(y=2x+3\) 建立一个5层的神经网络,用平方误差作为损失函数. 代码如下: import tensorflow as tf import numpy as np import tim ...

  2. 学习笔记CB010:递归神经网络、LSTM、自动抓取字幕

    递归神经网络可存储记忆神经网络,LSTM是其中一种,在NLP领域应用效果不错. 递归神经网络(RNN),时间递归神经网络(recurrent neural network),结构递归神经网络(recu ...

  3. 使用TensorFlow的递归神经网络(LSTM)进行序列预测

    本篇文章介绍使用TensorFlow的递归神经网络(LSTM)进行序列预测.作者在网上找到的使用LSTM模型的案例都是解决自然语言处理的问题,而没有一个是来预测连续值的. 所以呢,这里是基于历史观察数 ...

  4. tensorflow中使用mnist数据集训练全连接神经网络-学习笔记

    tensorflow中使用mnist数据集训练全连接神经网络 ——学习曹健老师“人工智能实践:tensorflow笔记”的学习笔记, 感谢曹老师 前期准备:mnist数据集下载,并存入data目录: ...

  5. TensorFlow(十一):递归神经网络(RNN与LSTM)

    RNN RNN(Recurrent Neural Networks,循环神经网络)不仅会学习当前时刻的信息,也会依赖之前的序列信息.由于其特殊的网络模型结构解决了信息保存的问题.所以RNN对处理时间序 ...

  6. 深度学习原理与框架-Tensorflow卷积神经网络-cifar10图片分类(代码) 1.tf.nn.lrn(局部响应归一化操作) 2.random.sample(在列表中随机选值) 3.tf.one_hot(对标签进行one_hot编码)

    1.tf.nn.lrn(pool_h1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75) # 局部响应归一化,使用相同位置的前后的filter进行响应归一化操作 参数 ...

  7. 深度学习原理与框架-Tensorflow卷积神经网络-神经网络mnist分类

    使用tensorflow构造神经网络用来进行mnist数据集的分类 相比与上一节讲到的逻辑回归,神经网络比逻辑回归多了隐藏层,同时在每一个线性变化后添加了relu作为激活函数, 神经网络使用的损失值为 ...

  8. 02基于python玩转人工智能最火框架之TensorFlow人工智能&深度学习介绍

    人工智能之父麦卡锡给出的定义 构建智能机器,特别是智能计算机程序的科学和工程. 人工智能是一种让计算机程序能够"智能地"思考的方式 思考的模式类似于人类. 什么是智能? 智能的英语 ...

  9. tensorflow(深度学习框架)详细讲解及实战

    还未完全写完,本人会一直持续更新!~ 各大深度学习框架总结和比较 各个开源框架在GitHub上的数据统计,如下表: 主流深度学习框架在各个维度的评分,如下表: Caffe可能是第一个主流的工业级深度学 ...

随机推荐

  1. MySQL InnoDB与MyISAM存储引擎差异

    言: 之前简单介绍过 MySQL 常用的存储引擎,今天对两个主流的存储简单分析下差异,书上没有参考的笔试题解答注解: 差异: MyISAM 只支持表锁,不支持事务,表损坏率较高.较老的存储引擎.   ...

  2. VMware设置NAT网络及 CentOS 7IP配置

    1.打开VMware,选择  编辑, 虚拟网络编辑器 2.默认情况下,VMware8为我们NAT所使用的网卡,选中VMnet8 3.此处设置我们的IP地址,这个随便指定,我这里设置成192.168.2 ...

  3. SQLSERVER XML 类型列的模糊查询

    select <column_name> from MyTable where <column_name>.value('(/root/sub-tag)[1]', 'varch ...

  4. 如何学习html画布呢(canvas)

    我列出了canvas教学资源 http://www.gbtags.com/gb/gbliblist/1.htm  这是极客标签(不是极客学院) http://study.163.com/course/ ...

  5. 字符集、字符编码、XML中的中文编码

    字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...

  6. Unity3D自定义资源配置文件

    http://blog.csdn.net/candycat1992/article/details/52181814 写在前面 我竟然最近两天才知道Unity中ScriptableObject的存在… ...

  7. 第二篇 Mysql常用操作记录(转载)

    我们在创建网站的时候,一般需要用到数据库.考虑到安全性,建议使用非root用户.常用命令如下: 1.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户my ...

  8. python中print的几种用法

    python中的print有几种常用的用法: 1. print("first example") 2. print("second", "exampl ...

  9. ping错误详解

    在网络中Ping 是一个十分好用的TCP/IP工具,它主要的功能是用来检测网络的连通情况和分析网络速度. 输入 ping /? 例出ping的参数 使用Ping检查连通性有五个步骤 1. 使用ipco ...

  10. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...