Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Natural Language Processing Fine-Tuning Sandbox

This repository contains examples and scripts for fine-tuning popular transformer-based models from Hugging Face using PyTorch. The main notebook demonstrates two different NLP tasks: sequence classification (sentiment analysis) and causal language modeling (instruction tuning).

Features

1. Sequence Classification (bert-base-cased)

  • Dataset: Yelp Review Full
  • Task: Predict review star ratings (1 to 5 stars) classification.
  • Model: bert-base-cased
  • Techniques:
    • Custom PyTorch training loop using DataLoader, AdamW, and LambdaLR scheduler.
    • Evaluation using torchmetrics.Accuracy.
    • Manual model saving and loading of .pt weights.

2. Causal Language Modeling / Instruct Fine-Tuning (facebook/opt-350m)

  • Dataset: timdettmers/openassistant-guanaco
  • Task: Instruction tuning for conversational agents.
  • Model: facebook/opt-350m
  • Techniques:
    • Training using Hugging Face's Trainer API.
    • bfloat16 precision to prevent gradient overflow/NaN and reduce memory usage.
    • Custom TrainingArguments optimization (gradient accumulation, weight decay, learning rate).
    • Inference using the Hugging Face pipeline API to compare pre- and post-fine-tuning outputs.

Requirements

To run the notebook, install the following dependencies:

pip install torch torchvision torchaudio 
pip install transformers datasets trl accelerate torchmetrics matplotlib tqdm

Note: A CUDA-enabled GPU and appropriate PyTorch installation are highly recommended for reasonable training times.

Usage

  1. Open the Jupyter Notebook fine-tuning_tf.ipynb.
  2. The notebook is divided into two distinct sections:
    • Cell 1–20: BERT fine-tuning (Sequence Classification).
    • Cell 21–28: OPT fine-tuning (Language Modeling).
  3. Run the cells sequentially.
    • Note: The language modeling section uses bfloat16, requiring an Ampere architecture GPU (e.g., RTX 30-series, A100) or newer for optimal hardware support natively.

Troubleshooting

  • Loss going to NaN during LM Fine-Tuning: If using bf16=True in TrainingArguments, ensure the model is loaded with torch_dtype=torch.bfloat16 from the start to prevent fp32-to-bf16 cast overflow.
  • Out of Memory (OOM): Reduce per_device_train_batch_size and increase gradient_accumulation_steps proportionately to keep the effective batch size the same while lowering VRAM footprint.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages