This repository contains a Google Colab notebook for training a sequence-to-sequence (Seq2Seq) model in a Pointer-Generator Network (PGN) using the PubMedQA dataset. The notebook uses Hugging Face Transformers and Datasets to fine-tune a BART model for biomedical question answering.
Trained Model: The fine-tuned model is available on Hugging Face at dev2004v/pgn_bart_pubmed_qa_80k
To train a text generation model that:
- Answers biomedical questions based on research abstracts
- Uses supporting context from scientific literature
- Produces long-form, context-aware answers
- Supports copying important terms from the input
- Name: PubMedQA
- Split:
pqa_artificial - Source: Hugging Face Datasets
Fields used:
question- The biomedical question to answercontext- Research abstract providing supporting informationlong_answer- Detailed answer to be generated
Question: <question_text>
Context: <abstract_text>
<long_answer>
- Load PubMedQA dataset from Hugging Face
- Split data into training and validation sets
- Preprocess data into PGN-style prompts
- Tokenize inputs and targets
- Apply dynamic padding using a data collator
- Configure Seq2Seq training arguments
- Train the model using
Seq2SeqTrainer
- Dynamic padding for computational efficiency
- Gradient accumulation for effective batch sizing
- Gradient checkpointing for reduced memory usage
- Mixed-precision (FP16) training for faster computation
- Automatic best model selection based on validation loss
The notebook installs all required dependencies automatically:
transformers
datasets
torch
accelerateRecommended: A GPU-enabled Google Colab runtime
- Open the notebook in Google Colab
- Select GPU as the runtime accelerator (
Runtime > Change runtime type > GPU) - Run all cells sequentially
- Model checkpoints will be saved to the output directory
You can directly use the trained model from Hugging Face:
from transformers import BartTokenizer, BartForConditionalGeneration
model_name = "dev2004v/pgn_bart_pubmed_qa_80k"
tokenizer = BartTokenizer.from_pretrained(model_name)
model = BartForConditionalGeneration.from_pretrained(model_name)
# Example usage
question = "Does caffeine improve athletic performance?"
context = "Your research abstract here..."
input_text = f"Question: {question}\nContext: {context}"
inputs = tokenizer(input_text, return_tensors="pt", max_length=1024, truncation=True)
outputs = model.generate(**inputs, max_length=150, num_beams=4)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(answer)- Training and validation loss logs
- Saved model checkpoints per epoch
- Best model loaded automatically at the end of training
- Evaluation metrics on validation set
This project is intended for academic and learning purposes.
- PubMedQA Dataset
- Hugging Face Datasets - Dataset implementation used in this project
- Hugging Face Transformers
- Pointer-Generator Networks (See et al., 2017)