Knowledge / Artificial Intelligence and Data Science / Transfer learning and fine-tuning

Transfer learning and fine-tuning

Artificial Intelligence and Data Science (B.E./B.Tech) · Deep learning · free study notes for exams, placements and interviews

In short

Transfer learning reuses the representations a network has already learnt on a large source task (ImageNet, a web-scale text corpus) to solve a different but related target task with far less data and compute. Fine-tuning is the main technique: keep the pretrained weights as the initialisation, replace the output head, and continue training on the target data at a small learning rate. It matters because almost no Indian industry project has millions of labelled examples — a crop-disease app, a chest X-ray classifier or a Hindi-English support-ticket classifier typically has a few thousand labels, and training from scratch then overfits badly. It is examined in AI/DS and CSE deep-learning papers (types of transfer, freezing strategies, catastrophic forgetting, parameter-efficient tuning) and appears in GATE DA-style questions on trainable-parameter counts and in nearly every ML placement interview (TCS, Infosys, Wipro, Fractal, Mu Sigma, Qure.ai, Sarvam AI, Flipkart, Jio).

Explained

Definitions and formal setting

Why pretrained features transfer: the layer hierarchy

The fine-tuning recipe, step by step

  1. Choose a backbone matched to the modality and budget: ResNet/EfficientNet/ViT or ConvNeXt for vision; BERT/RoBERTa/DeBERTa, IndicBERT or MuRIL for Indian-language text; Whisper for speech; a Llama/Mistral/Gemma-class decoder for generation. Check the licence (Apache-2.0 vs a community licence with use restrictions).
  2. Match the preprocessing exactly to pretraining: input size (224×224), the same mean/std normalisation, the same tokenizer and vocabulary. A mismatch silently destroys accuracy.
  3. Replace the head: drop the source classifier and attach a new linear layer of size (feature dim + 1) × C for C target classes (or a regression head, CRF, segmentation decoder).
  4. Decide what to train — the four options below.
  5. Warm up the head first: train only the new head for 1–3 epochs so its random gradients do not corrupt good backbone weights, then unfreeze.
  6. Use a small learning rate for pretrained weights: typically 1e-5 to 5e-5 for BERT-class models (2–4 epochs, batch 16–32) and 1e-4 to 1e-3 for a CNN head, with linear warmup then cosine or step decay, plus weight decay and early stopping on a validation set.
  7. Regularise for small data: augmentation (flip, crop, colour jitter, RandAugment, mixup; back-translation or synonym swap for text), dropout, label smoothing, layer-wise decay, and freeze BatchNorm statistics.
  8. Evaluate on a held-out target set with the metric that matters (macro-F1 or AUROC under class imbalance, not accuracy), and compare against a from-scratch baseline and a frozen-features baseline.

Four strategies, and how to choose

Target dataSimilar to sourceDifferent from source
Small (hundreds–few thousand)Freeze backbone, train headTrain head + mid layers; heavy augmentation; expect modest gains
Large (tens of thousands+)Fine-tune all layers, low LRFine-tune all layers, higher LR, longer; scratch training becomes viable

Problems: forgetting, negative transfer, shift and leakage

Fine-tuning large language models

Key points

Formulas and examples

Fine-tuning objective: θ* = argmin_θ Σ L(f(x_i; θ), y_i) + λ‖θ‖², with θ initialised at θ_pretrained (not random). L = loss (cross-entropy, nats), λ = weight-decay coefficient.
New head parameters: P_head = (d + 1) × C — d = feature dimension (2048 for ResNet-50, 768 for BERT-base), C = number of target classes, +1 for bias.
Trainable fraction: φ = P_trainable / P_total × 100 %.
Layer-wise (discriminative) learning rate: lr_l = lr_top / ηL − l — l = layer index, L = top layer, η ≈ 2.6 (ULMFiT) a decay factor per group.
LoRA: W = W₀ + (α/r)·B·A, B ∈ ℝd×r, A ∈ ℝr×k; trainable parameters per matrix = r(d + k); α = scaling, r = rank (4–64). A is Gaussian-initialised, B = 0 so training starts from W₀.
Distillation loss: L = α·T²·KL(softmax(z_t/T) ‖ softmax(z_s/T)) + (1 − α)·CE(y, softmax(z_s)) — z_t, z_s = teacher/student logits, T = temperature (1–10), α = mixing weight.
MMD domain distance: MMD² = ‖(1/n_s)Σφ(x_is) − (1/n_t)Σφ(x_jt)‖² in a kernel feature space φ; zero when the two feature distributions match.
EWC penalty: L = L_new(θ) + (λ/2)·Σ_i F_i(θ_i − θ*_i)² — F_i = Fisher information of parameter i on the old task, θ* = old optimum.
Optimiser memory (Adam, mixed precision): ≈ 2 bytes (fp16 weight) + 2 (gradient) + 4 + 4 (Adam m, v) + 4 (fp32 master) ≈ 16 bytes per *trainable* parameter.

Example 1 — Trainable parameters and compute saving (ResNet-50, 5 crop diseases).

Example 2 — LoRA parameter count and memory (7B decoder).

Example 3 — Discriminative learning rates. Head LR 1 × 10⁻³, divisor η = 2.6 per group downward:

GroupLearning rate
Classifier head1.00 × 10⁻³
Top block3.85 × 10⁻⁴
Middle block1.48 × 10⁻⁴
Early block5.69 × 10⁻⁵

(1e-3/2.6 = 3.85e-4; /2.6 = 1.48e-4; /2.6 = 5.69e-5.) Early layers barely move, protecting generic filters.

Example 4 — Distillation temperature. Teacher logits z = (4, 2, 1).

Example 5 — Strategy comparison on a small Indian dataset (1,200 leaf images, 6 classes, 200 each; 70/15/15 split; single T4 GPU). Indicative figures from this kind of project, not universal constants:

StrategyTrainable paramsTrain timeTest macro-F1
ResNet-50 from scratch23.5 M35 min≈ 0.62 (overfits)
Frozen features + linear head10 k3 min≈ 0.88
Fine-tune layer4 + head15.0 M18 min≈ 0.93
Full fine-tune, LR 1e-423.5 M30 min≈ 0.94

Reading: with 200 images per class, pretraining is worth ≈ 30 F1 points, and most of the benefit arrives with the cheapest option.

Where it is used

Interview questions

Common mistakes

Related topics

Convolutional neural networks · Transformers and attention · Training deep networks: optimisers, normalisation and regularisation · Neural networks and backpropagation · Model compression and knowledge distillation · MLOps and model deployment

Open in the Knowledge reader →Find jobs →