How to use multilayered bidirectional LSTM in Tensorflow?

前端 未结 4 1877
囚心锁ツ
囚心锁ツ 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:39

    On top of Taras's answer. Here is another example using just 2-layer Bidirectional RNN with GRU cells

        embedding_weights = tf.Variable(tf.random_uniform([vocabulary_size, state_size], -1.0, 1.0))
        embedding_vectors = tf.nn.embedding_lookup(embedding_weights, tokens)
    
        #First BLSTM
        cell = tf.nn.rnn_cell.GRUCell(state_size)
        cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=1-dropout)
        (forward_output, backward_output), _ = \
            tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs=embedding_vectors,
                                            sequence_length=lengths, dtype=tf.float32,scope='BLSTM_1')
        outputs = tf.concat([forward_output, backward_output], axis=2)
    
        #Second BLSTM using the output of previous layer as an input.
        cell2 = tf.nn.rnn_cell.GRUCell(state_size)
        cell2 = tf.nn.rnn_cell.DropoutWrapper(cell2, output_keep_prob=1-dropout)
        (forward_output, backward_output), _ = \
            tf.nn.bidirectional_dynamic_rnn(cell2, cell2, inputs=outputs,
                                            sequence_length=lengths, dtype=tf.float32,scope='BLSTM_2')
        outputs = tf.concat([forward_output, backward_output], axis=2)
    

    BTW, don't forget to add different scope name. Hope this help.

提交回复
热议问题