DL_LSTM_3.ipynb - Colab
DL_LSTM_3.ipynb - Colab
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
# 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()
# 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
Classification Report:
precision recall f1-score support
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