4.3.12Pretraining & Fine-Tuning LLMs

Catastrophic forgetting

1,938 words9 min readdifficulty · medium4 backlinks

WHY does this happen? (First principles)

A neural network stores everything it knows in one shared set of weights θ\theta. There is no separate "memory slot" per task — knowledge is distributed and overlapping.

When you fine-tune on a new dataset DnewD_{\text{new}}, you minimize a loss that only mentions the new data:

Lnew(θ)=1Dnew(x,y)Dnew(fθ(x),y)\mathcal{L}_{\text{new}}(\theta) = \frac{1}{|D_{\text{new}}|}\sum_{(x,y)\in D_{\text{new}}} \ell(f_\theta(x), y)

Gradient descent takes steps:

θθηθLnew(θ)\theta \leftarrow \theta - \eta \,\nabla_\theta \mathcal{L}_{\text{new}}(\theta)

Nothing in this update references the old task's loss Lold\mathcal{L}_{\text{old}}. So the optimizer happily moves θ\theta out of the region that was good for the old task, as long as it improves the new task. The old skill is collateral damage.


WHAT does it look like?

Imagine two loss landscapes over the same weight vector θ\theta:

  • Lold(θ)\mathcal{L}_{\text{old}}(\theta) — a valley whose bottom is the "good at task A" region.
  • Lnew(θ)\mathcal{L}_{\text{new}}(\theta) — a different valley for task B.

Their minima are usually in different places. Pure fine-tuning slides θ\theta into the new valley, climbing out of the old one.

Figure — Catastrophic forgetting

HOW do we derive a fix? (EWC from scratch)

Goal: train on the new task without leaving the old task's good region. We want θ\theta that is good on new and stays near the old optimum θ\theta^*, but only "stiff" along directions that mattered for the old task.

Step 1 — Bayesian view. After learning the old task we have a posterior p(θDold)p(\theta \mid D_{\text{old}}). When new data arrives, Bayes says:

logp(θDold,Dnew)=logp(Dnewθ)+logp(θDold)logp(Dnew)\log p(\theta \mid D_{\text{old}}, D_{\text{new}}) = \log p(D_{\text{new}}\mid\theta) + \log p(\theta\mid D_{\text{old}}) - \log p(D_{\text{new}})

Why this step? The old data's influence is fully captured by the posterior logp(θDold)\log p(\theta\mid D_{\text{old}}) — we don't need to keep the old data around, just remember what it told us about θ\theta.

Step 2 — Approximate the old posterior as a Gaussian (Laplace approximation) centered at θ\theta^* with precision given by the Fisher information FF:

logp(θDold)12iFi(θiθi)2+const\log p(\theta\mid D_{\text{old}}) \approx -\tfrac{1}{2}\sum_i F_i\,(\theta_i - \theta_i^*)^2 + \text{const}

Why this step? Near a minimum any smooth loss looks quadratic; the curvature (2nd derivative) tells us how important each weight was. FiF_i large ⇒ weight ii mattered a lot ⇒ don't move it.

Step 3 — Combine into a trainable loss (Elastic Weight Consolidation):

  LEWC(θ)=Lnew(θ)+λ2iFi(θiθi)2  \boxed{\;\mathcal{L}_{\text{EWC}}(\theta) = \mathcal{L}_{\text{new}}(\theta) + \frac{\lambda}{2}\sum_i F_i\,(\theta_i - \theta_i^*)^2\;}


Other mitigation strategies (80/20)

The 20% you must know:

Method Core idea WHY it works
Rehearsal / Replay Mix a slice of old data (or generated pseudo-data) into training The old loss is now in the objective, so gradients defend it
EWC / regularization Penalize moving important weights Restricts θ\theta to stay near old optimum along key directions
Parameter isolation (LoRA/adapters) Freeze base weights; add small new trainable modules Old weights literally can't change ⇒ can't be forgotten
Lower LR + fewer epochs Take smaller steps away from θ\theta^* Less drift out of the old valley

Steel-manned mistakes


Recall Feynman: explain to a 12-year-old

Imagine your brain is a lump of clay and every skill you learn presses a shape into it. If you learn to ride a bike, then press really hard learning to swim in the same spot, you might squish the bike-shape flat and forget it! Catastrophic forgetting is exactly that — the computer uses one lump of "clay weights" for everything, so pushing hard on a new thing can erase an old thing. To fix it, we either (a) keep practicing the old thing a little (rehearsal), or (b) put little "keep me!" springs on the parts of the clay that hold important old shapes (EWC), or (c) learn the new skill in a fresh piece of clay stuck on the side (adapters/LoRA).


