From 636337fd06cbac939e774cee26c6647fc4823e6c Mon Sep 17 00:00:00 2001 From: Ramesh Sampath <1437573+sampathweb@users.noreply.github.com> Date: Fri, 9 Jun 2023 05:28:30 +0530 Subject: [PATCH] Add example - bidir-lstm-imdb (#305) --- .../keras_io/nlp/bidirectional_lstm_imdb.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/keras_io/nlp/bidirectional_lstm_imdb.py diff --git a/examples/keras_io/nlp/bidirectional_lstm_imdb.py b/examples/keras_io/nlp/bidirectional_lstm_imdb.py new file mode 100644 index 000000000..57c786083 --- /dev/null +++ b/examples/keras_io/nlp/bidirectional_lstm_imdb.py @@ -0,0 +1,58 @@ +""" +Title: Bidirectional LSTM on IMDB +Author: [fchollet](https://twitter.com/fchollet) +Date created: 2020/05/03 +Last modified: 2020/05/03 +Description: Train a 2-layer bidirectional LSTM on the IMDB movie review sentiment classification dataset. +Accelerator: GPU +""" +""" +## Setup +""" + +import numpy as np +import keras_core as keras +from keras_core import layers + +max_features = 20000 # Only consider the top 20k words +maxlen = 200 # Only consider the first 200 words of each movie review + +""" +## Build the model +""" + +# Input for variable-length sequences of integers +inputs = keras.Input(shape=(None,), dtype="int32") +# Embed each integer in a 128-dimensional vector +x = layers.Embedding(max_features, 128)(inputs) +# Add 2 bidirectional LSTMs +x = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(x) +x = layers.Bidirectional(layers.LSTM(64))(x) +# Add a classifier +outputs = layers.Dense(1, activation="sigmoid")(x) +model = keras.Model(inputs, outputs) +model.summary() + +""" +## Load the IMDB movie review sentiment data +""" + +(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") +# Use pad_sequence to standardize sequence length: +# this will truncate sequences longer than 200 words and zero-pad sequences shorter than 200 words. +x_train = keras.utils.pad_sequences(x_train, maxlen=maxlen) +x_val = keras.utils.pad_sequences(x_val, maxlen=maxlen) + +""" +## 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). +""" + +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))