Attention mechanism
এই পাঠে যা শিখবেন
- Bottleneck problem — recap থেকে motivation
- Attention-এর core idea — soft alignment
- Score function — additive (Bahdanau) ও dot-product (Luong)
- Attention weights interpretation — visualization
- PyTorch implementation — Bangla → English translation
- Attention-এর modern legacy — Transformer-এর foundation
১ · Motivation — bottleneck recap
Vanilla Seq2Seq-এ encoder পুরো input একটি single context vector $\mathbf{c}$-এ compress করে। Long sentence-এ — information loss inevitable। Decoder-এর প্রতি step-এ একই $\mathbf{c}$ available — কিন্তু প্রতি target word আসলে source-এর ভিন্ন অংশ "related"।
- "আমি ঢাকায় থাকি" → "I live in Dhaka"।
- "I" generate-এ — "আমি" most relevant।
- "Dhaka" generate-এ — "ঢাকায়" relevant।
- Single $\mathbf{c}$-এ — এই differentiation নেই।
একটি single context-এ confine না। Decoder-কে encoder-এর সব hidden state $h_1, h_2, \ldots, h_T$-এ access দিন। প্রতি decoder step-এ — সংশ্লিষ্ট hidden states-এ "focus" করুন।
২ · Attention — তিন ধাপে
Decoder step $t$, hidden state $s_t$। Encoder hidden states $h_1, \ldots, h_T$।
Step 1 — Score: প্রতিটি $h_i$-এর সাথে $s_t$-এর "compatibility" measure।
$$e_{t,i} = \text{score}(s_t, h_i)$$
Step 2 — Softmax: score-গুলোকে probability distribution-এ convert।
$$\alpha_{t,i} = \frac{\exp(e_{t,i})}{\sum_{j=1}^{T} \exp(e_{t,j})}$$
Step 3 — Weighted sum: context vector — encoder hidden states-এর weighted average।
$$\mathbf{c}_t = \sum_{i=1}^{T} \alpha_{t,i} h_i$$
এই $\mathbf{c}_t$ — প্রতি decoder step-এ ভিন্ন। Decoder predict করে $s_t$ ও $\mathbf{c}_t$ — দু'টি একসাথে use করে।
৩ · Score function — variants
-
Additive (Bahdanau, ২০১৪):
$\text{score}(s_t, h_i) = v^\top \tanh(W_1 s_t + W_2 h_i)$
Learned MLP — flexible কিন্তু slow। -
Dot-product (Luong, ২০১৫):
$\text{score}(s_t, h_i) = s_t^\top h_i$
সরল, GPU-friendly। Default modern attention-এ। -
General/Multiplicative:
$\text{score}(s_t, h_i) = s_t^\top W h_i$
Trainable matrix। Dot-product-এর extension। -
Scaled dot-product:
$\text{score}(s_t, h_i) = s_t^\top h_i / \sqrt{d}$
Transformer (২০১৭)-এ। Numerical stability।
৪ · PyTorch implementation — Bahdanau attention
import torch
import torch.nn as nn
import torch.nn.functional as F
class BahdanauAttention(nn.Module):
def __init__(self, hidden_dim):
super().__init__()
self.W1 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.W2 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.v = nn.Linear(hidden_dim, 1, bias=False)
def forward(self, decoder_state, encoder_outputs):
# decoder_state: (B, H)
# encoder_outputs: (B, T, H)
s = decoder_state.unsqueeze(1) # (B, 1, H)
scores = self.v(torch.tanh(
self.W1(s) + self.W2(encoder_outputs)
)).squeeze(-1) # (B, T)
alpha = F.softmax(scores, dim=-1) # (B, T)
context = torch.bmm(alpha.unsqueeze(1),
encoder_outputs).squeeze(1)
# context: (B, H), alpha: (B, T)
return context, alpha
attn = BahdanauAttention(hidden_dim=64)
s = torch.randn(2, 64) # decoder state
enc = torch.randn(2, 10, 64) # 10 encoder steps
ctx, a = attn(s, enc)
print(ctx.shape, a.shape) # (2, 64), (2, 10)
print(a.sum(-1)) # ~1.0 each row
৫ · Decoder-এ attention integrate
class AttnDecoder(nn.Module):
def __init__(self, vocab_size, embed_dim=128,
hidden_dim=256):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_dim)
self.attn = BahdanauAttention(hidden_dim)
self.lstm = nn.LSTM(embed_dim + hidden_dim,
hidden_dim,
batch_first=True)
self.fc = nn.Linear(hidden_dim * 2, vocab_size)
def forward_step(self, y_prev, h, c, encoder_outputs):
# y_prev: (B, 1), h, c: (1, B, H)
emb = self.embed(y_prev) # (B, 1, E)
s = h[-1] # (B, H)
context, alpha = self.attn(s, encoder_outputs)
# Concat embedding + context
rnn_in = torch.cat([emb, context.unsqueeze(1)], dim=-1)
out, (h, c) = self.lstm(rnn_in, (h, c))
# Predict using both decoder out + context
logits = self.fc(torch.cat([out.squeeze(1), context],
dim=-1))
return logits, h, c, alpha
৬ · Attention visualization
Attention weights $\alpha_{t,i}$ — interpretable। প্রতিটি target word-এ source-এর কোন word focused — heatmap দিয়ে visualize।
- "আমি ঢাকায় থাকি" → "I live in Dhaka" — attention matrix ৩×৪।
- Diagonal pattern — monotonic alignment (sequential)।
- Off-diagonal — word reordering (Bangla SOV vs English SVO)।
- "in" — source-এ "ঢাকায়"-এর "-এ" suffix-এ attend (Bangla locative)।
Practical insight: attention pattern model debug-এ help করে। Misalignment-এ — translation quality পড়বে। Visualize → understand → improve।
৭ · Attention-এর impact
- Bahdanau et al. (২০১৫) WMT'১৪ — BLEU significant boost।
- Long sentence translation — robust।
- Bottleneck eliminate।
- Interpretability — alignment visualize।
- Modular design — encoder + attention + decoder।
৮ · Beyond translation — attention everywhere
- Image captioning: "Show, Attend and Tell" — image regions-এ attention।
- Speech recognition: audio frame-এ attention।
- QA system: context passage-এ attention।
- Document summarization: source sentence-এ attention।
- Self-attention (২০১৭): Transformer — সবচেয়ে impactful descendant।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ "Soft" vs "hard" attention — পার্থক্য কী, কোনটা কেন better? Differentiability-র role?
Soft vs hard — attention design-এর fundamental dichotomy। Differentiability gradient-based learning-এর key।
Soft attention:
- $\alpha_i \in [0, 1]$ continuous — softmax output।
- Weighted combination — সব hidden states contribute (varying degrees)।
- Differentiable — gradient backprop through।
- Standard practice modern DL।
Hard attention:
- একটি specific position select — discrete decision।
- $\alpha$ — one-hot vector।
- Non-differentiable — REINFORCE/Gumbel-softmax লাগে।
- Memory efficient — শুধু selected position store।
Soft-এর benefit:
- End-to-end gradient flow — easy training।
- Multiple position-এর partial focus capture।
- Smooth optimization landscape।
- Interpretable weight visualization।
Soft-এর drawback:
- সব position compute — $O(T)$ memory ও compute।
- Long sequence — expensive।
- Diffuse attention — sometimes unfocused।
Hard-এর benefit:
- Sparse computation — speed gain।
- Focused decision — interpretable।
- Inference time — faster।
Hard-এর challenges:
- REINFORCE — high variance gradient।
- Training unstable।
- Performance generally lower।
Hybrid approaches:
- Sparsemax: soft কিন্তু sparse (some weights exact zero)।
- Top-k attention: soft within top-k।
- Local attention: window-restricted।
- Routing transformer: sparse via clustering।
Modern context:
- Vanilla Transformer — soft, full O(n²)।
- Long sequence — sparse attention popular।
- Linear attention — efficient approximation।
- Flash attention — same compute, optimized memory access।
Theoretical perspective:
- Soft = expected hard — differentiable surrogate।
- Both converge to similar pattern।
- Soft training, hard inference — efficient deployment।
Image captioning case:
- Xu et al. (২০১৫) — both compared।
- Soft — better metrics, smoother attention।
- Hard — sharper visualization, harder train।
Vision Transformer:
- Patches-এ soft attention।
- Image-এ visual concept emerge।
- Interpretability via attention map।
মূল উপলব্ধি: Soft attention — differentiability key, end-to-end gradient flow। Hard discrete, REINFORCE হার্ডে। Modern DL — soft default। Sparse approximation efficiency-এ। Soft-হার্ড equivalence theoretical। Practical — soft sufficient most cases। Differentiability — DL-এর fundamental enabler।
প্র ০২ Bahdanau (additive) vs Luong (dot-product) attention — কোনটা কোথায় ব্যবহার? Computational ও empirical trade-off?
দু'টি foundational attention variant। Choice subtle — both correct, context-dependent।
Bahdanau (২০১৪) — additive:
- $\text{score} = v^\top \tanh(W_1 s + W_2 h)$।
- Learned MLP — flexible।
- Different dimension $s, h$ accommodate (separate $W_1, W_2$)।
- Slow — multiple matmul, tanh nonlinearity।
Luong (২০১৫) — dot-product:
- $\text{score} = s^\top h$।
- সরল — single matmul।
- Same dimension required।
- GPU efficient — batched matmul।
Computational comparison:
- Bahdanau — $O(THd)$ per step, $T$ encoder positions, $d$ hidden।
- Luong — $O(THd)$ same big-O, smaller constant।
- Practice — Luong ৩-৫x faster।
Empirical comparison:
- Translation tasks — similar accuracy।
- Bahdanau marginal edge small data।
- Luong scales better।
- Modern Transformer — scaled dot-product (Luong descendant)।
Why dot-product won:
- GPU friendly — matmul highly optimized।
- Scales — large model possible।
- Self-attention compatible।
- Simple — fewer parameter।
Scaling factor (Transformer):
- $\text{score} = s^\top h / \sqrt{d}$।
- Large $d$ — dot-product variance grow।
- Softmax saturate — gradient vanish।
- $\sqrt{d}$ scale — variance ১, gradient flow।
Different formulations:
- General: $s^\top W h$ — learnable matrix।
- Concat: $v^\top \tanh(W [s; h])$ — Bahdanau variant।
- Location: $W s$ — pure decoder-based।
- Content + location: hybrid।
Modern attention (Transformer):
- $\text{Attn}(Q, K, V) = \text{softmax}(QK^\top / \sqrt{d}) V$।
- Scaled dot-product।
- Generalize — Q, K, V separate projection।
- Multi-head — diverse attention।
Implementation detail:
- Bahdanau — separate W_1, W_2 layer।
- Luong — single matmul (or none)।
- Memory — Bahdanau intermediate tensor large।
Specialized usage:
- Speech recognition — Bahdanau (ESPNet)।
- Translation — Luong/Transformer।
- Image — pixel attention (CBAM) — variants।
- Time-series — additive helpful।
When Bahdanau preferred:
- Encoder-decoder dimension different।
- Small model, small data।
- Interpretability key।
- Custom architecture।
When Luong preferred:
- Large model — speed critical।
- Modern Transformer-compatible।
- GPU efficiency।
- Standard NLP।
Bangla translation:
- Default Luong — speed, modern stack।
- Bahdanau — research/teaching।
- Production — Transformer attention।
মূল উপলব্ধি: Bahdanau (additive) vs Luong (dot-product) — design choice। Empirically similar accuracy, Luong faster। Modern Transformer scaled dot-product — Luong heir। GPU efficiency winning factor। Implementation detail matter at scale। Both correct, context-driven choice।
প্র ০৩ Bangladesh-এ Bangla → English translation — attention model train করছেন। Attention map দেখে কী debug সম্ভব?
Attention visualization — model debugging-এর powerful tool। Interpretability gives actionable insight।
Healthy attention pattern:
- Diagonal-like — monotonic alignment।
- Bangla SOV vs English SVO — slight off-diagonal expected।
- Sharp focus — confidence high।
- Coverage — সব source token visited।
Pathological pattern 1 — Diagonal absent:
- Random attention — model not learning alignment।
- Cause — insufficient training, poor init, learning rate issue।
- Fix — train more, check optimizer।
Pattern 2 — Always last token:
- Attention "collapse" to single position।
- Decoder-encoder bottleneck similar issue।
- Fix — temperature in softmax, attention dropout।
Pattern 3 — Repetition:
- Same source token attended multiple decoder steps।
- Output repetition issue।
- Fix — coverage penalty, n-gram blocking decoding।
Pattern 4 — Skipping:
- Source token never attended।
- Translation incomplete।
- Fix — coverage encouragement loss।
Bangla-specific patterns:
- Verb position: Bangla last → English mid। Off-diagonal at end।
- Postpositions: "ঢাকায়" → "in Dhaka" — split attention।
- Honorifics: "তিনি"/"সে" — attention captures formality।
- Compound verbs: "করে দিয়েছিল" — multi-token attention।
Visualization implementation:
import matplotlib.pyplot as plt
def plot_attention(src_tokens, tgt_tokens, alpha):
# alpha: (len_tgt, len_src)
fig, ax = plt.subplots(figsize=(10, 8))
ax.imshow(alpha.cpu(), cmap='viridis', aspect='auto')
ax.set_xticks(range(len(src_tokens)))
ax.set_xticklabels(src_tokens, rotation=45,
fontfamily='Anek Bangla')
ax.set_yticks(range(len(tgt_tokens)))
ax.set_yticklabels(tgt_tokens)
ax.set_xlabel('Source (Bangla)')
ax.set_ylabel('Target (English)')
plt.colorbar(ax.images[0])
plt.tight_layout()
plt.show()
Diagnostic checklist:
- Multiple example visualize — sample diversity।
- Length variation — short ও long both।
- Domain variation — formal ও colloquial।
- Error case especially — what attention says?
Quantitative metrics:
- Entropy: attention sharpness measure।
- Coverage: source token attended at least once?
- Monotonicity: diagonal-like score।
- Alignment error rate (AER): human-aligned vs predicted।
Connection to translation quality:
- Sharp attention — high confidence, often correct।
- Diffuse — uncertainty, often error।
- Pathological pattern strong correlation translation quality।
Improvement strategies:
- Coverage loss: coverage vector penalize repeated attention।
- Attention regularization: entropy bonus or sparsity।
- Multi-head: different head capture different alignment।
- Cross-attention layer-wise: Transformer multiple level।
Bangla-English specific:
- Word order flip — verb attention key insight।
- Subject-honorific agreement — attention captures?
- Conjunct character — multi-position attention।
- Code-mix English token — attention pattern?
Production debugging:
- Failure case attention save।
- Pattern cluster — common error mode identify।
- Targeted data augmentation।
- A/B test improvement।
Modern context:
- Transformer multi-head — interpretability complex।
- Different head different role।
- Layer-wise pattern emerge।
- Attention rollout — aggregate visualization।
মূল উপলব্ধি: Attention visualization — model debug-এর powerful tool। Pattern healthy vs pathological distinguish। Bangla-English alignment — verb, postposition specific। Coverage, monotonicity, entropy quantitative metric। Production-এ continuous monitoring। Interpretability — DL-এর rare gift, fully exploit।
প্র ০৪ Attention $O(n^2)$ memory — long document-এ computational issue। Modern alternatives — sparse, linear, flash attention।
Attention scaling — modern AI-এর core bottleneck। ChatGPT-এর "context length" এই issue-এর সরাসরি ফল।
Standard attention complexity:
- $Q \in \mathbb{R}^{n \times d}$, $K \in \mathbb{R}^{n \times d}$।
- $QK^\top \in \mathbb{R}^{n \times n}$ — $O(n^2 d)$ compute, $O(n^2)$ memory।
- $n = 1000$ — ১M attention scores।
- $n = 100,000$ — ১০B scores। Infeasible।
Sparse attention:
- Local window: $k$-nearest position। $O(nk)$।
- Strided: every $s$-th position।
- Sparse Transformer (২০১৯): mixed pattern।
- Longformer (২০২০): sliding + global token।
- BigBird: random + window + global।
Linear attention:
- Reformulate $\text{softmax}(QK^\top)V \to \phi(Q)(\phi(K)^\top V)$।
- $\phi$ — kernel feature map।
- $O(n d^2)$ — linear in $n$।
- Performer (২০২০), Linear Transformer (২০২০)।
- Approximation — small accuracy loss।
Flash attention (২০২২):
- $O(n^2)$ compute same, কিন্তু $O(n)$ memory।
- Tile-based — GPU memory hierarchy exploit।
- Tri Dao paper — exact attention।
- Training ২-৪x faster, longer context।
State-space models (Mamba):
- Recurrent formulation — $O(n)$ inference।
- Selective mechanism — content-aware।
- Transformer-competitive accuracy।
- RWKV — similar idea, RNN renaissance।
Linformer:
- $K, V$ project to lower dimension।
- $O(nk)$ where $k \ll n$।
- Approximation — most info preserve।
Practical context lengths:
- BERT (২০১৮) — ৫১২ token।
- GPT-3 (২০২০) — ২,০৪৮।
- GPT-4 (২০২৩) — ৩২,০০০ → ১২৮,০০০।
- Claude (২০২৪) — ২০০,০০০।
- Gemini ১.৫ — ১,০০০,০০০।
Trade-off matrix:
- Standard: exact, $O(n^2)$ memory।
- Sparse: approximate, $O(nk)$।
- Linear: approximate, $O(nd)$।
- Flash: exact, $O(n)$ memory।
- Mamba: different paradigm, $O(n)$।
When use which:
- Short context (<১K) — standard fine।
- Medium (১K-৩২K) — Flash attention।
- Long (৩২K+) — sparse + Flash।
- Very long (১M+) — Mamba/sparse।
Memory hierarchy:
- HBM — slow, large।
- SRAM — fast, small।
- Flash — SRAM fully exploit।
- Memory bandwidth bottleneck — compute-bound to memory-bound।
Bangladesh deployment context:
- Document analysis — long context need।
- Bangla legal document — ১০-৫০ page।
- Standard attention — fail।
- Sparse/Flash — production necessary।
Implementation considerations:
- HuggingFace — flash attention support।
- PyTorch ২.০ — built-in optimization।
- Mamba implementation — selective state space।
- Hardware-aware — A100, H100 differ।
Future direction:
- Hybrid — Transformer + state space।
- Mixture of experts — conditional compute।
- Hierarchical — document structure exploit।
- Retrieval-augmented — external memory।
Cost economics:
- $O(n^2)$ — context $২ \times$ → cost $৪ \times$।
- $O(n)$ — context $২ \times$ → cost $২ \times$।
- API pricing reflect this — long context expensive।
মূল উপলব্ধি: Attention $O(n^2)$ — modern AI's central scaling challenge। Sparse, linear, Flash, Mamba — diverse approaches। ১M context length now possible। Bangladesh long document need this। Hardware-software co-design key। Future — hybrid architecture। AI's frontier — context length scaling।
অনুশীলন
-
Compute attention: $s = (1, 0)$, $h_1 = (1, 0)$, $h_2 = (0, 1)$, $h_3 = (1, 1)$। Dot-product score, softmax, context vector।
- Scores: $1, 0, 1$।
- Softmax: $e^1, e^0, e^1$ / $(2 e^1 + 1) = (0.422, 0.155, 0.422)$।
- Context = $0.422 \cdot (1,0) + 0.155 \cdot (0,1) + 0.422 \cdot (1,1) = (0.844, 0.578)$।
-
Bahdanau implement: PyTorch-এ একটি attention layer — input
(B, T, H), query(B, H)।উপরের কোডে BahdanauAttention class দেখুন। Test:
attn = BahdanauAttention(64) ctx, alpha = attn(torch.randn(2, 64), torch.randn(2, 10, 64)) # ctx: (2, 64), alpha: (2, 10), alpha.sum(-1) ≈ 1 -
Visualize: matplotlib দিয়ে attention matrix heatmap। Source-target alignment চিহ্নিত করুন।
উপরের
plot_attentionfunction দেখুন। Bangla font support-এfontfamily='Anek Bangla'ব্যবহার করুন। Diagonal pattern — monotonic; off-diagonal — reordering।
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ৩১ · Self-attention বিস্তারিত পরবর্তী পাঠ Attention নিজের উপর — Transformer-এর core।
- পাঠ ২৯ · Seq2Seq আগের পাঠ Bottleneck context — যা attention solve করে।
- পাঠ ৩২ · Multi-head attention আসছে একাধিক attention parallel।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।