Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a deep bidirectional LSTM with Keras?

I am trying to implement a LSTM based speech recognizer. So far I could set up bidirectional LSTM (i think it is working as a bidirectional LSTM) by following the example in Merge layer. Now I want to try it with another bidirectional LSTM layer, which make it a deep bidirectional LSTM. But I am unable to figure out how to connect the output of the previously merged two layers into a second set of LSTM layers. I don't know whether it is possible with Keras. Hope someone can help me with this.

Code for my single layer bidirectional LSTM is as follows

left = Sequential()
left.add(LSTM(output_dim=hidden_units, init='uniform', inner_init='uniform',
               forget_bias_init='one', return_sequences=True, activation='tanh',
               inner_activation='sigmoid', input_shape=(99, 13)))
right = Sequential()
right.add(LSTM(output_dim=hidden_units, init='uniform', inner_init='uniform',
               forget_bias_init='one', return_sequences=True, activation='tanh',
               inner_activation='sigmoid', input_shape=(99, 13), go_backwards=True))

model = Sequential()
model.add(Merge([left, right], mode='sum'))

model.add(TimeDistributedDense(nb_classes))
model.add(Activation('softmax'))

sgd = SGD(lr=0.1, decay=1e-5, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
print("Train...")
model.fit([X_train, X_train], Y_train, batch_size=1, nb_epoch=nb_epoches, validation_data=([X_test, X_test], Y_test), verbose=1, show_accuracy=True)

Dimensions of my x and y values are as follows.

(100, 'train sequences')
(20, 'test sequences')
('X_train shape:', (100, 99, 13))
('X_test shape:', (20, 99, 13))
('y_train shape:', (100, 99, 11))
('y_test shape:', (20, 99, 11))
like image 323
udani Avatar asked Feb 03 '16 05:02

udani


People also ask

What is bidirectional LSTM in deep learning?

Bidirectional LSTMs are an extension of traditional LSTMs that can improve model performance on sequence classification problems. In problems where all timesteps of the input sequence are available, Bidirectional LSTMs train two instead of one LSTMs on the input sequence.

Is bidirectional LSTM better than LSTM?

It can also be helpful in Time Series Forecasting problems, like predicting the electric consumption of a household. However, we can also use LSTM in this but Bidirectional LSTM will also do a better job in it.

What is bidirectional LSTM?

Bidirectional long-short term memory(bi-lstm) is the process of making any neural network o have the sequence information in both directions backwards (future to past) or forward(past to future). In bidirectional, our input flows in two directions, making a bi-lstm different from the regular LSTM.


3 Answers

Well, I got the answer for the issue posted on the Keras issues. Hope this would be useful to anyone who look for this kind of approach. How to implement deep bidirectional -LSTM

like image 183
udani Avatar answered Sep 23 '22 14:09

udani


model.add(Bidirectional(LSTM(64)))

Keras example

like image 38
rosefun Avatar answered Sep 24 '22 14:09

rosefun


You can use keras.layers.wrappers.Bidirectional. Official manual can be referenced here, https://keras.io/layers/wrappers/#bidirectional

like image 45
Tom Avatar answered Sep 24 '22 14:09

Tom