Sequence-to-Sequence
এই পাঠে যা শিখবেন
- কেন vanilla RNN translation-এ যথেষ্ট না
- Encoder-decoder architecture — input → context → output
- Teacher forcing — efficient training
- Greedy vs beam search decoding
- PyTorch দিয়ে Bangla → English mini-translator
- Bottleneck problem — কেন attention দরকার
১ · কেন Seq2Seq
Vanilla RNN-এ — input ও output-এর দৈর্ঘ্য সাধারণত একই (per-token output)। কিন্তু:
- "আমি বাংলা ভালোবাসি" (৩ token) → "I love Bangla" (৩ token)। মিল।
- "আমি বাংলাদেশের রাজধানী ঢাকায় থাকি" (৫ token) → "I live in Dhaka, the capital of Bangladesh" (৮ token)। ভিন্ন।
Translation, summarization, dialog — input ও output-এর দৈর্ঘ্য আলাদা। Seq2Seq এই asymmetry handle করে।
১) Encoder: পুরো input sequence read করে — শেষ hidden state-এ পুরোটা compress (context vector)।
২) Decoder: context vector থেকে শুরু করে — token by token output generate।
২ · Encoder — input কে compress
একটি LSTM/GRU input sequence $x_1, x_2, \ldots, x_T$ পড়ে। শেষ hidden state $h_T$ (LSTM-এ $(h_T, c_T)$) — পুরো input-এর প্রতিনিধিত্ব। একে বলে context vector বা thought vector।
$$\mathbf{c} = \text{Encoder}(x_1, x_2, \ldots, x_T) = h_T$$
৩ · Decoder — output generate
Decoder একটি আলাদা LSTM/GRU। এটি $\mathbf{c}$-কে initial hidden state হিসেবে নেয়। Special <START> token দিয়ে শুরু — প্রতিটি step-এ পরের token predict করে। আগের predicted token পরের input হয়। <END> predict হলে থামে।
$$y_t = \text{Decoder}(y_{t-1}, h_t)$$
Token sampling — softmax probability থেকে। Greedy (argmax) সরল কিন্তু suboptimal। Beam search আরও ভাল।
৪ · Teacher forcing — efficient training
Training-এ — decoder-এর প্রতিটি step-এ ground-truth previous token feed করা (model-এর own prediction না)। এটাই teacher forcing।
- সুবিধা: error propagate করে না। Training stable, fast convergence।
- সমস্যা — exposure bias: training-এ ground-truth, inference-এ own prediction। Distribution mismatch।
- সমাধান: scheduled sampling — মাঝে মাঝে own prediction feed। Bengio et al. (২০১৫)।
৫ · PyTorch Seq2Seq — full implementation
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(embed_dim, hidden_dim,
batch_first=True)
def forward(self, x):
emb = self.embed(x)
_, (h, c) = self.lstm(emb)
return h, c # context
class Decoder(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(embed_dim, hidden_dim,
batch_first=True)
self.fc = nn.Linear(hidden_dim, vocab_size)
def forward(self, y, h, c):
emb = self.embed(y)
out, (h, c) = self.lstm(emb, (h, c))
return self.fc(out), h, c
class Seq2Seq(nn.Module):
def __init__(self, src_vocab, tgt_vocab,
embed_dim=128, hidden_dim=256):
super().__init__()
self.encoder = Encoder(src_vocab, embed_dim, hidden_dim)
self.decoder = Decoder(tgt_vocab, embed_dim, hidden_dim)
def forward(self, src, tgt):
# Teacher forcing
h, c = self.encoder(src)
logits, _, _ = self.decoder(tgt[:, :-1], h, c)
return logits # predict tgt[:, 1:]
model = Seq2Seq(src_vocab=10000, tgt_vocab=10000)
src = torch.randint(0, 10000, (4, 12)) # 4 Bangla sentences
tgt = torch.randint(0, 10000, (4, 14)) # 4 English sentences
logits = model(src, tgt)
print(logits.shape) # (4, 13, 10000)
৬ · Inference — greedy ও beam search
@torch.no_grad()
def greedy_decode(model, src, start_token, end_token,
max_len=50):
model.eval()
h, c = model.encoder(src)
y = torch.tensor([[start_token]])
output = []
for _ in range(max_len):
logits, h, c = model.decoder(y, h, c)
next_tok = logits[:, -1, :].argmax(-1)
output.append(next_tok.item())
if next_tok.item() == end_token:
break
y = next_tok.unsqueeze(0)
return output
Beam search — top-$k$ partial hypothesis maintain। প্রতিটি step-এ each hypothesis-এর top-$k$ extension keep। সম্ভাব্যতা গুণনে best sequence। Greedy-এর চেয়ে high-quality output, কিন্তু $k$ গুণ slower।
৭ · Bottleneck problem
Encoder পুরো input একটি fixed-size vector $\mathbf{c}$-এ compress করে। ছোট sequence-এ ঠিক, কিন্তু long sentence-এ critical information loss।
- "বাংলাদেশের সবচেয়ে বড় শহর ঢাকা — যা একটি ব্যস্ত megacity" — ১২ token, একটি ৫১২-D vector-এ compress।
- Decoder-এর প্রতিটি step-এ একই $\mathbf{c}$ available — কিন্তু প্রতি step-এ আসলে input-এর ভিন্ন অংশ relevant।
- Long input — accuracy quickly degrade।
Solution: Attention (পরের পাঠ L30) — decoder-কে input-এর সব hidden state-এ access দেয়, প্রতি step-এ relevant অংশ "focus" করে।
৮ · Bangladesh-এ Seq2Seq usecase
- Bangla → English translation: e-commerce, government document।
- Summarization: news article, legal document।
- Chatbot: customer service, banking।
- Voice command → action: Bangla voice assistant।
- Code generation: Bangla description → Python।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ Sutskever (২০১৪) original Seq2Seq paper-এ — input sequence "reverse" করা হয়েছিল। কেন? এই trick-এর justification কী?
এটি famous practical trick — paper-এ "introducing many short term dependencies" বলে described। বুঝার গভীর insight।
Original setup:
- "আমি বাংলা ভালোবাসি" → encoder reads "আমি, বাংলা, ভালোবাসি"।
- Decoder generates "I, love, Bangla"।
- "আমি" ↔ "I" — এদের মধ্যে gap = source full length + decoder positions।
Reverse trick:
- Encoder reads "ভালোবাসি, বাংলা, আমি"।
- Decoder generates "I, love, Bangla"।
- "আমি" এখন encoder-এর শেষে — context vector-এ fresh।
- "আমি" ↔ "I" — gap minimum।
Why it works:
- Recency bias — context vector-এ শেষ token-এর information dominant।
- Reversed-এ — translation-এর শুরু-এর word encoder-এ শেষ।
- "Many short-term dependencies" — মূল mapping pair-এ gap কম।
Empirical impact:
- BLEU score WMT'14 — ৪.৭ → ৩৫.৬ improvement (with deeper model)।
- Long sentence — particularly helpful।
- Without reverse — long sentence accuracy poor।
Why this trick disappears today:
- Attention mechanism — gap concept-ই irrelevant।
- Decoder প্রতি step-এ source-এর সব position access।
- Reverse implicit-এ achieved।
- Modern Transformer — completely position-flexible।
Related ideas:
- Bidirectional encoder — both forward + backward।
- Attention — direct source position access।
- Copy mechanism — source token directly use।
Theoretical interpretation:
- Vanishing gradient — long path through compression bottleneck।
- Reverse — gradient path shorter for early target tokens।
- Optimization landscape easier।
Modern lesson:
- Architecture limitation — clever data hack overcome।
- "Trick" temporary — better architecture permanent solution।
- Engineering insight valuable in research।
Bangla translation context:
- Bangla SOV (subject-object-verb), English SVO।
- Word order significant difference।
- Reverse trick may help less — verb in Bangla last anyway।
- Modern Transformer handle natively।
মূল উপলব্ধি: Reverse trick — ২০১৪-এ pragmatic engineering। Vanishing gradient + bottleneck-এর symptom। Recency bias exploit। Attention mechanism এর "need" eliminate। Modern Transformer order-agnostic। Lesson — architecture limitation-এর জন্য temporary hack acceptable, কিন্তু ultimately better architecture দরকার।
প্র ০২ Beam search vs greedy — quality difference কেন? Beam size $k$ infinite হলে optimal sequence পাবেন কি?
Beam search NLP generation-এর foundational algorithm। এর nuance বুঝা production-এ critical।
Greedy decoding:
- প্রতি step-এ argmax probability।
- Locally optimal, globally not necessarily।
- Fast — single pass।
Beam search:
- Top-$k$ partial sequences maintain।
- Each step expand each by top-$k$।
- Overall top-$k$ keep।
- Greedy = beam=১।
Quality difference reasons:
- Greedy commit early — backtrack impossible।
- "The dog" vs "The cat" — first word commit affect rest।
- Beam — multiple option keep, recover possible।
Quantitative impact:
- Translation BLEU — beam ৫ vs greedy ~১-৩ point gain।
- Summarization ROUGE — similar।
- Speech recognition WER — beam essential।
$k = \infty$ — exhaustive:
- Theoretically — exact MAP sequence।
- Computationally infeasible — exponential।
- Practical $k$ — ৫-১০ typical।
- $k$ বাড়ালে — diminishing return।
Surprising finding:
- Very large $k$ — quality decrease।
- "Beam search curse" — Murray-Chiang (২০১৮)।
- Reason — model probability mismatch true distribution।
- High-probability sequence often empty/short।
Beam search problems:
- Length bias — short sequence preferred।
- Repetition — same phrase repeat (low-prob loop trap)।
- Diversity low — beam similar each other।
Practical fixes:
- Length normalization: $\log P / |y|^\alpha$, $\alpha = 0.7$।
- Coverage penalty: source attention coverage encourage।
- N-gram blocking: repetition prevent।
- Diverse beam: beam-এর মধ্যে diversity penalize।
Sampling alternatives:
- Top-k sampling: top-k থেকে random। Diversity।
- Nucleus (top-p): cumulative probability $p$ পর্যন্ত। ChatGPT use।
- Temperature: softmax sharpness control।
- Open-ended generation — sampling preferred।
Task-specific guidance:
- Translation — beam search (correctness)।
- Summarization — beam search।
- Story generation — sampling (creativity)।
- Dialog — sampling + temperature।
Computational cost:
- Beam $k$ — memory $k$x, compute $k$x।
- Production — beam ৪-৬ typical।
- Real-time — greedy/beam ২।
Modern context (LLM):
- GPT-style — sampling primarily।
- Beam search — task-specific (translation, code)।
- Constrained generation — beam helpful।
Bangla translation deployment:
- Beam ৫ — quality acceptable।
- Length normalization essential।
- Repetition handling।
- BLEU evaluate।
মূল উপলব্ধি: Beam search — quality boost over greedy, কিন্তু infinite ≠ optimal। Length bias, repetition, diversity issues। Modern sampling (top-p) often preferred open-ended task-এ। Constrained task — beam still relevant। Decoding strategy — task-specific empirical choice।
প্র ০৩ Bangla → English translation system Bangladesh-এ — train data কোথা থেকে, evaluation কীভাবে, deployment এর challenges?
Bangla translation Bangladesh-এর জন্য huge opportunity। ১৭ কোটি speaker, কিন্তু resource-poor language।
Training data sources:
- BUET Parallel Corpus: ~৩০০K sentence pairs।
- SUST NMT corpus: ~১M পেয়ার।
- Wikipedia Bangla — English aligned: ~২০০K।
- Government documents: Bangladesh Gazette।
- Religious texts: Bible, Quran translations।
- Movie subtitles: OpenSubtitles।
Data quality issues:
- Noisy alignment — sentence-level not always accurate।
- Domain bias — religious, governmental over-represented।
- Modern conversational underrepresented।
- Cleaning crucial।
Data augmentation:
- Back-translation: EN→BN, then BN→EN — synthetic pairs।
- Pivot translation: BN → HI → EN (Hindi pivot)।
- Paraphrase generation: diversity বাড়ান।
- Crowdsource: Bangla speaker translate।
Architecture choice:
- Vanilla Seq2Seq — baseline।
- Seq2Seq + Attention — significant gain।
- Transformer (vanilla) — production quality।
- Pretrained — mBART, IndicBERT fine-tune।
- NLLB-200 — Meta-এর recent multi-lingual।
Tokenization:
- SentencePiece — BPE for both Bangla and English।
- Vocab ~৩২K — balance।
- Bangla যুক্তাক্ষর — special handling।
- Number, URL, name — placeholder।
Training setup:
import torch
from torch.optim.lr_scheduler import LambdaLR
# Adam + warmup + decay
optimizer = torch.optim.Adam(model.parameters(),
lr=1e-4)
def lr_schedule(step, warmup=4000, max_lr=1e-4):
if step < warmup:
return step / warmup
return (warmup / step) ** 0.5
scheduler = LambdaLR(optimizer, lr_schedule)
# Label smoothing — overconfidence reduce
criterion = nn.CrossEntropyLoss(label_smoothing=0.1,
ignore_index=PAD)
Evaluation metrics:
- BLEU: n-gram overlap। Standard but flawed।
- ChrF: character F-score। Bangla-এর জন্য ভাল।
- METEOR: semantic alignment।
- BLEURT: learned metric। Best correlation with human।
- Human evaluation: ultimate test।
Bangladesh-specific challenges:
- Code-mix: "আমি office যাচ্ছি" — common। Translation tricky।
- Honorifics: আপনি/তুমি/তুই — English-এ "you" only।
- Named entity: "ঢাকা" → "Dhaka" — copy mechanism helpful।
- Cultural concepts: "মা শা আল্লাহ" — direct translation impossible।
- Idioms: "মাথা গরম" → "angry" (literal "head hot" ভুল)।
Production deployment:
- API service — REST/gRPC।
- Batch translation — government documents।
- Real-time — chat application।
- On-device — privacy-sensitive।
- Caching — frequent translations।
Latency requirements:
- API: ১-২ second per sentence acceptable।
- Real-time chat: <১ second।
- Batch: throughput-optimized।
Cost considerations:
- GPU server — $৫০০-২০০০/মাস depending scale।
- API call vs self-host break-even ~১M call/month।
- Cloud Google/Azure translation expensive long-term।
Current state:
- Google Translate Bangla — reasonable, not perfect।
- Local startup — Pathao, bKash internal use।
- Open-source — IndicTrans2, NLLB available।
- Commercial opportunity — domain-specific (legal, medical)।
Realistic accuracy:
- Vanilla Seq2Seq — BLEU ১৫-২০ (poor)।
- Seq2Seq + Attention — BLEU ২৫-৩০।
- Transformer + sufficient data — BLEU ৩৫-৪০।
- Production threshold — BLEU ৩০+।
- Specialized domain — higher achievable।
মূল উপলব্ধি: Bangla translation — practical impact বিশাল। Vanilla Seq2Seq learning purpose। Production — Transformer + pretrained। Data quality > model complexity। Bangladesh-specific challenge (code-mix, idiom, honorifics)। Continuous improvement requirement। ১৭ কোটি speaker market — opportunity untapped।
প্র ০৪ Bottleneck (single context vector) — কেন এটা long sequence-এ fail করে? Information theoretic bound কী?
Bottleneck — Seq2Seq-এর core limitation। Information theory দিয়ে formally analyze করা যায়।
Information capacity calculation:
- Context vector $\mathbf{c} \in \mathbb{R}^{H}$, FP32।
- Each dim ৩২ bit — practical-এ ২-৪ bit useful (precision)।
- $H = 1024$ — ~২K-৪K bit total।
- Average word — ১৫-২০ bit information (entropy)।
- ~১০০-২০০ word capacity theoretical।
Practical capacity:
- Effective bits — much less।
- Distributed representation — redundant।
- ~৫০ word practical limit।
- Beyond — quality drop sharp।
Empirical observation:
- Cho et al. (২০১৪) — sentence length vs accuracy plot।
- Length ১০ — high accuracy।
- Length ৩০+ — sharp degradation।
- Length ৬০+ — barely usable।
Why information loss:
- Encoder LSTM — forget irrelevant during compression।
- What's "relevant" — task-specific, encoder doesn't know decoder need।
- Some early information lost as later tokens overwrite cell state।
- Single fixed-size representation — fundamentally limited।
Theoretical perspective:
- Mutual information $I(X; \mathbf{c}) \le H(\mathbf{c})$ — bottleneck।
- Long sequence high entropy — impossible to compress losslessly।
- Lossy compression — task-relevant retain, irrelevant discard।
- "Task-relevant" learned — bias inherent।
Workaround attempts:
- Larger context: $H$ increase — diminishing return।
- Multiple context: per-decoder-step আলাদা context।
- Hierarchical: sentence-level, document-level।
- Memory networks: external memory।
Attention solution (preview):
- Decoder প্রতি step-এ encoder hidden states-এর সব access।
- Step-specific weighted combination।
- Effectively variable-size context।
- Bottleneck eliminate।
Information bottleneck principle:
- Tishby et al. — IB principle।
- Optimal compression — task-relevant preserve।
- $\min I(X; T)$ subject to $I(T; Y)$ adequate।
- DL training implicit IB optimization।
Modern alternatives:
- Attention — explicit access।
- Memory networks — external storage।
- Pointer networks — copy mechanism।
- Retrieval-augmented — external knowledge।
Bangla long document:
- News article — ৫০০-১০০০ word।
- Vanilla Seq2Seq — fail।
- Hierarchical encoder — sentence then document।
- Modern Transformer + chunking।
Practical implications:
- Vanilla Seq2Seq — short sentence only।
- Sentence-by-sentence document translation।
- Context lost across sentence — coherence issue।
- Document-level model required।
Lesson for ML design:
- Architectural bottleneck — fundamental limit।
- "More parameter" — not always solution।
- Mechanism design — capacity matter।
- Information flow analysis — design tool।
মূল উপলব্ধি: Bottleneck information-theoretic। Single fixed vector — long sequence impossible। Empirical degradation length ৫০+। Workaround লাগে — attention solution। DL design — information capacity matter। Practical Bangla — short sentence vanilla, long document need attention/Transformer।
অনুশীলন
-
Encoder output: nn.LSTM input shape
(2, 5, 10)(batch ২, seq ৫, feat ১০), hidden ৬৪। Output ও hidden shape কী?- Output:
(2, 5, 64)— সব timestep। - Hidden $h$:
(1, 2, 64)— শেষ step। - Cell $c$:
(1, 2, 64)। - Seq2Seq-এ context = $(h, c)$।
- Output:
-
Teacher forcing: training-এ decoder-এর input কী? Test-এ?
Training:
tgt[:, :-1]— ground-truth previous token (teacher forcing)।Test/Inference:
<START>token দিয়ে শুরু, প্রতি step-এ own prediction feed।<END>পর্যন্ত।Mismatch — exposure bias।
-
Beam search: beam ৩, vocab ১০০০। প্রতি step-এ কতগুলো sequence এক্সপ্যান্ড করতে হবে?
৩ partial sequence, প্রতিটি ১০০০ extension = ৩,০০০ candidate।
এদের মধ্যে top ৩ keep — পরের step-এ আবার।
Compute cost — beam $k$ গুণ greedy।
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ৩০ · Attention mechanism পরবর্তী পাঠ Bottleneck-এর সমাধান — decoder-এর "focus" mechanism।
- পাঠ ২৮ · GRU আগের পাঠ Encoder-decoder building block।
- পাঠ ৩৩ · Transformer M5-এ Modern Seq2Seq — RNN ছাড়া।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।