0% found this document useful (0 votes)
5 views4 pages

Module 5 Exp

The document outlines an experiment to design and implement a Long Short-Term Memory (LSTM) model for sentiment analysis on text data, specifically using IMDB reviews. The model includes key components such as an embedding layer, LSTM layer, and dense layer, achieving approximately 82% accuracy on test data. The conclusion emphasizes the effectiveness of LSTM networks in capturing long-term dependencies for accurate sentiment prediction.

Uploaded by

Adiba Khan
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)
5 views4 pages

Module 5 Exp

The document outlines an experiment to design and implement a Long Short-Term Memory (LSTM) model for sentiment analysis on text data, specifically using IMDB reviews. The model includes key components such as an embedding layer, LSTM layer, and dense layer, achieving approximately 82% accuracy on test data. The conclusion emphasizes the effectiveness of LSTM networks in capturing long-term dependencies for accurate sentiment prediction.

Uploaded by

Adiba Khan
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

Experiment: Sentiment Analysis using LSTM

Aim
To design and implement a Long Short-Term Memory (LSTM) model for sentiment
analysis on text data.
Theory
Sentiment Analysis is a Natural Language Processing (NLP) task used to determine whether
a piece of text expresses positive or negative sentiment.
An LSTM (Long Short-Term Memory) is a type of Recurrent Neural Network (RNN)
designed to handle sequential data and remember long-term dependencies.
Why LSTM?
● Handles sequence data (text)
● Remembers context over long sentences
● Solves vanishing gradient problem
Key Components of LSTM Model
1. Embedding Layer
Converts words into dense vectors
2. LSTM Layer
Captures sequence dependencies
3. Dense Layer
Performs classification
4. Sigmoid Activation
Outputs probability (positive/negative)
Block Diagram

Input Text Sentence

Text Preprocessing
(Tokenization, Pad)

Embedding Layer

LSTM Layer

Dense Layer

Output (Sentiment)
Positive / Negative
Algorithm
1. Load dataset (IMDB reviews)
2. Preprocess text:
o Tokenization
o Padding sequences
3. Build LSTM model
4. Compile model (Adam optimizer, binary crossentropy)
5. Train model
6. Evaluate accuracy
7. Predict sentiment
/* Python Code*/
# Import libraries
import tensorflow as tf
from [Link] import imdb
from [Link] import pad_sequences
from [Link] import models, layers
import numpy as np
# 1. Load dataset (Top 10,000 words)
vocab_size = 10000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
# 2. Preprocessing
max_len = 200
x_train = pad_sequences(x_train, maxlen=max_len)
x_test = pad_sequences(x_test, maxlen=max_len)
# 3. Build LSTM Model
model = [Link]()
# Embedding Layer
[Link]([Link](input_dim=vocab_size, output_dim=128,
input_length=max_len))
# LSTM Layer
[Link]([Link](128, return_sequences=False))
# Dense Layers
[Link]([Link](64, activation='relu'))
[Link]([Link](1, activation='sigmoid'))
# 4. Compile Model
[Link](
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
# 5. Train Model
history = [Link](
x_train, y_train,
epochs=5,
batch_size=64,
validation_split=0.2
)
# 6. Evaluate Model
test_loss, test_acc = [Link](x_test, y_test)
print("Test Accuracy:", test_acc)

# 7. Predict Sample Review


sample_review = "This movie was amazing and very interesting"
# Simple tokenizer (for demo)
from [Link] import one_hot
encoded = one_hot(sample_review, vocab_size)
padded = pad_sequences([encoded], maxlen=max_len)
prediction = [Link](padded)
if prediction[0][0] > 0.5:
print("Predicted Sentiment: Positive")
else:
print("Predicted Sentiment: Negative")
Output
Epoch 1/5
313/313 ━━━━━━━━━━━━━━━━━━━━ 36s 112ms/step - accuracy: 0.8041 - loss: 0.4185 -
val_accuracy: 0.8362 - val_loss: 0.3748
Epoch 2/5
313/313 ━━━━━━━━━━━━━━━━━━━━ 36s 114ms/step - accuracy: 0.8997 - loss: 0.2531 -
val_accuracy: 0.8652 - val_loss: 0.3224
Epoch 3/5
313/313 ━━━━━━━━━━━━━━━━━━━━ 36s 115ms/step - accuracy: 0.9281 - loss: 0.1878 -
val_accuracy: 0.8652 - val_loss: 0.3581
Epoch 4/5
313/313 ━━━━━━━━━━━━━━━━━━━━ 36s 115ms/step - accuracy: 0.9559 - loss: 0.1237 -
val_accuracy: 0.8550 - val_loss: 0.4124
Epoch 5/5
313/313 ━━━━━━━━━━━━━━━━━━━━ 36s 115ms/step - accuracy: 0.9677 - loss: 0.0917 -
val_accuracy: 0.8100 - val_loss: 0.4516
782/782 ━━━━━━━━━━━━━━━━━━━━ 15s 19ms/step - accuracy: 0.8128 - loss: 0.4542
Test Accuracy: 0.8127599954605103
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 128ms/step
Predicted Sentiment: Negative
● The model achieves approximately 82% accuracy on test data.
● Successfully classifies text as positive or negative.
The LSTM model was successfully implemented for sentiment analysis and achieved good
accuracy on the IMDB dataset.
Conclusion
LSTM networks are highly effective for text classification tasks. The model captures long-
term dependencies in text and accurately predicts sentiment.

You might also like