Ali Assi commited on
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,206 +1,165 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
tags:
|
| 5 |
-
-
|
|
|
|
| 6 |
- lora
|
| 7 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
## Model Details
|
| 17 |
-
|
| 18 |
-
### Model Description
|
| 19 |
-
|
| 20 |
-
<!-- Provide a longer summary of what this model is. -->
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 26 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 27 |
-
- **Model type:** [More Information Needed]
|
| 28 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 29 |
-
- **License:** [More Information Needed]
|
| 30 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 31 |
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
- **Paper [optional]:** [More Information Needed]
|
| 38 |
-
- **Demo [optional]:** [More Information Needed]
|
| 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 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 73 |
-
|
| 74 |
-
## How to Get Started with the Model
|
| 75 |
-
|
| 76 |
-
Use the code below to get started with the model.
|
| 77 |
-
|
| 78 |
-
[More Information Needed]
|
| 79 |
|
| 80 |
## Training Details
|
| 81 |
|
| 82 |
-
###
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
### Training
|
| 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 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 142 |
-
|
| 143 |
-
[More Information Needed]
|
| 144 |
-
|
| 145 |
-
## Environmental Impact
|
| 146 |
-
|
| 147 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 148 |
-
|
| 149 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 150 |
-
|
| 151 |
-
- **Hardware Type:** [More Information Needed]
|
| 152 |
-
- **Hours used:** [More Information Needed]
|
| 153 |
-
- **Cloud Provider:** [More Information Needed]
|
| 154 |
-
- **Compute Region:** [More Information Needed]
|
| 155 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 156 |
-
|
| 157 |
-
## Technical Specifications [optional]
|
| 158 |
-
|
| 159 |
-
### Model Architecture and Objective
|
| 160 |
-
|
| 161 |
-
[More Information Needed]
|
| 162 |
-
|
| 163 |
-
### Compute Infrastructure
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Hardware
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
#### Software
|
| 172 |
-
|
| 173 |
-
[More Information Needed]
|
| 174 |
-
|
| 175 |
-
## Citation [optional]
|
| 176 |
-
|
| 177 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 178 |
-
|
| 179 |
-
**BibTeX:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
**APA:**
|
| 184 |
-
|
| 185 |
-
[More Information Needed]
|
| 186 |
-
|
| 187 |
-
## Glossary [optional]
|
| 188 |
-
|
| 189 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## More Information [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Authors [optional]
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
| 200 |
-
|
| 201 |
-
## Model Card Contact
|
| 202 |
-
|
| 203 |
-
[More Information Needed]
|
| 204 |
-
### Framework versions
|
| 205 |
-
|
| 206 |
-
- PEFT 0.18.0
|
|
|
|
| 1 |
---
|
| 2 |
+
language: en
|
| 3 |
+
license: apache-2.0
|
| 4 |
tags:
|
| 5 |
+
- text-classification
|
| 6 |
+
- bert
|
| 7 |
- lora
|
| 8 |
+
- peft
|
| 9 |
+
- 20-newsgroups
|
| 10 |
+
datasets:
|
| 11 |
+
- SetFit/20_newsgroups
|
| 12 |
+
base_model: bert-base-uncased
|
| 13 |
+
metrics:
|
| 14 |
+
- accuracy
|
| 15 |
+
model-index:
|
| 16 |
+
- name: bert-lora-20newsgroups
|
| 17 |
+
results:
|
| 18 |
+
- task:
|
| 19 |
+
type: text-classification
|
| 20 |
+
name: Text Classification
|
| 21 |
+
dataset:
|
| 22 |
+
name: 20 Newsgroups
|
| 23 |
+
type: SetFit/20_newsgroups
|
| 24 |
+
metrics:
|
| 25 |
+
- type: accuracy
|
| 26 |
+
value: 0.82
|
| 27 |
+
name: Accuracy
|
| 28 |
---
|
| 29 |
|
| 30 |
+
# BERT-LoRA for 20 Newsgroups Classification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
## Model Description
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
This model is a **BERT-base-uncased** fine-tuned with **LoRA (Low-Rank Adaptation)** for multi-class text classification on the 20 Newsgroups dataset.
|
| 35 |
|
| 36 |
+
- **Base Model:** bert-base-uncased
|
| 37 |
+
- **Method:** LoRA (Parameter-Efficient Fine-Tuning)
|
| 38 |
+
- **Task:** Multi-class text classification (20 categories)
|
| 39 |
+
- **Dataset:** 20 Newsgroups (~11K training, ~7K test samples)
|
| 40 |
+
- **Trainable Parameters:** ~300K (0.3% of total)
|
| 41 |
+
- **Adapter Size:** ~2 MB
|
| 42 |
|
| 43 |
+
## Categories
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
The model classifies text into 20 newsgroup topics:
|
| 46 |
+
- `alt.atheism`, `comp.graphics`, `comp.os.ms-windows.misc`, `comp.sys.ibm.pc.hardware`
|
| 47 |
+
- `comp.sys.mac.hardware`, `comp.windows.x`, `misc.forsale`, `rec.autos`
|
| 48 |
+
- `rec.motorcycles`, `rec.sport.baseball`, `rec.sport.hockey`, `sci.crypt`
|
| 49 |
+
- `sci.electronics`, `sci.med`, `sci.space`, `soc.religion.christian`
|
| 50 |
+
- `talk.politics.guns`, `talk.politics.mideast`, `talk.politics.misc`, `talk.religion.misc`
|
| 51 |
|
| 52 |
+
## Usage
|
| 53 |
|
| 54 |
+
### Installation
|
| 55 |
|
| 56 |
+
```bash
|
| 57 |
+
pip install transformers peft torch
|
| 58 |
+
```
|
| 59 |
|
| 60 |
+
### Load Model
|
| 61 |
|
| 62 |
+
```python
|
| 63 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 64 |
+
from peft import PeftModel
|
| 65 |
+
import torch
|
| 66 |
|
| 67 |
+
# Load tokenizer
|
| 68 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 69 |
|
| 70 |
+
# Load base model
|
| 71 |
+
base_model = AutoModelForSequenceClassification.from_pretrained(
|
| 72 |
+
"bert-base-uncased",
|
| 73 |
+
num_labels=20
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
# Load LoRA adapters
|
| 77 |
+
model = PeftModel.from_pretrained(base_model, "your-username/bert-lora-20newsgroups")
|
| 78 |
+
model.eval()
|
| 79 |
+
```
|
| 80 |
|
| 81 |
+
### Make Predictions
|
| 82 |
|
| 83 |
+
```python
|
| 84 |
+
text = "NASA announced a new mission to Mars with advanced rovers."
|
| 85 |
|
| 86 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 87 |
|
| 88 |
+
with torch.no_grad():
|
| 89 |
+
outputs = model(**inputs)
|
| 90 |
+
prediction = outputs.logits.argmax(-1).item()
|
| 91 |
|
| 92 |
+
categories = [
|
| 93 |
+
"alt.atheism", "comp.graphics", "comp.os.ms-windows.misc",
|
| 94 |
+
"comp.sys.ibm.pc.hardware", "comp.sys.mac.hardware", "comp.windows.x",
|
| 95 |
+
"misc.forsale", "rec.autos", "rec.motorcycles", "rec.sport.baseball",
|
| 96 |
+
"rec.sport.hockey", "sci.crypt", "sci.electronics", "sci.med",
|
| 97 |
+
"sci.space", "soc.religion.christian", "talk.politics.guns",
|
| 98 |
+
"talk.politics.mideast", "talk.politics.misc", "talk.religion.misc"
|
| 99 |
+
]
|
| 100 |
|
| 101 |
+
print(f"Predicted category: {categories[prediction]}")
|
| 102 |
+
# Output: sci.space
|
| 103 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
## Training Details
|
| 106 |
|
| 107 |
+
### LoRA Configuration
|
| 108 |
+
- **Rank (r):** 8
|
| 109 |
+
- **Alpha:** 16
|
| 110 |
+
- **Dropout:** 0.1
|
| 111 |
+
- **Target modules:** query, value (attention layers)
|
| 112 |
+
|
| 113 |
+
### Training Hyperparameters
|
| 114 |
+
- **Learning rate:** 2e-4
|
| 115 |
+
- **Batch size:** 16
|
| 116 |
+
- **Epochs:** 3
|
| 117 |
+
- **Optimizer:** AdamW
|
| 118 |
+
- **Max sequence length:** 512
|
| 119 |
+
|
| 120 |
+
### Training Results
|
| 121 |
+
- **Training time:** ~15 minutes on GPU
|
| 122 |
+
- **Test accuracy:** ~82%
|
| 123 |
+
- **Parameters trained:** 294,912 (0.27% of total)
|
| 124 |
+
|
| 125 |
+
## Performance
|
| 126 |
+
|
| 127 |
+
| Metric | Value |
|
| 128 |
+
|--------|-------|
|
| 129 |
+
| Accuracy | 82% |
|
| 130 |
+
| Precision (macro) | 81% |
|
| 131 |
+
| Recall (macro) | 81% |
|
| 132 |
+
| F1-score (macro) | 81% |
|
| 133 |
+
|
| 134 |
+
## Limitations
|
| 135 |
+
|
| 136 |
+
- Trained only on newsgroup-style text from the 1990s
|
| 137 |
+
- May not generalize well to modern social media or short texts
|
| 138 |
+
- Performance varies across categories (some topics easier than others)
|
| 139 |
+
- Max input length is 512 tokens (longer texts are truncated)
|
| 140 |
+
|
| 141 |
+
## Why LoRA?
|
| 142 |
+
|
| 143 |
+
LoRA provides:
|
| 144 |
+
- **99% smaller model size** (2 MB vs 440 MB)
|
| 145 |
+
- **100x fewer trainable parameters** (300K vs 110M)
|
| 146 |
+
- **Faster training** (15 min vs 2+ hours)
|
| 147 |
+
- **Same accuracy** as full fine-tuning (~82%)
|
| 148 |
+
|
| 149 |
+
Perfect for deployment, experimentation, and resource-constrained environments.
|
| 150 |
+
|
| 151 |
+
## Citation
|
| 152 |
+
|
| 153 |
+
```bibtex
|
| 154 |
+
@misc{bert-lora-20newsgroups,
|
| 155 |
+
author = {Your Name},
|
| 156 |
+
title = {BERT-LoRA for 20 Newsgroups Classification},
|
| 157 |
+
year = {2024},
|
| 158 |
+
publisher = {HuggingFace},
|
| 159 |
+
howpublished = {\url{https://huggingface.co/your-username/bert-lora-20newsgroups}}
|
| 160 |
+
}
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
## License
|
| 164 |
+
|
| 165 |
+
Apache 2.0 (following base BERT model license)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|