Text Classification
Transformers
Safetensors
PyTorch
English
bert
sentiment-analysis
imdb
text-embeddings-inference
Instructions to use ullahhafeez321/bert-sentiment-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ullahhafeez321/bert-sentiment-classifier with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="ullahhafeez321/bert-sentiment-classifier")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("ullahhafeez321/bert-sentiment-classifier") model = AutoModelForSequenceClassification.from_pretrained("ullahhafeez321/bert-sentiment-classifier", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -15,12 +15,14 @@ tags:
|
|
| 15 |
|
| 16 |
# BERT Sentiment Classifier
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
- **NEGATIVE** — The review expresses a negative opinion.
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
## Model Details
|
| 26 |
|
|
@@ -28,7 +30,7 @@ The model is based on the pretrained `google-bert/bert-base-uncased` architectur
|
|
| 28 |
|---|---|
|
| 29 |
| Model architecture | BERT Base Uncased |
|
| 30 |
| Base model | `google-bert/bert-base-uncased` |
|
| 31 |
-
| Task | Sentiment Classification |
|
| 32 |
| Number of classes | 2 |
|
| 33 |
| Positive label | 1 |
|
| 34 |
| Negative label | 0 |
|
|
@@ -40,7 +42,7 @@ The model is based on the pretrained `google-bert/bert-base-uncased` architectur
|
|
| 40 |
|
| 41 |
The model was fine-tuned using the [IMDb Movie Reviews Dataset](https://huggingface.co/datasets/stanfordnlp/imdb).
|
| 42 |
|
| 43 |
-
The notebook uses the following
|
| 44 |
|
| 45 |
| Dataset | Number of samples |
|
| 46 |
|---|---:|
|
|
@@ -49,20 +51,20 @@ The notebook uses the following data split:
|
|
| 49 |
| Testing | 2,000 |
|
| 50 |
| **Total** | **11,000** |
|
| 51 |
|
| 52 |
-
The
|
| 53 |
|
| 54 |
- `0`: Negative
|
| 55 |
- `1`: Positive
|
| 56 |
|
| 57 |
-
The training
|
| 58 |
|
| 59 |
## Training Configuration
|
| 60 |
|
| 61 |
-
The model was fine-tuned using the following configuration:
|
| 62 |
|
| 63 |
| Parameter | Value |
|
| 64 |
|---|---|
|
| 65 |
-
| Base
|
| 66 |
| Epochs | 3 |
|
| 67 |
| Batch size | 16 |
|
| 68 |
| Learning rate | 2e-5 |
|
|
@@ -71,32 +73,101 @@ The model was fine-tuned using the following configuration:
|
|
| 71 |
| Maximum sequence length | 192 |
|
| 72 |
| Random seed | 42 |
|
| 73 |
| Best checkpoint metric | Validation accuracy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
The
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
## Usage
|
| 90 |
|
| 91 |
You can load this model directly using Hugging Face Transformers.
|
| 92 |
|
| 93 |
-
### Install
|
| 94 |
|
| 95 |
```bash
|
| 96 |
pip install transformers torch
|
| 97 |
```
|
| 98 |
|
| 99 |
-
### Load the
|
| 100 |
|
| 101 |
```python
|
| 102 |
import torch
|
|
@@ -111,7 +182,7 @@ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
| 111 |
model.eval()
|
| 112 |
```
|
| 113 |
|
| 114 |
-
### Make a
|
| 115 |
|
| 116 |
```python
|
| 117 |
def predict_sentiment(text):
|
|
@@ -151,16 +222,16 @@ result = predict_sentiment(
|
|
| 151 |
print(result)
|
| 152 |
```
|
| 153 |
|
| 154 |
-
### Example
|
| 155 |
|
| 156 |
-
| Input
|
| 157 |
|---|---|
|
| 158 |
| This movie was absolutely wonderful! | POSITIVE |
|
| 159 |
| This movie was terrible and boring. | NEGATIVE |
|
| 160 |
| I loved every moment of this film. | POSITIVE |
|
| 161 |
| I hated this movie. It was a waste of time. | NEGATIVE |
|
| 162 |
|
| 163 |
-
These are illustrative
|
| 164 |
|
| 165 |
## Applications
|
| 166 |
|
|
@@ -180,6 +251,7 @@ This model can be integrated into:
|
|
| 180 |
- It may not perform equally well on other domains, such as product reviews, news, or social media.
|
| 181 |
- Sarcasm, irony, ambiguous language, and mixed sentiment can be difficult to classify.
|
| 182 |
- Confidence scores are model probabilities, not guarantees of correctness.
|
|
|
|
| 183 |
|
| 184 |
## Technologies Used
|
| 185 |
|
|
@@ -188,7 +260,7 @@ This model can be integrated into:
|
|
| 188 |
- Hugging Face Transformers
|
| 189 |
- Hugging Face Datasets
|
| 190 |
- Scikit-learn
|
| 191 |
-
- IMDb Dataset
|
| 192 |
- Google BERT Base Uncased
|
| 193 |
|
| 194 |
## Training Notebook
|
|
@@ -205,10 +277,16 @@ The training notebook includes:
|
|
| 205 |
8. Custom sentiment prediction.
|
| 206 |
9. Saving and uploading the model to Hugging Face.
|
| 207 |
|
| 208 |
-
##
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
**
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
## License
|
| 213 |
|
| 214 |
-
MIT License.
|
|
|
|
|
|
| 15 |
|
| 16 |
# BERT Sentiment Classifier
|
| 17 |
|
| 18 |
+
A fine-tuned **BERT Base Uncased** model for English movie review sentiment classification.
|
| 19 |
|
| 20 |
+
This model classifies movie reviews into two sentiment categories:
|
|
|
|
| 21 |
|
| 22 |
+
- **POSITIVE (1):** The review expresses a positive opinion.
|
| 23 |
+
- **NEGATIVE (0):** The review expresses a negative opinion.
|
| 24 |
+
|
| 25 |
+
The model was fine-tuned using the IMDb Movie Reviews dataset and evaluated on a separate test set containing 2,000 examples.
|
| 26 |
|
| 27 |
## Model Details
|
| 28 |
|
|
|
|
| 30 |
|---|---|
|
| 31 |
| Model architecture | BERT Base Uncased |
|
| 32 |
| Base model | `google-bert/bert-base-uncased` |
|
| 33 |
+
| Task | Binary Sentiment Classification |
|
| 34 |
| Number of classes | 2 |
|
| 35 |
| Positive label | 1 |
|
| 36 |
| Negative label | 0 |
|
|
|
|
| 42 |
|
| 43 |
The model was fine-tuned using the [IMDb Movie Reviews Dataset](https://huggingface.co/datasets/stanfordnlp/imdb).
|
| 44 |
|
| 45 |
+
The notebook uses the following dataset split:
|
| 46 |
|
| 47 |
| Dataset | Number of samples |
|
| 48 |
|---|---:|
|
|
|
|
| 51 |
| Testing | 2,000 |
|
| 52 |
| **Total** | **11,000** |
|
| 53 |
|
| 54 |
+
The dataset contains English movie reviews with binary sentiment labels:
|
| 55 |
|
| 56 |
- `0`: Negative
|
| 57 |
- `1`: Positive
|
| 58 |
|
| 59 |
+
The training set is used to fine-tune the model, the validation set is used to monitor training and evaluate checkpoints, and the test set is used for final performance evaluation.
|
| 60 |
|
| 61 |
## Training Configuration
|
| 62 |
|
| 63 |
+
The model was fine-tuned for three epochs using the following configuration:
|
| 64 |
|
| 65 |
| Parameter | Value |
|
| 66 |
|---|---|
|
| 67 |
+
| Base model | BERT Base Uncased |
|
| 68 |
| Epochs | 3 |
|
| 69 |
| Batch size | 16 |
|
| 70 |
| Learning rate | 2e-5 |
|
|
|
|
| 73 |
| Maximum sequence length | 192 |
|
| 74 |
| Random seed | 42 |
|
| 75 |
| Best checkpoint metric | Validation accuracy |
|
| 76 |
+
| Training framework | PyTorch |
|
| 77 |
+
|
| 78 |
+
A two-class classification head was added to the pretrained BERT encoder and fine-tuned for sentiment prediction.
|
| 79 |
+
|
| 80 |
+
## Training Results
|
| 81 |
+
|
| 82 |
+
The following table shows the training progress across all three epochs.
|
| 83 |
+
|
| 84 |
+
| Epoch | Training Loss | Validation Loss | Accuracy | Precision | Recall | F1-score |
|
| 85 |
+
|---|---:|---:|---:|---:|---:|---:|
|
| 86 |
+
| 1 | 0.376433 | 0.259453 | 0.896000 | 0.919321 | 0.867735 | 0.892784 |
|
| 87 |
+
| 2 | 0.166479 | 0.358983 | 0.884000 | 0.894845 | 0.869739 | 0.882114 |
|
| 88 |
+
| 3 | 0.134408 | 0.445110 | 0.894000 | 0.887574 | 0.901804 | 0.894632 |
|
| 89 |
+
|
| 90 |
+
Training loss decreased throughout training. However, validation loss increased after the first epoch. This may indicate overfitting, even though the validation accuracy remained relatively stable.
|
| 91 |
+
|
| 92 |
+
## Final Validation Results
|
| 93 |
|
| 94 |
+
The final validation evaluation produced the following results:
|
| 95 |
+
|
| 96 |
+
| Metric | Result |
|
| 97 |
+
|---|---:|
|
| 98 |
+
| Validation Loss | 0.259079 |
|
| 99 |
+
| Accuracy | **89.70%** |
|
| 100 |
+
| Precision | 91.95% |
|
| 101 |
+
| Recall | 86.97% |
|
| 102 |
+
| F1-score | **89.39%** |
|
| 103 |
+
|
| 104 |
+
## Final Test Results
|
| 105 |
+
|
| 106 |
+
The final evaluation was performed on a separate test set containing **2,000 movie reviews**, with 1,000 Negative and 1,000 Positive examples.
|
| 107 |
+
|
| 108 |
+
| Metric | Result |
|
| 109 |
+
|---|---:|
|
| 110 |
+
| Test Loss | 0.259910 |
|
| 111 |
+
| Accuracy | **88.80%** |
|
| 112 |
+
| Precision | 91.54% |
|
| 113 |
+
| Recall | 85.50% |
|
| 114 |
+
| F1-score | **88.42%** |
|
| 115 |
|
| 116 |
+
The model correctly classified 1,776 out of 2,000 test examples.
|
| 117 |
|
| 118 |
+
### Classification Report
|
| 119 |
|
| 120 |
+
| Class | Precision | Recall | F1-score | Support |
|
| 121 |
+
|---|---:|---:|---:|---:|
|
| 122 |
+
| Negative | 0.8640 | 0.9210 | 0.8916 | 1,000 |
|
| 123 |
+
| Positive | 0.9154 | 0.8550 | 0.8842 | 1,000 |
|
| 124 |
+
| **Macro Average** | **0.8897** | **0.8880** | **0.8879** | **2,000** |
|
| 125 |
+
| **Weighted Average** | **0.8897** | **0.8880** | **0.8879** | **2,000** |
|
| 126 |
|
| 127 |
+
### Confusion Matrix
|
| 128 |
+
|
| 129 |
+
The confusion matrix shows the actual and predicted sentiment labels.
|
| 130 |
+
|
| 131 |
+
| Actual / Predicted | Predicted Negative | Predicted Positive |
|
| 132 |
+
|---|---:|---:|
|
| 133 |
+
| Actual Negative | 921 | 79 |
|
| 134 |
+
| Actual Positive | 145 | 855 |
|
| 135 |
+
|
| 136 |
+
**Interpretation:**
|
| 137 |
+
|
| 138 |
+
- 921 Negative reviews were correctly classified.
|
| 139 |
+
- 855 Positive reviews were correctly classified.
|
| 140 |
+
- 79 Negative reviews were incorrectly classified as Positive.
|
| 141 |
+
- 145 Positive reviews were incorrectly classified as Negative.
|
| 142 |
+
|
| 143 |
+
The model achieved higher recall for Negative reviews (92.10%) than Positive reviews (85.50%).
|
| 144 |
+
|
| 145 |
+
## Model Architecture
|
| 146 |
+
|
| 147 |
+
This project uses an **encoder-only transformer architecture**.
|
| 148 |
+
|
| 149 |
+
BERT reads the input text and creates contextual representations of its tokens. The classification head uses these representations to predict the sentiment of the complete review.
|
| 150 |
+
|
| 151 |
+
The model follows this general workflow:
|
| 152 |
+
|
| 153 |
+
1. Receive an English movie review.
|
| 154 |
+
2. Tokenize the input text.
|
| 155 |
+
3. Pass the tokens through the pretrained BERT encoder.
|
| 156 |
+
4. Generate classification logits.
|
| 157 |
+
5. Apply softmax to obtain class probabilities.
|
| 158 |
+
6. Return the predicted sentiment and confidence score.
|
| 159 |
|
| 160 |
## Usage
|
| 161 |
|
| 162 |
You can load this model directly using Hugging Face Transformers.
|
| 163 |
|
| 164 |
+
### Install Dependencies
|
| 165 |
|
| 166 |
```bash
|
| 167 |
pip install transformers torch
|
| 168 |
```
|
| 169 |
|
| 170 |
+
### Load the Model
|
| 171 |
|
| 172 |
```python
|
| 173 |
import torch
|
|
|
|
| 182 |
model.eval()
|
| 183 |
```
|
| 184 |
|
| 185 |
+
### Make a Prediction
|
| 186 |
|
| 187 |
```python
|
| 188 |
def predict_sentiment(text):
|
|
|
|
| 222 |
print(result)
|
| 223 |
```
|
| 224 |
|
| 225 |
+
### Example Predictions
|
| 226 |
|
| 227 |
+
| Input Review | Expected Sentiment |
|
| 228 |
|---|---|
|
| 229 |
| This movie was absolutely wonderful! | POSITIVE |
|
| 230 |
| This movie was terrible and boring. | NEGATIVE |
|
| 231 |
| I loved every moment of this film. | POSITIVE |
|
| 232 |
| I hated this movie. It was a waste of time. | NEGATIVE |
|
| 233 |
|
| 234 |
+
These examples are illustrative and are not guaranteed predictions.
|
| 235 |
|
| 236 |
## Applications
|
| 237 |
|
|
|
|
| 251 |
- It may not perform equally well on other domains, such as product reviews, news, or social media.
|
| 252 |
- Sarcasm, irony, ambiguous language, and mixed sentiment can be difficult to classify.
|
| 253 |
- Confidence scores are model probabilities, not guarantees of correctness.
|
| 254 |
+
- Validation loss increased after the first epoch, suggesting possible overfitting.
|
| 255 |
|
| 256 |
## Technologies Used
|
| 257 |
|
|
|
|
| 260 |
- Hugging Face Transformers
|
| 261 |
- Hugging Face Datasets
|
| 262 |
- Scikit-learn
|
| 263 |
+
- IMDb Movie Reviews Dataset
|
| 264 |
- Google BERT Base Uncased
|
| 265 |
|
| 266 |
## Training Notebook
|
|
|
|
| 277 |
8. Custom sentiment prediction.
|
| 278 |
9. Saving and uploading the model to Hugging Face.
|
| 279 |
|
| 280 |
+
## Project Integration
|
| 281 |
+
|
| 282 |
+
This model is also used as the sentiment classification component in my NLP Analysis Studio application, alongside a separate decoder-only Qwen model for Named Entity Recognition.
|
| 283 |
|
| 284 |
+
- **Live Application:** https://sentimentclassifynamedentitymodels.streamlit.app/
|
| 285 |
+
- **GitHub Repository:** https://github.com/ullahhafeez321/SentimentClassify_NamedEntityModels
|
| 286 |
+
- **BERT Model:** https://huggingface.co/ullahhafeez321/bert-sentiment-classifier
|
| 287 |
+
- **Qwen NER Model:** https://huggingface.co/ullahhafeez321/Qwen-NER-Decoder
|
| 288 |
|
| 289 |
## License
|
| 290 |
|
| 291 |
+
This project is released under the MIT License.
|
| 292 |
+
|