Skip to main content

Chapter 3: Training Arguments and Optimization

Fine-Tuning Your Training Configuration​

Optimal training arguments are crucial for successful fine-tuning.


Complete TrainingArguments​

from transformers import TrainingArguments

training_args = TrainingArguments(
# Output
output_dir="./results",
overwrite_output_dir=True,

# Training duration
num_train_epochs=3,
max_steps=-1, # -1 means use epochs

# Batch size
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
gradient_accumulation_steps=4,

# Learning rate
learning_rate=2e-4,
lr_scheduler_type="cosine",
warmup_ratio=0.03,
weight_decay=0.01,

# Precision
bf16=True,

# Memory optimization
gradient_checkpointing=True,
optim="paged_adamw_8bit",

# Logging
logging_steps=10,
logging_first_step=True,

# Saving
save_strategy="epoch",
save_total_limit=2,

# Evaluation
evaluation_strategy="epoch",
load_best_model_at_end=True,

# Other
seed=42,
dataloader_num_workers=4,
)

Learning Rate Schedules​

ScheduleBehaviorBest For
linearLinear decaySimple tasks
cosineCosine decayMost fine-tuning
constantNo decayVery short training
polynomialPolynomial decayLonger training

Hyperparameter Guidelines​

ParameterLoRAQLoRA
Learning rate1e-4 to 5e-41e-4 to 2e-4
Epochs1-51-3
Batch size (effective)8-328-16
Warmup3-10%3-5%

Troubleshooting​

IssueSolution
Loss not decreasingIncrease learning rate
NaN lossDecrease LR, enable grad clipping
OOM errorReduce batch size, enable checkpointing
OverfittingEarly stopping, more data, regularization

Continue to Quiz →