Deep Learning · Transformers · Step-by-Step Mathematics

Attention Mechanism:
Q, K, V అంటే ఏమిటి?

Detailed mathematics తో Transformers యొక్క heart అర్థం చేసుకుందాం

Author: M. Nagesh
Institution: KL University
Domain: AI / Machine Learning

ChatGPT, Gemini, Claude — ఇవన్నీ ఒక ముఖ్యమైన idea పై పని చేస్తాయి. ఆ idea పేరు Attention. ఈ blog లో మనం detailed mathematics తో దాన్ని పూర్తిగా అర్థం చేసుకుందాం.

CONCEPT 01Attention అంటే ఏమిటి?

ఒక sentence చదివేటప్పుడు ప్రతి word సమానంగా important కాదు. కొన్ని words మరికొన్ని words తో ఎక్కువ connected గా ఉంటాయి. Attention Mechanism ఈ connections ను mathematically నేర్చుకుంటుంది.

Core Idea: "ఏ word, ఏ word ను ఎక్కువ గమనించాలి?" — దీనికి mathematical గా answer చెప్పడమే Attention.

ఉదాహరణ: మన పూర్తి blog లో ఈ sentence ని use చేస్తాం మరియు exact numbers తో calculate చేద్దాం:

"Birds  fly  high"

STEP 01Words → Embedding Vectors

Computer కి words అర్థం కావు కాబట్టి ప్రతి word ను numbers రూపంలో vector గా మారుస్తాం. మన example లో 4-dimensional vectors తో చూద్దాం.

INPUT EMBEDDING MATRIX  X  — 3 words × 4 dimensions
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 లో ఉంటాయి.


STEP 02Weight Matrices WQ, WK, WV

Transformer లో మూడు learnable weight matrices ఉంటాయి. Training లో నేర్చుకుంటాయి. మన example లో d_model=4 → d_k=3.

WQ  (4×3)
101
100
001
011
WK  (4×3)
011
110
101
011
WV  (4×3)
100
010
001
110
Formulas: Q = X · WQ  |  K = X · WK  |  V = X · WV

STEP 03Q, K, V Matrices లెక్కించడం

X ను Weight matrices తో multiply చేసి Q, K, V ని step-by-step గా calculate చేద్దాం.

Q = X · WQ  →  (3×4) · (4×3) = (3×3)

Result — Q Matrix:

Wordq₁q₂q₃
Birds102
fly111
high201
K = X · WK  →  (3×4) · (4×3) = (3×3)

Result — K Matrix:

Wordk₁k₂k₃
Birds112
fly121
high222
V = X · WV  →  (3×4) · (4×3) = (3×3)

Result — V Matrix:

Wordv₁v₂v₃
Birds101
fly110
high110
Q
Query (Q)

"Birds" ఏమి వెతుకుతుందో. Row = [1, 0, 2]

K
Key (K)

ప్రతి word offer చేయగలిగే information signal.

V
Value (V)

ప్రతి word యొక్క actual content.


STEP 04Score Matrix — S = Q · Kᵀ

Q ను K transpose తో multiply చేస్తాం. ఇది ప్రతి word pair మధ్య similarity ని కొలుస్తుంది.

S = Q · Kᵀ  (3×3) · (3×3) = (3×3)  —  Birds row మాత్రమే shown

Full Score Matrix S:

Q \ KBirdsflyhigh
Birds536 ⭐
fly436 ⭐
high648 ⭐

STEP 05Scaling — S ÷ √dk

Scores చాలా large గా ఉంటే softmax లో gradients vanish అవుతాయి. dk=3, √3≈1.732 తో divide చేద్దాం.

S_scaled = S / √3  ≈  S / 1.732  — Birds row

S_scaled (Birds row) = [ 2.887,  1.732,  3.464 ]

ఎందుకు scale చేస్తారు?  Large scores → softmax లో one value కి ~100% probability → gradient ≈ 0 → training fails. Scaling దీన్ని fix చేస్తుంది.

STEP 06Softmax — Probabilities గా మార్చడం

Softmax: softmax(xᵢ) = eˣⁱ / Σeˣʲ — scores ని 0–1 probabilities గా మారుస్తుంది, sum = 1.

softmax( [2.887, 1.732, 3.464] ) — Birds row
Birds
score: 2.887
32.3%
fly
score: 1.732
10.2%
high ⭐
score: 3.464
57.5%
అర్థం: "Birds" process చేసేటప్పుడు model "high" కి 57.5%, తనకు 32.3%, "fly" కి 10.2% attention ఇస్తుంది.

STEP 07Final Output — Z = A · V

చివరగా attention weights ని Value matrix తో multiply చేస్తాం. ఇది ప్రతి word కి context-aware representation ఇస్తుంది.

Z[Birds] = 0.323 × V_Birds + 0.102 × V_fly + 0.575 × V_high

Full Output Matrix Z:

Wordz₁z₂z₃
Birds ✓1.0000.6770.323
fly1.0000.6840.310
high1.0000.7050.295
BEFORE ATTENTION
AFTER ATTENTION

FULL FORMULAComplete Attention Formula

SCALED DOT-PRODUCT ATTENTION
Attention(Q, K, V)  =  softmax( Q·Kᵀ / √dₖ ) · V
1.Q = X · WQ ← project to query space
2.K = X · WK ← project to key space
3.V = X · WV ← project to value space
4.S = Q · Kᵀ ← raw similarity scores (dot product)
5.S_s = S / √dₖ ← scale to prevent vanishing gradients
6.A = softmax(S_s) ← probabilities, row-wise, sum=1
7.Z = A · V ← final context-enriched output
Q · Kᵀ

ప్రతి Query ని అన్ని Keys తో dot product → similarity. High = more related.

÷ √dₖ

Scores normalize చేయడం. dk=3 → √3≈1.732 తో divide.

softmax(·)

ex/Σex — అన్ని probabilities sum=1.

· V

Attention weights తో Values ని weighted sum → context-rich output.


CONCEPT 02Attention ఎందుకు Powerful?

OLD NLP (RNN / LSTM)
TRANSFORMERS (Attention)
🎓 Classroom Analogy

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.

#AI#MachineLearning#DeepLearning #Transformers#AttentionMechanism#LLM #Mathematics#NLP#DataScience#KLUniversity