tf.nn.dynamic_rnn(cell,inputs,sequence_length=None, initial_state=None,dtype=None, parallel_iterations=None,swap_memory=False, time_major=False, scope=None)

tf.nn.dynamic_rnn的作用:

  对于单个 RNNCell ,使用call 函数进行运算时,只在序列时间上前进了一步 ,如使用 x1、 ho 得到此h1,通过 x2 、h1 得到 h2 等 。

  如果序列长度为n,要调用n次call函数,比较麻烦。对此提供了一个tf.nn.dynamic_mn函数,使用该函数相当于调用了n次call函数。通过{ho, x1 , x2,…,xn} 直接得到{h1 , h2,…,hn} 。

  具体来说,设输入数据inputs格式为(batch_size, time_steps, input_size),其中batch_size表示batch的大小。time_steps序列长度,input_size输入数据单个序列单个时间维度上固有的长度。得到的outputs是time_steps步里所有的输出。它的形状为(batch_size, time_steps, cell.output_size)。state 是最后一步的隐状态,形状为(batch_size, cell . state_size) 。

参数:

cell

自己定义的LSTM的细胞单元,如是convLSTM,自己写也可以。

inputs

一个三维的变量,[batchsize,timestep,input_size],搭配time_major=False。其中batch_size表示batch的大小。time_steps序列长度,input_size输入数据单个序列单个时间维度上固有的长度。

这里还补充一点,就是叫dynamic的原因,就是输入数据的time_step不一定要相同,如果长短不一,会自动跟短的补0,但是处理时候,不会处理0,在0前面就截止了.这就是dynamic对比static的好处.

time_major

