{"id":27427323,"url":"https://github.com/deepbiolab/gpt2-classification-peft","last_synced_at":"2025-08-10T08:13:07.813Z","repository":{"id":284172891,"uuid":"954061955","full_name":"deepbiolab/gpt2-classification-peft","owner":"deepbiolab","description":"How to fine-tune a GPT-2 model for medical question-pair similarity tasks using Hugging Face's PEFT (Parameter-Efficient Fine-Tuning) library and LoRA (Low-Rank Adaptation)","archived":false,"fork":false,"pushed_at":"2025-03-24T15:41:26.000Z","size":3413,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T12:58:22.918Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deepbiolab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-24T14:01:41.000Z","updated_at":"2025-03-24T15:41:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"0e5d2c4f-a380-4e3a-9a39-ef5e44b1d134","html_url":"https://github.com/deepbiolab/gpt2-classification-peft","commit_stats":null,"previous_names":["deepbiolab/gpt2-classification-peft"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/deepbiolab/gpt2-classification-peft","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepbiolab%2Fgpt2-classification-peft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepbiolab%2Fgpt2-classification-peft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepbiolab%2Fgpt2-classification-peft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepbiolab%2Fgpt2-classification-peft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deepbiolab","download_url":"https://codeload.github.com/deepbiolab/gpt2-classification-peft/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepbiolab%2Fgpt2-classification-peft/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269693593,"owners_count":24460248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-14T12:49:54.078Z","updated_at":"2025-08-10T08:13:07.803Z","avatar_url":"https://github.com/deepbiolab.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Medical QA Fine-Tuning with PEFT and LoRA\n\nThis project demonstrates how to fine-tune a GPT-2 model for medical question-pair similarity tasks using Hugging Face's PEFT (Parameter-Efficient Fine-Tuning) library and LoRA (Low-Rank Adaptation). The goal is to achieve high performance while keeping the number of trainable parameters minimal.\n\n\u003cimg src=\"assets/x4.png\" style=\"zoom:50%;\" /\u003e\n\n## Features\n- **Lightweight Fine-Tuning**: Uses LoRA to efficiently fine-tune GPT-2 without updating the entire model.\n- **Medical QA Dataset**: Processes medical question pairs to train a similarity classifier.\n- **Metrics Logging**: Tracks accuracy, F1 score, precision, and recall during training and evaluation.\n- **WandB Integration**: Logs training progress and performance metrics to Weights \u0026 Biases.\n- **Comparison of Models**: Evaluates the performance of the base model and the LoRA-tuned model.\n\n## Workflow\n\n### 1. Dataset Preparation\nThe medical question-pair dataset is used to train a similarity classifier. This dataset contains pairs of medical questions, where each pair is labeled to indicate whether the questions are semantically similar or not.\n\n### 2. Model Fine-Tuning\n- The base GPT-2 model is fine-tuned using LoRA, which updates only a small subset of parameters.\n- LoRA configuration includes:\n  - Low-rank matrices (`r=8`)\n  - Target modules (`c_attn`, `c_proj`)\n  - LoRA alpha scaling (`lora_alpha=32`)\n  - Dropout to prevent overfitting (`lora_dropout=0.1`)\n\n### 3. Evaluation\nThe fine-tuned model is evaluated on the validation and test datasets. The following metrics are computed:\n- Accuracy\n- F1 Score\n- Precision\n- Recall\n\n### 4. Performance Comparison\nThe performance of the base model and the LoRA-tuned model is compared. The results are visualized using a bar chart generated in WandB.\n\n\u003cimg src=\"assets/improved_performance.png\" alt=\"Model Performance Comparison\" style=\"zoom: 33%;\" /\u003e\n\n## How to Run\n\n### Prerequisites\n- Python 3.10.x\n- Install required libraries:\n  ```bash\n  pip install torch transformers datasets peft wandb scikit-learn pandas\n  ```\n\n### Steps\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/deepbiolab/gpt2-classification-peft.git\n   cd gpt2-classification-peft\n   ```\n2. Run the training script:\n   ```bash\n   python main.py\n   ```\n3. View logs and metrics in WandB.\n\n### Results\nAfter training, the fine-tuned model achieves improved performance compared to the base model while keeping the number of trainable parameters minimal. The comparison chart illustrates the accuracy improvement.\n\n## File Structure\n- `main.py`: Main script for training and evaluation.\n- `assets/improved_performance.png`: Visualization of model performance comparison.\n- `README.md`: Project documentation.\n\n## Key Libraries\n- **Hugging Face Transformers**: For model loading and training.\n- **PEFT**: Implements parameter-efficient fine-tuning methods like LoRA.\n- **WandB**: Logs training metrics and visualizations.\n- **Scikit-learn**: Computes evaluation metrics.\n\n## Future Work\n- Experiment with different LoRA configurations to optimize performance.\n- Apply PEFT techniques to other tasks like text summarization or translation.\n\n## License\nThis project is licensed under the MIT License. See the `LICENSE` file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepbiolab%2Fgpt2-classification-peft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeepbiolab%2Fgpt2-classification-peft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepbiolab%2Fgpt2-classification-peft/lists"}