Implementing bidirectional RNNs
Until now, we've considered the to flow in one direction through our network. However, sometimes it's beneficial to run the data through the network in both directions. We call such networks bidirectional RNNs. In the following example, we will implement the same LSTM network as we've implemented previously, but this time we will use a bidirectional RNN to classify the sentiment.
How to do it...
- Let's start with importing the libraries as follows:
import numpy as np from keras.preprocessing import sequence from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Embedding, LSTM, Bidirectional from keras.callbacks import EarlyStopping from keras.datasets import imdb
- We will be using the IMDB dataset from Keras; load the data with the following code:
n_words = 1000 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=n_words) print('Train seq: {}'.format(len(X_train))) print('Test seq: {}'.format(len(X_train)))
- Let's print...