DistilBERT sentiment classifier for Amazon food reviews
Fine-tuned distilbert-base-uncased that classifies an Amazon food review as Negative, Neutral, or Positive.
Built by Group Psi for the CSE 3521 (Survey of Artificial Intelligence) group project at The Ohio State University, Autumn 2026. Project repository: https://github.com/ColinWenzel24/amazon-food-review-sentiment-analysis
How to use
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
name = "thekluna/amazon-food-sentiment-distilbert"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name)
text = "Not soft cookies at all. These were very hard and dry."
inputs = tokenizer(text, truncation=True, max_length=256, return_tensors="pt")
with torch.no_grad():
probs = torch.softmax(model(**inputs).logits, dim=-1)[0]
print({model.config.id2label[i]: round(p.item(), 3) for i, p in enumerate(probs)})
For best results, format the input like the training data: the review summary followed by a space and the review text, with HTML tags (e.g. <br />) removed.
Training data
- Source: Amazon Fine Food Reviews (SNAP).
- Sample: 50,000 reviews drawn with the course-assigned group seed (48).
- Cleaning: removed 2 rows with a missing summary and 3,737 duplicate review texts (the same review posted under several product variants), leaving 46,261 reviews. HTML tags removed; summary and review text combined.
- Labels from star ratings: 1–2★ → Negative (14.4%), 3★ → Neutral (7.7%), 4–5★ → Positive (78.0%).
- Split: 80/20 stratified train/test (seed 48), then 10% of the training set held out for validation → 33,307 train / 3,701 validation / 9,253 test.
Training procedure
| Setting | Value |
|---|---|
| Max sequence length | 256 tokens |
| Batch size | 32 |
| Epochs | 2 |
| Optimizer | AdamW, learning rate 3e-5, weight decay 0.01 |
| Schedule | Linear warmup (10% of steps), then linear decay |
| Loss | Cross-entropy with balanced class weights (to counter class imbalance) |
| Precision | Mixed (fp16) |
| Checkpoint | Best validation macro-F1 (evaluated every 200 steps) |
| Hardware / time | AMD Radeon RX 9070 XT (ROCm), 3.6 minutes |
Results (held-out test set, 9,253 reviews)
| Metric | Score |
|---|---|
| Accuracy | 0.8835 |
| Macro precision | 0.7299 |
| Macro recall | 0.7864 |
| Macro F1 | 0.7482 |
| Majority-class baseline accuracy (always "Positive") | 0.7795 |
| Class | Precision | Recall | F1 | Support |
|---|---|---|---|---|
| Negative | 0.808 | 0.784 | 0.796 | 1,331 |
| Neutral | 0.403 | 0.650 | 0.498 | 709 |
| Positive | 0.978 | 0.925 | 0.951 | 7,213 |
Limitations
- Neutral is the weakest class. 3-star reviews are often mildly positive or mixed, so star ratings are a noisy proxy for sentiment. The class-weighted loss raises Neutral recall but lowers its precision: the model over-predicts Neutral, especially for mildly positive reviews.
- Domain-specific. Trained only on Amazon food product reviews; performance on other domains is untested.
- Long reviews are truncated at 256 tokens (about 6% of reviews).
- Mild overfitting in epoch 2: validation loss rose slightly while training loss kept falling; the checkpoint with the best validation macro-F1 was kept.
- Built for a course project and not evaluated for production use.