قواعد Broadcasting التي توفّر عليك الحلقات
اجمع Tensors مختلفة الأشكال عنصرًا بعنصر
قواعد Broadcasting التي توفّر عليك الحلقات درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Combine Without Matching Shapes
Broadcasting lets PyTorch stretch a smaller tensor to fit a bigger one, so you skip writing loops to repeat values.
Add a Scalar to Everything
The simplest broadcast: add one number to a whole tensor. PyTorch applies it to every element at once. ✨
x = torch.tensor([1, 2, 3])
print(x + 10) # tensor([11, 12, 13])Compare Shapes Right to Left
Broadcasting lines up dimensions from the right. It then checks each pair to decide if they can combine.
Rule One: Equal Sizes Match
Two dimensions are compatible when they are equal. Matching sizes line up one to one with no stretching needed.
Rule Two: A Size of 1 Stretches
If one dimension is 1, it expands to match the other. That single value is reused across the whole axis.
a = torch.tensor([[1], [2], [3]])
b = torch.tensor([10, 20])
print((a + b).shape) # torch.Size([3, 2])Missing Dimensions Count as 1
When one tensor has fewer dimensions, PyTorch pads it with leading ones. A vector can broadcast against a matrix this way.
m = torch.ones(2, 3)
v = torch.tensor([1, 2, 3])
print((m + v).shape) # torch.Size([2, 3])Add a Bias Across Rows
A classic use: add a row bias to every sample in a batch. One small vector reaches every row for free.
batch = torch.zeros(4, 3)
bias = torch.tensor([1.0, 2.0, 3.0])
print((batch + bias).shape) # torch.Size([4, 3])Normalize a Column at Once
Subtract a per-column mean with broadcasting and center your data in one line, no loop over rows required.
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
m = x.mean(dim=0)
print(x - m)When Shapes Don't Broadcast
If two dimensions differ and neither is 1, the broadcast fails and PyTorch raises a clear size error.
Add Dimensions to Steer Broadcasting
Use unsqueeze to insert a size-1 axis exactly where you need it. This guides broadcasting toward the shape you want.
col = torch.tensor([1, 2, 3]).unsqueeze(1)
print(col.shape) # torch.Size([3, 1])Why Broadcasting Is Fast
Broadcasting never copies the stretched values, it reuses them. That makes vectorized math far faster than Python loops. 🚀
Quick Check
Can you predict whether two shapes will broadcast?
Recap: Broadcasting Rules
You learned the two rules: dimensions match when equal or when one is 1. Master this and loops melt into clean, fast tensor math. 💪
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «قواعد Broadcasting التي توفّر عليك الحلقات» مجاني؟
نعم — نص درس «قواعد Broadcasting التي توفّر عليك الحلقات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «قواعد Broadcasting التي توفّر عليك الحلقات»؟
اجمع Tensors مختلفة الأشكال عنصرًا بعنصر تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «قواعد Broadcasting التي توفّر عليك الحلقات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الأشكال وأنواع البيانات والفهرسة
- إعادة التشكيل وView وSqueeze وUnsqueeze
- قواعد Broadcasting التي توفّر عليك الحلقات
- تتخاطب Tensors مع NumPy