الضرب النقطي المُقاس ومتعدد الرؤوس
نفّذ الانتباه بالتوازي في فضاءات فرعية
الضرب النقطي المُقاس ومتعدد الرؤوس درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Scaling Problem
When key vectors are long, dot-product scores grow huge and softmax becomes razor-sharp. That kills gradients, so we need to scale the scores down.
Divide by Root d
The fix is to divide each score by the square root of the key dimension. This keeps the numbers in a stable range before softmax.
scores = (q @ k.transpose(-2, -1)) / d_k ** 0.5Scaled Dot-Product Attention
Put it together: score, scale, softmax, then blend values. This four-step recipe is the famous scaled dot-product attention.
w = scores.softmax(dim=-1)
out = w @ vOne Head, One View
A single attention head learns just one way to relate tokens. But language has many relationships at once, so one head is limiting.
Many Heads in Parallel
Multi-head attention runs several attention heads side by side, each with its own Q, K, V projections, each attending in a different subspace.
Split the Dimension
You do not make the model wider. You split the model dimension across heads, so each head works on a smaller slice in parallel.
d_head = d_model // num_headsReshape into Heads
To run heads in parallel, you reshape Q, K, V to add a heads axis, then attend within each head independently.
q = q.view(B, T, H, d_head).transpose(1, 2)Different Patterns
One head may track grammar, another may link a pronoun to its noun. Multiple heads capture diverse patterns the same input contains.
Concatenate Heads
After each head produces its output, you concatenate them back into one vector of the original model dimension.
out = out.transpose(1, 2).reshape(B, T, d_model)Final Projection
A last output projection mixes the concatenated head results, letting the model combine what every head discovered.
out = self.W_o(out)Use the Built-in
In practice PyTorch ships nn.MultiheadAttention, so you rarely hand-roll the reshapes. Knowing the math still helps you debug.
attn = nn.MultiheadAttention(d_model, num_heads)Quick Check
Let's confirm why we scale the scores.
Recap
You saw how scaling stabilizes softmax and how multi-head attention splits the work across parallel heads, then concatenates and projects. Great progress!
الأسئلة الشائعة
هل درس «الضرب النقطي المُقاس ومتعدد الرؤوس» مجاني؟
نعم — نص درس «الضرب النقطي المُقاس ومتعدد الرؤوس» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «الضرب النقطي المُقاس ومتعدد الرؤوس»؟
نفّذ الانتباه بالتوازي في فضاءات فرعية تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الضرب النقطي المُقاس ومتعدد الرؤوس»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الانتباه الذاتي: Query وKey وValue
- الضرب النقطي المُقاس ومتعدد الرؤوس
- الترميز الموضعي للترتيب
- تكديس كتلة Transformer Encoder