File size: 7,232 Bytes
9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 1d3a1a4 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 1394f8b 28364ab 9770453 1d3a1a4 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 28364ab 9770453 |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
---
library_name: transformers
tags:
- question-answering
- extractive-qa
- modernbert
- transformers
- squad
language: en
datasets:
- squad
metrics:
- f1
- exact_match
model_type: modernbert
license: apache-2.0
finetuned_from: answerdotai/ModernBERT-base
pipeline_tag: question-answering
---
# ModernBERT-QnA-base-squad 馃殌
Welcome to the **ModernBERT-QnA-base-squad** repository! This repository hosts the fine-tuned **ModernBERT** model for Question Answering tasks. The model achieves impressive performance on the SQuAD dataset, making it an excellent choice for extractive question-answering applications.
---
## Model Overview 馃専
- **Model ID:** `rankyx/ModernBERT-QnA-base-squad`
- **Base Model:** [answerdotai/ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base)
- **Dataset:** [SQuAD](https://rajpurkar.github.io/SQuAD-explorer/)
- **Evaluation Metrics:**
- F1 Score: **92.59**
- Exact Match: **86.45**
- **Training Framework:** [Hugging Face Transformers](https://huggingface.co/transformers/)
Read more about ModernBERT's capabilities in this [Hugging Face blog post](https://huggingface.co/blog/modernbert).
---
## Usage 馃捇
The following example demonstrates how to use the fine-tuned model for question answering using the Hugging Face `pipeline`.
For now, you have to install a specific `transformers` fork, until the official PR will be merged.
```bash
> pip uninstall transformers -y
> git clone https://github.com/bakrianoo/transformers.git
> cd transformers && git checkout feat-ModernBert-QnA-Support && pip install -e .
```
### Quick Start
```python
from transformers.models.modernbert.modular_modernbert import ModernBertForQuestionAnswering
from transformers import AutoTokenizer, pipeline
# Load the model and tokenizer
model_id = "rankyx/ModernBERT-QnA-base-squad"
model = ModernBertForQuestionAnswering.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Initialize the question-answering pipeline
question_answerer = pipeline("question-answering", model=model, tokenizer=tokenizer)
# Example input
question = "How many parameters does BLOOM contain?"
context = "BLOOM has 176 billion parameters and can generate text in 46 natural languages and 13 programming languages."
# Get the answer
result = question_answerer(question=question, context=context)
print(result)
```
### Sample Output
```python
{'score': 0.7719728946685791, 'start': 9, 'end': 21, 'answer': '176 billion'}
```
---
## Performance Demonstrations 馃敟
### Example 1: Short Context
```python
from transformers import pipeline
model_id = "rankyx/ModernBERT-QnA-base-squad"
question_answerer = pipeline("question-answering", model=model_id)
# Input
question = "What is the capital of France?"
context = "France's capital is Paris, known for its art, gastronomy, and culture."
# Get the answer
result = question_answerer(question=question, context=context)
print(result)
```
**Predicted Answer:**
```python
{'score': 0.9913662075996399, 'start': 19, 'end': 25, 'answer': ' Paris'}
```
### Example 2: Long Context
```python
from transformers import pipeline
model_id = "rankyx/ModernBERT-QnA-base-squad"
question_answerer = pipeline("question-answering", model=model_id)
# Input
question = "What are the major achievements of Isaac Newton?"
context = """
Isaac Newton, born on January 4, 1643, was an English mathematician, physicist, astronomer, and author. He is widely recognized as one of the greatest mathematicians and most influential scientists of all time. Newton made groundbreaking contributions to many fields, including the laws of motion and universal gravitation. He also developed calculus independently, providing the mathematical foundation for classical mechanics. Additionally, Newton's work in optics led to the invention of the reflecting telescope.
"""
# Get the answer
result = question_answerer(question=question, context=context)
print(result)
```
**Predicted Answer:**
```python
{'score': 0.5126065015792847, 'start': 278, 'end': 323, 'answer': ' the laws of motion and universal gravitation'}
```
### Example 3: Extremely Long Context
```python
from transformers import pipeline
model_id = "rankyx/ModernBERT-QnA-base-squad"
question_answerer = pipeline("question-answering", model=model_id)
# Input
question = "Describe the primary focus of the United Nations."
context = """
The United Nations (UN) is an international organization founded in 1945. It is currently made up of 193 Member States. The mission and work of the United Nations are guided by the purposes and principles contained in its founding Charter. The UN is best known for its peacekeeping, peacebuilding, conflict prevention, and humanitarian assistance. It also works on promoting sustainable development, protecting human rights, upholding international law, and delivering humanitarian aid. Through its various specialized agencies, funds, and programs, the UN addresses issues ranging from health to education to climate change.
"""
# Get the answer
result = question_answerer(question=question, context=context)
print(result)
```
**Predicted Answer:**
```python
{'score': 0.08445773273706436, 'start': 269, 'end': 347, 'answer': ' peacekeeping, peacebuilding, conflict prevention, and humanitarian assistance'}
```
---
## Fine-tuning Process 鈿欙笍
The model was fine-tuned using the [Hugging Face Transformers](https://github.com/huggingface/transformers/blob/main/examples/pytorch/question-answering/run_qa.py) library with the official script for question answering.
### Command Used for Fine-tuning
```bash
python run_qa.py \
--model_name_or_path "answerdotai/ModernBERT-base" \
--dataset_name squad \
--do_train \
--do_eval \
--overwrite_output_dir \
--per_device_train_batch_size 25 \
--per_device_eval_batch_size 20 \
--eval_strategy="steps" \
--save_strategy="epoch" \
--logging_steps 50 \
--eval_steps 500 \
--learning_rate 3e-5 \
--warmup_ratio 0.1 \
--weight_decay 0.01 \
--doc_stride 128 \
--max_seq_length 384 \
--max_answer_length 128 \
--num_train_epochs 2 \
--run_name="ModernBERT-QnA-base-squad" \
--output_dir="/path/to/output/directory"
```
If you have multiple GPUs and getting the error: `RuntimeError: Detected that you are using FX to symbolically trace a dynamo-optimized function` try this:
```bash
accelerate launch run_qa.py \
...other parameters
```
---
## Results 馃搳
### Evaluation Metrics
- **F1 Score:** 92.59
- **Exact Match:** 86.45
- **Training Loss:** 0.860
---
## License 馃摐
This model is licensed under the Apache 2.0 License. See [LICENSE](LICENSE) for details.
---
## Citation 鉁嶏笍
If you use this model in your research, please cite it as follows:
```
@misc{rankyx2024modernbertqna,
title={ModernBERT-QnA-base-squad},
author={Abu Bakr},
year={2024},
howpublished={\url{https://huggingface.co/rankyx/ModernBERT-QnA-base-squad}}
}
```
---
## Acknowledgments 馃檶
Special thanks to the Hugging Face team for providing the tools and resources to fine-tune and deploy this model.
---
## Feedback and Contributions 馃
We welcome feedback and contributions! Please feel free to open an issue or submit a pull request.
|