Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bi-directional LSTM for variable-length sequence in Tensorflow

I want to train a bi-directional LSTM in tensorflow to perform a sequence classification problem (sentiment classification).

Because sequences are of variable lengths, batches are normally padded with vectors of zero. Normally, I use the sequence_length parameter in the uni-directional RNN to avoid training on the padding vectors.

How can this be managed with bi-directional LSTM. Does the "sequence_length" parameter work automatically starts from an advanced position in the sequence for the backward direction?

Thank you

like image 810
Ramy Baly Avatar asked Mar 21 '17 19:03

Ramy Baly


People also ask

Can we use bidirectional LSTM for time series?

In summary, this concise demonstration stresses the idea that bidirectional LSTMs are effective models for time series forecasting — here, using the Bitstamp dataset for Bitcoin as input data for the network.

How do you use bidirectional LSTM?

Code Implementation of Bidirectional-LSTMSetting up the environment in google colab. In the above, we have defined some objects we will use in the next steps. In the next step, we will load the data set from the Keras library. To fit the data into any neural network, we need to convert the data into sequence matrices.

Why bidirectional LSTM is better than unidirectional LSTM?

The main reason is that every component of an input sequence has information from both the past and present. For this reason, BiLSTM can produce a more meaningful output, combining LSTM layers from both directions.


1 Answers

bidirectional_dynamic_rnn also has a sequence_length parameter that takes care of sequences of variable lengths.

https://www.tensorflow.org/api_docs/python/tf/nn/bidirectional_dynamic_rnn (mirror):

sequence_length: An int32/int64 vector, size [batch_size], containing the actual lengths for each of the sequences.

You can see an example here: https://github.com/Franck-Dernoncourt/NeuroNER/blob/master/src/entity_lstm.py

like image 65
Franck Dernoncourt Avatar answered Sep 29 '22 02:09

Franck Dernoncourt