BERT Sentiment Classifier

A fine-tuned BERT model for classifying English reviews as Negative, Neutral, or Positive.

The model was fine-tuned from google-bert/bert-base-uncased using a subset of Yelp Review Full.

Labels

Class ID Sentiment Original Yelp rating
0 Negative 1–2 stars
1 Neutral 3 stars
2 Positive 4–5 stars

Three-star reviews serve as a neutral proxy and may contain mixed opinions.

Usage

import html
import re

from transformers import pipeline

classifier = pipeline(
    "text-classification",
    model="godlykmathews/BERT-Sentiment_Classifier",
)

def clean_text(text):
    text = html.unescape(text)
    text = re.sub(r"<br\s*/?>", " ", text, flags=re.IGNORECASE)
    text = text.replace("\\n", " ").replace("\\t", " ")
    return re.sub(r"\s+", " ", text).strip()

review = "The food was wonderful and the service was excellent!"

result = classifier(
    clean_text(review),
    truncation=True,
    max_length=128,
)

print(result)

Training Data

Split Reviews
Training 15,000
Validation 1,500
Test 3,000

Training and validation samples were selected from Yelp’s official training split. Test samples were selected from its official test split.

Sampling was stratified with a random seed of 42. Empty reviews, duplicate normalized text, and conflicting duplicate labels were removed from the sampled pools. Duplicate text shared between the development and test samples was excluded.

Preprocessing decoded HTML entities, normalized line breaks, and collapsed whitespace. Negation, punctuation, and stopwords were retained.

Training Configuration

Setting Value
Framework PyTorch
Transformers version 5.12.1
Optimizer AdamW
Learning rate 2e-5
Epochs 3
Batch size 4
Gradient accumulation steps 4
Effective batch size 16 on one device
Maximum sequence length 128 tokens
Weight decay 0.01
Warmup Approximately 10% of update steps
Random seed 42

The BERT encoder and classification layer were both fine-tuned. Validation ran after each epoch, and the checkpoint with the highest validation macro F1 was selected for final evaluation.

Evaluation Results

Results on the held-out sample of 3,000 Yelp reviews:

Metric Value
Accuracy 0.7740
Macro precision 0.7281
Macro recall 0.7275
Macro F1 0.7278
Weighted F1 0.7734

Per-class Results

Sentiment Precision Recall F1 Support
Negative 0.8280 0.8425 0.8352 1,200
Neutral 0.5042 0.4950 0.4996 600
Positive 0.8521 0.8450 0.8485 1,200

Macro averages weight each class equally. Neutral classification was the weakest area in this evaluation.

Intended Uses

  • Educational sentiment-analysis projects.
  • Classifying English review text.
  • Demonstrations through a Streamlit interface.

Limitations

  • Neutral labels are derived from three-star ratings rather than independent neutral-sentiment annotations.
  • Performance on Yelp does not establish performance on movie reviews, other product domains, or other languages.
  • Reviews longer than 128 tokens are truncated.
  • Sarcasm, mixed opinions, and ambiguous language may produce incorrect predictions.
  • Output probabilities are not calibrated guarantees of correctness.

Project

Source code, training notebook, and Streamlit app

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for godlykmathews/BERT-Sentiment_Classifier

Finetuned
(7,261)
this model

Dataset used to train godlykmathews/BERT-Sentiment_Classifier