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
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# BERT Topic Classification
|
| 2 |
+
|
| 3 |
+
This repository contains a fine-tuned `bert-base-uncased` model for **topic classification** using the AG News dataset.
|
| 4 |
+
|
| 5 |
+
The model classifies news articles into four categories:
|
| 6 |
+
|
| 7 |
+
* World
|
| 8 |
+
* Sports
|
| 9 |
+
* Business
|
| 10 |
+
* Sci/Tech
|
| 11 |
+
|
| 12 |
+
## Model Description
|
| 13 |
+
|
| 14 |
+
This model was developed as part of an experiment comparing different BERT adaptation strategies for NLP classification.
|
| 15 |
+
|
| 16 |
+
Two approaches were evaluated:
|
| 17 |
+
|
| 18 |
+
1. **Feature-based adaptation**: BERT was frozen and its `[CLS]` representations were used as features for a Logistic Regression classifier.
|
| 19 |
+
2. **Full fine-tuning**: The complete BERT model and the classification head were trained on the topic classification task.
|
| 20 |
+
|
| 21 |
+
The final model in this repository uses the **full fine-tuning** approach.
|
| 22 |
+
|
| 23 |
+
## Base Model
|
| 24 |
+
|
| 25 |
+
* Base model: `bert-base-uncased`
|
| 26 |
+
* Architecture: BERT for sequence classification
|
| 27 |
+
* Number of labels: 4
|
| 28 |
+
* Total parameters: approximately 109.5 million
|
| 29 |
+
* Trainable parameters: approximately 109.5 million
|
| 30 |
+
* Maximum sequence length: 128 tokens
|
| 31 |
+
|
| 32 |
+
## Training Data
|
| 33 |
+
|
| 34 |
+
The model was trained using the **AG News** dataset:
|
| 35 |
+
|
| 36 |
+
`fancyzhx/ag_news`
|
| 37 |
+
|
| 38 |
+
The original training set contains 120,000 examples. For this experiment, the original training set was split into:
|
| 39 |
+
|
| 40 |
+
* Training: 108,000 examples
|
| 41 |
+
* Validation: 12,000 examples
|
| 42 |
+
* Test: 7,600 examples
|
| 43 |
+
|
| 44 |
+
The original AG News test set was used as the final test set.
|
| 45 |
+
|
| 46 |
+
The four labels are:
|
| 47 |
+
|
| 48 |
+
| ID | Label |
|
| 49 |
+
| -: | -------- |
|
| 50 |
+
| 0 | World |
|
| 51 |
+
| 1 | Sports |
|
| 52 |
+
| 2 | Business |
|
| 53 |
+
| 3 | Sci/Tech |
|
| 54 |
+
|
| 55 |
+
## Training Procedure
|
| 56 |
+
|
| 57 |
+
The model was trained using Hugging Face Transformers and the `Trainer` API.
|
| 58 |
+
|
| 59 |
+
### Main configuration
|
| 60 |
+
|
| 61 |
+
* Epochs: 2
|
| 62 |
+
* Training batch size: 16
|
| 63 |
+
* Evaluation batch size: 32
|
| 64 |
+
* Gradient accumulation steps: 2
|
| 65 |
+
* BERT learning rate: `2e-5`
|
| 66 |
+
* Classification head learning rate: `1e-3`
|
| 67 |
+
* Weight decay: `0.01`
|
| 68 |
+
* Mixed precision: FP16
|
| 69 |
+
* Random seed: 42
|
| 70 |
+
* Hardware: NVIDIA Tesla T4
|
| 71 |
+
|
| 72 |
+
Two learning rates were used because the pretrained BERT parameters require smaller updates, while the newly initialized classification head can use a larger learning rate.
|
| 73 |
+
|
| 74 |
+
## Evaluation
|
| 75 |
+
|
| 76 |
+
The main evaluation metrics were:
|
| 77 |
+
|
| 78 |
+
* Accuracy
|
| 79 |
+
* Macro F1
|
| 80 |
+
* Cross-entropy loss
|
| 81 |
+
|
| 82 |
+
Accuracy measures the proportion of correctly classified examples.
|
| 83 |
+
|
| 84 |
+
Macro F1 calculates the F1 score independently for each class and then averages the scores, giving each class equal importance.
|
| 85 |
+
|
| 86 |
+
## Results
|
| 87 |
+
|
| 88 |
+
### Full Fine-Tuning
|
| 89 |
+
|
| 90 |
+
The final model achieved the following results on the test set:
|
| 91 |
+
|
| 92 |
+
| Metric | Score |
|
| 93 |
+
| ------------- | -----: |
|
| 94 |
+
| Test Loss | 0.3470 |
|
| 95 |
+
| Test Accuracy | 94.49% |
|
| 96 |
+
| Test Macro F1 | 94.49% |
|
| 97 |
+
|
| 98 |
+
### Per-Class Results
|
| 99 |
+
|
| 100 |
+
| Class | Precision | Recall | F1 |
|
| 101 |
+
| -------- | --------: | -----: | -----: |
|
| 102 |
+
| World | 0.9648 | 0.9521 | 0.9584 |
|
| 103 |
+
| Sports | 0.9895 | 0.9879 | 0.9887 |
|
| 104 |
+
| Business | 0.9248 | 0.9063 | 0.9155 |
|
| 105 |
+
| Sci/Tech | 0.9054 | 0.9368 | 0.9208 |
|
| 106 |
+
|
| 107 |
+
## Comparison with Feature-Based Adaptation
|
| 108 |
+
|
| 109 |
+
A feature-based approach was also trained using frozen BERT representations and Logistic Regression.
|
| 110 |
+
|
| 111 |
+
| Method | Test Accuracy | Test Macro F1 |
|
| 112 |
+
| ----------------------------------- | ------------: | ------------: |
|
| 113 |
+
| Feature-based + Logistic Regression | 90.20% | 90.19% |
|
| 114 |
+
| Full Fine-Tuning | 94.49% | 94.49% |
|
| 115 |
+
|
| 116 |
+
The full fine-tuning experiment obtained higher test-set accuracy and Macro F1 than the feature-based approach in this experiment.
|
| 117 |
+
|
| 118 |
+
The feature-based approach kept all BERT parameters frozen, while full fine-tuning allowed the BERT encoder and classification head to adapt to the AG News classification task.
|
| 119 |
+
|
| 120 |
+
## Intended Use
|
| 121 |
+
|
| 122 |
+
This model is intended for:
|
| 123 |
+
|
| 124 |
+
* Topic classification of English news text
|
| 125 |
+
* Educational experiments involving BERT fine-tuning
|
| 126 |
+
* Research and experimentation with text classification
|
| 127 |
+
* Demonstrations of BERT adaptation methods
|
| 128 |
+
|
| 129 |
+
The model predicts one of four topics: World, Sports, Business, or Sci/Tech.
|
| 130 |
+
|
| 131 |
+
## Limitations
|
| 132 |
+
|
| 133 |
+
This model was trained specifically on English news articles from the AG News dataset.
|
| 134 |
+
|
| 135 |
+
Potential limitations include:
|
| 136 |
+
|
| 137 |
+
* It is not designed for languages other than English.
|
| 138 |
+
* Its topic categories are limited to the four AG News classes.
|
| 139 |
+
* Performance on text outside the news domain may differ from the reported test results.
|
| 140 |
+
* The model may inherit biases or limitations present in the training data.
|
| 141 |
+
* The reported metrics were obtained on the AG News test set and may not represent performance on other datasets or real-world data.
|
| 142 |
+
|
| 143 |
+
## Example Usage
|
| 144 |
+
|
| 145 |
+
```python
|
| 146 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 147 |
+
import torch
|
| 148 |
+
|
| 149 |
+
model_id = "karencardiel/classification-bert"
|
| 150 |
+
|
| 151 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 152 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 153 |
+
|
| 154 |
+
text = "Researchers developed a new artificial intelligence system for analyzing genomic data."
|
| 155 |
+
|
| 156 |
+
inputs
|
| 157 |
+
```
|