How to use PyTorch for sentiment analysis on textual data?
Last Updated :
28 Mar, 2024
Sentiment Analysis is a natural language processing (NLP) task that involves identifying the emotion present in a given text. The amount of textual data on social media, consumer reviews, and other platforms is increasing, making sentiment analysis more and more crucial.
In this article, we present a step-by-step guide to performing sentiment analysis using PyTorch
Sentiment analysis on Textual Data using PyTorch
Sentiment analysis is a natural language processing (NLP) task that involves determining the sentiment expressed in a piece of text. PyTorch provides a flexible and efficient platform for building neural networks, making it well-suited for sentiment analysis tasks.
PyTorch for sentiment analysis on textual data is implemented with following steps:
Step 1: Importing Necessary Libraries
To begin, we import essential libraries, laying the foundation for our sentiment analysis implementation with PyTorch.
Python3
import torch
from torchtext import data
from torch.nn.utils.rnn import pad_sequence
from collections import Counter
import spacy
import torch.optim as optim
from torchtext.data import BucketIterator
from pprint import pprint # Importing pprint for pretty-printing
Step 2: Loading the Dataset
Spacy tokenizer is used alongside TorchText for tokenization purposes due to its effectiveness in splitting text into meaningful tokens.
The TEXT and LABEL are Field objects, representing the text and label fields of the dataset.
- TEXT is responsible for tokenizing and preprocessing the textual data.
- LABEL is responsible for handling the labels associated with the data
Python3
# Download and load the spaCy language model
!python -m spacy download en_core_web_sm
nlp = spacy.load('en_core_web_sm')
# Define data fields
TEXT = data.Field(include_lengths=True)
LABEL = data.LabelField(dtype=torch.float)
# Load the IMDB dataset using torchtext.datasets
from torchtext.datasets import IMDB
train_data, test_data = IMDB.splits(TEXT, LABEL)
Step 3: Tokenizing and Removing Punctuation
- We tokenize each review using the Spacy tokenizer with torchtext for loading the data. The tokenized reviews are stored in the 'text' field of the dataset.
- All individual words are extracted from the tokenized reviews and with count of the occurrences of each word.
- Each tokenized review is encoded into a list of integers using the created dictionary that maps each unique word to an integer.
- We use 'pad_sequence' to ensure that all encoded reviews have the same length by padding with zeros.
Step 1: Tokenizing and removing punctuation
Python
# Tokenization function using spaCy
def tokenize(text):
return [token.text for token in nlp(text)]
TEXT.tokenize = tokenize # tokenization and remove punctuation
TEXT.build_vocab(train_data, max_size=10000) # Building vocabulary
LABEL.build_vocab(train_data)
# Tokenize and preprocess reviews
tokenized_reviews = [data.Example.fromlist([review.text, None], [('text', TEXT), ('label', None)]) for review in train_data.examples]
all_words = [word for example in tokenized_reviews for word in example.text]
Step 2: Count and sort all words based on count. Also, a dictionary to convert words to Integers based on the number of occurrence of the word.
Python3
word_counts = Counter(all_words) # Sorting based on occurrences
sorted_words = sorted(word_counts, key=word_counts.get, reverse=True)
# dictionary for integers
word_to_int = {word: idx + 1 for idx, word in enumerate(sorted_words)}
Step 3: Encode review in list of Integer with dictionary
Python3
encoded_reviews = [[word_to_int[word] for word in example.text] for example in tokenized_reviews] # Encoding reviews into lists of integers
# Making encoded reviews of the same length
padded_reviews = pad_sequence([torch.tensor(review) for review in encoded_reviews])
# Pretty-print a subset of the outputs
pprint({
"Tokenized Reviews": [example.text[:10] for example in tokenized_reviews[:5]], # Show first 5 reviews, first 10 tokens
"All Words": all_words[:50], # Show first 50 words
"Sorted Words": sorted_words[:50], # Show first 50 sorted words
"Word to Integer Dictionary": {k: word_to_int[k] for k in list(word_to_int)[:10]}, # Show first 10 entries
"Encoded Reviews": encoded_reviews[:5], # Show first 5 encoded reviews
"Padded Reviews Shape": padded_reviews.shape # Show shape of padded_reviews
})
Output:
# Sample Output
{'All Words': ['Michelle',
'Rodriguez',
'is',
'the',
'defining',
'actress',]
'Encoded Reviews': [[3450,
11802,
6,
1,
10912,
684,
34,
85,
26,
1,
18108,
1601,
16]]
'Padded Reviews Shape': torch.Size([2470, 25000]),
'Sorted Words': ['the',
'a',
'and',
'of',
'to',
'is',
'in',
'I',
'that',
'this',
'it',
'/><br',
'was',
'as',
'with',
'for']
'Tokenized Reviews': [['Michelle',
'Rodriguez',
'is',
'the',
'defining',
'actress',
'who',
'could',
'be',
'the']]
'Word to Integer Dictionary': {'I': 8,
'a': 2,
'and': 3,
'in': 7,
'is': 6,
'of': 4,
'that': 9,
'the': 1,
'this': 10,
'to': 5}}
This code essentially demonstrates the process of creating a simple dataset from raw reviews, tokenizing the text, and preparing the data for further processing, such as training a machine learning model for sentiment analysis.
Transforming Sentiment Labels
Converting sentiment labels to numeric form (e.g., assigning 0 for negative and 1 for positive) - streamlines model training. This approach equips the model with a numerical target it can predict; thus enhancing its performance.
Python
# Get the labels from the dataset
labels = [example.label for example in train_data.examples]
# Convert labels to numeric format
numeric_labels = [1 if label == 'positive' else 0 for label in labels]
# Print the numeric labels
print("Numeric Labels:", numeric_labels)
Output:
# Sample Output
Numeric Labels: [1, 0, 1, 0, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Splitting the dataset
We use the training set for rigorous model training; meanwhile, the validation set for functioning as an independent evaluator.
- The first two lines convert the
padded_reviews and numeric_labels lists into PyTorch tensors. Convert the features (padded_reviews) and labels (numeric_labels) to PyTorch tensors as it is important to convert it into a tensor to enable efficient processing within PyTorch. - Check and Correct Size Mismatch: This conditional statement checks if the number of samples in
padded_reviews_tensor matches the number of samples in numeric_labels_tensor. If there is a mismatch, it ensures that both tensors have the same number of samples by trimming the longer tensor to match the length of the shorter one. This step ensures that each text review is paired with its corresponding sentiment label correctly.
The subsequent lines of code split the dataset into training and validation sets, extract features and labels for training and validation, and convert them into tensors for further processing.
Python
padded_reviews_tensor = torch.tensor(padded_reviews, dtype=torch.long)
numeric_labels_tensor = torch.tensor(numeric_labels, dtype=torch.float)
# Check if sizes match and correct if needed
if padded_reviews_tensor.size(0) != numeric_labels_tensor.size(0):
min_size = min(padded_reviews_tensor.size(0), numeric_labels_tensor.size(0))
padded_reviews_tensor = padded_reviews_tensor[:min_size]
numeric_labels_tensor = numeric_labels_tensor[:min_size]
# Combine features and labels into a TensorDataset
dataset = torch.utils.data.TensorDataset(padded_reviews_tensor, numeric_labels_tensor)
# Calculate the size of the training set
train_size = int(0.8 * len(dataset))
valid_size = len(dataset) - train_size
# Split the dataset into training and validation sets
train_data, valid_data = torch.utils.data.random_split(dataset, [train_size, valid_size])
# Extract features and labels for training and validation
X_train, y_train = zip(*train_data)
X_valid, y_valid = zip(*valid_data)
# Convert X_train, y_train, X_valid, y_valid to tensors (if needed)
X_train_tensor = torch.stack(X_train)
y_train_tensor = torch.stack(y_train)
X_valid_tensor = torch.stack(X_valid)
y_valid_tensor = torch.stack(y_valid)
# Print the shapes of the tensors (for verification)
print("X_train_tensor shape:", X_train_tensor.shape)
print("y_train_tensor shape:", y_train_tensor.shape)
print("X_valid_tensor shape:", X_valid_tensor.shape)
print("y_valid_tensor shape:", y_valid_tensor.shape)
# Print shapes of training and validation sets
print(f"Training Set - Features: {len(X_train)}, Labels: {len(y_train)}")
print(f"Validation Set - Features: {len(X_valid)}, Labels: {len(y_valid)}")
Output:
X_train_tensor shape: torch.Size([1976, 25000])
y_train_tensor shape: torch.Size([1976])
X_valid_tensor shape: torch.Size([494, 25000])
y_valid_tensor shape: torch.Size([494])
Training Set - Features: 1976, Labels: 1976
Validation Set - Features: 494, Labels: 494
Model Building
In this instance, we employ a Long Short-Term Memory (LSTM) model renowned for its ability to capture sequential dependencies within data.
Incorporate an embedding layer, LSTM layer, and a linear layer into the model architecture using PyTorch's 'nn.Module' class.
Python
class SentimentModel(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.rnn = nn.LSTM(embedding_dim, hidden_dim)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, text, text_lengths):
embedded = self.embedding(text)
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths.to('cpu'), enforce_sorted=False)
packed_output, (hidden, cell) = self.rnn(packed_embedded)
output = self.fc(hidden[-1])
return output
Initialization and Training
The code snippet sets up the necessary components for training a sentiment analysis model using PyTorch, including data iterators, model architecture, optimizer, and loss criterion, and then proceeds to train the model using the training data.
BucketIterator provided by the torchtext.data module automatically iterates batches sequences of similar lengths together to minimize padding within batches, which improves efficiency during training. By specifying a batch size of 64 and enabling shuffling, we ensure that the data is shuffled and processed in batches of 64 samples.
Python
# Create iterators
train_iterator, valid_iterator = BucketIterator.splits(
(train_data, test_data), batch_size=64, sort_key=lambda x: len(x.text), shuffle=True
)
# Define hyperparameters
vocab_size = len(TEXT.vocab)
embedding_dim = 100
hidden_dim = 256
output_dim = 1 # Binary classification
# Create model, optimizer, and criterion objects
model = SentimentModel(vocab_size, embedding_dim, hidden_dim, output_dim)
optimizer = optim.Adam(model.parameters())
criterion = nn.BCEWithLogitsLoss()
# Training the model
def train_model(model, iterator, optimizer, criterion, epochs=3):
model.train()
for epoch in range(epochs):
epoch_loss = 0
for batch in iterator:
optimizer.zero_grad()
text, text_lengths = batch.text
predictions = model(text, text_lengths).squeeze(1)
loss = criterion(predictions, batch.label)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
print(f'Epoch {epoch+1}/{epochs}, Loss: {epoch_loss / len(iterator):.4f}')
train_model(model, train_iterator, optimizer, criterion)
Output:
#Sample output
Epoch 1/3, Loss: 0.6802
Epoch 2/3, Loss: 0.6548
Epoch 3/3, Loss: 0.6301
The section delves into three key aspects: model initialization, optimizer setup, and the training process. It specifically addresses these components for a predetermined number of epochs.
Evaluation
Python
# Define evaluate_model function
def evaluate_model(model, iterator, criterion):
model.eval()
total_correct = 0
total_examples = 0
with torch.no_grad():
for batch in iterator:
text, text_lengths = batch.text
predictions = model(text, text_lengths).squeeze(1)
rounded_preds = torch.round(torch.sigmoid(predictions))
total_correct += (rounded_preds == batch.label).sum().item()
total_examples += batch.label.size(0)
accuracy = total_correct / total_examples
return accuracy
accuracy = evaluate_model(model, valid_iterator, criterion)
print(f'Validation Accuracy: {accuracy * 100:.2f}%')
Output:
# Sample Output
Validation Accuracy: 86.45%
Understanding and implementing each of these steps enables you to construct a comprehensive pipeline for sentiment analysis using PyTorch; this ensures not only effective handling of textual data but also robust model training.
Conclusion
The comprehensive guide demonstrates the intricacies of sentiment analysis using PyTorch, from data preprocessing to model training and evaluation.
Similar Reads
How to use PyTorch for sentiment analysis on textual data?
Sentiment Analysis is a natural language processing (NLP) task that involves identifying the emotion present in a given text. The amount of textual data on social media, consumer reviews, and other platforms is increasing, making sentiment analysis more and more crucial. In this article, we present
8 min read
How to Use the Hugging Face Transformer Library for Sentiment Analysis
The Hugging Face Transformer library is now a popular choice for developers working on Natural Language Processing (NLP) projects. It simplifies access to a range of pretrained models like BERT, GPT, and RoBERTa, making it easier for developers to utilize advanced models without extensive knowledge
5 min read
Sentiment Analysis of Hindi Text - Python
Sentiment Analysis for Indic Language: This article exhibits how to use the library VADER for doing the sentiment analysis of the Indic Language'Hindi'. Sentiment analysis is a metric that conveys how positive or negative or neutral the text or data is. It is performed on textual data to help busine
3 min read
How to Use AI in Excel for Automated Text Analysis?
Text analysis is a machine learning technique used to automatically extract valuable insights from unstructured text data. Companies use text analysis tools to quickly digest online data and documents, and transform them into actionable insights. We can use text analysis to extract specific informat
2 min read
How to Get the Data Type of a Pytorch Tensor?
In this article, we are going to create a tensor and get the data type. The Pytorch is used to process the tensors. Tensors are multidimensional arrays. PyTorch accelerates the scientific computation of tensors as it has various inbuilt functions. Vector: A vector is a one-dimensional tensor that ho
3 min read
How to use a DataLoader in PyTorch?
Operating with large datasets requires loading them into memory all at once. In most cases, we face a memory outage due to the limited amount of memory available in the system. Also, the programs tend to run slowly due to heavy datasets loaded once. PyTorch offers a solution for parallelizing the da
2 min read
Tweet Sentiment Analysis Using Python Streamlit
This article covers the sentiment analysis of by parsing the tweets fetched from Twitter using the streamlit Python framework. What is Sentiment Analysis? Sentiment Analysis is the process of ‘computationally’ determining whether a piece of writing is positive, negative or neutral. It’s also known a
4 min read
How to Create Sentiment Analysis Application using Node.js ?
Sentiment Analysis is a natural language processing technique used to determine emotions from textual data. It's often performed to know customer requirements, study product sentiment in user feedback, decision-making, and more. Types of Sentiment Analysis: Fine-grained Sentiment Analysis: It is don
10 min read
NLP Sentiment Analysis for US Election
Natural Language Processing is a subfield of Artificial Intelligence that enables computers to understand, analyze, and manipulate human language. This article discusses the prediction of U.S. election results based on Sentiment Analysis using tweets. What is Sentiment Analysis?Sentiment analysis is
15+ min read
Facebook Sentiment Analysis using python
This article is a Facebook sentiment analysis using Vader, nowadays many government institutions and companies need to know their customers' feedback and comment on social media such as Facebook. What is sentiment analysis? Sentiment analysis is one of the best modern branches of machine learning, w
6 min read
Dataset for Sentiment Analysis
Sentiment analysis, which helps understand how people feel and what they think, is very important in studying public opinions, customer thoughts, and social media buzz. But to make sentiment analysis work well, we need good datasets to train and test our systems. In this article, we will look at som
8 min read
Sentiment Analysis in Ancient Texts Using NLP Techniques.
Natural language processing (NLP) has undergone significant advancements in recent years, with applications ranging from chatbots to language translation. One intriguing application is sentiment analysis, where the goal is to determine the emotional tone behind a body of text. While this is relative
5 min read
NLTK Sentiment Analysis Tutorial For Beginners
Sentiment analysis, also known as opinion mining, is a crucial aspect of natural language processing (NLP) that involves determining the sentiment or emotion expressed in a piece of text. It is widely used in various applications such as social media monitoring, customer feedback analysis, and marke
7 min read
Sentiment Analysis using CatBoost
Sentiment analysis is crucial for understanding the emotional tone behind text data, making it invaluable for applications such as customer feedback analysis, social media monitoring, and market research. In this article, we will explore how to perform sentiment analysis using CatBoost. Table of Con
4 min read
Twitter Sentiment Analysis on Russia-Ukraine War Using Python
In this article, we are going to see how we can perform the Twitter sentiment analysis on the Russia-Ukraine War using Python. Twitter Sentiment Analysis on Russia-Ukraine WarThe role of social media in public opinion has been profound and evident since it started gaining attention. Social media all
9 min read
Sentiment Analysis of YouTube Comments
YouTube has become a significant platform for communication and expression, where people from all over the world can share their thoughts and opinions on various videos. These comments can provide a deep insight into what the viewer perceives and their feedback on the content. This allows content cr
13 min read
Sentiment Analysis using VADER - Using Python
Sentiment analysis involves determining the emotional tone behind a body of text. One popular tool for sentiment analysis is VADER (Valence Aware Dictionary and sEntiment Reasoner), a lexicon and rule-based sentiment analysis tool specifically designed for texts that contain informal language like s
4 min read
10 Best AI Tools for Sentiment Analysis (Free & Paid)
In Digital marketing and customer feedback, understanding public sentiment is more crucial than ever. That's where AI tools for sentiment analysis come in. Sentiment analysis tools, powered by Artificial Intelligence (AI), are transforming how businesses understand and respond to customer emotions a
9 min read
Sentiment Analysis Using 'quanteda' in R
Sentiment analysis is the technique used to determine the sentiment expressed in the piece of text, classifying it as positive, negative, or neutral. In R, the quanteda package is the robust tool for text processing. While sentimentr can be used for sentiment analysis. This article will guide you th
5 min read