Detailed mathematics తో Transformers యొక్క heart అర్థం చేసుకుందాం
ChatGPT, Gemini, Claude — ఇవన్నీ ఒక ముఖ్యమైన idea పై పని చేస్తాయి. ఆ idea పేరు Attention. ఈ blog లో మనం detailed mathematics తో దాన్ని పూర్తిగా అర్థం చేసుకుందాం.
ఒక sentence చదివేటప్పుడు ప్రతి word సమానంగా important కాదు. కొన్ని words మరికొన్ని words తో ఎక్కువ connected గా ఉంటాయి. Attention Mechanism ఈ connections ను mathematically నేర్చుకుంటుంది.
ఉదాహరణ: మన పూర్తి blog లో ఈ sentence ని use చేస్తాం మరియు exact numbers తో calculate చేద్దాం:
Computer కి words అర్థం కావు కాబట్టి ప్రతి word ను numbers రూపంలో vector గా మారుస్తాం. మన example లో 4-dimensional vectors తో చూద్దాం.
| Word | d₁ | d₂ | d₃ | d₄ |
| Birds | 1 | 0 | 1 | 0 |
| fly | 0 | 1 | 0 | 1 |
| high | 1 | 1 | 0 | 0 |
X is a (3 × 4) matrix. Real models లో ఈ vectors 512–12288 dimensions లో ఉంటాయి.
Transformer లో మూడు learnable weight matrices ఉంటాయి. Training లో నేర్చుకుంటాయి. మన example లో d_model=4 → d_k=3.
| 1 | 0 | 1 |
| 1 | 0 | 0 |
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
| 1 | 1 | 0 |
X ను Weight matrices తో multiply చేసి Q, K, V ని step-by-step గా calculate చేద్దాం.
Result — Q Matrix:
| Word | q₁ | q₂ | q₃ |
| Birds | 1 | 0 | 2 |
| fly | 1 | 1 | 1 |
| high | 2 | 0 | 1 |
Result — K Matrix:
| Word | k₁ | k₂ | k₃ |
| Birds | 1 | 1 | 2 |
| fly | 1 | 2 | 1 |
| high | 2 | 2 | 2 |
Result — V Matrix:
| Word | v₁ | v₂ | v₃ |
| Birds | 1 | 0 | 1 |
| fly | 1 | 1 | 0 |
| high | 1 | 1 | 0 |
"Birds" ఏమి వెతుకుతుందో. Row = [1, 0, 2]
ప్రతి word offer చేయగలిగే information signal.
ప్రతి word యొక్క actual content.
Q ను K transpose తో multiply చేస్తాం. ఇది ప్రతి word pair మధ్య similarity ని కొలుస్తుంది.
Full Score Matrix S:
| Q \ K | Birds | fly | high |
| Birds | 5 | 3 | 6 ⭐ |
| fly | 4 | 3 | 6 ⭐ |
| high | 6 | 4 | 8 ⭐ |
Scores చాలా large గా ఉంటే softmax లో gradients vanish అవుతాయి. dk=3, √3≈1.732 తో divide చేద్దాం.
S_scaled (Birds row) = [ 2.887, 1.732, 3.464 ]
Softmax: softmax(xᵢ) = eˣⁱ / Σeˣʲ — scores ని 0–1 probabilities గా మారుస్తుంది, sum = 1.
చివరగా attention weights ని Value matrix తో multiply చేస్తాం. ఇది ప్రతి word కి context-aware representation ఇస్తుంది.
Full Output Matrix Z:
| Word | z₁ | z₂ | z₃ |
| Birds ✓ | 1.000 | 0.677 | 0.323 |
| fly | 1.000 | 0.684 | 0.310 |
| high | 1.000 | 0.705 | 0.295 |
ప్రతి Query ని అన్ని Keys తో dot product → similarity. High = more related.
Scores normalize చేయడం. dk=3 → √3≈1.732 తో divide.
ex/Σex — అన్ని probabilities sum=1.
Attention weights తో Values ని weighted sum → context-rich output.
Student doubt అడుగుతాడు → అది Query.
మిగతా students చేతులు ఎత్తుతారు → "నాకు తెలుసు" signal అది Key.
Teacher most relevant student నుండి answer తీసుకుంటాడు → ఆ actual answer అది Value.
Mathematically: teacher ప్రతి student కి softmax score ఇస్తాడు → weighted average of all answers — exactly like Z = A·V.
Attention వల్ల ఒక word, sentence లోని ఇతర words నుండి mathematically weighted గా useful information collect చేసుకుంటుంది — Z = softmax(QKᵀ/√dₖ)·V — ఇది Modern AI యొక్క heart.