tensorflow学习之stack_bidirectional_rnn使用详解

tf.contrib.rnn.stack_bidirectional_rnn

tf.contrib.rnn.stack_bidirectional_rnn(
    cells_fw,
    cells_bw,
    inputs,
    initial_states_fw=None,
    initial_states_bw=None,
    dtype=None,
    sequence_length=None,
    scope=None
)

创建一个双向循环神经网络。

堆叠几个双向rnn层。组合的前向和后向层输出用作下一层的输入。Tf.bidirectional_rnn不允许在层之间共享前向和后向信息。第一个前向和后向cells必须皮匹配。两个方向的初始状态为0.并且不返回中间状态。

参数说明:

  • cells_fw:RNNCell的实例list,每层一个,用于前向。
  • cells_bw: RNNCell的实例list,每层一个,用于后向。
  • inputs:一个长度为T的输出list,每个张量的形状为[batch_size,input_size]
  • initial_states_fw:可选参数。前向RNN的初始状态list.每个张量必须具有适当的类型和形状[batch_size,cell_fw.state_size].
  •  
  • Initial_state_bw: 可选参数。后向RNN的初始状态list.每个张量必须具有适当的类型和形状[batch_size,cell_bw.state_size].
  • dtype:可选参数。初始状态的数据类型。初始状态没提供时则要求该参数。
  • sequence_length:可选参数,一个int32/int64的向量,大小为[batch_size],包含每个序列的实际长度。
  • scope:创建的子图的VariableScope。默认为None。

返回:

一个(outputs, output_state_fw, output_state_bw)元组,其中:

  • outputs:是一个长度为T的列表(每个输入对应一个),它们是深度级联的前向和后向输出。
  • output_states_fw:前向rnn的最终输出状态,每个层一个。
  • output_states_bw:后向rnn的最终输出状态,每个层一个。

 

代码实例:

import tensorflow as tf


fw_units=[10,20,30]
bw_units=[10,20,30]
fw_cells=[tf.nn.rnn_cell.BasicLSTMCell(unit) for unit in fw_units] #前向LSTM层
bw_cells=[tf.nn.rnn_cell.BasicLSTMCell(unit) for unit in bw_units] #后向LSTM层
batch_size=10
max_time=100
depth=64
inputs=tf.Variable(tf.random_normal([batch_size,max_time,depth]))
inputs=tf.unstack(inputs,axis=1)
outputs, output_state_fw, output_state_bw=tf.contrib.rnn.stack_bidirectional_rnn(fw_cells,bw_cells,inputs,dtype=tf.float32)
print(len(outputs)) #100
print(outputs[0].shape) #(10, 60)
print(outputs[-1].shape) #(10, 60)
print(len(output_state_fw)) #3
print(output_state_fw[0].h)
print(output_state_fw[0].c.shape) #(10, 10)
print(output_state_fw[1].c.shape) #(10, 20)
print(output_state_bw[0].c.shape) #(10, 10)
print(output_state_fw[2].h.shape) #(10, 30)

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值