Path: blob/master/examples/nlp/bidirectional_lstm_imdb.py
3507 views
"""1Title: Bidirectional LSTM on IMDB2Author: [fchollet](https://twitter.com/fchollet)3Date created: 2020/05/034Last modified: 2020/05/035Description: Train a 2-layer bidirectional LSTM on the IMDB movie review sentiment classification dataset.6Accelerator: GPU7"""89"""10## Setup11"""1213import numpy as np14import keras15from keras import layers1617max_features = 20000 # Only consider the top 20k words18maxlen = 200 # Only consider the first 200 words of each movie review1920"""21## Build the model22"""2324# Input for variable-length sequences of integers25inputs = keras.Input(shape=(None,), dtype="int32")26# Embed each integer in a 128-dimensional vector27x = layers.Embedding(max_features, 128)(inputs)28# Add 2 bidirectional LSTMs29x = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(x)30x = layers.Bidirectional(layers.LSTM(64))(x)31# Add a classifier32outputs = layers.Dense(1, activation="sigmoid")(x)33model = keras.Model(inputs, outputs)34model.summary()3536"""37## Load the IMDB movie review sentiment data38"""3940(x_train, y_train), (x_val, y_val) = keras.datasets.imdb.load_data(41num_words=max_features42)43print(len(x_train), "Training sequences")44print(len(x_val), "Validation sequences")45# Use pad_sequence to standardize sequence length:46# this will truncate sequences longer than 200 words and zero-pad sequences shorter than 200 words.47x_train = keras.utils.pad_sequences(x_train, maxlen=maxlen)48x_val = keras.utils.pad_sequences(x_val, maxlen=maxlen)4950"""51## Train and evaluate the model5253You can use the trained model hosted on [Hugging Face Hub](https://huggingface.co/keras-io/bidirectional-lstm-imdb)54and try the demo on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/bidirectional_lstm_imdb).55"""5657model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])58model.fit(x_train, y_train, batch_size=32, epochs=2, validation_data=(x_val, y_val))596061