Experiment No 6
Experiment No 6
6. Add a Dense output layer with sigmoid activation for binary classification.
7. Compile the model with binary cross-entropy loss and the Adam optimizer,
tracking accuracy.
8. Train the model on the training set with validation split for monitoring.
9. Evaluate the model on the test set to obtain accuracy and loss.
Program
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
# Load and preprocess the IMDB dataset
max_features = 10000 # Vocabulary size (most common words)
maxlen = 200 # Cut off reviews after this many words
# Load dataset
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
# Pad sequences to make each review the same length
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
# Build the RNN model with LSTM layers
model = Sequential()
model.add(Embedding(input_dim=max_features, output_dim=128,
input_length=maxlen))
model.add(LSTM(64, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(x_train, y_train, epochs=3, batch_size=64,
validation_split=0.2)
# Evaluate the model on the test set
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test Loss: {test_loss}')
print(f'Test Accuracy: {test_accuracy}')
Dataset:
https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb.npz
Output:
Epoch 1/3
625/625 [==============================] - 48s 77ms/step -
loss: 0.4751 - accuracy: 0.7600 - val_loss: 0.3552 - val_accuracy: 0.8494
Epoch 2/3
625/625 [==============================] - 46s 74ms/step -
loss: 0.2778 - accuracy: 0.8895 - val_loss: 0.3620 - val_accuracy: 0.8518
Epoch 3/3
625/625 [==============================] - 45s 72ms/step -
loss: 0.2085 - accuracy: 0.9172 - val_loss: 0.3847 - val_accuracy: 0.8514
782/782 [==============================] - 5s 7ms/step - loss:
0.3779 - accuracy: 0.8453
Test Loss: 0.3779
Test Accuracy: 0.8453
Result
Thus The LSTM-based sentiment analysis model achieved a test accuracy of
approximately 84.5%, indicating a good performance on the IMDB dataset for
binary sentiment classification.