This project showcases the fine-tuning of a BERT-based Sentiment Classifier to predict the sentiment (positive or negative) of Amazon product reviews. By leveraging the power of BERT (Bidirectional Encoder Representations from Transformers), the model provides highly accurate sentiment classification for real-world customer feedback.
The model has been trained on a dataset of verified Amazon reviews and further enhanced with techniques such as class weighting, sarcasm handling, emoji processing, and more. The final model can be deployed to classify reviews in real-time, offering an intuitive solution for analyzing customer sentiment at scale.
Pre-Trained BERT Model:
The project fine-tunes a BERT (bert-base-uncased) model, a state-of-the-art transformer model for natural language processing (NLP) tasks, on a dataset of verified Amazon reviews.
Handling Complex Sentiment:
The model is designed to handle complex sentiment cases, including:
Sarcasm: Reviews with sarcastic undertones are often misclassified by traditional models. This project enhances the model’s ability to detect sarcasm using a combination of dataset augmentation and emoji-to-text conversions.
Negation: Reviews with negations like "not good" or "didn't work" are captured more accurately.
Emoji Handling: By converting emojis to their textual equivalents (e.g., 😊 → "smiling face"), the model interprets reviews containing emojis more effectively.
Dealing with Imbalanced Data:
The dataset was heavily imbalanced, with a high number of positive reviews. To tackle this, class weighting was applied during training, which improved the model's performance in detecting negative reviews.
Deployment Ready:
The model is built for deployment using frameworks such as Streamlit, allowing users to input reviews in real-time and get sentiment predictions. The model and tokenizer are saved for easy deployment in production environments.
BERT (Bidirectional Encoder Representations from Transformers):
The pre-trained transformer model used for fine-tuning.
Click here for more details about model used
Hugging Face Transformers:
Used for loading and fine-tuning the BERT model.
PyTorch:
For model training and fine-tuning.
Pandas & NumPy:
For data manipulation and analysis.
Scikit-learn:
Used for evaluation metrics such as accuracy, F1 score, and confusion matrix.
Streamlit :
For building an interactive web interface to deploy the model.
Preprocessing:
The dataset was cleaned by handling null values, converting emojis to text descriptions, and analyzing token lengths. Reviews longer than 512 tokens were truncated to optimize model performance.
Tokenization:
The BERT tokenizer was used to convert text into tokens that can be fed into the model. Tokenization ensures that the model can understand and process the reviews effectively.
Model Fine-Tuning:
The pre-trained BERT model was fine-tuned on the Amazon reviews dataset for binary classification (positive or negative sentiment). Fine-tuning was performed using PyTorch and Hugging Face Transformers.
Evaluation:
The model was evaluated using standard metrics such as accuracy, F1 score, and the confusion matrix, ensuring that the model is reliable and robust across different review types.
The model was evaluated on a test set, with the following metric :
Accuracy: 95.77%
F1 Score: 0.9775
Precision and Recall:
Positive Sentiment: Precision = 0.97, Recall = 0.99
Negative Sentiment: Precision = 0.73, Recall = 0.55
Confusion Matrix:
The model demonstrates strong performance in predicting positive reviews while handling negative reviews reasonably well, especially considering the class imbalance.
Handling Sarcasm and Informal Language:
Traditional sentiment analysis models often misclassify sarcastic or slang-heavy reviews.
To overcome this, I enhanced the training dataset by adding sarcastic and informal reviews, improving the model's robustness in real-world scenarios.
Class Imbalance:
With a large proportion of positive reviews, the model initially struggled with detecting negative sentiment.
This issue was mitigated by applying class weighting to ensure the model could learn to identify both positive and negative reviews effectively.
Fine-Tuning for Complex Cases:
The model was fine-tuned with special consideration for edge cases such as mixed sentiment reviews, negation (e.g., "not good"), and reviews containing emojis.
This allowed the model to become more flexible and accurate in handling real-world reviews.
Deploy the Model:
The model is ready for deployment via Streamlit or cloud platforms like AWS, Google Cloud, or Heroku.
You can easily integrate this model into your product or service to classify reviews in real-time.
Improve Handling of Sarcasm and Negation:
The next step is to further improve the model's performance on sarcastic and negation-heavy reviews by adding more such examples to the training data.
Domain-Specific Fine-Tuning:
This model can be further fine-tuned on reviews from other domains (e.g., healthcare, e-commerce) to improve performance on specific types of sentiment.
For more details please check my git repository or kaggle code