Views
No views yet
distilbert/distilbert-base-uncased Fine-Tuned on SQuADdistilbert_squad is a lightweight transformer-based model fine-tuned for context-based question-answering tasks. It adapts the pretrained DistilBERT architecture to extract precise answers from passages. This model was trained and fine-tuned on the Stanford Question Answering Dataset (SQuAD), leveraging its efficiency for resource-constrained environments.1import torch
2from transformers import AutoTokenizer, AutoModelForQuestionAnswering
3
4# Load the model and tokenizer
5model_name = "YourHuggingFaceModelPath/distilbert_squad"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForQuestionAnswering.from_pretrained(model_name)
8
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model.to(device)
11
12context = """
13Thomas Edison is credited with inventing the light bulb. He was born in 1847 and was a prolific inventor.
14"""
15question = "Who invented the light bulb?"
16
17inputs = tokenizer(question, context, return_tensors="pt", truncation=True, max_length=512)
18input_ids = inputs["input_ids"].to(device)
19attention_mask = inputs["attention_mask"].to(device)
20
21# Perform inference
22with torch.no_grad():
23 outputs = model(input_ids=input_ids, attention_mask=attention_mask)
24 start_scores = outputs.start_logits
25 end_scores = outputs.end_logits
26
27# Get start and end indices
28start_idx = torch.argmax(start_scores)
29end_idx = torch.argmax(end_scores) + 1
30
31# Decode the answer
32if start_idx >= end_idx:
33 print("Model did not predict a valid answer. Please check context and question.")
34else:
35 answer = tokenizer.convert_tokens_to_string(
36 tokenizer.convert_ids_to_tokens(input_ids[0][start_idx:end_idx])
37 )
38 print(f"Question: {question}")
39 print(f"Answer: {answer}")| Step | Training Loss | Validation Loss | Exact Match | Squad F1 | Start Accuracy | End Accuracy |
|---|---|---|---|---|---|---|
| 100 | 0.719900 | 0.941330 | 84.66% | 84.66% | 84.66% | 89.92% |
| 500 | 0.640500 | 0.555793 | 84.87% | 84.87% | 84.87% | 89.92% |
| 1000 | 0.413100 | 0.551416 | 84.93% | 84.93% | 84.93% | 89.92% |
| 1500 | 0.522600 | 0.518057 | 85.17% | 85.17% | 85.17% | 89.92% |
| 2000 | 0.464500 | 0.504376 | 85.59% | 85.59% | 85.59% | 89.92% |
1@misc{distilbert_squad_finetune,
2 title = {DistilBERT Fine-tuned for SQuAD},
3 author = {Sadat Parvej},
4 year = {2024},
5 url = {https://huggingface.co/your-model-repository}
6}