How to use multilayered bidirectional LSTM in Tensorflow?

前端 未结 4 1871
囚心锁ツ
囚心锁ツ 2021-02-06 05:02

I want to know how to use multilayered bidirectional LSTM in Tensorflow.

I have already implemented the contents of bidirectional LSTM, but I wanna compare this model wi

4条回答
  •  [愿得一人]
    2021-02-06 05:58

    You can use two different approaches to apply multilayer bilstm model:

    1) use out of previous bilstm layer as input to the next bilstm. In the beginning you should create the arrays with forward and backward cells of length num_layers. And

    for n in range(num_layers):
            cell_fw = cell_forw[n]
            cell_bw = cell_back[n]
    
            state_fw = cell_fw.zero_state(batch_size, tf.float32)
            state_bw = cell_bw.zero_state(batch_size, tf.float32)
    
            (output_fw, output_bw), last_state = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, output,
                                                                                 initial_state_fw=state_fw,
                                                                                 initial_state_bw=state_bw,
                                                                                 scope='BLSTM_'+ str(n),
                                                                                 dtype=tf.float32)
    
            output = tf.concat([output_fw, output_bw], axis=2)
    

    2) Also worth a look at another approach stacked bilstm.

提交回复
热议问题