0% found this document useful (0 votes)
12 views5 pages

Movie Review Classification

The document outlines a TensorFlow implementation for sentiment analysis using the IMDB dataset, where it preprocesses the data, builds an LSTM model, and trains it over 10 epochs. The model achieves an accuracy of approximately 81.65% on the test set, along with calculated precision, recall, and F1-score metrics. Additionally, it visualizes the training process and displays a confusion matrix for performance evaluation.

Uploaded by

satishbokka1619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Movie Review Classification

The document outlines a TensorFlow implementation for sentiment analysis using the IMDB dataset, where it preprocesses the data, builds an LSTM model, and trains it over 10 epochs. The model achieves an accuracy of approximately 81.65% on the test set, along with calculated precision, recall, and F1-score metrics. Additionally, it visualizes the training process and displays a confusion matrix for performance evaluation.

Uploaded by

satishbokka1619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

import numpy as np

import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences

# Load the IMDB dataset


max_features = 10000 # Number of words to consider as features
maxlen = 100 # Cut texts after this number of words

(x_train, y_train), (x_test, y_test) =


imdb.load_data(num_words=max_features)

# Pad sequences to ensure uniform input size


x_train = pad_sequences(x_train, maxlen=maxlen)
x_test = pad_sequences(x_test, maxlen=maxlen)

print("Shape of x_train:", x_train.shape)


print("Shape of x_test:", x_test.shape)

Shape of x_train: (25000, 100)


Shape of x_test: (25000, 100)

from tensorflow.keras.models import Sequential


from tensorflow.keras.layers import Embedding, LSTM, Dense

# Define the model


model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen)) #
Embedding layer
model.add(LSTM(128)) # LSTM layer
model.add(Dense(1, activation='sigmoid')) # Output layer

C:\Users\nagal\anaconda3\pranathi\Lib\site-packages\keras\src\layers\
core\embedding.py:90: UserWarning: Argument `input_length` is
deprecated. Just remove it.
warnings.warn(

# Compile the model


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

print(model.summary())

Model: "sequential_1"

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳
━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇
━━━━━━━━━━━━━━━━━┩
│ embedding (Embedding) │ ? │
0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼
─────────────────┤
│ lstm (LSTM) │ ? │
0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼
─────────────────┤
│ dense_2 (Dense) │ ? │
0 (unbuilt) │
└──────────────────────────────────────┴─────────────────────────────┴
─────────────────┘

Total params: 0 (0.00 B)

Trainable params: 0 (0.00 B)

Non-trainable params: 0 (0.00 B)

None

# Train the model


history = model.fit(x_train, y_train, epochs=10, batch_size=32,
validation_split=0.2)

Epoch 1/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 91s 134ms/step - accuracy: 0.7139 - loss:
0.5298 - val_accuracy: 0.8074 - val_loss: 0.4533
Epoch 2/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 94s 150ms/step - accuracy: 0.8923 - loss:
0.2733 - val_accuracy: 0.8414 - val_loss: 0.3885
Epoch 3/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 86s 137ms/step - accuracy: 0.9335 - loss:
0.1813 - val_accuracy: 0.8374 - val_loss: 0.4053
Epoch 4/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 80s 127ms/step - accuracy: 0.9561 - loss:
0.1267 - val_accuracy: 0.8426 - val_loss: 0.5007
Epoch 5/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 88s 141ms/step - accuracy: 0.9712 - loss:
0.0813 - val_accuracy: 0.8352 - val_loss: 0.5686
Epoch 6/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 80s 127ms/step - accuracy: 0.9745 - loss:
0.0730 - val_accuracy: 0.8300 - val_loss: 0.6115
Epoch 7/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 76s 122ms/step - accuracy: 0.9885 - loss:
0.0370 - val_accuracy: 0.8296 - val_loss: 0.7257
Epoch 8/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 85s 136ms/step - accuracy: 0.9912 - loss:
0.0287 - val_accuracy: 0.8276 - val_loss: 0.7387
Epoch 9/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 73s 117ms/step - accuracy: 0.9923 - loss:
0.0273 - val_accuracy: 0.8274 - val_loss: 0.8670
Epoch 10/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 73s 116ms/step - accuracy: 0.9961 - loss:
0.0136 - val_accuracy: 0.8262 - val_loss: 1.0333

import matplotlib.pyplot as plt

# Plot training & validation accuracy values


plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()

# Import required libraries


from sklearn.metrics import accuracy_score, precision_score,
recall_score, f1_score, confusion_matrix
import seaborn as sns

# Predict on the test set


y_pred = (model.predict(x_test) > 0.5).astype("int32")
782/782 ━━━━━━━━━━━━━━━━━━━━ 32s 41ms/step

# Calculate classification metrics


accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-Score:", f1)

Accuracy: 0.81652
Precision: 0.8560244758391073
Recall: 0.76104
F1-Score: 0.8057425994155761

# Plot confusion matrix


cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6,6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Negative', 'Positive'], yticklabels=['Negative',
'Positive'])
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()

You might also like