This project implements knowledge distillation for text classification using the AG News dataset. It demonstrates how to transfer knowledge from a larger BERT model (teacher) to a smaller DistilBERT model (student) while maintaining good performance.
The project implements a knowledge distillation pipeline that:
Knowledge distillation is a technique where a smaller model (student) learns to mimic the behavior of a larger model (teacher). The process involves:
The project requires the following Python packages:
numpy==1.24.3
pandas==2.0.3
scikit-learn==1.3.0
matplotlib==3.7.2
seaborn==0.12.2
jupyter==1.0.0
transformers==4.36.2
datasets==2.18.0
torch==2.2.1
accelerate==0.25.0
git clone https://github.com/kevin-mlengineer/model-finetuning.git
cd model-finetuning
python3 -m venv venv
source venv/bin/activate # On Unix/macOS
# or
.\venv\Scripts\activate # On Windows
pip install -r requirements.txt
Data Loading and Preparation (load_and_prepare_data):
Baseline Student Training (train_baseline_student):
Knowledge Distillation (DistillationTrainer):
Distilled Student Training (train_distilled_student):
Teacher Evaluation (evaluate_teacher):
Run the main script:
python finetune.py
training_args = TrainingArguments(
output_dir='./custom_model',
num_train_epochs=5, # Increase epochs
per_device_train_batch_size=64, # Larger batch size
learning_rate=1e-5, # Custom learning rate
# ... other parameters
)
distillation_trainer = DistillationTrainer(
model=student_model,
teacher_model=teacher_model,
temp=2.0, # Lower temperature
alpha=0.7, # More weight on student loss
# ... other parameters
)
The script provides:
./baseline_student_model./distilled_student_model./tokenizerThe project uses Python's logging module with the following features:
The code automatically detects and uses the appropriate device:
The code includes comprehensive error handling for:
This project implements knowledge distillation for text classification using the AG News dataset. It demonstrates how to transfer knowledge from a larger BERT model (teacher) to a smaller DistilBERT model (student) while maintaining good performance.
The project implements a knowledge distillation pipeline that:
Knowledge distillation is a technique where a smaller model (student) learns to mimic the behavior of a larger model (teacher). The process involves:
The project requires the following Python packages:
numpy==1.24.3
pandas==2.0.3
scikit-learn==1.3.0
matplotlib==3.7.2
seaborn==0.12.2
jupyter==1.0.0
transformers==4.36.2
datasets==2.18.0
torch==2.2.1
accelerate==0.25.0
git clone https://github.com/kevin-mlengineer/model-finetuning.git
cd model-finetuning
python3 -m venv venv
source venv/bin/activate # On Unix/macOS
# or
.\venv\Scripts\activate # On Windows
pip install -r requirements.txt
Data Loading and Preparation (load_and_prepare_data):
Baseline Student Training (train_baseline_student):
Knowledge Distillation (DistillationTrainer):
Distilled Student Training (train_distilled_student):
Teacher Evaluation (evaluate_teacher):
Run the main script:
python finetune.py
training_args = TrainingArguments(
output_dir='./custom_model',
num_train_epochs=5, # Increase epochs
per_device_train_batch_size=64, # Larger batch size
learning_rate=1e-5, # Custom learning rate
# ... other parameters
)
distillation_trainer = DistillationTrainer(
model=student_model,
teacher_model=teacher_model,
temp=2.0, # Lower temperature
alpha=0.7, # More weight on student loss
# ... other parameters
)
The script provides:
./baseline_student_model./distilled_student_model./tokenizerThe project uses Python's logging module with the following features:
The code automatically detects and uses the appropriate device:
The code includes comprehensive error handling for: