CoolFace
Modelpublic

Sadat07/bert-SQuAD

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes21downloads
Model Card

google-bert/bert-base-uncased Fine-Tuned on SQuAD

bert_squad

Pretrained model on context-based Question Answering using the SQuAD dataset. This model is fine-tuned from the BERT architecture for extracting answers from passages.

Model Description

<!-- Provide a longer summary of what this model is. -->

bert_squad is a transformer-based model trained for context-based question answering tasks. It leverages the pretrained BERT architecture and adapts it for extracting precise answers given a question and a related context. This model uses the Stanford Question Answering Dataset (SQuAD), available via Hugging Face datasets, for training and fine-tuning.

The model was trained using free computational resources, demonstrating its accessibility for educational and small-scale research purposes.

Fine-tuned by: SADAT PARVEJ, RAFIFA BINTE JAHIR

Shared by: SADAT PARVEJ

Language(s) (NLP): ENGLISH

Finetuned from model: https://huggingface.co/google-bert/bert-base-uncased

Training Objective

The model predicts the most relevant span of text in a given passage that answers a specific question. It fine-tunes BERT's ability to analyze context using supervised data from SQuAD.

Performance Benchmarks

Training Loss: 0.477800

Validation Loss: 0.465936

Exact Match (EM): 87.568590%

Intended Uses & Limitations

This model is designed for tasks such as:

Extractive Question Answering Reading comprehension applications Known Limitations:

As BERT is inherently a masked language model (MLM), its original pretraining limits its ability for generative tasks or handling queries outside the SQuAD-style question-answering setup. The model's predictions may be biased or overly reliant on the training dataset, as SQuAD comprises structured and fact-based question-answer pairs.

How to Get Started with the Model

Use the code below to get started with the model.

python
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering

# Load the model and tokenizer
model_name = "Sadat07/bert_squad"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)


context = """
The person who invented light was 
Thomas Edison.He was born in 1879.
"""
question = "When did Thomas Edison invent?"


inputs = tokenizer(question, context, return_tensors="pt", truncation=True, max_length=512)
input_ids = inputs["input_ids"].to(device)
attention_mask = inputs["attention_mask"].to(device)


print("Tokenized Input:", tokenizer.decode(input_ids[0]))

# Perform inference
with torch.no_grad():
    outputs = model(input_ids=input_ids, attention_mask=attention_mask)
    start_scores = outputs.start_logits
    end_scores = outputs.end_logits

# Logits
print("Start logits:", start_scores)
print("End logits:", end_scores)

# Get start and end indices
start_idx = torch.argmax(start_scores)
end_idx = torch.argmax(end_scores) + 1

# Decode the answer
if start_idx >= end_idx:
    print("Model did not predict a valid answer. Please check context and question.")
else:
    answer = tokenizer.convert_tokens_to_string(
        tokenizer.convert_ids_to_tokens(input_ids[0][start_idx:end_idx])
    )
    print(f"Question: {question}")
    print(f"Answer: {answer}")
  

Training Details

StepTraining LossValidation LossExact MatchSquad F1Start AccuracyEnd Accuracy
1000.6322000.81180984.74929084.7492900.8474930.899243
2000.7515000.62719884.76821284.7682120.8476820.899243
3000.6626000.55751586.24408786.2440870.8624410.899243
4000.6004000.56769386.17786286.1778620.8617790.899243
5000.6132000.52354686.49952786.4995270.8649950.899243
6000.4952000.53922586.56575286.5657520.8656580.899243
7000.6453000.55235885.35477885.3547780.8535480.899243
8000.4991000.56231786.33869486.3386940.8633870.899243
9000.4828000.49974786.81173186.8117310.8681170.899243
10000.3728000.54351386.97256486.9725640.8697260.900000
11000.5540000.50274785.96972685.9697260.8596970.894797
12000.4598000.48494187.01986887.0198680.8701990.900662
13000.4636000.47752787.40775887.4077580.8740780.899905
14000.3568000.49911987.54966987.5496690.8754970.901608
15000.4942000.48528787.54966987.5496690.8754970.901703
16000.5211000.46606287.28476887.2847680.8728480.899243
17000.4612000.46270487.54020887.5402080.8754020.901419
18000.4157000.47429587.69158087.6915800.8769160.901892
19000.6229000.46290087.41721987.4172190.8741720.901987
20000.4778000.46593687.56859087.5685900.8756860.901892

Training Data

The model was trained on the SQuAD dataset, a widely used benchmark for context-based question-answering tasks. It consists of passages from Wikipedia and corresponding questions, with human-annotated answers.

During training, the dataset was processed to extract contexts, questions, and answers, ensuring compatibility with the BERT architecture for QA. The training utilized free resources to minimize costs and focus on model efficiency.

Training Procedure

Training Objective The model was trained with the objective of performing context-based question answering using the SQuAD dataset. The fine-tuning process adapts BERT's masked language model (MLM) architecture for QA tasks by leveraging its ability to encode contextual relationships between the passage, question, and answer.

Optimization The training utilized the AdamW optimizer with a linear learning rate scheduler and warm-up steps to ensure effective weight updates and prevent overfitting. The training was run for 2000 steps, with early stopping applied based on the validation loss and exact match score.

Hardware and Resources Training was conducted on free resources, such as Google Colab or equivalent free GPU resources. While this limited the scale, adjustments in batch size and learning rate were optimized to make the training efficient within these constraints.

Unique Features The model fine-tuning procedure emphasizes efficient learning, leveraging BERT's pre-trained knowledge while adapting it specifically to QA tasks in a resource-constrained environment.

Metrics

Performance was evaluated using the following metrics:

  • Exact Match (EM): Measures the percentage of predictions that match the ground-truth answers exactly.
  • F1 Score: Assesses the overlap between the predicted and true answers at a token level, balancing precision and recall.
  • Start and End Accuracy: Tracks the model’s ability to correctly identify the start and end indices of answers within the context.

Results

The model trained on the SQuAD dataset achieved the following key performance metrics:

Exact Match (EM): Up to 87.69%

F1 Score: Up to 87.69%

Validation Loss: Reduced to 0.46

Start Accuracy: Peaked at 87.69%

End Accuracy: Peaked at 90.19%

Summary

The model, bert_squad, was fine-tuned for context-based question answering using the SQuAD dataset from Hugging Face. Key metrics include an Exact Match (EM) and F1 score of up to 87.69%, demonstrating strong accuracy. Performance benchmarks show consistent improvement in loss and accuracy over 2000 steps, with validation loss reaching as low as 0.46.

The training utilized free resources, leveraging BERT’s robust pretraining, although BERT’s limitation as a Masked Language Model (MLM) remains a consideration. This work highlights the potential for effective question-answering systems built on pre-existing datasets and infrastructure.

Model Architecture and Objective

The model uses BERT, a pre-trained Transformer-based architecture, fine-tuned for context-based question answering tasks. It aims to predict answers based on the given input text and context.

Compute Infrastructure

Hardware

GPU: Tesla P100, NVIDIA T4

Software

Framework: Hugging Face Transformers

Dataset: SQuAD (from Hugging Face)

Other tools: Python, PyTorch

BibTeX:

bibtex

@misc{bert_squad_finetune,
  title = {BERT Fine-tuned for SQuAD},
  author = {Your Name or Team Name},
  year = {2024},
  url = {https://huggingface.co/your-model-repository}
}

Glossary

Exact Match (EM): A metric measuring the percentage of predictions that match the ground truth exactly.

Masked Language Model (MLM): Pre-training objective for BERT, predicting masked words in input sentences.