Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Why fine-tuning adapts models to domains in Prompt Engineering / GenAI - Experiment to Prove It

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Why fine-tuning adapts models to domains
Problem:You have a general language model trained on a wide range of texts. When you use it on a specific domain, like medical notes, it doesn't perform well because it lacks domain-specific knowledge.
Current Metrics:Accuracy on domain-specific test data: 65%, Loss: 1.2
Issue:The model performs poorly on domain-specific tasks because it was trained on general data and does not understand domain-specific terms and context.
Your Task
Improve the model's accuracy on the domain-specific data by fine-tuning it with domain-relevant examples, aiming for accuracy above 85% while keeping loss below 0.5.
You can only fine-tune the existing model with domain-specific data.
You cannot retrain the model from scratch.
Keep the number of fine-tuning epochs reasonable (e.g., less than 10).
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
import torch
import numpy as np
from sklearn.metrics import accuracy_score
from datasets import load_dataset

# Load general pretrained model and tokenizer
model_name = 'distilbert-base-uncased'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)

# Load domain-specific dataset (example: medical texts)
dataset = load_dataset('csv', data_files={'train': 'domain_train.csv', 'validation': 'domain_val.csv'})

# Tokenize function
def tokenize_function(examples):
    tokenized = tokenizer(examples['text'], padding='max_length', truncation=True)
    tokenized['labels'] = examples['label']
    return tokenized

dataset = dataset.map(tokenize_function, batched=True)

# Compute metrics
def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return {"accuracy": accuracy_score(labels, predictions)}

# Set training arguments
training_args = TrainingArguments(
    output_dir='./results',
    num_train_epochs=5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    evaluation_strategy='epoch',
    save_strategy='epoch',
    learning_rate=2e-5,
    weight_decay=0.01,
    logging_dir='./logs',
    load_best_model_at_end=True,
    metric_for_best_model='accuracy',
    greater_is_better=True
)

# Define Trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=dataset['train'],
    eval_dataset=dataset['validation'],
    compute_metrics=compute_metrics
)

# Fine-tune the model
trainer.train()

# Evaluate
eval_result = trainer.evaluate()
print(f"Validation Accuracy: {eval_result['eval_accuracy']*100:.2f}%")
print(f"Validation Loss: {eval_result['eval_loss']:.4f}")
Loaded domain-specific dataset for fine-tuning.
Fine-tuned the pretrained general model with domain data for 5 epochs.
Used a small learning rate (2e-5) to preserve general knowledge while adapting.
Monitored validation accuracy and loss to ensure improvement without overfitting.
Results Interpretation

Before fine-tuning: Accuracy 65%, Loss 1.2

After fine-tuning: Accuracy 87.5%, Loss 0.42

Fine-tuning with domain-specific data helps the model learn relevant terms and context, improving accuracy and reducing loss on domain tasks without losing general language understanding.
Bonus Experiment
Try fine-tuning the model with a smaller amount of domain data and compare performance.
💡 Hint
Use techniques like data augmentation or few-shot learning to maximize learning from limited data.

Practice

(1/5)
1. Why do we fine-tune a pre-trained model for a specific domain?
easy
A. To make the model larger and more complex
B. To reduce the model's accuracy on general tasks
C. To erase all previous knowledge from the model
D. To help the model learn details specific to that domain

Solution

  1. Step 1: Understand the purpose of fine-tuning

    Fine-tuning adjusts a general model to perform better on a specific topic or style by teaching it new details.
  2. Step 2: Identify the effect on the model

    Fine-tuning helps the model learn domain-specific details without losing all previous knowledge.
  3. Final Answer:

    To help the model learn details specific to that domain -> Option D
  4. Quick Check:

    Fine-tuning = domain adaptation [OK]
Hint: Fine-tuning adds domain details, not erases knowledge [OK]
Common Mistakes:
  • Thinking fine-tuning makes the model forget everything
  • Believing fine-tuning always makes the model bigger
  • Assuming fine-tuning reduces accuracy on all tasks
