kohendru/distilbert-base-uncased-amazon-sentiment-analysis
020
distilbert-base-uncased-amazon-sentiment-analysis
Base Model
- BERT: BERT is a transformer-based model designed to pre-train deep bidirectional representations by jointly conditioning on both left and right context in all layers.
- DistilBERT: DistilBERT is a smaller, faster, and more efficient version of BERT. It uses knowledge distillation to reduce the model size by approximately 60% while retaining 97% of BERT’s language understanding capabilities.
Dataset
The dataset obtained from kaggle with title "Amazon Reviews for Sentiment Analysis" by Adam Bittlingmayer. The dataset contains columns "title," "text," and "label," with a total of 4,000,000 data entries (I only use 5% of the data for now).
Dataset Example
Evaluation
When I try to train the model with a large number of epochs, it starts to overfit when the epoch reaches 6 or 7. So, I only use 5 epochs for this model.
results = trainer.evaluate()
print(results)
"""
{
'eval_accuracy': 0.953925,
'eval_precision_macro': 0.9539209871607255,
'eval_recall_macro': 0.9539319939428168,
'eval_f1_macro': 0.9539242719746999,
'eval_loss': 0.15511418879032135,
'eval_runtime': 90.9442,
'eval_samples_per_second': 439.83,
'eval_steps_per_second': 6.872,
'epoch': 5.0
}
"""How to use the model?
from transformers import pipeline
model_name = "kohendru/distilbert-base-uncased-amazon-sentiment-analysis"
nlp = pipeline("text-classification", model=model_name, tokenizer=model_name)
reviews = [
"I love this product! It works great and has exceeded my expectations.",
"Worst purchase ever. Completely useless and waste of money.",
"The product is okay, but could be improved in terms of quality.",
"Amazing! Will definitely buy again."
]
for review in reviews:
result = nlp(review)
print(f"Review: {review}")
print(f"Sentiment: {result[0]['label']}, Confidence: {result[0]['score']:.4f}")
print("-" * 50)
"""
Review: I love this product! It works great and has exceeded my expectations.
Sentiment: Good Review, Confidence: 0.9950
--------------------------------------------------
Review: Worst purchase ever. Completely useless and waste of money.
Sentiment: Bad Review, Confidence: 0.9958
--------------------------------------------------
Review: The product is okay, but could be improved in terms of quality.
Sentiment: Bad Review, Confidence: 0.5947
--------------------------------------------------
Review: Amazing! Will definitely buy again.
Sentiment: Good Review, Confidence: 0.9942
--------------------------------------------------
"""