0% found this document useful (0 votes)
39 views3 pages

Experiment No 6

Uploaded by

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

Experiment No 6

Uploaded by

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

6.

Train a sentiment analysis model on IMDB dataset, use RNN layers


with LSTM/GRU nodes
Aim
To develop and train a Recurrent Neural Network (RNN) model using Long Short-
Term Memory (LSTM) layers for sentiment analysis on the IMDB movie review
dataset. The goal is to classify each review as either positive or negative,
achieving high accuracy on unseen test data.
Algorithm
1. Load the IMDB dataset, setting the vocabulary size to 10,000 words.

2. Preprocess data by padding sequences to a fixed length (200 words).

3. Define a Sequential model with an Embedding layer for word


representations.
4. Add an LSTM layer with 64 units, returning sequences.

5. Add a second LSTM layer with 32 units.

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.

10. Display final test accuracy and loss values.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy