Bidirectional LSTM,由两个LSTMs上下叠加在 一起组成。输出由这两个LSTMs的隐藏层的状态决定。
def bilstm(self,x):
# 输入的数据格式转换
# x.shape [batch_size, time_steps, input_size]
x=tf.transpose(x,[1,0,2])
fw_x = tf.reshape(x, [-1, self.n_input_text]) # step*batch, feature
fw_x = tf.split(0, self.n_step_text, fw_x)
with tf.variable_scope('bilstm_lt'):
#定义Cell,单层LSTM
lstm_fw_cell = rnn_cell.BasicLSTMCell(self.n_hidden_text, forget_bias=1.0, state_is_tuple=True)#前向的lstm cell
lstm_bw_cell = rnn_cell.BasicLSTMCell(self.n_hidden_text, forget_bias=1.0, state_is_tuple=True)#反向的rnn cell
#dropout
lstm_fw_cell = rnn_cell.DropoutWrapper(cell=lstm_fw_cell, input_keep_prob=1.0, output_keep_prob=keep_prob)