If true,   these Tensors must be shaped [max_time, batch_size, depth].
If false, these Tensors must be shaped `[batch_size, max_time, depth]

返回:

outputs:

If time_major == False, this will be a Tensor shaped: [batch_size, max_time, cell.output_size].(默认这种方式)

If time_major == True , this will be a Tensor shaped: [max_time, batch_size, cell.output_size].

cell.output_size就是cell的num_units

  这里output是每个cell输出的叠加,比如我输入数据[1,5,100,100,3],是一个长度为5 的视频序列,则返回output为[1,5,100,100,3],5个cell细胞的输出状态,state是一个元组类型的数据,有(c和h两个变量)就是存储LSTM最后一个cell的输出状态,我一般用的是output的最后一个输出.用state输出也行,就是取元组中的h变量.

state:

If cell.state_size is an int, this will be shaped [batch_size,cell.state_size].

If it is a TensorShape,             this will be shaped [batch_size] + cell.state_size.

If it is a (possibly nested) tuple of ints or TensorShape, this will be a tuple having the corresponding shapes.

If cells are LSTMCells state will be a tuple containing a LSTMStateTuple for each cell.

cell.state_size就是cell的num_units

例子:

#create a BasicRNNCell
rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size)
#'outputs' is a tensor of shape [batch_size, max_time, cell_state_size] #defining initial state
initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32) #'state' is a tensor of shape [batch_size, cell_state_size]
outputs, state = tf.nn.dynamic_rnn(cell=rnn_cell,inputs=input_data,initial_state=initial_state,dtype=tf.float32)
#create 2 LSTMCells
rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [128, 256]] #create a RNN cell composed sequentially of a number of RNNCells
multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers) #'outputs' is a tensor of shape [batch_size, max_time, 256]
#'state' is a N-tuple where N is the number of LSTMCells containing a
#tf.contrib.rnn.LSTMStateTuple for each cell
outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell,inputs=data,dtype=tf.float32)

tf.nn.dynamic_rnn的更多相关文章

  1. 深度学习原理与框架-递归神经网络-RNN_exmaple(代码) 1.rnn.BasicLSTMCell(构造基本网络) 2.tf.nn.dynamic_rnn(执行rnn网络) 3.tf.expand_dim(增加输入数据的维度) 4.tf.tile(在某个维度上按照倍数进行平铺迭代) 5.tf.squeeze(去除维度上为1的维度)

    1. rnn.BasicLSTMCell(num_hidden) #  构造单层的lstm网络结构 参数说明:num_hidden表示隐藏层的个数 2.tf.nn.dynamic_rnn(cell, ...

  2. 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 ...

  3. 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/78238 ...

  4. TF-卷积函数 tf.nn.conv2d 介绍

    转自 http://www.cnblogs.com/welhzh/p/6607581.html 下面是这位博主自己的翻译加上测试心得 tf.nn.conv2d是TensorFlow里面实现卷积的函数, ...

  5. tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例

    tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例 #!/usr/bin/env python # -*- coding: utf-8 ...

  6. tf.nn.conv2d 和 tf.nn.max_pool 中 padding 分别为 'VALID' 和 'SAME' 的直觉上的经验和测试代码

    这个地方一开始是迷糊的,写代码做比较分析,总结出直觉上的经验. 某人若想看精准的解释,移步这个网址(http://blog.csdn.net/fireflychh/article/details/73 ...

  7. 【TensorFlow基础】tf.add 和 tf.nn.bias_add 的区别

    1. tf.add(x,  y, name) Args: x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, ...

  8. tf.nn.conv2d。卷积函数

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  9. 深度学习原理与框架-图像补全(原理与代码) 1.tf.nn.moments(求平均值和标准差) 2.tf.control_dependencies(先执行内部操作) 3.tf.cond(判别执行前或后函数) 4.tf.nn.atrous_conv2d 5.tf.nn.conv2d_transpose(反卷积) 7.tf.train.get_checkpoint_state(判断sess是否存在

    1. tf.nn.moments(x, axes=[0, 1, 2])  # 对前三个维度求平均值和标准差,结果为最后一个维度,即对每个feature_map求平均值和标准差 参数说明:x为输入的fe ...

随机推荐

  1. hiveserver 占用内存过大的问题

    今天为了求解hiveserver占用内存过大的问题,特地加了hive在apache的邮件列表,讨论半天.特别说的是 里面的人确实很热情啊 ,外国人做事确实很认真,讨论帖发的时候都狠详细. 粘出一些记录 ...

  2. jquery 复选框(选中/取消)获取状态

    $("#login-rem").attr("checked","checked") //设置选中 $("#login-rem&qu ...

  3. Zabbix-2.4-安装-1

    前言 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix组件主要分两个: zabbix-server和zabbix-agent.支持的监控协议有I ...

  4. gets_s()函数的参数太少,strcpy_s():形参和实参 2 的类型不同,等c函数在Visual Studio上出现的问题, get()函数和scanf()读取字符串的区别,栈的随机性

    首先,这些C函数,在VS上要加_s后缀的原因是,这些函数存在字符串越界等问题,可以参考这篇文章,https://blog.csdn.net/silleyj/article/details/854540 ...

  5. Centos中iptables和firewall防火墙开启、关闭、查看状态、基本设置等(转)

    iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status   # 停止防火墙 service iptables stop   # 启动防火墙 servi ...

  6. Hanlp中使用纯JAVA实现CRF分词

    Hanlp中使用纯JAVA实现CRF分词 与基于隐马尔可夫模型的最短路径分词.N-最短路径分词相比,基于条件随机场(CRF)的分词对未登录词有更好的支持.本文(HanLP)使用纯Java实现CRF模型 ...

  7. MAC上使用Enterprise Architecture,附带安装步骤及破解链接

    绪论 网上找了半天这个主题也没有详细的步骤的昂,所以自己来造轮子了. 还有,百度搜EA破解版不靠谱,大搜狗更给力哦! 一.背景 穷逼只有一台存储空间不大MACAir,分给虚拟机Virtual Box的 ...

  8. 服务发现:Zookeeper vs etcd vs Consul 参考自http://dockone.io/article/667

    服务发现:Zookeeper vs etcd vs Consul [编者的话]本文对比了Zookeeper.etcd和Consul三种服务发现工具,探讨了最佳的服务发现解决方案,仅供参考. 如果使用预 ...

  9. NGINX 资料

    查看资料详情     nginx

  10. RTB业务知识之2-Impression概念和关键属性

    一.定义-impression This object describes an ad placement or impression being auctioned. A single bid re ...