Lecture 02 · NN Fundamentals
المحاضرة 02 · أساسيات الشبكات العصبية
Neurons, FC layers and MLP counting
العصبونات وطبقات FC وعدّ معاملات MLP
P1 MUSTOne formula, two synonym families, four tensor shapes and one counting rule. Get these right and every parameter question in the course becomes arithmetic.
صيغة واحدة، وعائلتان من المترادفات، وأربعة أشكال tensor، وقاعدة عدّ واحدة. أتقن هذه وستتحول كل أسئلة parameters في المقرر إلى مجرد حساب.
The artificial neuron: weighted sum, bias, nonlinearity
العصبون الاصطناعي: مجموع موزون، وانحياز (bias)، ودالة غير خطية
Slide 3 maps the biological neuron onto the math. Each input xi arrives through a synapse that scales it by a weight wi. The cell body adds all the scaled inputs plus a bias b. The activation function f squashes or gates that sum, and the result leaves on the output axon toward the next layer.
تربط الشريحة 3 الخلية العصبية البيولوجية بالرياضيات. كل مدخل xi يصل عبر synapse تضربه في وزن wi. ثم يجمع جسم الخلية كل المدخلات الموزونة مع bias واحد b. بعدها تطبّق activation function الدالة f على هذا المجموع، ويخرج الناتج عبر output axon إلى الطبقة التالية.
yj = f ( Σi wi xi + b )
Three facts to carry out of this formula. First, there is one weight per incoming edge and one bias per neuron, never one bias per edge. Second, without f the whole network collapses into a single linear map, because a composition of linear maps is linear. Third, the sum inside the brackets is a dot product, which is why the layer version becomes a matrix multiply.
ثلاث حقائق تخرج بها من هذه الصيغة. أولاً، هناك وزن واحد لكل حافة داخلة وbias واحد لكل neuron، وليس bias لكل حافة. ثانياً، بدون f تنهار الشبكة كلها إلى تحويل خطي واحد، لأن تركيب التحويلات الخطية خطي. ثالثاً، المجموع داخل القوسين هو dot product، ولهذا تصبح نسخة الطبقة الكاملة ضرب مصفوفات.
Two synonym families
عائلتان من المترادفات
Slide 4 puts two boxes next to the network diagram, and the exam reuses these words interchangeably. Memorize which family each word belongs to, because the two families live in different memories on a microcontroller.
تضع الشريحة 4 صندوقين بجانب رسم الشبكة، والامتحان يستخدم هذه الكلمات بالتبادل. احفظ إلى أي عائلة تنتمي كل كلمة، لأن العائلتين تُخزَّنان في ذاكرتين مختلفتين على microcontroller.
Edges: synapses = weights = parameters
الحواف: synapses = weights = parameters
Learned during training, then fixed. One number per connection plus one bias per neuron. Same values for every input image. On an MCU they sit in flash (read-only storage).
تُتعلَّم أثناء التدريب ثم تصبح ثابتة. رقم واحد لكل اتصال، مضافاً إليه bias واحد لكل neuron. القيم نفسها لكل صورة مدخلة. وعلى MCU تُخزَّن في flash، أي ذاكرة القراءة فقط.
Nodes: neurons = features = activations
العقد: neurons = features = activations
Recomputed for every input. One number per neuron per sample. They exist only while inference runs, so on an MCU they sit in RAM (SRAM), which is the scarcer of the two.
تُحسب من جديد لكل مدخل. رقم واحد لكل neuron لكل عيّنة. وهي موجودة فقط أثناء تشغيل inference، ولذلك تُخزَّن على MCU في RAM (SRAM)، وهي الأندر بين الذاكرتين.
The slide's caption adds one more term: the dimensionality of the hidden layers determines the width of the model. Width is neurons per layer; depth is the number of layers.
ويضيف تعليق الشريحة مصطلحاً آخر: أبعاد hidden layers هي التي تحدد width النموذج. فالـ width هو عدد neurons في الطبقة، والـ depth هو عدد الطبقات.
Counting layers: the input is not a layer
عدّ الطبقات: المدخلات ليست طبقة
Slide 4's title is "3-Layer Neural Network With 2 Hidden Layers", drawn as 5 inputs, then 4, then 3, then 2 outputs. Four columns of circles, but only three layers. A layer is a set of weights plus the neurons they produce. The input column has no weights feeding it, so it is not counted. Of the three real layers, the last produces the output, so 3 - 1 = 2 are hidden.
عنوان الشريحة 4 هو "3-Layer Neural Network With 2 Hidden Layers"، والرسم فيه 5 مدخلات ثم 4 ثم 3 ثم 2 مخرجات. أربعة أعمدة من الدوائر لكن ثلاث طبقات فقط. فالطبقة هي مجموعة أوزان مع neurons التي تنتجها، وعمود المدخلات لا تدخل إليه أي أوزان فلا يُعدّ. ومن الطبقات الحقيقية الثلاث، الأخيرة هي طبقة المخرجات، فيبقى 3 - 1 = 2 طبقتان مخفيتان.
"Count the arrow bundles, not the circle columns." N columns of circles means N - 1 layers, and N - 2 of them are hidden.
"عُدّ حزم الأسهم، لا أعمدة الدوائر." إذا كان لديك N عموداً من الدوائر فعندك N - 1 طبقة، منها N - 2 طبقات مخفية.
The fully-connected (linear) layer
الطبقة المتصلة كلياً (fully-connected، أو linear)
Slide 5: "The output neuron is connected to all input neurons." With ci input channels and co output channels, every one of the co outputs owns a full row of ci weights and one bias.
الشريحة 5: "The output neuron is connected to all input neurons." مع ci قناة مدخلة وco قناة مخرجة، كل واحد من المخرجات الـ co يملك صفاً كاملاً من ci وزناً وbias واحداً.
| Tensorالموتر | Slide 5 (one sample)الشريحة 5 (عينة واحدة) | Slide 6 (batch of n)الشريحة 6 (دفعة من n) | Memoryالذاكرة |
|---|---|---|---|
| Input features Xخصائص الدخل X | (1, c_i) | (n, c_i) | RAM |
| Output features Yخصائص الخرج Y | (1, c_o) | (n, c_o) | RAM |
| Weights Wالأوزان W | (c_o, c_i) | (c_o, c_i) | Flash |
| Bias bالانحياز b | (c_o,) | (c_o,) | Flash |
يلخّص هذا الجدول أشكال tensors الأربعة في طبقة FC. الملاحظة الأهم: عند الانتقال من عيّنة واحدة (الشريحة 5) إلى batch من n عيّنة (الشريحة 6)، يتغير فقط البعد الأول لـ X وY من 1 إلى n، بينما يبقى W بالشكل (c_o, c_i) وb بالشكل (c_o,) دون أي تغيير. والعمود الأخير يوضح أن الأوزان في flash والمدخلات والمخرجات في RAM.
The per-neuron formula on the slide is yi = Σj wij xj + bi: the first index of w is the output, the second is the input. That is exactly why W is stored as (co, ci). Stack the rows and the whole layer is one matrix product:
صيغة neuron الواحد في الشريحة هي yi = Σj wij xj + bi: الفهرس الأول في w هو المخرج، والثاني هو المدخل. ولهذا السبب بالضبط يُخزَّن W بالشكل (co, ci). ضع الصفوف فوق بعضها فتصبح الطبقة كلها ضرب مصفوفات واحداً:
Y = X WT + b (n, ci) × (ci, co) → (n, co)
Parameters per FC layer
المعاملات في كل طبقة FC
#params(FC) = co × ci + co
The first term is the weight matrix, the second is the bias vector. Apply it to the slide figures:
الحد الأول هو مصفوفة الأوزان، والثاني هو متجه bias. طبّقها على أشكال الشرائح:
| Networkالشبكة | Layersالطبقات | Weightsالأوزان | Biasesالانحيازات | Totalالمجموع |
|---|---|---|---|---|
| Slide 5 (5 → 3)الشريحة 5 (5 → 3) | one FCطبقة FC واحدة | 15 | 3 | 18 |
| Slide 7 MLP (5 → 3 → 2)الشريحة 7، MLP (5 → 3 → 2) | 18 + (3 × 2 + 2) | 15 + 6 = 21 | 3 + 2 = 5 | 26 |
| Slide 4 (5 → 4 → 3 → 2)الشريحة 4 (5 → 4 → 3 → 2) | 24 + 15 + 8 | 20 + 12 + 6 = 38 | 4 + 3 + 2 = 9 | 47 |
يطبّق هذا الجدول قاعدة العدّ على الشبكات الثلاث في الشرائح. طبقة الشريحة 5 وحدها: 5 × 3 = 15 وزناً و3 biases، المجموع 18. والـ MLP في الشريحة 7 يضيف طبقة 3 → 2 بـ 3 × 2 + 2 = 8، فيصبح المجموع 26. وشبكة الشريحة 4 مجموعها 24 + 15 + 8 = 47، منها 38 وزناً و9 biases. لاحظ أن عدد biases يساوي دائماً عدد neurons غير المدخلة.
A quick sanity check: the number of biases always equals the number of non-input neurons, and the number of weights always equals the number of arrows in the drawing.
فحص سريع: عدد biases يساوي دائماً عدد neurons غير المدخلة، وعدد الأوزان يساوي دائماً عدد الأسهم في الرسم.
Slides 5 to 7 label the edge from x4 to y2 as w42 (input first). But the slide's own formula yi = Σj wij xj puts the output first, and W has shape (co, ci) = (3, 5), which has no row 4. In matrix terms that edge is W[2][4]: row = output 2, column = input 4. If an exam asks where a weight lives in W, use (output, input).
تسمّي الشرائح 5 إلى 7 الحافة من x4 إلى y2 بـ w42، أي المدخل أولاً. لكن صيغة الشريحة نفسها yi = Σj wij xj تضع المخرج أولاً، والمصفوفة W شكلها (co, ci) = (3, 5)، وليس فيها صف رقمه 4. بلغة المصفوفات هذه الحافة هي W[2][4]: الصف هو المخرج 2 والعمود هو المدخل 4. إذا سألك الامتحان عن موضع وزن في W فاستخدم الترتيب (مخرج، مدخل).
Batching changes activations, never parameters
الدفعات تغيّر التنشيطات، ولا تغيّر المعاملات أبداً
Slide 6 swaps the leading 1 for n, the batch size. X becomes (n, ci) and Y becomes (n, co): n samples are pushed through the same weights at once. W and b are untouched, so the parameter count, and therefore the flash footprint, is identical at n = 1 and n = 64. What scales with n is the activation memory: n times as many outputs to hold in RAM. Embedded inference usually runs at n = 1 for exactly this reason.
تستبدل الشريحة 6 الرقم 1 في البداية بـ n، أي batch size. يصبح X بالشكل (n, ci) وY بالشكل (n, co): أي أن n عيّنة تمر عبر نفس الأوزان دفعة واحدة. لا يتغير W ولا b، فعدد parameters ومعه حجم flash متطابق عند n = 1 وعند n = 64. ما يتضاعف مع n هو ذاكرة activations: عدد المخرجات المطلوب حفظها في RAM يصبح n ضعفاً. ولهذا السبب بالذات يعمل inference المضمّن عادةً عند n = 1.
An MCU keyword spotter uses an MLP 784 → 128 → 64 → 10. How many layers, how many parameters, how many activations per sample, and what does it cost in memory at int8?
نظام لاكتشاف الكلمات على MCU يستخدم MLP بالأبعاد 784 → 128 → 64 → 10. كم طبقة، وكم parameter، وكم activation لكل عيّنة، وما تكلفة الذاكرة عند int8؟
Layers: 4 columns of neurons -> 3 layers (2 hidden + 1 output)
FC1 784 -> 128 784 x 128 + 128 = 100,352 + 128 = 100,480
FC2 128 -> 64 128 x 64 + 64 = 8,192 + 64 = 8,256
FC3 64 -> 10 64 x 10 + 10 = 640 + 10 = 650
total = 109,386 parameters
Activations per sample (computed, excludes the input):
128 + 64 + 10 = 202 values
the input itself is another 784 values
Flash at int8 (1 byte each): 109,386 B ~ 107 KB
Flash at fp32 (4 bytes each): 437,544 B ~ 427 KB
Batch n = 32: parameters still 109,386
activations become 32 x 202 = 6,464
Notice where the parameters are: the first layer holds 100,480 of 109,386, about 92%. Wide inputs into FC layers dominate the parameter budget, which is the motivation for convolution on the next page.
لاحظ أين تتركز parameters: الطبقة الأولى وحدها فيها 100,480 من أصل 109,386، أي نحو 92%. المدخلات العريضة التي تدخل طبقات FC تستهلك معظم ميزانية parameters، وهذا هو الدافع إلى convolution في الصفحة التالية.
Counting the input as a layer. 5-4-3-2 is 3 layers and 2 hidden layers, not 4 and 3. The slide's own title says so.
عدّ المدخلات كطبقة. الشبكة 5-4-3-2 فيها 3 طبقات و2 طبقتان مخفيتان، وليست 4 و3. عنوان الشريحة نفسه يقول ذلك.
Forgetting biases. 5-4-3-2 without biases gives 38. The answer is 47. One bias per output neuron, every layer.
نسيان biases. الشبكة 5-4-3-2 بدون biases تعطي 38، والإجابة الصحيحة 47. bias واحد لكل neuron مخرج، في كل طبقة.
Transposing W. W is (co, ci), output first. The forward pass uses WT precisely because W is stored that way.
قلب أبعاد W. شكل W هو (co, ci)، المخرج أولاً. والتمرير الأمامي يستخدم WT لأن W مخزّن بهذا الشكل تحديداً.
Thinking batch size adds parameters. It multiplies activations only.
الظن بأن batch size يزيد parameters. هو يضاعف activations فقط.
Check yourself
اختبر نفسك
A fully-connected network has neuron columns 5-4-3-2 (input on the left). How many layers, how many hidden layers, and how many parameters including biases?
شبكة fully-connected أعمدتها 5-4-3-2 (المدخلات على اليسار). كم طبقة، وكم طبقة مخفية، وكم parameter مع biases؟
C. The input column is not a layer, so 3 layers, of which the last is the output and 2 are hidden. Parameters: (5 × 4 + 4) + (4 × 3 + 3) + (3 × 2 + 2) = 24 + 15 + 8 = 47. Answer D forgets the 9 biases. Answer A makes both mistakes at once.
C. عمود المدخلات ليس طبقة، إذن هناك 3 طبقات، آخرها طبقة الخرج واثنتان منها مخفيتان. المعاملات: (5 × 4 + 4) + (4 × 3 + 3) + (3 × 2 + 2) = 24 + 15 + 8 = 47. الإجابة D تنسى الانحيازات التسعة. والإجابة A تقع في الخطأين معاً.
An FC layer maps ci = 256 inputs to co = 10 outputs with batch size n = 8. What is the shape of W?
طبقة FC تحوّل ci = 256 مدخلاً إلى co = 10 مخرجات مع batch size n = 8. ما شكل W؟
B. W is (co, ci), output first, matching yi = Σj wij xj. Batch size never enters the weight shape, which rules out C. D is the shape of X. A is WT, the thing you multiply by, not the thing that is stored.
B. شكل W هو (co, ci)، الخرج أولاً، بما يطابق yi = Σj wij xj. حجم الدفعة لا يدخل أبداً في شكل الأوزان، وهذا يستبعد C. والخيار D هو شكل X. والخيار A هو WT، أي ما نضرب فيه، لا ما يُخزَّن.
The MLP on slide 7 is 5 → 3 → 2. How many parameters does it have?
الـ MLP في الشريحة 7 أبعاده 5 → 3 → 2. كم parameter فيه؟
A. First layer 5 × 3 + 3 = 18, second 3 × 2 + 2 = 8, total 26. B is weights only (15 + 6). C is the first layer alone, which is the slide 5 count.
A. الطبقة الأولى 5 × 3 + 3 = 18، والثانية 3 × 2 + 2 = 8، والمجموع 26. الخيار B هو الأوزان فقط (15 + 6). والخيار C هو الطبقة الأولى وحدها، وهو عدد الشريحة 5.
You raise the batch size of an FC layer from 1 to 16. Which statement is correct?
رفعت batch size لطبقة FC من 1 إلى 16. أي العبارات صحيحة؟
D. Batching adds rows to X and Y only (slide 6). W stays (co, ci) and b stays (co,), so flash is unaffected, while RAM for the inputs and outputs scales with n.
D. الدفعات (batching) تضيف صفوفاً إلى X وY فقط (الشريحة 6). يبقى W بشكل (co, ci) ويبقى b بشكل (co,)، فلا تتأثر ذاكرة flash، بينما تكبر ذاكرة RAM للمدخلات والمخرجات مع n.
Which pair of terms belongs to the same family on slide 4?
أي زوج من المصطلحات ينتمي إلى العائلة نفسها في الشريحة 4؟
C. The two families are synapses = weights = parameters (edges, stored, flash) and neurons = features = activations (nodes, computed per input, RAM). Every other option mixes one word from each family.
C. العائلتان هما synapses = weights = parameters (حواف، تُخزَّن، في flash) وneurons = features = activations (عقد، تُحسب لكل مدخل، في RAM). كل خيار آخر يخلط كلمة من كل عائلة.