4.3.2 · HinglishPretraining & Fine-Tuning LLMs

BERT and encoder models

1,979 words9 min readRead in English

4.3.2 · AI-ML › Pretraining & Fine-Tuning LLMs


Encoder model kya hota hai?

CONTRAST kya hai?

Family Attention Trained for Example
Encoder-only bidirectional understanding BERT
Decoder-only causal (left→right) next-token generation GPT
Encoder-decoder dono seq-to-seq (translate) T5

Pretraining objective 1: Masked Language Modelling (MLM)

HOW 15% handle hote hain (80/10/10 rule):

  • 80% → ek special [MASK] token se replace kiya jaata hai
  • 10% → ek random token se replace kiya jaata hai
  • 10% → unchanged chhoda jaata hai

MLM loss ko first principles se derive karna. Maano masked positions ka set hai. Ek masked position ke liye, model contextual vector output karta hai, use vocab logits mein project karta hai, aur vocabulary par ek probability banata hai:

Hum true token ki probability maximize karna chahte hain. Probability maximize karna = uska negative log minimize karna. Saare masked positions par sum karte hue:


Pretraining objective 2: Next Sentence Prediction (NSP)

WHY: bahut saare downstream tasks (QA, entailment) ko do sentences ke beech relationships ki zarurat hoti hai, jo single-sentence MLM nahi sikhata.

Total pretraining loss hai .


Special tokens aur input format

Har BERT input aisa dikhta hai:

  • ==[CLS]== — ek token jiska final vector classification ke liye sentence-level summary ke roop mein use hota hai.
  • [SEP] — sentence A ko sentence B se alag karta hai.
  • Tin embeddings sum ki jaati hain har token ke liye: token + position + segment (A vs B).
Figure — BERT and encoder models

Fine-tuning: WHY ek model → bahut saare tasks

  • Sentence classification (sentiment): → linear → softmax feed karo.
  • Token classification (NER, POS): har → linear → per-token label feed karo.
  • Span extraction (SQuAD QA): tokens par start/end positions predict karo.

Worked examples


Common mistakes


Active recall

Recall Khud test karo (hidden — pehle answer karo!)
  • BERT next-token prediction kyun use nahi kar sakta? → bidirectional attention isko answer dekhne deta.
  • 80/10/10 rule kis liye hai? → sirf [MASK] wale train/test mismatch ko rokne ke liye.
  • RoBERTa ne kaunsa objective drop kiya? → NSP.
  • Sentence summary kaunsa token deta hai? → [CLS].
Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho ek sentence hai jismein kuch words stickers se chhupe hain. Ek bahut smart bachcha har sticker ke aas-paas sab kuch padhta hai — pehle ke words AND baad ke words — aur chhupa hua word guess karta hai. Yeh pakka karne ke liye ki bachcha actually padhe na ki yaad kare ki stickers kahan hain, kabhi kabhi hum sticker ki jagah galat word daalte hain, aur kabhi asli word chhod dete hain. Bahut zyada practice ke baad, woh bachcha sentences itne achhe se samajhta hai ki aap use chhote naye kaam — jaise "kya yeh review khush hai ya udaas?" — sirf thode examples se jaldi seekha sakte ho. Woh smart reader BERT hai.


Flashcards

BERT kis tarah ka Transformer hai (encoder/decoder)?
Encoder-only, bidirectional attention.
BERT ko next-token prediction se kyun train nahi kar sakte?
Bidirectional attention har token ko us future token ko attend karne deta hai jise use predict karna hai — yeh trivial cheating hai.
MLM mein kitne fraction tokens select hote hain?
15%.
80/10/10 rule describe karo.
Selected tokens mein se: 80% → [MASK], 10% → random token, 10% → unchanged.
10% unchanged / 10% random kyun rakhte hain hamesha [MASK] ki jagah?
[MASK] inference par kabhi nahi aata; har token ke baare mein uncertainty force karna har token ke liye achhi representations deta hai (train/test mismatch avoid karta hai).
MLM loss likho.
, sirf masked positions par cross-entropy.
NSP kya hai aur kya yeh zaruri hai?
Binary "kya B, A ke baad ka next sentence hai"; RoBERTa ne dikhaya ki yeh aksar unnecessary/harmful hota hai.
BERT mein teen summed input embeddings kya hain?
Token + position + segment embeddings.
Position embeddings kyun zaruri hain?
Self-attention permutation-invariant hai; positions word order inject karte hain.
[CLS] token kis liye use hota hai?
Classification ke liye aggregated sentence-level representation (NSP aur fine-tuning mein use hota hai).
Sentiment ke liye BERT ko fine-tune kaise karte hain?
par ek linear+softmax head add karo aur labelled data par train karo.
Token classification (NER) kaise hota hai?
Har token ke par ek shared linear layer apply karo.

Connections

  • Transformer Architecture — BERT apne encoder blocks stack karta hai.
  • Self-Attention — bidirectional (unmasked) version BERT ko power deta hai.
  • GPT and decoder models — generative, causal-masked counterpart.
  • T5 and encoder-decoder models — text-to-text unifier.
  • Cross-Entropy Loss — MLM aur NSP objectives.
  • Fine-Tuning vs Pretraining — do-stage paradigm.
  • WordPiece Tokenization — BERT text ko sub-word tokens mein kaise split karta hai.
  • RoBERTa — BERT better train hua, NSP drop kiya.

Concept Map

motivates

is a

uses

no

enables cheating on

forces need for

masks

split by

prevents

projects h_i to

minimizes

contrasts with

Language is bidirectional

BERT encoder-only model

Encoder-only Transformer

Bidirectional attention

Causal mask

Next-word prediction

Masked Language Modelling

15% of tokens

80/10/10 rule

Train/test mismatch

Vocab logits and softmax

Cross-entropy loss

Decoder-only GPT causal