sentiment classification based on multiple deep learning models
This repository provides implementations of various deep learning models for sentiment classification tasks. The models include:
- TextCNN
- LSTM
- BiLSTM
- LSTM with Attention
- BiLSTM with Attention
- BERT
- RoBERTa
Each model is implemented using PyTorch and designed to classify sentiment polarity (positive/negative) from text data.
The following datasets can be used with the provided code:
- Description: The IMDB dataset contains 50,000 movie reviews, labeled as positive or negative.
- Source: The dataset can be loaded directly using
torchtextor thedatasetslibrary from Hugging Face. - Where to Access:
- For
torchtext: Automatically downloaded when using thedatasets.IMDB.splitsmethod. - For
datasets: Automatically downloaded using thedatasets.load_dataset("imdb")method.
- For
- Description: A large-scale dataset containing customer reviews and ratings from Amazon.
- Source: Available from AWS Open Data Registry.
- How to Download:
- Use the
datasetslibrary:datasets.load_dataset("amazon_polarity").
- Use the
- Description: Yelp dataset containing customer reviews with sentiment labels (positive/negative).
- Source: Available from the Yelp Dataset Challenge.
- How to Download:
- Use the
datasetslibrary:datasets.load_dataset("yelp_polarity").
- Use the
- Description: Dataset of tweets annotated with sentiment labels.
- Source: Various repositories, such as Kaggle (Twitter US Airline Sentiment).
- How to Download:
- Manually download from Kaggle or preprocess a Twitter dataset using custom scripts.
- Dataset: IMDB (via
torchtext) - Code Location:
textcnn_sentiment.py - Usage:
- Automatically loads and preprocesses IMDB data using
torchtext. - Embeddings are initialized with GloVe vectors.
- Automatically loads and preprocesses IMDB data using
- Dataset: IMDB (via
torchtext) - Code Location:
lstm_sentiment.py,bilstm_sentiment.py - Usage:
- Processes data using
torchtextand tokenizes using SpaCy. - Can be trained on other datasets by replacing the dataset loading part with any binary sentiment dataset compatible with
torchtext.
- Processes data using
- Dataset: IMDB (via
torchtext) - Code Location:
lstm_attention_sentiment.py,bilstm_attention_sentiment.py - Usage:
- Extends LSTM/BiLSTM models with an attention mechanism.
- Dataset: IMDB (via
datasets) - Code Location:
bert_sentiment.py - Usage:
- Loads IMDB dataset using the Hugging Face
datasetslibrary. - Tokenizes using the BERT tokenizer (
BertTokenizerfromtransformers). - Adapts the pre-trained BERT model (
BertForSequenceClassification) for sentiment classification.
- Loads IMDB dataset using the Hugging Face
- Dataset: IMDB (via
datasets) - Code Location:
roberta_sentiment.py - Usage:
- Similar to the BERT implementation but uses the RoBERTa tokenizer (
RobertaTokenizer) and model (RobertaForSequenceClassification).
- Similar to the BERT implementation but uses the RoBERTa tokenizer (
- Python 3.8+
- PyTorch 1.10+
- Hugging Face Transformers Library
torchtextfor TextCNN, LSTM, BiLSTMdatasetslibrary for BERT and RoBERTa- SpaCy (for tokenization)
pip install torch torchvision torchtext transformers datasets spacy
python -m spacy download en_core_web_sm-
Select a Model:
- Each model is stored in a separate file (e.g.,
textcnn_sentiment.py,bert_sentiment.py). - Open the file corresponding to the model you want to train/test.
- Each model is stored in a separate file (e.g.,
-
Choose a Dataset:
- By default, all scripts use the IMDB dataset.
- To use a different dataset, modify the dataset loading and preprocessing sections.
-
Run the Script:
python <script_name>.py
-
Evaluate the Model:
- After training, the script will evaluate the model on the test dataset and print the accuracy and loss.
- The IMDB dataset is binary-labeled (positive/negative). Ensure other datasets you use are similarly formatted.
- For other datasets, ensure compatibility with the tokenizer and input pipeline of the model.
- Hugging Face
datasetslibrary supports many datasets out of the box, and you can replace the dataset name (e.g.,"imdb") to experiment with others.