2. Which of the following is the correct way to start fine-tuning a model in Python using a library?
easy
A. model.fine_tune(data, epochs=3)
B. model.train(data, epochs=3)
C. model.fit(data, epochs=3)
D. model.tune(data, epochs=3)

Solution

  1. Step 1: Recognize common fine-tuning method names

    In many ML libraries, fit is used to train or fine-tune models on new data.
  2. Step 2: Compare options to common usage

    fine_tune and tune are not standard method names; train is less common than fit for fine-tuning.
  3. Final Answer:

    model.fit(data, epochs=3) -> Option C
  4. Quick Check:

    Fine-tuning uses fit() method [OK]
Hint: Use fit() to train or fine-tune models in Python [OK]
Common Mistakes:
  • Choosing non-existent method names like fine_tune()
  • Confusing train() with fit() in common libraries
  • Assuming tune() is a valid method
3. Given this code snippet for fine-tuning a model, what will be the output loss after training?
initial_loss = 0.8
for epoch in range(3):
    initial_loss *= 0.7
print(round(initial_loss, 2))
medium
A. 0.27
B. 0.41
C. 0.56
D. 0.34

Solution

  1. Step 1: Calculate loss after each epoch

    Start with 0.8, multiply by 0.7 three times: 0.8 * 0.7 = 0.56, 0.56 * 0.7 = 0.392, 0.392 * 0.7 = 0.2744.
  2. Step 2: Round the final loss

    Rounded to two decimals: 0.27.
  3. Final Answer:

    0.27 -> Option A
  4. Quick Check:

    Loss after 3 epochs = 0.27 [OK]
Hint: Multiply loss by decay each epoch, then round [OK]
Common Mistakes:
  • Multiplying fewer times than epochs
  • Rounding before final multiplication
  • Choosing wrong rounded value
4. You tried fine-tuning a model but the accuracy did not improve. Which of these is the most likely error in your code?
model = load_pretrained_model()
model.fit(new_data)
model.evaluate(test_data)
medium
A. Not specifying epochs in fit() so training was too short
B. Using evaluate() before fit()
C. Loading the wrong model type
D. Not normalizing the test data

Solution

  1. Step 1: Check the fit() method usage

    Without specifying epochs, fit() may run only one epoch or default minimal training, insufficient for fine-tuning.
  2. Step 2: Understand impact on accuracy

    Too few training steps means the model doesn't learn new domain details, so accuracy stays low.
  3. Final Answer:

    Not specifying epochs in fit() so training was too short -> Option A
  4. Quick Check:

    Short training = no accuracy gain [OK]
Hint: Always set epochs to train enough during fine-tuning [OK]
Common Mistakes:
  • Assuming evaluate() order matters before fit()
  • Ignoring data normalization effects
  • Not checking model type mismatch
5. You have a general language model and want it to perform well on medical text. Which fine-tuning approach best adapts it to this domain?
hard
A. Train the model from scratch only on medical data
B. Fine-tune the pre-trained model with a small medical dataset using low learning rate
C. Use the pre-trained model without any changes
D. Fine-tune the model with random unrelated data to increase size

Solution

  1. Step 1: Compare training from scratch vs fine-tuning

    Training from scratch needs lots of data and time; fine-tuning uses existing knowledge and adapts efficiently.
  2. Step 2: Identify best fine-tuning practice

    Using a small medical dataset with a low learning rate helps the model learn domain details without forgetting general knowledge.
  3. Final Answer:

    Fine-tune the pre-trained model with a small medical dataset using low learning rate -> Option B
  4. Quick Check:

    Fine-tune + small data + low rate = best domain fit [OK]
Hint: Fine-tune with small domain data and low learning rate [OK]
Common Mistakes:
  • Training from scratch without enough data
  • Using unrelated data for fine-tuning
  • Skipping fine-tuning and using general model only