Bird
Raised Fist0

Transfer Learning and Fine-Tuning - Why Pretrain Then Finetune

THEORYintermediateInterview freq: very_high
Choose your preparation mode6 modes available
β–Ά
Execution Trace
Step 1/4
Pretrained Model

Model trained on ImageNet achieves 75% accuracy on general image classification.

🎯 Pretrained models capture general features that are transferable.
from torchvision import models
model = models.resnet50(pretrained=True)
Transfer Weights

Weights from the pretrained model are loaded into the new model architecture. This allows the new model to start with a strong foundation.

🎯 Transferring weights reduces training time and resources.
new_model.load_state_dict(model.state_dict(), strict=False)
Fine-Tuned Model

Model fine-tuned on cat images improves accuracy to 90% on cat classification. This demonstrates the model's adaptation to the specific task.

🎯 Fine-tuning adapts the model to specific tasks, improving performance.
for param in new_model.parameters():
    param.requires_grad = False
new_model.fc = nn.Linear(2048, num_classes)
Target Dataset

The model is trained on a smaller dataset with specific labels, allowing it to learn task-specific features.

🎯 Using a smaller dataset for fine-tuning is efficient and effective.
optimizer = optim.SGD(new_model.fc.parameters(), lr=0.001, momentum=0.9)
criterion = nn.CrossEntropyLoss()
πŸ“Š
Diagram
Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Pretrained    β”‚     β”‚ Fine-Tuned    β”‚
β”‚ Model         β”‚     β”‚ Model         β”‚
β”‚ (General)     β”‚     β”‚ (Specific)    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                     β”‚
       β”‚ Transfer Weights    β”‚
       β”‚                     β”‚
       β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Large Dataset β”‚     β”‚ Target Datasetβ”‚
β”‚ (e.g.,        β”‚     β”‚ (e.g., Cats)  β”‚
β”‚ ImageNet)     β”‚     β”‚               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • Pretrained Model: A model trained on a large, general dataset
  • Fine-Tuned Model: A model adapted to a specific task
  • Transfer Weights: Reusing learned parameters from the pretrained model
  • Large Dataset: A comprehensive dataset for initial training
  • Target Dataset: A smaller dataset for task-specific fine-tuning
πŸ”‘
Key Moments
β–²
Q: Why is it beneficial to use a pretrained model?
A: Pretrained models provide a strong starting point by leveraging learned features from large datasets, reducing the need for extensive data and computation.
Q: How does fine-tuning improve model performance?
A: Fine-tuning adjusts the pretrained model to the specific nuances of the target dataset, enhancing its ability to perform well on the specific task.
Q: What is the role of the target dataset in fine-tuning?
A: The target dataset provides task-specific examples that guide the model to adapt its general features to the specific requirements of the task.

The diagram illustrates the concept of transfer learning and fine-tuning. Initially, a model is pretrained on a large, general dataset like ImageNet, capturing broad features. These learned weights are then transferred to a new model, which is fine-tuned on a smaller, task-specific dataset, such as images of cats. This process leverages the generalization capabilities of the pretrained model while adapting it to the specific task, resulting in improved performance with reduced training time and data requirements.

Primary Focus

βœ“ Understanding the mechanism of transfer learning and fine-tuning

Practice

(1/5)
1. Given the following PyTorch code snippet, what will be the output shape of the gradient for W after backpropagation?
import torch
W = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
x = torch.tensor([1.0, 2.0])
y = W @ x
loss = y.sum()
loss.backward()
easy
A. (2, 2)
B. (2,)
C. (2, 1)
D. (1, 2)

Solution

  1. Step 1: Determine the gradient shape

    The gradient of W will have the same shape as W, which is (2, 2).
  2. Step 2: Eliminate wrong options

    (2,) is incorrect because it suggests a vector shape. (2, 1) and D are incorrect because they suggest mismatched dimensions.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Gradient shape matches tensor shape. [OK]
Hint: Gradient shape matches tensor shape. [OK]
Common Mistakes:
2.
import numpy as np

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

result = np.dot(x, y)
print(result)
What does this code output?
easy
A. Error: shapes not aligned
B. [4, 10, 18]
C. 32
D. [5, 7, 9]

Solution

  1. Step 1: Understand the dot product operation.

    The dot product of two vectors is the sum of the products of their corresponding elements.
  2. Step 2: Calculate the dot product.

    (1*4) + (2*5) + (3*6) = 32.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Dot product sums element-wise products. [OK]
Hint: Dot product sums element-wise products. [OK]
Common Mistakes:
3. A model performs well on the validation set but poorly on the test set. What is the most likely cause?
medium
A. The test set is not representative of the validation set.
B. The validation set is too small.
C. The model is underfitting the data.
D. The model has too many parameters.

Solution

  1. Step 1: Identify the discrepancy

    Good validation performance but poor test performance suggests a mismatch between sets.
  2. Step 2: Eliminate less likely causes

    The model is underfitting the data. is incorrect as underfitting would affect both sets. The validation set is too small. is less likely unless the validation set is extremely small. The model has too many parameters. would likely affect both sets similarly.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Mismatch between validation and test sets. [OK]
Hint: Mismatch between validation and test sets. [OK]
Common Mistakes:
4. When choosing between Least Squares and Gradient Descent for linear regression, which scenario makes Gradient Descent more suitable?
medium
A. When the dataset is small and fits in memory
B. When the dataset has a lot of noise
C. When the dataset is very large and doesn't fit in memory
D. When the model needs to be interpretable

Solution

  1. Step 1: Identify the constraint

    Gradient Descent is more suitable for large datasets that don't fit in memory because it can process data in batches.
  2. Step 2: Eliminate wrong options

    Least Squares is efficient for small datasets (A), interpretability is not a deciding factor (C), and noise handling depends on regularization, not the optimization method (D).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Gradient Descent handles large datasets in batches. [OK]
Hint: Gradient Descent handles large datasets in batches. [OK]
Common Mistakes:
5. A deployed model is showing unexpected behavior with high variance in predictions. What is the most likely cause?
medium
A. Overfitting due to a complex model
B. Incorrect hyperparameter tuning
C. Underfitting due to a simple model
D. Data drift in the input features

Solution

  1. Step 1: Identify the symptom.

    High variance in predictions suggests the model is not generalizing well.
  2. Step 2: Determine the most likely cause.

    Data drift, where the input data distribution changes over time, is a common cause of unexpected behavior in production.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Data drift causes unexpected variance. [OK]
Hint: Data drift causes unexpected variance. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard