title bidirectional lstm on imdb fchollethttps twitter comfchollet date created 20200503 last modified 20200503 description train a 2layer bidirectional lstm on the imdb movie review sentiment classification dataset accelerator gpu setup build the model input for variablelength sequences of integers embed each integer in a 128dimensional vector add 2 bidirectional lstms add a classifier load the imdb movie review sentiment data use padsequence to standardize sequence length this will truncate sequences longer than 200 words and zeropad sequences shorter than 200 words train and evaluate the model you can use the trained model hosted on hugging face hubhttps huggingface cokerasiobidirectionallstmimdb and try the demo on hugging face spaceshttps huggingface cospaceskerasiobidirectionallstmimdb setup only consider the top 20k words only consider the first 200 words of each movie review build the model input for variable length sequences of integers embed each integer in a 128 dimensional vector add 2 bidirectional lstms add a classifier load the imdb movie review sentiment data use pad_sequence to standardize sequence length this will truncate sequences longer than 200 words and zero pad sequences shorter than 200 words train and evaluate the model you can use the trained model hosted on hugging face hub https huggingface co keras io bidirectional lstm imdb and try the demo on hugging face spaces https huggingface co spaces keras io bidirectional_lstm_imdb
import numpy as np
import keras
from keras import layers

max_features = 20000  
maxlen = 200  




inputs = keras.Input(shape=(None,), dtype="int32")

x = layers.Embedding(max_features, 128)(inputs)

x = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(x)
x = layers.Bidirectional(layers.LSTM(64))(x)

outputs = layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs, outputs)
model.summary()



(x_train, y_train), (x_val, y_val) = keras.datasets.imdb.load_data(
    num_words=max_features
)
print(len(x_train), "Training sequences")
print(len(x_val), "Validation sequences")


x_train = keras.utils.pad_sequences(x_train, maxlen=maxlen)
x_val = keras.utils.pad_sequences(x_val, maxlen=maxlen)



model.compile(
    optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]
)
model.fit(
    x_train, y_train, batch_size=32, epochs=2, validation_data=(x_val, y_val)
)