Transformers and BERT

अब हम NLP के सबसे क्रांतिकारी अविष्कारों की ओर बढ़ते हैं —
🚀 Transformers और BERT — जिन्होंने NLP की दुनिया को पूरी तरह बदल दिया है।


🔶 1. Transformers: Introduction

Transformer architecture 2017 में Google ने पेश किया, पेपर:

📄 “Attention is All You Need” — Vaswani et al.

इसने Recurrent Networks (RNN, LSTM) की dependency को हटा दिया और NLP को पूरी तरह से revolutionize कर दिया।


📐 Transformer की Key Idea: Self-Attention

हर word sentence के बाकी सभी words के context को साथ में समझता है, न कि केवल पिछले शब्दों को।


🔧 Architecture Overview

Transformer दो मुख्य हिस्सों में बंटा होता है:

[Encoder] →→→→→→→→→ [Decoder]
PartRole
EncoderInput text को समझना (e.g., sentence meaning)
DecoderOutput generate करना (e.g., translation, caption)

Note: BERT सिर्फ Encoder यूज़ करता है, GPT सिर्फ Decoder।


🔁 Self-Attention Mechanism

हर शब्द input में बाकी सभी शब्दों से relate करता है:

Sentence: "The cat sat on the mat"
"cat" → attends to "the", "sat", "mat" etc. via attention scores

🔢 Attention Equation:

जहाँ:

  • Q: Query
  • K: Key
  • V: Value
  • dk: Key vector dimension

⚙️ Transformer के Components:

ComponentExplanation
🔹 Multi-Head AttentionParallel attention layers for better learning
🔹 Positional EncodingSequence order की जानकारी add करता है
🔹 Feedforward NetworkLinear + non-linear layers
🔹 Layer NormalizationStable training
🔹 Residual ConnectionsGradient flow बनाए रखता है

🧠 2. BERT: Bidirectional Encoder Representations from Transformers

📄 “BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding” – Devlin et al., 2018


🎯 मुख्य उद्देश्य:

  • Language Understanding — Chatbots, Q&A, classification

🔧 कैसे काम करता है?

  • BERT केवल Transformer Encoder architecture पर आधारित है।
  • यह दोनों तरफ के context को एक साथ पढ़ता है — इसलिए Bidirectional है।

📊 Pretraining Tasks:

  1. Masked Language Modeling (MLM)
    • Sentence में कुछ शब्दों को mask किया जाता है, और model को predict करना होता है।
    textCopyEditInput: "The [MASK] is shining" Output: "sun"
  2. Next Sentence Prediction (NSP)
    • दो sentences दिए जाते हैं — model को यह predict करना होता है कि दूसरा sentence पहले के बाद आता है या नहीं।

📦 Pretrained BERT Models:

VariantDescription
bert-base-uncasedLowercase English, 12 layers
bert-large-uncased24 layers, large model
DistilBERTLightweight, faster
Multilingual BERT100+ languages

🔧 BERT Applications:

TaskExample
✅ Sentiment Analysis“I love this product!” → Positive
🧠 Question Answering“Where is Taj Mahal?” → “Agra”
✍️ Named Entity Recognition“Barack Obama is from USA” → Person, Country
💬 ChatbotsIntent understanding
📃 Text ClassificationNews, spam, legal docs

🧰 Example: HuggingFace Transformers

from transformers import BertTokenizer, BertModel

tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
model = BertModel.from_pretrained("bert-base-uncased")

inputs = tokenizer("I love deep learning", return_tensors="pt")
outputs = model(**inputs)

🧠 Transformer vs BERT

AspectTransformerBERT
TypeGeneral architecturePretrained NLP model
StructureEncoder + DecoderOnly Encoder
DirectionDependsBidirectional
ApplicationTranslation, captioningUnderstanding, classification

📈 Transformers & BERT Impact

AreaImpact
📚 ResearchNLP को neural-level accuracy
🗣️ ChatbotsSmarter conversations
🧾 Legal/MedicalAutomated document understanding
🧠 AI ModelsFoundation for GPT, T5, RoBERTa, etc.

📝 Practice Questions:

  1. Transformer architecture में self-attention का क्या role है?
  2. BERT bidirectional क्यों है?
  3. Masked Language Modeling का मतलब क्या है?
  4. BERT किन NLP tasks के लिए use होता है?
  5. HuggingFace से BERT कैसे load करते हैं?

🧠 Summary Table

TermDescription
TransformerSequence model using attention mechanism
BERTBidirectional encoder for NLP tasks
MLMMask words and predict
NSPPredict sentence relationship
ApplicationsQ&A, classification, chatbot, NER