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)