Connections

  • Fine-Tuning LLMs — where forgetting bites hardest.
  • LoRA and Adapters — parameter-isolation fix.
  • Fisher Information — the "importance" FiF_i in EWC.
  • Laplace Approximation — how the Gaussian posterior arises.
  • Continual Learning — the broader problem setting.
  • Multi-task Learning — the "train everything jointly" ideal EWC/rehearsal approximate.
  • Learning Rate & Optimization — smaller LR reduces drift.

Flashcards

What is catastrophic forgetting?
The abrupt loss of previously learned knowledge when a network is trained on a new task, because gradient updates overwrite shared weights encoding the old task.
Why does fine-tuning on new data cause forgetting?
The new loss Lnew\mathcal{L}_{\text{new}} contains no term for the old task, so gradient descent freely moves θ\theta out of the old task's good region.
Write the EWC loss.
LEWC=Lnew(θ)+λ2iFi(θiθi)2\mathcal{L}_{\text{EWC}}=\mathcal{L}_{\text{new}}(\theta)+\frac{\lambda}{2}\sum_i F_i(\theta_i-\theta_i^*)^2
What does the Fisher information FiF_i represent in EWC?
The importance/curvature of weight ii for the old task; large FiF_i ⇒ that weight mattered ⇒ penalize moving it.
Why doesn't EWC block all learning?
Weights unimportant to the old task have Fi0F_i\approx0, so they're free to change — learning happens in the spare capacity.
Name three families of mitigation.
Rehearsal/replay, regularization (EWC), and parameter isolation (LoRA/adapters).
Why does LoRA avoid forgetting by design?
It freezes the base weights and only trains small added modules, so original knowledge weights literally cannot be overwritten.
Steel-man: why do more epochs make forgetting worse?
Extra epochs on the new task push θ\theta further from the old optimum θ\theta^*, increasing drift out of the old task's valley.
Where does the EWC quadratic penalty come from mathematically?
A Laplace (Gaussian) approximation of the old-task posterior p(θDold)p(\theta\mid D_{\text{old}}) around θ\theta^*, giving curvature FiF_i.

Concept Map

encode

encode

minimizes

gradient descent

ignores

overwrites

abrupt loss

fix via

derived from

Laplace approx

weighted by

large F stiffens

Shared weights theta

Old task knowledge

New task knowledge

Fine-tune on new data

Loss on new data only

Update theta

Old task loss

Catastrophic forgetting

Elastic Weight Consolidation

Bayesian posterior

Gaussian around theta star

Fisher information F_i

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek neural network ka saara "knowledge" ek hi shared weight vector θ\theta me stored hota hai — koi alag alag dabba har task ke liye nahi hota. Jab tum ek naye task pe fine-tune karte ho, to loss sirf naye data ko dekhta hai. Gradient descent bas naye task ko achha karne ke liye weights ghuma deta hai, aur purane task ka to naam bhi loss me nahi hai — isliye purani skill "collateral damage" ban jaati hai. Yahi hai catastrophic forgetting: naya seekhte hi purana achanak bhool jaana.

Socho do valleys (ghaati) hain same θ\theta ke upar — ek old task ke liye, ek new task ke liye, aur dono ke minimum alag jagah pe. Naive fine-tuning θ\theta ko new valley me sarka deta hai, matlab old valley se bahar nikaal deta hai. Isliye medical data pe LLM fine-tune karo aur woh coding aur maths bhool jaaye — bilkul aam baat hai.

Fix kaise? Teen simple ideas: (1) Rehearsal — thoda purana data mix kardo taaki old loss wapas gradient me aa jaaye. (2) EWC — important weights pe "spring" laga do; Fisher information FiF_i batata hai konsa weight purane task ke liye zaroori tha, usko hilne mat do. (3) LoRA/adapters — base weights ko freeze karke sirf chhote naye modules train karo, to purana knowledge chhua hi nahi jaata. Yaad rakhne ka mantra: "REPLAY the FIsher, FREEZE the base."

Ek important trap: log sochte hain "zyada epochs = zyada achha." Single task ke liye theek, par sequential learning me zyada epochs θ\theta ko θ\theta^* se aur door dhakelte hain, matlab forgetting badh jaati hai. Isliye LLM fine-tuning me chhota learning rate aur kam epochs rakhna smart move hai.

Go deeper — visual, from zero

Test yourself — Pretraining & Fine-Tuning LLMs

Connections