如何构建多层双向RNN?

时间:2018-05-05 16:34:14

标签: tensorflow

我在网上搜索了一些文章并尝试了相应的方法。

类型1:

def biLSTM(inputs, n_layers, seq_len, n_hidden, batch_size):
    output = inputs
    for n in range(n_layers):
        lstm_fw = tf.nn.rnn_cell.LSTMCell(n_hidden, state_is_tuple=True)
        lstm_bw = tf.nn.rnn_cell.LSTMCell(n_hidden, state_is_tuple=True)

        _initial_state_fw = lstm_fw.zero_state(batch_size, tf.float32)
        _initial_state_bw = lstm_bw.zero_state(batch_size, tf.float32)

        output, _states = tf.nn.bidirectional_dynamic_rnn(lstm_fw, lstm_bw, output,
                                                      initial_state_fw=_initial_state_fw,
                                                      initial_state_bw=_initial_state_bw,
                                                      sequence_length=seq_len,
                                                      scope='biLSTM' + str(n + 1))
        output = tf.concat([output[0], output[1]], 2)
        return output

其可视化图表:Graph of type1

类型2:

# Use "tf.nn.rnn_cell.MultiRNNCell" to create two different directional multi-layers cell

fw_multi_layers_cell = tf.nn.rnn_cell.MultiRNNCell(fw_multi_layers_list)
fw_states = multi_layers_cell.zero_state(batch_size, tf.float32)

bw_multi_layers_cell = tf.nn.rnn_cell.MultiRNNCell(bw_multi_layers_list)
bw_states = multi_layers_cell.zero_state(batch_size, tf.float32)
outputs, states = tf.nn.bidirectional_dynamic_rnn(fw_multi_layers_cell,
                                                 bw_multi_layers_cell,
                                                 input_data,
                                                 initial_state_fw=fw_states,
                                           initial_state_bw=bw_states,
                                           dtype=tf.float32,                                                         
                                           time_major=True)

其可视化图表:Graph of type2

我想知道它们之间有什么区别?

0 个答案:

没有答案