Lecture 02 · NN Fundamentals
المحاضرة 02 · أساسيات الشبكات العصبية
Convolution: shapes, one output by hand, parameters
الالتفاف (convolution): الأشكال، وحساب مخرج واحد باليد، والمعاملات
P1 MUSTA convolution is an FC layer that only looks at a small window and reuses the same weights at every position. That one idea explains its four tensor shapes, its arithmetic, and why it is so cheap in parameters.
convolution هي طبقة FC لا تنظر إلا إلى نافذة صغيرة، وتعيد استخدام الأوزان نفسها في كل موضع. هذه الفكرة الواحدة تفسّر أشكال tensors الأربعة، وطريقة الحساب، ولماذا هي رخيصة جداً في عدد parameters.
Two changes from FC: local connectivity and weight sharing
تغييران عن FC: الاتصال المحلي ومشاركة الأوزان
Slide 8 rewrites the FC sentence: "The output neuron is connected to input neurons in the receptive field." Not all inputs any more, only the kh × kw window under the kernel, across all input channels. That is local connectivity.
تعيد الشريحة 8 كتابة جملة FC: "The output neuron is connected to input neurons in the receptive field." أي ليس كل المدخلات بعد الآن، بل فقط النافذة kh × kw تحت kernel، عبر كل قنوات المدخل. هذا هو local connectivity.
The same filter then slides to every spatial position, and every position uses the same weights. That is weight sharing (slides 8 and 12 label it). The consequence: the number of weights depends only on the filter, never on how big the image is.
ثم ينزلق الـ filter نفسه إلى كل موضع مكاني، وكل موضع يستخدم الأوزان نفسها. هذا هو weight sharing (تسمّيه الشريحتان 8 و12). والنتيجة: عدد الأوزان يعتمد على الـ filter فقط، ولا يعتمد أبداً على حجم الصورة.
The four tensor shapes
أشكال الموترات الأربعة
Slides 8 and 9 strike out the FC shapes and replace them. Each new spatial axis is appended to the end; nothing is reordered.
تشطب الشريحتان 8 و9 أشكال FC وتستبدلها. كل محور مكاني جديد يُضاف في النهاية، ولا يتغير ترتيب أي شيء.
| Tensorالموتر | FC | 1D convconv أحادي البعد (1D) | 2D convconv ثنائي البعد (2D) |
|---|---|---|---|
| Input Xالدخل X | (n, c_i) | (n, c_i, w_i) | (n, c_i, h_i, w_i) |
| Output Yالخرج Y | (n, c_o) | (n, c_o, w_o) | (n, c_o, h_o, w_o) |
| Weights Wالأوزان W | (c_o, c_i) | (c_o, c_i, k_w) | (c_o, c_i, k_h, k_w) |
| Bias bالانحياز b | (c_o,) | (c_o,) | (c_o,) |
يقارن هذا الجدول أشكال tensors في FC و1D conv و2D conv. ما يجب ملاحظته: W يبدأ دائماً بـ (c_o, c_i) تماماً كما في FC، ثم تُضاف أبعاد kernel المكانية في النهاية. والـ bias يبقى (c_o,) في كل الحالات، أي bias واحد لكل قناة مخرجة وليس لكل موضع. وأشكال W وb لا تحتوي n ولا h_i ولا w_i.
What one filter is
ما هو المرشح (filter) الواحد
Slides 9 to 13 build the picture one filter at a time. A single filter has shape (ci, kh, kw): it is as deep as the input. Placed on one window, it multiplies all ci × kh × kw overlapping numbers, sums them across every channel, adds one bias, and produces one number. Slid over every position, it produces one output channel. Stack co filters and you get co output channels.
تبني الشرائح 9 إلى 13 الصورة filter واحداً في كل مرة. الـ filter الواحد شكله (ci, kh, kw): أي بعمق المدخل نفسه. عند وضعه على نافذة واحدة، يضرب كل الأرقام المتقابلة وعددها ci × kh × kw، ويجمعها عبر كل القنوات، ويضيف bias واحداً، فينتج رقماً واحداً. وعند تحريكه على كل المواضع ينتج قناة مخرجة واحدة. ضع co من filters فوق بعضها فتحصل على co قناة مخرجة.
"ci is how deep, co is how many." The input decides the depth of each filter; you, the designer, decide how many filters to train. co is a free choice and has nothing to do with ci.
"ci هو العمق، وco هو العدد." المدخل هو الذي يحدد عمق كل filter، وأنت كمصمم تحدد عدد filters التي ستدرّبها. co اختيار حر ولا علاقة له بـ ci.
Output size with no padding and stride 1
حجم الخرج بلا حشو (padding) ومع stride 1
Slide 14: the kernel can only sit where it fits entirely inside the input. Along one axis there are hi - kh + 1 such positions.
الشريحة 14: الـ kernel لا يمكن وضعه إلا حيث يتسع بالكامل داخل المدخل. وعلى محور واحد يوجد hi - kh + 1 موضعاً من هذا النوع.
ho = hi − kh + 1 wo = wi − kw + 1
Slide 14's numbers: hi = wi = 4, k = 3, so ho = 4 - 3 + 1 = 2. The slide's red note says it plainly: "Feature map size becomes smaller." The next page adds padding and stride to this formula.
أرقام الشريحة 14: hi = wi = 4 وk = 3، إذن ho = 4 - 3 + 1 = 2. والملاحظة الحمراء في الشريحة تقولها بوضوح: "Feature map size becomes smaller." والصفحة التالية تضيف padding وstride إلى هذه الصيغة.
Slide 15 by hand: two channels, stride 2
الشريحة 15 باليد: قناتان، وstride 2
The problem: 2 input channels of 4 × 4, one output channel, a 2 × 2 filter, stride 2. One output channel means one filter, and that filter must be 2 channels deep: shape (1, 2, 2, 2), 8 weights. The bias is ignored on the slide.
المسألة: قناتان مدخلتان بحجم 4 × 4، وقناة مخرجة واحدة، وfilter بحجم 2 × 2، وstride = 2. قناة مخرجة واحدة تعني filter واحداً، ويجب أن يكون عمقه قناتين: الشكل (1, 2, 2, 2)، أي 8 أوزان. والشريحة تتجاهل الـ bias.
Output size: (h_i - k) / s + 1 = (4 - 2) / 2 + 1 = 2 -> 2 x 2
Value (1,1): rows 1-2, cols 1-2
ch1 window [1 2; 1 2] . filter [2 1; 1 2] = 2 + 2 + 1 + 4 = 9
ch2 window [5 6; 5 6] . filter [1 2; 2 1] = 5 + 12 + 10 + 6 = 33
total = 9 + 33 = 42
Value (1,2): rows 1-2, cols 3-4 (stride 2 jumps two columns)
ch1 window [3 4; 3 4] . filter [2 1; 1 2] = 6 + 4 + 3 + 8 = 21
ch2 window [7 8; 7 8] . filter [1 2; 2 1] = 7 + 16 + 14 + 8 = 45
total = 21 + 45 = 66
Values (2,1), (2,2): rows 3-4 are identical to rows 1-2
-> 42 and 66 again
Output = [[42, 66],
[42, 66]]
The slide writes each value as one long sum of eight products, for example 1st value = (2·1 + 1·2 + 1·1 + 2·2) + (1·5 + 2·6 + 2·5 + 1·6) = 42. Splitting it per channel as above makes it easy to check: 9 + 33 and 21 + 45.
تكتب الشريحة كل قيمة كمجموع طويل من ثمانية حواصل ضرب، مثلاً 1st value = (2·1 + 1·2 + 1·1 + 2·2) + (1·5 + 2·6 + 2·5 + 1·6) = 42. وتقسيمه لكل قناة كما فعلنا أعلاه يجعل التحقق سهلاً: 9 + 33 و21 + 45.
Cost of this tiny layer: 4 outputs × 8 multiplies = 32 MACs, and 8 parameters (9 with a bias).
تكلفة هذه الطبقة الصغيرة: 4 مخرجات × 8 عمليات ضرب = 32 MACs، و8 parameters (و9 مع bias).
Parameter count, and why it ignores image size
عدد المعاملات، ولماذا يتجاهل حجم الصورة
#params(Conv2D) = co · ci · kh · kw + co
It is just the size of W plus the size of b from the shape table. No hi, wi, ho or wo anywhere, because of weight sharing.
هي ببساطة حجم W مضافاً إليه حجم b من جدول الأشكال. لا يوجد hi ولا wi ولا ho ولا wo في أي مكان، والسبب هو weight sharing.
A 32 × 32 RGB image (ci = 3) goes into a conv layer with 16 filters of 5 × 5 (the ConvNetJS first layer).
صورة RGB بحجم 32 × 32 (ci = 3) تدخل طبقة conv فيها 16 filters بحجم 5 × 5 (الطبقة الأولى في ConvNetJS).
Conv: 16 x 3 x 5 x 5 + 16 = 1200 + 16 = 1,216 parameters
Same input flattened into an FC layer with 16 outputs:
32 x 32 x 3 = 3072 inputs
3072 x 16 + 16 = 49,152 + 16 = 49,168 parameters
Ratio: 49,168 / 1,216 ~ 40x more for FC
Feed the conv a 224 x 224 x 3 image instead:
still 16 x 3 x 5 x 5 + 16 = 1,216
the FC version would need 150,528 x 16 + 16 = 2,408,464
And the FC comparison is generous to FC: it only produces 16 numbers, whereas the conv (with padding 2) produces a full 32 × 32 × 16 map. An FC layer producing that same 16,384-value output would need 3072 × 16,384 + 16,384 = 50,348,032 parameters.
والمقارنة متساهلة مع FC: فهي تنتج 16 رقماً فقط، بينما تنتج طبقة conv (مع padding = 2) خريطة كاملة 32 × 32 × 16. ولو أردت طبقة FC تنتج المخرج نفسه المكوّن من 16,384 قيمة لاحتجت 3072 × 16,384 + 16,384 = 50,348,032 parameter.
What does depend on image size: the activations (co · ho · wo outputs per sample) and the MACs (co · ho · wo · ci · kh · kw). For the 1216-parameter layer with a 32 × 32 output: 16,384 activations and 16 · 1024 · 75 = 1,228,800 MACs. Conv layers are cheap in flash and expensive in compute and RAM; FC layers are the opposite.
ما يعتمد فعلاً على حجم الصورة: activations (عددها co · ho · wo لكل عيّنة) وMACs (عددها co · ho · wo · ci · kh · kw). للطبقة ذات 1216 parameter ومخرج 32 × 32: 16,384 activation و16 · 1024 · 75 = 1,228,800 MACs. طبقات conv رخيصة في flash ومكلفة في الحساب وRAM، وطبقات FC عكس ذلك.
Forgetting to sum across input channels. In slide 15 the answer is 42, not two separate maps of 9 and 33. One filter gives one output channel, no matter how many input channels it reads.
نسيان الجمع عبر قنوات المدخل. في الشريحة 15 الإجابة 42، وليست خريطتين منفصلتين فيهما 9 و33. الـ filter الواحد يعطي قناة مخرجة واحدة مهما كان عدد قنوات المدخل التي يقرؤها.
Thinking co depends on ci. ci sets the depth of each filter; co is simply how many filters you chose.
الظن بأن co يعتمد على ci. ci يحدد عمق كل filter، وco هو ببساطة عدد filters الذي اخترته.
Thinking the parameter count depends on image size. It does not. Bigger images mean more activations and more MACs, the same 1216 parameters.
الظن بأن عدد parameters يعتمد على حجم الصورة. لا يعتمد. الصور الأكبر تعني activations أكثر وMACs أكثر، مع نفس الـ 1216 parameter.
Writing a filter as k × k. A 5 × 5 filter on an RGB input holds 5 × 5 × 3 = 75 weights, not 25.
كتابة الـ filter على أنه k × k فقط. الـ filter بحجم 5 × 5 على مدخل RGB يحتوي 5 × 5 × 3 = 75 وزناً، وليس 25.
Check yourself
اختبر نفسك
A Conv2D layer takes a 3-channel 64 × 64 input and has 32 filters of 3 × 3. What is the shape of W?
طبقة Conv2D مدخلها 3 قنوات بحجم 64 × 64، وفيها 32 filters بحجم 3 × 3. ما شكل W؟
B. W is (co, ci, kh, kw) = (32, 3, 3, 3), output channels first exactly as in FC. A swaps co and ci. C puts the image size into the weights, which weight sharing rules out. D drops the input-channel depth of each filter.
B. شكل W هو (co, ci, kh, kw) = (32, 3, 3, 3)، قنوات الخرج أولاً تماماً كما في FC. الخيار A يبدّل co وci. والخيار C يُدخل حجم الصورة في الأوزان، وهذا ما تستبعده مشاركة الأوزان (weight sharing). والخيار D يُسقط عمق قنوات الدخل في كل مرشح.
How many parameters does that layer have (with bias)?
كم parameter في تلك الطبقة (مع bias)؟
C. 32 × 3 × 3 × 3 + 32 = 864 + 32 = 896. A forgets the input depth and the bias (32 × 9). B is 32 × 9 + 32, still missing the depth of 3. D multiplies by the 64 × 64 image, which never enters the parameter count.
C. 32 × 3 × 3 × 3 + 32 = 864 + 32 = 896. الخيار A ينسى عمق الدخل والانحياز (32 × 9). والخيار B هو 32 × 9 + 32، وما زال ينقصه العمق 3. والخيار D يضرب في الصورة 64 × 64، وهي لا تدخل أبداً في عدد المعاملات.
In the slide 15 example, suppose filter channel 2 were all zeros. What would the top-left output be?
في مثال الشريحة 15، لو كانت قناة filter الثانية كلها أصفاراً، فما قيمة المخرج في الزاوية العليا اليسرى؟
A. The output is the channel 1 contribution plus the channel 2 contribution. Channel 1 gives 2·1 + 1·2 + 1·1 + 2·2 = 9 and channel 2 would now give 0, so 9. 33 is the channel 2 contribution in the original, and 42 is the original total.
A. الخرج هو مساهمة القناة 1 مضافاً إليها مساهمة القناة 2. القناة 1 تعطي 2·1 + 1·2 + 1·1 + 2·2 = 9، والقناة 2 ستعطي الآن 0، إذن 9. أما 33 فهي مساهمة القناة 2 في المثال الأصلي، و42 هو المجموع الأصلي.
You change a conv layer's input from 32 × 32 to 128 × 128 without touching anything else. What happens?
غيّرت مدخل طبقة conv من 32 × 32 إلى 128 × 128 دون تغيير أي شيء آخر. ماذا يحدث؟
D. Weight sharing makes the parameter count independent of spatial size. The output map has about 16 times as many positions (4 × 4 in area, exactly 16 with same padding), and every position costs the same ci kh kw MACs per output channel, so activations and MACs scale with the area.
D. مشاركة الأوزان تجعل عدد المعاملات مستقلاً عن الحجم المكاني. خريطة الخرج فيها مواضع أكثر بنحو 16 مرة (4 × 4 في المساحة، و16 بالضبط مع same padding)، وكل موضع يكلف نفس ci kh kw من MACs لكل قناة خرج، لذلك تكبر التنشيطات وMACs مع المساحة.
With no padding and stride 1, a 7 × 7 input is convolved with a 3 × 3 kernel. What is the output size?
بدون padding ومع stride = 1، يُطبَّق kernel بحجم 3 × 3 على مدخل 7 × 7. ما حجم المخرج؟
B. ho = hi - k + 1 = 7 - 3 + 1 = 5. C is the off-by-one error of forgetting the +1. A is what you would get with padding 1, covered on the next page.
B. ho = hi - k + 1 = 7 - 3 + 1 = 5. الخيار C هو خطأ الفرق بواحد (off-by-one) الناتج عن نسيان +1. والخيار A هو ما تحصل عليه مع padding 1، وهو موضوع الصفحة التالية.