Skip to main content

Chapter 1: What is Fine-Tuning?

Customizing Pre-Trained Models for Your Needs​

Fine-tuning is the process of taking a pre-trained language model and continuing its training on a specific dataset to adapt it for particular tasks or domains.


The Transfer Learning Foundation​

What Happens During Pre-Training?​

Before a model can be fine-tuned, it goes through pre-training:

  1. Data Collection: Trillions of tokens from books, websites, code, etc.
  2. Next Token Prediction: The model learns to predict what comes next
  3. Knowledge Acquisition: The model develops understanding of language, facts, reasoning
  4. General Capabilities: Reading, writing, summarizing, coding, and more
Example

Models like Llama-3.1 are pre-trained on over 15 trillion tokens, giving them broad knowledge and capabilities before any customization.


What Fine-Tuning Changes​

When you fine-tune a model, you're modifying its internal weights to:

1. Adapt to Specific Tasks​

  • Following particular instruction formats
  • Generating specific output structures (JSON, markdown, etc.)
  • Focusing on certain types of problems

2. Learn Domain Knowledge​

  • Medical terminology and protocols
  • Legal reasoning and citations
  • Company-specific processes and language

3. Adjust Behavior and Style​

  • Tone of voice (formal, casual, technical)
  • Response length preferences
  • Personality and character traits

Pre-Training vs Fine-Tuning​

AspectPre-TrainingFine-Tuning
Data SizeTrillions of tokensThousands to millions
ComputeThousands of GPUs, months1-8 GPUs, hours to days
GoalGeneral language understandingSpecific task adaptation
Who Does ItLarge AI labsAnyone with GPU access
CostMillions of dollarsHundreds to thousands

The Fine-Tuning Process​

Step 1: Choose a Base Model​

Select a pre-trained model appropriate for your use case:

from transformers import AutoModelForCausalLM

# Load a pre-trained base model
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B")

Step 2: Prepare Your Dataset​

Format your data for the training task:

# Example instruction-following format
training_example = {
"instruction": "Summarize the following article",
"input": "The European Central Bank announced...",
"output": "The ECB raised interest rates by 0.25%..."
}

Step 3: Configure Training​

Set up training parameters:

from transformers import TrainingArguments

training_args = TrainingArguments(
output_dir="./fine-tuned-model",
learning_rate=2e-5,
num_train_epochs=3,
per_device_train_batch_size=4,
)

Step 4: Train​

Run the fine-tuning process:

from trl import SFTTrainer

trainer = SFTTrainer(
model=model,
train_dataset=dataset,
args=training_args,
)
trainer.train()

Step 5: Evaluate and Deploy​

Test your model and deploy it:

# Save the fine-tuned model
model.save_pretrained("./my-custom-model")

# Use it for inference
output = model.generate(input_ids, max_new_tokens=100)

What Fine-Tuning Can Achieve​

✅ Excellent Results​

  • Style Transfer: Making a model write in your company's voice
  • Format Following: Outputting specific JSON structures reliably
  • Domain Specialization: Medical, legal, financial expertise
  • Instruction Following: Following complex multi-step instructions
  • Character/Persona: Consistent personality and behavior

⚠️ Moderate Results​

  • New Languages: Works but may need significant data
  • Complex Reasoning: Improvements possible but limited
  • Factual Knowledge: Better to use RAG for dynamic facts

❌ Poor Results​

  • Real-time Information: Model can't learn what it wasn't trained on
  • Mathematical Accuracy: Fine-tuning alone won't fix calculation errors
  • Fundamental Architecture Changes: Can't add new capabilities

Types of Fine-Tuning​

1. Full Fine-Tuning​

Updating all model parameters:

  • Highest potential for customization
  • Requires the most memory and compute
  • Risk of catastrophic forgetting

2. Parameter-Efficient Fine-Tuning (PEFT)​

Updating only a small subset of parameters:

  • Much lower memory requirements
  • Faster training
  • Often comparable quality to full fine-tuning
  • Examples: LoRA, QLoRA, Prefix Tuning, IA3

3. Instruction Tuning​

Training on instruction-response pairs:

  • Teaches models to follow instructions
  • Uses formats like Alpaca or ShareGPT
  • Foundation for chat-style models

4. Alignment/RLHF​

Training models to align with human preferences:

  • Uses human feedback or preference data
  • Methods: RLHF, DPO, ORPO
  • Creates helpful, harmless, and honest models

Real-World Example​

Let's say you're building a customer support bot for a software company:

Without Fine-Tuning​

The base model might:

  • Give generic, lengthy responses
  • Not know your product's features
  • Use an inconsistent tone
  • Miss your company's specific terminology

With Fine-Tuning​

The customized model can:

  • Respond concisely in your company's voice
  • Reference specific product features correctly
  • Follow your support team's response templates
  • Escalate appropriately when needed
# Example training data for support bot
examples = [
{
"instruction": "Customer asks about pricing",
"input": "How much does the Pro plan cost?",
"output": "Great question! Our Pro plan is $29/month when billed annually, or $35/month for monthly billing. It includes unlimited projects, priority support, and advanced analytics. Would you like me to explain any specific features?"
},
# ... hundreds more examples
]

Key Takeaways​

✅ Fine-tuning adapts pre-trained models to specific tasks or domains

✅ It builds on transfer learning—leveraging knowledge from pre-training

✅ Different methods exist (full, PEFT, instruction tuning, alignment)

✅ Fine-tuning is powerful but not always the right solution

✅ The process involves: base model → your data → training → custom model


Next Steps​

Now that you understand what fine-tuning is, let's explore when it's the right choice in Chapter 2: When to Fine-Tune vs Use RAG.


Quick Check​

Before moving on, make sure you can answer:

  1. What is the relationship between pre-training and fine-tuning?
  2. What are three things fine-tuning can help a model learn?
  3. What is the difference between full fine-tuning and PEFT?