Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Exp 2-1

The document outlines a process for building and evaluating a sentiment analysis model using the IMDB dataset with TensorFlow and Keras. It includes steps for data preparation, model architecture, training, and evaluation, achieving a test accuracy of approximately 86%. Additionally, it visualizes the training history and confusion matrix to assess model performance.

Uploaded by

nagallahema2004
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)
11 views5 pages

Exp 2-1

The document outlines a process for building and evaluating a sentiment analysis model using the IMDB dataset with TensorFlow and Keras. It includes steps for data preparation, model architecture, training, and evaluation, achieving a test accuracy of approximately 86%. Additionally, it visualizes the training history and confusion matrix to assess model performance.

Uploaded by

nagallahema2004
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 matplotlib.pyplot as plt


from tensorflow.keras.datasets import imdb
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Flatten, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.metrics import confusion_matrix
import seaborn as sns

# Load IMDB dataset


(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=10000)
# Limit to top 10000 words

# Pad sequences
max_length = 200
X_train = pad_sequences(X_train, maxlen=max_length)
X_test = pad_sequences(X_test, maxlen=max_length)

# Build the model


model = Sequential()
model.add(Embedding(10000, 16, input_length=max_length))
model.add(Flatten())
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

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'])

# Train the model


history = model.fit(X_train, y_train, epochs=5, batch_size=128,
validation_split=0.2, verbose=1)

Epoch 1/5
157/157 ━━━━━━━━━━━━━━━━━━━━ 5s 17ms/step - accuracy: 0.5822 - loss:
0.6559 - val_accuracy: 0.8580 - val_loss: 0.3370
Epoch 2/5
157/157 ━━━━━━━━━━━━━━━━━━━━ 2s 14ms/step - accuracy: 0.8961 - loss:
0.2596 - val_accuracy: 0.8738 - val_loss: 0.2938
Epoch 3/5
157/157 ━━━━━━━━━━━━━━━━━━━━ 2s 14ms/step - accuracy: 0.9523 - loss:
0.1519 - val_accuracy: 0.8682 - val_loss: 0.3210
Epoch 4/5
157/157 ━━━━━━━━━━━━━━━━━━━━ 2s 13ms/step - accuracy: 0.9838 - loss:
0.0751 - val_accuracy: 0.8638 - val_loss: 0.3610
Epoch 5/5
157/157 ━━━━━━━━━━━━━━━━━━━━ 2s 11ms/step - accuracy: 0.9952 - loss:
0.0331 - val_accuracy: 0.8640 - val_loss: 0.3899

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {accuracy}")

782/782 ━━━━━━━━━━━━━━━━━━━━ 3s 4ms/step - accuracy: 0.8584 - loss:


0.4000
Test Accuracy: 0.8600000143051147

# Make predictions
y_pred_probs = model.predict(X_test)
y_pred = (y_pred_probs > 0.5).astype(int).flatten()

782/782 ━━━━━━━━━━━━━━━━━━━━ 3s 4ms/step

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.title("Confusion Matrix")
plt.xlabel("Predicted Labels")
plt.ylabel("True Labels")
plt.show()
# Plot training history
plt.figure(figsize=(12, 4))

<Figure size 1200x400 with 0 Axes>

<Figure size 1200x400 with 0 Axes>

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

<matplotlib.legend.Legend at 0x2b037df36b0>
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()

You might also like