IMDb Sentiment RNN Models
LSTM, GRU, and LSTM–GRU models trained in TensorFlow for positive/negative movie-review classification.
Results
| Model | Test accuracy | Test macro F1 | Test ROC AUC |
|---|---|---|---|
| LSTM | 86.42% | 0.8642 | 0.9364 |
| GRU | 86.24% | 0.8624 | 0.9337 |
| LSTM-GRU | 85.96% | 0.8596 | 0.9361 |
The evaluation uses 9,916 real test labels. After duplicate removal, the split contains 31,728 training, 7,933 validation, and 9,916 test reviews. Vocabulary fitting uses training text only. Early stopping uses validation loss; the default GRU is selected by validation ROC AUC. Results use one seed and one split.
The LSTM and GRU have 32 units. The hybrid contains a 16-unit LSTM followed by a 16-unit GRU. Each model uses a 32-dimensional embedding, masked padding, dropout 0.2, sigmoid output, and binary cross-entropy. Inputs use up to 160 tokens and a 10,000-token vocabulary.
Use a model
Use Python 3.12 and download the repository files.
python -m pip install -r requirements.txt
import tensorflow as tf
from preprocessing import clean_text
model = tf.keras.models.load_model("gru.keras", compile=False, safe_mode=True)
review = clean_text("The acting was excellent and the story kept me interested.")
p = float(model(tf.constant([review]), training=False).numpy()[0, 0])
print({"positive": p, "negative": 1-p})
The saved models include their vocabulary and vectorization layer. Use preprocessing.py for the same HTML and whitespace cleanup used in training. There is no neutral class; scores are uncalibrated estimates.
Retrain
Obtain the original IMDb CSV, then run:
python train.py --csv "data/IMDB Dataset.csv"
metrics.json records the dataset hash, split rows, parameters, training histories, and full evaluation. test-predictions.npz stores probabilities and real labels for metric checks. roc-curves.png shows test ROC curves.
These models were trained for the corrected project update. The original notebook's dummy-label ROC values are not reused. Raw review text is not redistributed. Data source: IMDb Dataset of 50K Movie Reviews. Original source and data retain their existing terms.