File size: 3,956 Bytes
ea2d938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8a63f
 
ea2d938
db8a63f
ea2d938
db8a63f
ea2d938
db8a63f
ea2d938
 
 
 
 
db8a63f
ea2d938
db8a63f
ea2d938
db8a63f
ea2d938
 
 
db8a63f
ea2d938
db8a63f
ea2d938
 
 
 
db8a63f
ea2d938
 
 
 
 
db8a63f
ea2d938
db8a63f
ea2d938
 
 
 
 
db8a63f
ea2d938
db8a63f
 
ea2d938
db8a63f
ea2d938
 
 
 
 
db8a63f
ea2d938
db8a63f
ea2d938
 
 
db8a63f
ea2d938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8a63f
ea2d938
db8a63f
ea2d938
db8a63f
ea2d938
db8a63f
 
 
ea2d938
db8a63f
 
 
ea2d938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8a63f
ea2d938
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
---
language:
- en
library_name: transformers
pipeline_tag: text-classification
base_model: google-bert/bert-base-uncased
datasets:
- Yelp/yelp_review_full
tags:
- bert
- sentiment-analysis
- pytorch
- text-classification
metrics:
- accuracy
- precision
- recall
- f1
---

# 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](https://huggingface.co/google-bert/bert-base-uncased) using a subset of [Yelp Review Full](https://huggingface.co/datasets/Yelp/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

```python
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](https://github.com/godlykmathews/bert-sentiment-classifier)