Text Classification
Transformers
Safetensors
English
bert
ag-news
topic-classification
fine-tuning
text-embeddings-inference
Instructions to use karencardiel/topic-classification-bert with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use karencardiel/topic-classification-bert with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="karencardiel/topic-classification-bert")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("karencardiel/topic-classification-bert") model = AutoModelForSequenceClassification.from_pretrained("karencardiel/topic-classification-bert", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -17,108 +17,49 @@ tags:
|
|
| 17 |
- topic-classification
|
| 18 |
- fine-tuning
|
| 19 |
---
|
|
|
|
| 20 |
# BERT Topic Classification
|
| 21 |
|
| 22 |
## Training Data
|
| 23 |
|
| 24 |
-
This model was fine-tuned
|
| 25 |
-
|
| 26 |
-
The four categories are:
|
| 27 |
-
|
| 28 |
-
* **World**
|
| 29 |
-
* **Sports**
|
| 30 |
-
* **Business**
|
| 31 |
-
* **Sci/Tech**
|
| 32 |
-
|
| 33 |
-
For this experiment, the original training data was divided into:
|
| 34 |
|
| 35 |
-
*
|
| 36 |
-
*
|
| 37 |
-
*
|
|
|
|
| 38 |
|
| 39 |
-
The
|
| 40 |
-
|
| 41 |
-
A fixed random seed of **42** was used for reproducibility.
|
| 42 |
-
|
| 43 |
-
## Model Details
|
| 44 |
-
|
| 45 |
-
* **Base model:** `bert-base-uncased`
|
| 46 |
-
* **Task:** News topic classification
|
| 47 |
-
* **Number of classes:** 4
|
| 48 |
-
* **Maximum sequence length:** 128 tokens
|
| 49 |
-
* **Adaptation method:** Full fine-tuning
|
| 50 |
-
* **Total parameters:** 109,485,316
|
| 51 |
-
* **Trainable parameters:** 109,485,316
|
| 52 |
-
* **Training epochs:** 2
|
| 53 |
-
* **BERT learning rate:** `2e-5`
|
| 54 |
-
* **Classification head learning rate:** `1e-3`
|
| 55 |
-
* **Weight decay:** `0.01`
|
| 56 |
-
* **Random seed:** 42
|
| 57 |
-
* **Training hardware:** NVIDIA Tesla T4
|
| 58 |
|
| 59 |
## Metrics
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
Final test-set results:
|
| 64 |
|
| 65 |
-
| Metric
|
| 66 |
-
| --------
|
| 67 |
-
|
|
| 68 |
-
|
|
| 69 |
-
|
|
| 70 |
-
|
| 71 |
-
### Per-Class Results
|
| 72 |
-
|
| 73 |
-
| Class | Precision | Recall | F1-score |
|
| 74 |
-
| -------- | --------: | -----: | -------: |
|
| 75 |
-
| World | 0.9648 | 0.9521 | 0.9584 |
|
| 76 |
-
| Sports | 0.9895 | 0.9879 | 0.9887 |
|
| 77 |
-
| Business | 0.9248 | 0.9063 | 0.9155 |
|
| 78 |
-
| Sci/Tech | 0.9054 | 0.9368 | 0.9208 |
|
| 79 |
-
|
| 80 |
-
### Comparison with Feature-Based Adaptation
|
| 81 |
-
|
| 82 |
-
A feature-based adaptation approach was also evaluated. In this approach, BERT was frozen and its `[CLS]` representations were used as input to a Logistic Regression classifier.
|
| 83 |
-
|
| 84 |
-
| Adaptation Method | Test Accuracy | Test Macro F1 |
|
| 85 |
-
| ----------------------------------- | ------------: | ------------: |
|
| 86 |
-
| Feature-based + Logistic Regression | 90.20% | 90.19% |
|
| 87 |
-
| Full Fine-Tuning | **94.49%** | **94.49%** |
|
| 88 |
-
|
| 89 |
-
The full fine-tuning approach obtained higher test-set accuracy and Macro F1 in this experiment.
|
| 90 |
|
| 91 |
## Intended Use
|
| 92 |
|
| 93 |
-
This model is intended for **English news topic classification**.
|
| 94 |
-
|
| 95 |
-
It can classify news text into one of the following four categories:
|
| 96 |
|
| 97 |
-
|
| 98 |
-
* **Sports**
|
| 99 |
-
* **Business**
|
| 100 |
-
* **Sci/Tech**
|
| 101 |
-
|
| 102 |
-
The model is primarily intended for educational purposes, experimentation with BERT adaptation methods, and research involving text classification.
|
| 103 |
|
| 104 |
## Example Usage
|
| 105 |
|
| 106 |
-
The model can be loaded directly from the Hugging Face Hub using `transformers`:
|
| 107 |
-
|
| 108 |
```python
|
| 109 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 110 |
import torch
|
| 111 |
|
| 112 |
model_id = "karencardiel/topic-classification-bert"
|
| 113 |
|
| 114 |
-
# Load tokenizer and model
|
| 115 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 116 |
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 117 |
|
| 118 |
-
# Example text
|
| 119 |
text = "Researchers developed a new artificial intelligence system for analyzing genomic data."
|
| 120 |
|
| 121 |
-
# Tokenize the input
|
| 122 |
inputs = tokenizer(
|
| 123 |
text,
|
| 124 |
return_tensors="pt",
|
|
@@ -126,62 +67,38 @@ inputs = tokenizer(
|
|
| 126 |
padding=True
|
| 127 |
)
|
| 128 |
|
| 129 |
-
# Run inference
|
| 130 |
with torch.no_grad():
|
| 131 |
outputs = model(**inputs)
|
| 132 |
|
| 133 |
-
# Get predicted class
|
| 134 |
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 135 |
-
|
| 136 |
-
# Convert class ID to label
|
| 137 |
label = model.config.id2label[prediction]
|
| 138 |
|
| 139 |
print("Predicted topic:", label)
|
| 140 |
```
|
| 141 |
|
| 142 |
-
|
| 143 |
|
| 144 |
```text
|
| 145 |
Predicted topic: Sci/Tech
|
| 146 |
```
|
| 147 |
|
| 148 |
-
The model can also be used with multiple texts by passing a list of strings to the tokenizer.
|
| 149 |
-
|
| 150 |
## Limitations
|
| 151 |
|
| 152 |
-
* The model
|
| 153 |
-
*
|
| 154 |
* Performance on real-world data may differ from the reported test-set results.
|
| 155 |
-
* The model may reflect biases and limitations present in the training dataset.
|
| 156 |
-
* The reported metrics were obtained on the AG News test set and should not be interpreted as guaranteed performance on other datasets.
|
| 157 |
* The model is not intended for high-stakes decision-making.
|
| 158 |
|
| 159 |
## References
|
| 160 |
|
| 161 |
-
### BERT
|
| 162 |
-
|
| 163 |
Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2018).
|
| 164 |
-
|
| 165 |
*BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.*
|
| 166 |
-
|
| 167 |
https://arxiv.org/abs/1810.04805
|
| 168 |
|
| 169 |
-
### AG News and Topic Classification
|
| 170 |
-
|
| 171 |
Zhang, X., Zhao, J., & LeCun, Y. (2015).
|
| 172 |
-
|
| 173 |
*Character-level Convolutional Networks for Text Classification.*
|
| 174 |
-
|
| 175 |
-
Advances in Neural Information Processing Systems 28 (NIPS 2015).
|
| 176 |
-
|
| 177 |
https://arxiv.org/abs/1509.01626
|
| 178 |
|
| 179 |
-
The AG News dataset is a four-class topic classification benchmark covering **World, Sports, Business, and Sci/Tech**.
|
| 180 |
-
|
| 181 |
-
### Natural Language Processing with Transformers
|
| 182 |
-
|
| 183 |
Tunstall, L., von Werra, L., & Wolf, T.
|
|
|
|
| 184 |
|
| 185 |
-
*Natural Language Processing with Transformers.* O'Reilly Media.
|
| 186 |
-
|
| 187 |
-
Chapters 1–3.
|
|
|
|
| 17 |
- topic-classification
|
| 18 |
- fine-tuning
|
| 19 |
---
|
| 20 |
+
|
| 21 |
# BERT Topic Classification
|
| 22 |
|
| 23 |
## Training Data
|
| 24 |
|
| 25 |
+
This model was fine-tuned on the **AG News** dataset (`fancyzhx/ag_news`) for four-class news topic classification:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
* World
|
| 28 |
+
* Sports
|
| 29 |
+
* Business
|
| 30 |
+
* Sci/Tech
|
| 31 |
|
| 32 |
+
The dataset was divided into 108,000 training examples, 12,000 validation examples, and 7,600 test examples. A random seed of 42 was used.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
## Metrics
|
| 35 |
|
| 36 |
+
Test-set results:
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
| Metric | Score |
|
| 39 |
+
| -------- | ---------: |
|
| 40 |
+
| Accuracy | **94.49%** |
|
| 41 |
+
| Macro F1 | **94.49%** |
|
| 42 |
+
| Loss | **0.3470** |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
## Intended Use
|
| 45 |
|
| 46 |
+
This model is intended for **English news topic classification** into the four AG News categories: World, Sports, Business, and Sci/Tech.
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
It was developed for educational purposes and experimentation with BERT adaptation methods.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
## Example Usage
|
| 51 |
|
|
|
|
|
|
|
| 52 |
```python
|
| 53 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 54 |
import torch
|
| 55 |
|
| 56 |
model_id = "karencardiel/topic-classification-bert"
|
| 57 |
|
|
|
|
| 58 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 59 |
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 60 |
|
|
|
|
| 61 |
text = "Researchers developed a new artificial intelligence system for analyzing genomic data."
|
| 62 |
|
|
|
|
| 63 |
inputs = tokenizer(
|
| 64 |
text,
|
| 65 |
return_tensors="pt",
|
|
|
|
| 67 |
padding=True
|
| 68 |
)
|
| 69 |
|
|
|
|
| 70 |
with torch.no_grad():
|
| 71 |
outputs = model(**inputs)
|
| 72 |
|
|
|
|
| 73 |
prediction = torch.argmax(outputs.logits, dim=1).item()
|
|
|
|
|
|
|
| 74 |
label = model.config.id2label[prediction]
|
| 75 |
|
| 76 |
print("Predicted topic:", label)
|
| 77 |
```
|
| 78 |
|
| 79 |
+
Example output:
|
| 80 |
|
| 81 |
```text
|
| 82 |
Predicted topic: Sci/Tech
|
| 83 |
```
|
| 84 |
|
|
|
|
|
|
|
| 85 |
## Limitations
|
| 86 |
|
| 87 |
+
* The model is trained on English news data and may not generalize well to other domains or languages.
|
| 88 |
+
* It only supports the four categories present in AG News.
|
| 89 |
* Performance on real-world data may differ from the reported test-set results.
|
|
|
|
|
|
|
| 90 |
* The model is not intended for high-stakes decision-making.
|
| 91 |
|
| 92 |
## References
|
| 93 |
|
|
|
|
|
|
|
| 94 |
Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2018).
|
|
|
|
| 95 |
*BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.*
|
|
|
|
| 96 |
https://arxiv.org/abs/1810.04805
|
| 97 |
|
|
|
|
|
|
|
| 98 |
Zhang, X., Zhao, J., & LeCun, Y. (2015).
|
|
|
|
| 99 |
*Character-level Convolutional Networks for Text Classification.*
|
|
|
|
|
|
|
|
|
|
| 100 |
https://arxiv.org/abs/1509.01626
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
Tunstall, L., von Werra, L., & Wolf, T.
|
| 103 |
+
*Natural Language Processing with Transformers.* O'Reilly Media, Chapters 1–3.
|
| 104 |
|
|
|
|
|
|
|
|
|