0% found this document useful (0 votes)
1 views

DL_LSTM_3.ipynb - Colab

The document outlines a process for building and evaluating an LSTM model for sentiment analysis using the IMDB dataset. It includes steps for data preparation, model construction, training, and evaluation, along with visualizations of accuracy and loss. The final model achieved a test accuracy of 82% with detailed classification metrics provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

DL_LSTM_3.ipynb - Colab

The document outlines a process for building and evaluating an LSTM model for sentiment analysis using the IMDB dataset. It includes steps for data preparation, model construction, training, and evaluation, along with visualizations of accuracy and loss. The final model achieved a test accuracy of 82% with detailed classification metrics provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/25/25, 9:14 AM DL_LSTM_3.

ipynb - Colab

3. Design RNN or its variant including LSTM or GRU a) Select a suitable time series
keyboard_arrow_down dataset. Example – predict sentiments based on product reviews b) Apply for
prediction

import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay
import numpy as np

# Set seed for reproducibility


tf.random.set_seed(42)

# 1. Download the IMDB dataset directly from TensorFlow/Keras


num_words = 10000 # Limit the vocabulary to the top 10,000 words
max_length = 100 # Maximum review length (in words)

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data(num_words=num_words)

# 2. Pad sequences to ensure all have the same length


x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_length)
x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_length)

# 3. Build LSTM model


model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=num_words, output_dim=32, input_length=max_length),
tf.keras.layers.LSTM(32, dropout=0.2, recurrent_dropout=0.2),
tf.keras.layers.Dense(1, activation='sigmoid')
])

# 4. Compile the model


model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])

# 5. Train the model (you can also include a validation split)


history = model.fit(x_train, y_train,
validation_split=0.2,
epochs=5,
batch_size=128)

# 6. Evaluate the model on the test data


test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"\nTest Accuracy: {test_acc:.4f}")

# 7. Make predictions
y_pred_prob = model.predict(x_test)
y_pred = (y_pred_prob > 0.5).astype(int)

# 8. Classification Report
print("\nClassification Report:")
print(classification_report(y_test, y_pred, target_names=["Negative", "Positive"]))

# 9. Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["Negative", "Positive"])
disp.plot(cmap='Blues')
plt.title("Confusion Matrix")
plt.show()

# 10. Plot Accuracy & Loss over epochs


plt.figure(figsize=(12, 5))

# Accuracy plot
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label="Train Acc")
plt.plot(history.history['val_accuracy'], label="Val Acc")
plt.title("Accuracy over Epochs")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend()

# Loss plot
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label="Train Loss")
plt.plot(history.history['val_loss'], label="Val Loss")
plt.title("Loss over Epochs")
plt.xlabel("Epoch")
https://colab.research.google.com/drive/1fOgdcp9sUCQjDIiA1eJYZHPVy5iSrfCk#printMode=true 1/3
4/25/25, 9:14 AM DL_LSTM_3.ipynb - Colab
plt.xlabel( Epoch )
plt.ylabel("Loss")
plt.legend()

plt.tight_layout()
plt.show()

WARNING:tensorflow:Layer lstm will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fal
Epoch 1/5
157/157 [==============================] - 99s 618ms/step - loss: 0.5307 - accuracy: 0.7204 - val_loss: 0.3662 - val_accuracy: 0.840
Epoch 2/5
157/157 [==============================] - 95s 608ms/step - loss: 0.3011 - accuracy: 0.8766 - val_loss: 0.3376 - val_accuracy: 0.851
Epoch 3/5
157/157 [==============================] - 95s 607ms/step - loss: 0.2354 - accuracy: 0.9101 - val_loss: 0.3552 - val_accuracy: 0.846
Epoch 4/5
157/157 [==============================] - 93s 592ms/step - loss: 0.1949 - accuracy: 0.9284 - val_loss: 0.3975 - val_accuracy: 0.842
Epoch 5/5
157/157 [==============================] - 99s 634ms/step - loss: 0.1668 - accuracy: 0.9408 - val_loss: 0.4957 - val_accuracy: 0.827
782/782 [==============================] - 48s 61ms/step - loss: 0.5208 - accuracy: 0.8200

Test Accuracy: 0.8200


782/782 [==============================] - 48s 61ms/step

Classification Report:
precision recall f1-score support

Negative 0.79 0.87 0.83 12500


Positive 0.85 0.77 0.81 12500

accuracy 0.82 25000


macro avg 0.82 0.82 0.82 25000
weighted avg 0.82 0.82 0.82 25000

https://colab.research.google.com/drive/1fOgdcp9sUCQjDIiA1eJYZHPVy5iSrfCk#printMode=true 2/3
4/25/25, 9:14 AM DL_LSTM_3.ipynb - Colab
Start coding or generate with AI.

https://colab.research.google.com/drive/1fOgdcp9sUCQjDIiA1eJYZHPVy5iSrfCk#printMode=true 3/3

You might also like