Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RoBERTa Fine-Tuned Model for Question Answering
|
| 2 |
+
This repository hosts a fine-tuned version of the RoBERTa model optimized for question-answering tasks using the [SQuAD](w) dataset. The model is designed to efficiently perform question answering while maintaining high accuracy.
|
| 3 |
+
## Model Details- **Model Architecture**: RoBERTa- **Task**: Question Answering- **Dataset**: [SQuAD](w) (Stanford Question Answering Dataset)- **Quantization**: FP16- **Fine-tuning Framework**: Hugging Face Transformers
|
| 4 |
+
## Usage### Installation```python
|
| 5 |
+
from transformers import RobertaForQuestionAnswering, RobertaTokenizer
|
| 6 |
+
import torch
|
| 7 |
+
# Load the fine-tuned RoBERTa model and tokenizer
|
| 8 |
+
model_name = 'roberta_finetuned_squad' # Your fine-tuned RoBERTa model
|
| 9 |
+
model = RobertaForQuestionAnswering.from_pretrained(model_name)
|
| 10 |
+
tokenizer = RobertaTokenizer.from_pretrained(model_name)
|
| 11 |
+
# Move the model to GPU if available
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
model.to(device)
|
| 14 |
+
# Quantize the model to FP16
|
| 15 |
+
model = model.half()
|
| 16 |
+
# Save the quantized model and tokenizer
|
| 17 |
+
model.save_pretrained("./quantized_roberta_model_squad")
|
| 18 |
+
tokenizer.save_pretrained("./quantized_roberta_model_squad")
|
| 19 |
+
'''
|
| 20 |
+
# Example input for testing (10 questions)input_texts = [
|
| 21 |
+
"What is the capital of France?",
|
| 22 |
+
"Who is the CEO of Tesla?",
|
| 23 |
+
"What is the largest ocean in the world?",
|
| 24 |
+
"When did World War II end?",
|
| 25 |
+
"Who wrote 'Pride and Prejudice'?",
|
| 26 |
+
"What is the square root of 64?",
|
| 27 |
+
"What is the boiling point of water?",
|
| 28 |
+
"Who painted the Mona Lisa?",
|
| 29 |
+
"What is the currency of Japan?",
|
| 30 |
+
"Who discovered penicillin?"]
|
| 31 |
+
# Context text for answering questionscontext = "Paris is the capital of France. Elon Musk is the CEO of Tesla. The Pacific Ocean is the largest ocean in the world. World War II ended in 1945. Jane Austen wrote 'Pride and Prejudice'. The square root of 64 is 8. Water boils at 100 degrees Celsius. Leonardo da Vinci painted the Mona Lisa. The currency of Japan is the Yen. Alexander Fleming discovered penicillin."# Process each input questionfor input_text in input_texts:
|
| 32 |
+
# Tokenize input question and context inputs = tokenizer(input_text, context, return_tensors="pt").to(device)
|
| 33 |
+
# Perform inferencewith torch.no_grad():
|
| 34 |
+
outputs = model(**inputs) start_scores = outputs.start_logits
|
| 35 |
+
end_scores = outputs.end_logits
|
| 36 |
+
# Get the predicted answer
|
| 37 |
+
start_index = torch.argmax(start_scores)
|
| 38 |
+
end_index = torch.argmax(end_scores)
|
| 39 |
+
# Decode the predicted answer
|
| 40 |
+
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs.input_ids[0][start_index:end_index+1]))
|
| 41 |
+
print(f"Question: {input_text}")
|
| 42 |
+
print(f"Answer: {answer}\n")
|
| 43 |
+
'''
|
| 44 |
+
|
| 45 |
+
📊 Evaluation Results
|
| 46 |
+
After fine-tuning the RoBERTa-base model for question answering, we evaluated the model's performance on the validation set from the SQuAD dataset. The following results were obtained:
|
| 47 |
+
Metric Score Meaning
|
| 48 |
+
F1 Score 89.5 Measures the balance between precision and recall for answer extraction.
|
| 49 |
+
Exact Match 82.4 Percentage of questions where the predicted answer matches the ground truth exactly.
|
| 50 |
+
Fine-Tuning Details
|
| 51 |
+
Dataset
|
| 52 |
+
The SQuAD dataset, containing over 100,000 question-answer pairs based on Wikipedia articles, was used for fine-tuning the model.
|
| 53 |
+
Training
|
| 54 |
+
Number of epochs: 3
|
| 55 |
+
Batch size: 8
|
| 56 |
+
Evaluation strategy: steps
|
| 57 |
+
Quantization
|
| 58 |
+
Post-training quantization was applied using PyTorch's built-in quantization framework to reduce the model size and improve inference efficiency.
|
| 59 |
+
Repository Structure
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
├── model/ # Contains the quantized model files├── tokenizer_config/ # Tokenizer configuration and vocabulary files├── model.safetensors/ # Quantized Model
|
| 63 |
+
├── README.md # Model documentation
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
Limitations
|
| 67 |
+
The model is primarily trained on the SQuAD dataset and may not perform well on domain-specific question-answering tasks without additional fine-tuning. The model may struggle with highly ambiguous or multi-answer questions.
|
| 68 |
+
Contributing
|
| 69 |
+
Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.
|
| 70 |
+
|
| 71 |
+
## License
|
| 72 |
+
This model and code are provided under the [MIT License](w). You are free to use, modify, and distribute this model for both academic and commercial purposes, provided that appropriate credit is given.
|
| 73 |
+
## Acknowledgments- [RoBERTa](w): A Robustly Optimized BERT Pretraining Approach by Facebook AI Research (FAIR).- [SQuAD](w): Stanford Question Answering Dataset, created by the Stanford NLP Group.
|
| 74 |
+
## Contact
|
| 75 |
+
For any questions or suggestions, please feel free to reach out via [Issues](w) on GitHub or contact the maintainers at [your_email@example.com](mailto:your_email@example.com).
|
| 76 |
+
## Citation
|
| 77 |
+
If you use this model in your research or project, please cite the following paper:
|
| 78 |
+
|
| 79 |
+
@article{liu2019roberta, title={RoBERTa: A Robustly Optimized BERT Pretraining Approach}, author={Liu, Yinhan and Ott, Myle and Goyal, Naman and Du, Jingfei and Joshi, Mandar and Chen, Danqi and Levy, Omer and Lewis, Mike and Zettlemoyer, Luke and Facebook AI}, journal={arXiv preprint arXiv:1907.11692}, year={2019} }
|
| 80 |
+
|
| 81 |
+
vbnetCopyEdit## Frequently Asked Questions (FAQ)
|
| 82 |
+
**Q: How can I fine-tune this model on a custom dataset?**
|
| 83 |
+
A: To fine-tune the model on your own dataset, you can follow these steps:1. Preprocess your dataset into a format compatible with the Hugging Face `Trainer`.2. Use the `RobertaForQuestionAnswering` class and set up the fine-tuning loop using the `Trainer` API from Hugging Face.3. Train the model on your dataset and evaluate it using metrics like F1 and Exact Match.
|
| 84 |
+
**Q: What if my model is running out of memory during inference?**
|
| 85 |
+
A: If you are running out of memory, try the following:
|
| 86 |
+
- Use smaller batch sizes or batch the inference.
|
| 87 |
+
- Perform inference on CPU if GPU memory is insufficient.
|
| 88 |
+
- Quantize the model further (e.g., FP16 to INT8) to reduce the model size.
|
| 89 |
+
**Q: Can I use this model for other NLP tasks?**
|
| 90 |
+
A: This model is primarily fine-tuned for question answering. If you want to adapt it for other NLP tasks (such as sentiment analysis or text classification), you will need to modify the head of the model accordingly and fine-tune it on the relevant dataset.
|
| 91 |
+
---
|
| 92 |
+
We hope this model helps you in your NLP tasks! Feel free to contribute improvements or share your results with us!
|