tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
https://blog.csdn.net/u014365862/article/details/78238807
MachineLP的Github(欢迎follow):https://github.com/MachineLP
我的GitHub:https://github.com/MachineLP/train_cnn-rnn-attention 自己搭建的一个框架,包含模型有:vgg(vgg16,vgg19), resnet(resnet_v2_50,resnet_v2_101,resnet_v2_152), inception_v4, inception_resnet_v2等。
- chunk_size = 256
- chunk_n = 160
- rnn_size = 256
- num_layers = 2
- n_output_layer = MAX_CAPTCHA*CHAR_SET_LEN # 输出层
单层rnn:
tf.contrib.rnn.static_rnn:
输入:[步长,batch,input]
输出:[n_steps,batch,n_hidden]
还有rnn中加dropout
- def recurrent_neural_network(data):
- data = tf.reshape(data, [-1, chunk_n, chunk_size])
- data = tf.transpose(data, [1,0,2])
- data = tf.reshape(data, [-1, chunk_size])
- data = tf.split(data,chunk_n)
- # 只用RNN
- layer = {'w_':tf.Variable(tf.random_normal([rnn_size, n_output_layer])), 'b_':tf.Variable(tf.random_normal([n_output_layer]))}
- lstm_cell = tf.contrib.rnn.BasicLSTMCell(rnn_size)
- outputs, status = tf.contrib.rnn.static_rnn(lstm_cell, data, dtype=tf.float32)
- # outputs = tf.transpose(outputs, [1,0,2])
- # outputs = tf.reshape(outputs, [-1, chunk_n*rnn_size])
- ouput = tf.add(tf.matmul(outputs[-1], layer['w_']), layer['b_'])
- return ouput
多层rnn:
tf.nn.dynamic_rnn:
输入:[batch,步长,input]
输出:[batch,n_steps,n_hidden]
所以我们需要tf.transpose(outputs, [1, 0, 2]),这样就可以取到最后一步的output
- def recurrent_neural_network(data):
- # [batch,chunk_n,input]
- data = tf.reshape(data, [-1, chunk_n, chunk_size])
- #data = tf.transpose(data, [1,0,2])
- #data = tf.reshape(data, [-1, chunk_size])
- #data = tf.split(data,chunk_n)
- # 只用RNN
- layer = {'w_':tf.Variable(tf.random_normal([rnn_size, n_output_layer])), 'b_':tf.Variable(tf.random_normal([n_output_layer]))}
- #1
- # lstm_cell1 = tf.contrib.rnn.BasicLSTMCell(rnn_size)
- # outputs1, status1 = tf.contrib.rnn.static_rnn(lstm_cell1, data, dtype=tf.float32)
- def lstm_cell():
- return tf.contrib.rnn.LSTMCell(rnn_size)
- def attn_cell():
- return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=keep_prob)
- # stack = tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(0, num_layers)], state_is_tuple=True)
- stack = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(0, num_layers)], state_is_tuple=True)
- # outputs, _ = tf.nn.dynamic_rnn(stack, data, seq_len, dtype=tf.float32)
- outputs, _ = tf.nn.dynamic_rnn(stack, data, dtype=tf.float32)
- # [batch,chunk_n,rnn_size] -> [chunk_n,batch,rnn_size]
- outputs = tf.transpose(outputs, (1, 0, 2))
- ouput = tf.add(tf.matmul(outputs[-1], layer['w_']), layer['b_'])
- return ouput
tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别的更多相关文章
- 深度学习原理与框架-递归神经网络-RNN网络基本框架(代码?) 1.rnn.LSTMCell(生成单层LSTM) 2.rnn.DropoutWrapper(对rnn进行dropout操作) 3.tf.contrib.rnn.MultiRNNCell(堆叠多层LSTM) 4.mlstm_cell.zero_state(state初始化) 5.mlstm_cell(进行LSTM求解)
问题:LSTM的输出值output和state是否是一样的 1. rnn.LSTMCell(num_hidden, reuse=tf.get_variable_scope().reuse) # 构建 ...
- 关于tensorflow里面的tf.contrib.rnn.BasicLSTMCell 中num_units参数问题
这里的num_units参数并不是指这一层油多少个相互独立的时序lstm,而是lstm单元内部的几个门的参数,这几个门其实内部是一个神经网络,答案来自知乎: class TRNNConfig(obje ...
- tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be replaced by tf.contrib.rnn.BasicLSTMCell.
For Tensorflow 1.2 and Keras 2.0, the line tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be repl ...
- tensorflow教程:tf.contrib.rnn.DropoutWrapper
tf.contrib.rnn.DropoutWrapper Defined in tensorflow/python/ops/rnn_cell_impl.py. def __init__(self, ...
- tf.contrib.rnn.LSTMCell 里面参数的意义
num_units:LSTM cell中的单元数量,即隐藏层神经元数量.use_peepholes:布尔类型,设置为True则能够使用peephole连接cell_clip:可选参数,float类型, ...
- tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入
一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...
- tf.nn.dynamic_rnn
tf.nn.dynamic_rnn(cell,inputs,sequence_length=None, initial_state=None,dtype=None, parallel_iteratio ...
- TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu
import tensorflow as tf # 22 scope (name_scope/variable_scope) from __future__ import print_function ...
- 第十六节,使用函数封装库tf.contrib.layers
这一节,介绍TensorFlow中的一个封装好的高级库,里面有前面讲过的很多函数的高级封装,使用这个高级库来开发程序将会提高效率. 我们改写第十三节的程序,卷积函数我们使用tf.contrib.lay ...
随机推荐
- 在windows下安装配置python开发环境及Ulipad开发工具(转)
最近开始学习Python,在网上寻找一下比较好的IDE.因为以前用C#做开发的,用Visual Studio作为IDE,鉴于用惯了VS这么强大的IDE,所以对IDE有一定的依赖性. Python的ID ...
- AutoFac简单入门
AutoFac是.net程序下一个非常灵活易用,且功能强大的DI框架,本文这里简单的介绍一下使用方法. 安装: Install-Package Autofac 简单的示例: static void M ...
- 【Go命令教程】5. go clean
执行 go clean 命令会删除掉执行其它命令时产生的一些文件和目录,包括: 在使用 go build 命令时在当前代码包下生成的与包名同名或者与Go源码文件同名的可执行文件.在Windows下,则 ...
- Apache Mina Filter
Mina中的过滤器处于IoService与IoHandler之间,用于过滤每一个I/O事件.本文分析Mina中的过滤器是怎么串起来的? 前面提到了IoFilter,FilterChain等接口和类,在 ...
- python websocket-client connection
参考:https://pypi.python.org/pypi/websocket-client/ https://www.cnblogs.com/saryli/p/6702260.html i ...
- Mysql5.6主从复制-基于binlog
MySQL5.6开始主从复制有两种方式:基于日志(binlog):基于GTID(全局事务标示符). 此文章是基于日志方式的配置步骤 环境: master数据库IP:192.168.247.128sla ...
- AngularJS路由系列(6)-- UI-Router的嵌套State
本系列探寻AngularJS的路由机制,在WebStorm下开发.本篇主要涉及UI-Route的嵌套State. 假设一个主视图上有两个部分视图,部分视图1和部分视图2,主视图对应着一个state,两 ...
- <c:otherwise>
<c:if>没有<c:else>可以用<c:choose>来取代结构:<c:choose> <c:when test=""&g ...
- ExtJS动态设置表头
if(document.getElementById("lxdj_radio").checked){ colQd = new Ext.grid.ColumnModel(colMAr ...
- python测试开发django-21.admin后台表名称和字段显示中文
前言 admin后台页面表名称(默认会多加一个s)和字段名称是直接显示在后台的,如果我们想设置成中文显示需加verbose_name和verbose_name_plural属性 verbose_nam ...