Majid Al-RaimiNeurons, weights and the fully connected layerالعصبونات والأوزان والطبقة المتصلة كليًا

COE 592Lecture 02المحاضرة 02Part 01الجزء 01

Neurons, weights and the fully connected layerالعصبونات والأوزان والطبقة المتصلة كليًا

The vocabulary of neural networks and the simplest layer, where every output neuron sees every input neuron.مفردات الشبكات العصبية وأبسط طبقة، حيث يرى كل عصبون في المخرج كل عصبون في المدخل.

Conceptsالمفاهيم
5
Slidesالشرائح
1-7
Readingمدة القراءة
30 min30 دقيقة
Understoodما فهمته
0/5 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

Every TinyML decision you will make in this course, whether to prune a model, quantize it, or pick a smaller architecture for a Cortex-M board, starts from two counts that this part defines. Parameters are what a model stores. Activations are what it computes for each input. Until you can produce both numbers from a network diagram, no later result about efficiency will mean anything concrete.

The lecture opens by fixing vocabulary that the rest of the course, and most papers you will read, take for granted: neuron, synapse, weight, activation, feature, parameter, width, depth. It then introduces the simplest layer, the fully connected layer, and writes it as a matrix product with named tensor shapes. Exams ask for those shapes and for parameter counts. Your research project needs the same arithmetic to decide whether a model fits the flash and RAM of a device.

كل قرار في TinyML ستتخذه في هذا المقرر، سواء أكان تقليم النموذج (pruning) أم تكميمه (quantization) أم اختيار معمارية أصغر للوحة Cortex-M، يبدأ من عددين يعرّفهما هذا الجزء. الـ parameters هي ما يخزّنه النموذج. والـ activations هي ما يحسبه لكل مُدخل. وما لم تستطع استخراج هذين الرقمين من رسم الشبكة، فلن يكون لأي نتيجة لاحقة عن الكفاءة معنى ملموس.

تبدأ المحاضرة بتثبيت المصطلحات التي يفترضها بقية المقرر ومعظم الأوراق التي ستقرؤها: neuron وsynapse وweight وactivation وfeature وparameter وwidth وdepth. ثم تقدّم أبسط طبقة، وهي الطبقة كاملة الاتصال (fully connected layer)، وتكتبها على شكل ضرب مصفوفات مع أشكال (shapes) مسمّاة للـ tensors. الامتحانات تسأل عن هذه الأشكال وعن عدد الـ parameters. ومشروعك البحثي يحتاج الحساب نفسه ليقرر هل يتسع النموذج في ذاكرة flash وذاكرة RAM للجهاز.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Write a neuron as y = f(sum_i w_i x_i + b) and name what each symbol corresponds to in the biological picture.اكتب العصبون (neuron) على الصورة y = f(sum_i w_i x_i + b)، وسمِّ ما يقابل كل رمز فيها في الصورة البيولوجية.
  2. Use the two synonym families (synapses = weights = parameters; neurons = features = activations) and count layers without counting the input.استخدم عائلتي المترادفات (synapses = weights = parameters؛ neurons = features = activations)، وعُدّ الطبقات دون احتساب طبقة الإدخال.
  3. State the shapes X (n, c_i), W (c_o, c_i), b (c_o,) and Y (n, c_o) of a fully connected layer and write Y = X W^T + b.اذكر أشكال (shapes) الطبقة كاملة الاتصال (fully connected layer): X (n, c_i) وW (c_o, c_i) وb (c_o,) وY (n, c_o)، واكتب Y = X W^T + b.
  4. Count the parameters and per-sample activations of any MLP, and say which count the batch size touches.احسب عدد الـ parameters وعدد الـ activations لكل عيّنة (per sample) في أي MLP، وحدّد أيّ العددين يتأثر بحجم الدفعة (batch size).
  5. Map parameters to flash storage and activations to runtime RAM on an embedded target.اربط الـ parameters بذاكرة التخزين flash، والـ activations بذاكرة التشغيل RAM أثناء التنفيذ على جهاز مدمج (embedded target).

Take a concrete board. The STM32F746, an ARM Cortex-M7 microcontroller used throughout the TinyML literature, has 320 kB of SRAM and 1 MB of flash (Lin et al., MCUNet, section 1). A ResNet-50 carries about 25.6 million parameters (torchvision), which is roughly 100 MB of weights in single precision. It does not fit, and no clever compiler will change that. Whether any network fits comes down to two numbers, and the whole point of this lecture is to teach you how to produce them from a diagram.

خذ لوحة حقيقية. المتحكم الدقيق STM32F746، وهو من نوع ARM Cortex-M7 ويُستخدم على نطاق واسع في أدبيات TinyML، فيه 320 kB من SRAM و1 MB من flash (Lin et al., MCUNet، القسم 1). أما ResNet-50 فيحمل نحو 25.6 million parameter (torchvision)، أي قرابة 100 MB من الأوزان بالدقة الأحادية (single precision). إنه لا يتسع، ولن يغيّر أي مترجم ذكي (compiler) هذه الحقيقة. سواء اتسعت شبكة ما أم لا، فالأمر يعود إلى رقمين، والهدف كله من هذه المحاضرة أن تتعلم كيف تستخرجهما من الرسم.

Two microcontrollers from the MCUNet paperمتحكمان دقيقان من ورقة MCUNet

STM32F746
320 kB SRAM, 1 MB flash320 kB من SRAM، و1 MB من flash
STM32H743
512 kB SRAM, 2 MB flash512 kB من SRAM، و2 MB من flash

The two memories play different roles, and MCUNet states the split in one sentence: SRAM constrains the activation size, which is read and written at runtime, while flash constrains the model size, which is read only (Lin et al., MCUNet). So a model has a storage cost, the number of parameters it carries, and a runtime cost, the number of activations it must hold in memory while it computes. This part defines both words precisely and shows how to count them for the simplest layer. The parts that follow do the same for convolutions, pooling, normalization and attention.

تؤدي الذاكرتان دورين مختلفين، وتلخّص MCUNet هذا التقسيم في جملة واحدة: ذاكرة SRAM تقيّد حجم الـ activations، التي تُقرأ وتُكتب أثناء التشغيل، بينما تقيّد ذاكرة flash حجم النموذج، الذي يُقرأ فقط (Lin et al., MCUNet). إذن للنموذج كلفة تخزين، وهي عدد الـ parameters التي يحملها، وكلفة تشغيل، وهي عدد الـ activations التي يجب أن يحتفظ بها في الذاكرة أثناء الحساب. يعرّف هذا الجزء الكلمتين بدقة، ويبيّن كيف تُعدّان لأبسط طبقة. والأجزاء التالية تفعل الشيء نفسه مع الـ convolutions والـ pooling والـ normalization والـ attention.

The lecture's outline is a two-step plan. First fix the terminology: neuron, synapse, activation, feature, weight, parameter. Then learn the building blocks one at a time: fully connected, convolution, grouped and depthwise convolution, pooling, normalization, and the transformer block. This part covers the terminology and the first block. Everything later is a variation on the counting you learn here.

مخطط المحاضرة خطة من خطوتين. أولًا تثبيت المصطلحات: neuron وsynapse وactivation وfeature وweight وparameter. ثم تعلّم وحدات البناء واحدة تلو الأخرى: الطبقة كاملة الاتصال (fully connected)، والـ convolution، والـ grouped convolution والـ depthwise convolution، والـ pooling، والـ normalization، وكتلة الـ transformer. يغطي هذا الجزء المصطلحات والوحدة الأولى. وكل ما يأتي لاحقًا تنويع على طريقة العدّ التي تتعلمها هنا.

Start with a single unit and three numbers arriving at it: x0 = 1, x1 = 2 and x2 = 0.5. The unit does not treat them equally. It trusts the first a little, distrusts the second, and trusts the third a lot: w0 = 0.5, w1 = -1, w2 = 2. It also has a personal lean of b = 0.25. Multiply each input by its trust, add everything up, and the total is 0.5 - 2 + 1 + 0.25 = -0.25. One last step decides whether the unit speaks: a function f applied to that total.

ابدأ بوحدة واحدة تصل إليها ثلاثة أرقام: x0 = 1 وx1 = 2 وx2 = 0.5. الوحدة لا تعاملها بالتساوي. فهي تثق بالأول قليلًا، ولا تثق بالثاني، وتثق بالثالث كثيرًا: w0 = 0.5 وw1 = -1 وw2 = 2. ولها أيضًا ميل شخصي مقداره b = 0.25. اضرب كل مُدخل في درجة الثقة به، واجمع كل شيء، فيكون المجموع 0.5 - 2 + 1 + 0.25 = -0.25. وخطوة أخيرة تقرر هل «تتكلم» الوحدة أم لا: دالة f تُطبَّق على هذا المجموع.

Worked exampleمثال محلول

One neuron with three inputsعصبون واحد بثلاثة مدخلات

  1. Scale each input by its weightاضرب كل مُدخل في وزنه

    0.5 × 1 = 0.5, -1 × 2 = -2, 2 × 0.5 = 1.
    0.5 × 1 = 0.5، -1 × 2 = -2، 2 × 0.5 = 1.
  2. Sum and add the biasاجمع النواتج وأضف الـ bias

    0.5 - 2 + 1 + 0.25 = -0.25.
    0.5 - 2 + 1 + 0.25 = -0.25.
  3. Apply the activation functionطبّق دالة التنشيط

    With f the identity the output is -0.25. With f = ReLU, which returns max(0, z), the output is 0.
    إذا كانت f هي الدالة المحايدة (identity) فالمخرج -0.25. وإذا كانت f = ReLU، التي تُرجع max(0, z)، فالمخرج 0.
  4. The activation function decides whether the neuron firesدالة التنشيط هي التي تقرر هل ينطلق العصبون

    Same inputs, same weights, same bias: -0.25 or 0 depending only on f.
    المدخلات نفسها، والأوزان نفسها، والـ bias نفسه: النتيجة -0.25 أو 0 بحسب f وحدها.

That is the whole artificial Neuron. Jurafsky and Martin put it in one sentence: a neural unit takes a weighted sum of its inputs with one additional term called the bias, then applies a function to the result. In symbols, with i ranging over the inputs of neuron j:

هذا هو الـ neuron الاصطناعي بأكمله. يلخّصه Jurafsky وMartin في جملة واحدة: الوحدة العصبية تأخذ مجموعًا موزونًا لمدخلاتها مع حدّ إضافي واحد يسمى الـ bias، ثم تطبّق دالة على الناتج. وبالرموز، حيث يمرّ i على مدخلات العصبون j:

yj=f(∑iwixi+b)y_j = f\left(\sum_i w_i x_i + b\right)
One neuron: weighted sum, one bias, one squashing functionعصبون واحد: مجموع موزون، وانحياز واحد، ودالة ضغط واحدة

The slide draws this formula on top of a biological neuron, and the mapping is worth learning because the course reuses the biological words as technical terms. Sze, Chen, Yang and Emer, whose survey carries the same figure (adapted from Stanford CS231n), describe the key property of a Synapse: it scales the signal crossing it, and that scaling factor is the Weight. Each raw input x_i arrives along an input axon, is scaled to w_i x_i at the synapse, and travels down a dendrite into the cell body, which sums the products; the output axon carries the result out. They add that the input and output signals of a neuron are what the field calls activations.

ترسم الشريحة هذه المعادلة فوق عصبون بيولوجي، ويستحق هذا الربط أن تتعلمه لأن المقرر يعيد استخدام الكلمات البيولوجية مصطلحاتٍ تقنية. يصف Sze وChen وYang وEmer، الذين يتضمن مسحهم الشكل نفسه (مقتبسًا من Stanford CS231n)، الخاصية الأساسية للـ synapse: فهو يضرب الإشارة التي تعبره في معامل، وهذا المعامل هو الـ weight. كل مُدخل خام x_i يصل عبر محوار إدخال (input axon)، ويُضرب في الـ synapse ليصبح w_i x_i، ثم ينتقل عبر تغصّن (dendrite) إلى جسم الخلية (cell body) الذي يجمع النواتج؛ ويحمل محوار الإخراج (output axon) النتيجة إلى الخارج. ويضيفون أن إشارات الدخل والخرج للعصبون هي ما يسميه هذا المجال activations.

Three inputs cross their synapses, arrive as w_i x_i at the summing cell body, pass the activation function f, and leave along the output axonثلاثة مدخلات تعبر الـ synapses الخاصة بها، وتصل على هيئة w_i x_i إلى جسم الخلية الذي يجمعها، ثم تمرّ بدالة التنشيط f، وتخرج عبر محوار الإخراج
StructureالبنيةWhat it does biologicallyما تفعله بيولوجيًّاWhat it is in the formulaما تمثّله في المعادلة
Input axonمحوار الإدخال (input axon)Carries the raw signal x_i from the previous neuronيحمل الإشارة الخام x_i من العصبون السابقAn input feature, one entry of the vector xخاصية مُدخلة (input feature)، أي عنصر واحد من المتجه x
Synapseالمشبك (synapse)Scales the signal crossing itيضرب الإشارة التي تعبره في معاملThe weight w_i multiplying x_iالوزن w_i الذي يُضرب في x_i
Dendriteالتغصّن (dendrite)Carries the scaled signal w_i x_i into the cell bodyيحمل الإشارة المضروبة w_i x_i إلى جسم الخليةOne product term of the sumحدّ واحد من حدود المجموع، أي ناتج ضرب واحد
Cell bodyجسم الخلية (cell body)Accumulates the incoming signalsيجمع الإشارات الواردةThe sum of all w_i x_i plus one bias bمجموع كل w_i x_i مضافًا إليه bias واحد b
Axonمحوار الإخراج (axon)Fires when the total is large enoughينطلق عندما يكون المجموع كبيرًا بما يكفيThe activation function f and the output y_jدالة التنشيط f والمُخرج y_j
Biology to arithmetic, one row per structureمن البيولوجيا إلى الحساب، صف لكل بنية

Two details of the formula matter for counting parameters later. There is one weight per incoming edge, so a neuron with three inputs owns three weights. There is exactly one Bias per neuron, not one per edge, which is why the figure shows three w symbols and a single b. The activation function f is applied once, to the finished sum. Later in this lecture you will meet several choices for f (sigmoid, ReLU, ReLU6, swish); for now it is enough to know that it is usually non-linear and that it is what lets stacked neurons compute anything more interesting than a single straight-line function.

تفصيلتان في المعادلة مهمتان لعدّ الـ parameters لاحقًا. هناك وزن واحد لكل حافة داخلة، فالعصبون ذو المدخلات الثلاثة يملك ثلاثة أوزان. وهناك bias واحد بالضبط لكل عصبون، لا لكل حافة، ولهذا يُظهر الشكل ثلاثة رموز w وb واحدًا فقط. أما دالة التنشيط (activation function) f فتُطبَّق مرة واحدة على المجموع النهائي. ستقابل لاحقًا في هذه المحاضرة عدة خيارات لـ f (sigmoid وReLU وReLU6 وswish)؛ ويكفيك الآن أن تعرف أنها غالبًا غير خطية، وأنها ما يسمح للعصبونات المكدّسة بأن تحسب شيئًا أهم من دالة خطية واحدة.

Recall

Write the formula for one neuron and name which symbol is the synapse, which arrives on the input axon, what the dendrite carries, and which is the output axon.اكتب معادلة العصبون الواحد، وحدّد أي رمز هو الـ synapse، وأيّها يصل عبر محوار الإدخال (input axon)، وماذا يحمل التغصّن (dendrite)، وأيّها محوار الإخراج (output axon).

y_j = f(Σ_i w_i x_i + b). Each w_i is a synapse (a weight), each x_i arrives on an input axon (an activation) and is scaled at the synapse, the dendrite carries the product w_i x_i into the cell body, and y_j is the output axon after the activation function f. The bias b is one per neuron.
y_j = f(Σ_i w_i x_i + b). كل w_i هو synapse (أي weight)، وكل x_i يصل عبر محوار إدخال (وهو activation) ويُضرب عند الـ synapse، والتغصّن يحمل الناتج w_i x_i إلى جسم الخلية، وy_j هو محوار الإخراج بعد دالة التنشيط f. والـ bias b واحد لكل عصبون.

Now wire many neurons together. The network on the slide has a row of 5 inputs, then rows of 4, 3 and 2 neurons, with every neuron connected to every neuron in the row above. Count the edges band by band: 5 × 4 = 20, 4 × 3 = 12, 3 × 2 = 6, so 38 edges. Count the biases, one per computed neuron: 4 + 3 + 2 = 9. That is 47 numbers the network stores and 9 numbers it computes per input. Everything else in this concept is the vocabulary for those two counts.

الآن صِل عصبونات كثيرة معًا. الشبكة في الشريحة فيها صف من 5 مدخلات، ثم صفوف من 4 و3 و2 عصبونات، وكل عصبون متصل بكل عصبون في الصف الذي فوقه. عُدّ الحواف شريحةً شريحة: 5 × 4 = 20 و4 × 3 = 12 و3 × 2 = 6، أي 38 حافة. ثم عُدّ الـ biases، واحد لكل عصبون محسوب: 4 + 3 + 2 = 9. هذه 47 قيمة تخزّنها الشبكة، و9 قيم تحسبها لكل مُدخل. وكل ما تبقى في هذا المفهوم هو المصطلحات الخاصة بهذين العددين.

The 5-4-3-2 ladder: 20, 12 and 6 edges draw in as weights, then the 9 computed nodes fill as activations, while the 5 input nodes stay grayالسلّم 5-4-3-2: تظهر 20 ثم 12 ثم 6 حواف على أنها weights، ثم تمتلئ العقد التسع المحسوبة على أنها activations، بينما تبقى عقد الإدخال الخمس رمادية

Two synonym familiesعائلتا المترادفات

The slide attaches two labels to the diagram, and each is a family of three words the course uses interchangeably. The edges are synapses, weights or parameters. The nodes are neurons, features or activations. Sze et al. state the convention directly: the outputs of the neurons are often referred to as activations, and the synapses are often referred to as weights. Which word a paper picks depends on what it is emphasising. "Synapse" stresses the biology, "weight" the arithmetic, "parameter" the fact that it is learned and stored. "Feature" stresses what a node represents, "activation" the fact that it is a computed value that must be held in memory.

تُلحق الشريحة بالرسم تسميتين، وكل تسمية عائلة من ثلاث كلمات يستخدمها المقرر بالتبادل. الحواف هي synapses أو weights أو parameters. والعقد هي neurons أو features أو activations. يذكر Sze et al. هذا العُرف صراحة: مخرجات العصبونات كثيرًا ما تسمى activations، والـ synapses كثيرًا ما تسمى weights. والكلمة التي تختارها الورقة تعتمد على ما تريد إبرازه. فكلمة «synapse» تبرز البيولوجيا، و«weight» تبرز الحساب، و«parameter» تبرز أنه يُتعلَّم ويُخزَّن. أما «feature» فتبرز ما تمثله العقدة، و«activation» تبرز أنها قيمة محسوبة يجب الاحتفاظ بها في الذاكرة.

LayerالطبقةWeightsالأوزان (weights)Biasesالـ biasesParametersالـ parametersNeuronsالعصبونات
Layer 0, hidden (5 to 4)الطبقة 0، مخفية (من 5 إلى 4)5 × 4 = 205 × 4 = 2044242444
Layer 1, hidden (4 to 3)الطبقة 1، مخفية (من 4 إلى 3)4 × 3 = 124 × 3 = 1233151533
Layer 2, output (3 to 2)الطبقة 2، إخراج (من 3 إلى 2)3 × 2 = 63 × 2 = 6228822
Totalالمجموع383899474799
Parameters of the slide 4 network, counted layer by layerالـ parameters في شبكة الشريحة 4، معدودةً طبقةً طبقة

CS231n gives a second example you can use to check your method: a network with layers of sizes 3, 4, 4 and 1 has 4 + 4 + 1 = 9 neurons, 3 × 4 + 4 × 4 + 4 × 1 = 32 weights and 9 biases, for 41 learnable parameters. The recipe is always the same: multiply adjacent layer sizes for weights, add each non-input layer size for biases.

يقدّم CS231n مثالًا ثانيًا تتحقق به من طريقتك: شبكة بطبقات أحجامها 3 و4 و4 و1 فيها 4 + 4 + 1 = 9 عصبونات، و3 × 4 + 4 × 4 + 4 × 1 = 32 وزنًا، و9 biases، أي 41 parameter قابلًا للتعلّم. والوصفة واحدة دائمًا: اضرب أحجام الطبقات المتجاورة لتحصل على الأوزان، واجمع حجم كل طبقة غير طبقة الإدخال لتحصل على الـ biases.

Counting layers, and why the input does not countعدّ الطبقات، ولماذا لا تُحتسب طبقة الإدخال

The slide calls this a 3-layer network with 2 hidden layers, and both numbers follow one convention: the input row is not a layer. CS231n states it plainly: when we say N-layer neural network, we do not count the input layer, so a single-layer network has no hidden layers. Jurafsky and Martin say the same in a figure caption. The reason is that the inputs compute nothing. They own no weights and no biases; they are simply the numbers handed in. The three computed rows are the layers, the two blue rows in the middle are the hidden layers, and the gray bottom row is the output layer, counted but not hidden.

تصف الشريحة هذه الشبكة بأنها شبكة من 3 طبقات فيها 2 طبقتان مخفيتان، والرقمان يتبعان عُرفًا واحدًا: صف الإدخال ليس طبقة. يقولها CS231n بوضوح: عندما نقول شبكة عصبية من N طبقة فإننا لا نعدّ طبقة الإدخال، لذا فالشبكة ذات الطبقة الواحدة لا تحتوي طبقات مخفية. ويقول Jurafsky وMartin الشيء نفسه في تعليق أحد الأشكال. والسبب أن المدخلات لا تحسب شيئًا. فهي لا تملك أوزانًا ولا biases؛ إنها مجرد أرقام تُسلَّم للشبكة. الصفوف الثلاثة المحسوبة هي الطبقات، والصفان الأزرقان في الوسط هما الطبقتان المخفيتان (hidden layers)، والصف الرمادي في الأسفل هو طبقة الإخراج، تُعدّ لكنها ليست مخفية.

Width and depthالعرض والعمق

The sentence at the bottom of the slide is quoted from Goodfellow, Bengio and Courville: the dimensionality of the hidden layers determines the width of the model. On the same pages they define the other axis: a network is a chain of functions, and the overall length of the chain gives its depth. So the slide network has width 4 at its widest hidden layer and depth 3. Making a network wider adds neurons to a row, which grows both parameters and activations. Making it deeper adds rows, which also grows both, but lets features be composed out of earlier features.

الجملة في أسفل الشريحة مقتبسة من Goodfellow وBengio وCourville: أبعاد الطبقات المخفية (dimensionality) هي التي تحدد عرض (width) النموذج. وفي الصفحات نفسها يعرّفون المحور الآخر: الشبكة سلسلة من الدوال، وطول السلسلة الكلي يعطي عمقها (depth). إذن شبكة الشريحة عرضها 4 عند أعرض طبقة مخفية فيها، وعمقها 3. جعل الشبكة أعرض يضيف عصبونات إلى الصف، فيزيد الـ parameters والـ activations معًا. وجعلها أعمق يضيف صفوفًا، فيزيد الاثنين أيضًا، لكنه يسمح ببناء الـ features من features سابقة.

Quick checkاختبار سريع

How many learnable parameters does the 5-4-3-2 network on slide 4 contain in total?كم عدد الـ parameters القابلة للتعلّم في الشبكة 5-4-3-2 في الشريحة 4 إجمالًا؟

Quick checkاختبار سريع

In the vocabulary of this lecture, what does the width of a model refer to?بمصطلحات هذه المحاضرة، إلامَ يشير عرض النموذج (width)؟

Recall

In the phrase "3-layer network with 2 hidden layers", which layer is counted but not hidden, and which row is not counted at all?في عبارة «شبكة من 3 طبقات فيها طبقتان مخفيتان»، أي طبقة تُعدّ لكنها ليست مخفية، وأي صف لا يُعدّ إطلاقًا؟

The output layer is counted but not hidden. The input row is not counted at all, because it computes nothing and owns no parameters.
طبقة الإخراج تُعدّ لكنها ليست مخفية. وصف الإدخال لا يُعدّ إطلاقًا، لأنه لا يحسب شيئًا ولا يملك أي parameters.

Recall

What do width and depth of a model mean?ما معنى عرض النموذج (width) وعمقه (depth)؟

Width is the dimensionality (number of neurons) of the hidden layers. Depth is the number of layers in the chain, not counting the input.
العرض هو أبعاد الطبقات المخفية (عدد العصبونات فيها). والعمق هو عدد الطبقات في السلسلة، دون احتساب طبقة الإدخال.

Take one band of the ladder and give it names: c_i = 5 inputs x0 to x4 and c_o = 3 outputs y0 to y2, every output connected to every input. Write out the first output by hand: y0 = w00 x0 + w01 x1 + w02 x2 + w03 x3 + w04 x4 + b0. Do the same for y1 and y2. That is 15 products and 3 biases, 18 parameters. Now stack the three rows of weights on top of each other and you have a 3 × 5 table. That table is W, and the three equations collapse into one matrix product.

خذ شريحة واحدة من السلّم وسمِّ عناصرها: c_i = 5 مدخلات من x0 إلى x4، وc_o = 3 مخرجات من y0 إلى y2، وكل مُخرج متصل بكل مُدخل. اكتب المُخرج الأول يدويًا: y0 = w00 x0 + w01 x1 + w02 x2 + w03 x3 + w04 x4 + b0. وافعل الشيء نفسه مع y1 وy2. هذه 15 عملية ضرب و3 biases، أي 18 parameters. الآن ضع صفوف weights الثلاثة بعضها فوق بعض فتحصل على جدول 3 × 5. هذا الجدول هو W، والمعادلات الثلاث تنطوي في عملية ضرب مصفوفات واحدة.

This is the fully connected layer, also called a linear layer. Jurafsky and Martin define fully connected exactly this way: each unit in a layer takes as input the outputs of all the units in the previous layer, with a link between every pair of units in adjacent layers. The word Channel is the lecture's general name for a feature dimension, so c_i and c_o count input and output channels; for a fully connected layer a channel is simply one neuron.

هذه هي الطبقة كاملة الاتصال (fully connected layer)، وتسمى أيضًا الطبقة الخطية (linear layer). يعرّف Jurafsky وMartin الاتصال الكامل بهذا الشكل تمامًا: كل وحدة في الطبقة تأخذ مدخلاتها من مخرجات جميع الوحدات في الطبقة السابقة، مع وصلة بين كل زوج من الوحدات في الطبقتين المتجاورتين. وكلمة channel هي الاسم العام في المحاضرة لبُعد الـ feature، لذا فإن c_i وc_o يعدّان قنوات الإدخال والإخراج؛ وفي الطبقة كاملة الاتصال تكون القناة ببساطة عصبونًا واحدًا.

yi=∑jwij xj+biy_i = \sum_j w_{ij}\, x_j + b_i
Output i sums over all inputs j. The first index of w is the output, the second the inputالمخرج i يجمع على كل المدخلات j. الفهرس الأول لـ w هو المخرج والثاني هو المدخل
Y=X WT+b\mathbf{Y} = \mathbf{X}\,\mathbf{W}^{T} + \mathbf{b}
The same layer as one matrix productالطبقة نفسها بوصفها حاصل ضرب مصفوفة واحد

Tensor shapes of a fully connected layerأشكال الـ tensors في الطبقة كاملة الاتصال

Input features Xخصائص الإدخال X
(1, c_i) for one sample, (n, c_i) for a batch(1, c_i) لعيّنة واحدة، و(n, c_i) لدفعة
Output features Yخصائص الإخراج Y
(1, c_o) for one sample, (n, c_o) for a batch(1, c_o) لعيّنة واحدة، و(n, c_o) لدفعة
Weights Wالأوزان W
(c_o, c_i)(c_o, c_i)
Bias bالـ bias b
(c_o,)(c_o,)

The transpose in Y = X Wᵀ is not decoration. W is stored with one row per output neuron, shape (c_o, c_i), so that row i holds the weights of y_i. To multiply a row vector x of length c_i by it, the matrix has to be flipped to (c_i, c_o). PyTorch uses precisely this convention: nn.Linear computes y = x Aᵀ + b with a weight of shape (out_features, in_features) and a bias of shape (out_features). Jurafsky and Martin write the same matrix as W ∈ R^(n1 × n0), with element W_ji the weight from input i to hidden unit j. Output index first, input index second, in every source.

المنقول (transpose) في Y = X Wᵀ ليس للزينة. تُخزَّن W بصف واحد لكل عصبون إخراج، بالشكل (c_o, c_i)، بحيث يحمل الصف i أوزان y_i. ولضرب متجه صفي x طوله c_i بها، يجب قلب المصفوفة إلى (c_i, c_o). ويستخدم PyTorch هذا العُرف بالضبط: nn.Linear تحسب y = x Aᵀ + b بوزن شكله (out_features, in_features) وbias شكله (out_features). ويكتب Jurafsky وMartin المصفوفة نفسها على الصورة W ∈ R^(n1 × n0)، حيث العنصر W_ji هو الوزن من المُدخل i إلى الوحدة المخفية j. فهرس الإخراج أولًا، وفهرس الإدخال ثانيًا، في كل المصادر.

Batching: more rows, same weightsالتجميع في دفعات: صفوف أكثر، والأوزان نفسها

Nothing in the product cares whether X has one row. Put n samples in as n rows and the same multiplication produces n rows of outputs. That is all the batch size is: a leading dimension on X and Y. PyTorch describes the input as (*, H_in), where the star means any number of leading dimensions, and the output as (*, H_out); the weight stays (H_out, H_in) no matter what the star is. That is the fact to hold on to: W and b never change with n.

لا شيء في عملية الضرب يهتم بأن تكون X صفًا واحدًا. أدخل n عيّنات على هيئة n صفوف، فتنتج عملية الضرب نفسها n صفوف من المخرجات. وهذا كل ما يعنيه حجم الدفعة (batch size): بُعد أول إضافي في X وY. يصف PyTorch المُدخل بأنه (*, H_in)، حيث تعني النجمة أي عدد من الأبعاد الأولى، والمُخرج بأنه (*, H_out)؛ ويبقى الوزن (H_out, H_in) مهما كانت النجمة. هذه هي الحقيقة التي يجب أن تتمسك بها: W وb لا يتغيران أبدًا مع n.

One row of X meets one column of W^T to light one cell of Y. Extra samples then add rows to X and Y while W^T stays exactly as it wasصف واحد من X يلتقي بعمود واحد من W^T فيُضيء خلية واحدة من Y. ثم تضيف العيّنات الإضافية صفوفًا إلى X وY بينما تبقى W^T كما هي تمامًا

Worked exampleمثال محلول

The slide 5 layer with a batch of 32طبقة الشريحة 5 مع دفعة حجمها 32

  1. Shapesالأشكال

    X (32, 5), W (3, 5), b (3,), Y (32, 3).
    X (32, 5)، W (3, 5)، b (3,)، Y (32, 3).
  2. Values heldالقيم المحفوظة

    X holds 32 × 5 = 160 numbers and Y holds 32 × 3 = 96. Both are activations.
    تحمل X عدد 32 × 5 = 160 قيمة، وتحمل Y عدد 32 × 3 = 96 قيمة. وكلتاهما activations.
  3. Parametersالـ parameters

    Still 15 weights and 3 biases, 18 in total, identical to the single-sample case.
    ما زالت 15 وزنًا و3 biases، أي 18 في المجموع، مطابقة تمامًا لحالة العيّنة الواحدة.
  4. Workالحِمل الحسابي

    Each output is 5 multiply-accumulates, so 15 per sample and 32 × 15 = 480 for the batch.
    كل مُخرج يحتاج 5 عمليات ضرب وتجميع (multiply-accumulates)، أي 15 لكل عيّنة و32 × 15 = 480 للدفعة كلها.
  5. Batch size scales activations and work, never parametersحجم الدفعة يضاعف الـ activations والحِمل الحسابي، ولا يمسّ الـ parameters أبدًا

    Activations went from 8 to 256 values; parameters stayed at 18.
    ارتفعت الـ activations من 8 قيم إلى 256 قيمة؛ وبقيت الـ parameters عند 18.
SimulatorمحاكيBuild a fully connected layerابنِ طبقة كاملة الاتصال
Presets
5
3
1
off
Value precision
x₀x₁x₂x₃x₄y₀y₁y₂w₀₀w₂₄
X (1 × 5)×W1ᵀ (5 × 3)+b1 (3,)=Y (1 × 3)

Move a slider to see which counts respond.حرّك أحد المنزلقات لترى أي الأعداد تستجيب.

FC1 weights, W (3, 5)أوزان FC1، W (3, 5)
3 × 515
Biases, one per output neuronالانحيازات، واحد لكل عصبون إخراج
33
Parameters, stored in flashالمعاملات، تُخزَّن في ذاكرة flash
15 + 318
Activations per batch, live in RAMالتنشيطات لكل دفعة، تعيش في RAM
1 × 88
Multiply-accumulates per batchعمليات الضرب والتجميع (MAC) لكل دفعة
1 × 1515
Parameter storageمساحة تخزين المعاملات
18 × 4 B72 B
Activation memoryذاكرة التنشيطات
8 × 4 B32 B
Reference: 784 to 128المرجع: من 784 إلى 128100,480parameters784 × 128 + 128, an MNIST-sized first layer784 × 128 + 128، طبقة أولى بحجم MNIST
Same layer in fp32الطبقة نفسها بدقة fp32402 kBexceeds 320 kB of SRAM, fits 1 MB of flashتتجاوز 320 kB من SRAM، وتتسع في 1 MB من flash
Same layer in int8الطبقة نفسها بدقة int8100 kBone byte per weight, four times smallerبايت واحد لكل وزن، أصغر بأربع مرات

The highlighted edges carry w₀₀ and, once the layer has more than one edge, the last weight, indexed (output, input) as in y_i = Σ_j w_ij x_j + b_i. Parameters count every edge plus one bias per output neuron; activations count every node value for every sample in the batch, inputs included.الحواف المظللة تحمل w₀₀، ومتى كان في الطبقة أكثر من حافة فهي تحمل أيضًا الوزن الأخير، مفهرسًا بالترتيب (إخراج، إدخال) كما في y_i = Σ_j w_ij x_j + b_i. تُعدّ المعاملات كل حافة إضافةً إلى انحياز واحد لكل عصبون إخراج، وتُعدّ التنشيطات كل قيمة عقدة لكل عينة في الدفعة، بما في ذلك المدخلات.

What the two counts cost on a microcontrollerكم يكلّف العددان على متحكم دقيق

Scale the same arithmetic to a realistic first layer, one that takes a flattened 28 × 28 MNIST digit (784 values) to 128 hidden units. The table separates what the layer stores from what it computes, and shows which memory of the STM32F746 each column lands in.

طبّق الحساب نفسه على طبقة أولى واقعية، تأخذ رقمًا من MNIST بحجم 28 × 28 بعد تسطيحه (784 قيمة) وتنقله إلى 128 وحدة مخفية. يفصل الجدول بين ما تخزّنه الطبقة وما تحسبه، ويبيّن في أي ذاكرة من ذاكرتي STM32F746 يقع كل عمود.

QuantityالكميةBatch n = 1دفعة n = 1Batch n = 8دفعة n = 8Memoryالذاكرة
Weightsالأوزان (weights)784 × 128 = 100,352784 × 128 = 100,352sameكما هوflashflash
Biasesالـ biases128128sameكما هوflashflash
Parametersالـ parameters100,480100,480sameكما هوflashflash
Parameter bytes, fp32بايتات الـ parameters بصيغة fp32401,920 B (about 392 KiB)401,920 B (نحو 392 KiB)sameكما هوflashflash
Parameter bytes, int8بايتات الـ parameters بصيغة int8100,480 B100,480 Bsameكما هوflashflash
Activationsالـ activations784 + 128 = 912 values784 + 128 = 912 قيمة8 × 912 = 7,296 values8 × 912 = 7,296 قيمةSRAMSRAM
Flash versus RAM for a 784 to 128 fully connected layerذاكرة flash مقابل RAM لطبقة كاملة الاتصال من 784 إلى 128

The 401,920 bytes of fp32 weights would not fit the board's 320 kB of SRAM but sit comfortably in its 1 MB of flash, which is exactly why weights live in flash. Storing them as 8-bit integers cuts the same column by four, the first hint of why quantization matters later. Only the activation row moved when the batch grew, and it moved linearly.

الـ 401,920 بايت من أوزان fp32 لن تتسع في 320 kB من SRAM في اللوحة، لكنها تتسع بارتياح في 1 MB من flash، وهذا بالضبط سبب وضع الأوزان في flash. وتخزينها أعدادًا صحيحة بعرض 8-bit يقسم العمود نفسه على أربعة، وهذه أول إشارة إلى سبب أهمية الـ quantization لاحقًا. ولم يتحرك عند زيادة الدفعة إلا صف الـ activations، وتحرك خطيًّا.

Quick checkاختبار سريع

A fully connected layer maps 5 input features to 3 output features. In the course's convention, what is the shape of its weight tensor W?طبقة كاملة الاتصال تنقل 5 خصائص إدخال إلى 3 خصائص إخراج. بحسب عُرف المقرر، ما شكل tensor الأوزان W فيها؟

Quick checkاختبار سريع

The batch size of an FC layer with 5 inputs and 3 outputs grows from 1 to 32. Which count changes?يزداد حجم الدفعة لطبقة FC فيها 5 مدخلات و3 مخرجات من 1 إلى 32. أي عدد يتغير؟

Recall

Write the shapes of X, W, b and Y for a batched FC layer with n samples, c_i inputs and c_o outputs, and the formula that joins them.اكتب أشكال X وW وb وY لطبقة FC تعمل على دفعة من n عيّنات، فيها c_i مُدخلًا وc_o مُخرجًا، والمعادلة التي تربطها.

X (n, c_i), W (c_o, c_i), b (c_o,), Y (n, c_o), computed as Y = X Wᵀ + b.
X (n, c_i)، W (c_o, c_i)، b (c_o,)، Y (n, c_o)، وتُحسب على الصورة Y = X Wᵀ + b.

Recall

Which quantity changes when the batch size doubles, and which microcontroller memory does each quantity map to?أي كمية تتغير عندما يتضاعف حجم الدفعة، وإلى أي ذاكرة في المتحكم الدقيق تنتمي كل كمية؟

Activations double, and they live in SRAM because they are read and written at runtime. Parameters are unchanged, and they live in flash because they are read only.
تتضاعف الـ activations، وتقيم في SRAM لأنها تُقرأ وتُكتب أثناء التشغيل. أما الـ parameters فلا تتغير، وتقيم في flash لأنها للقراءة فقط.

Put a second fully connected layer after the first. The slide takes the 5 to 3 layer and feeds its three outputs into a 3 to 2 layer that produces z0 and z1. The new layer has W2 of shape (2, 3), six weights, and b2 with two biases: 8 more parameters, 26 in total. Per sample it computes two more activations, so 5 computed values, 10 if you include the inputs held in memory.

ضع طبقة كاملة الاتصال (fully connected layer) ثانية بعد الأولى. تأخذ الشريحة الطبقة من 5 إلى 3 وتمرّر مخرجاتها الثلاثة إلى طبقة من 3 إلى 2 تُنتج z0 وz1. الطبقة الجديدة لها W2 بالشكل (2, 3)، أي ستة أوزان، وb2 فيه bias اثنان: 8 parameters إضافية، و26 في المجموع. ولكل عيّنة تحسب قيمتين إضافيتين من الـ activations، فيكون المجموع 5 قيم محسوبة، أو 10 إذا احتسبت المدخلات المحفوظة في الذاكرة.

A chain of fully connected layers is a multilayer perceptron, or MLP. Goodfellow, Bengio and Courville write the chain as f(x) = f⁽³⁾(f⁽²⁾(f⁽¹⁾(x))), call f⁽¹⁾ the first layer and f⁽²⁾ the second, and define depth as the length of that chain. Jurafsky and Martin add a caution about the name: modern multilayer networks are called perceptrons for historical reasons only, since their units are not perceptrons in the strict sense. Use the name, but do not read anything into it.

سلسلة الطبقات كاملة الاتصال هي بيرسبترون متعدد الطبقات (multilayer perceptron)، أو MLP. يكتب Goodfellow وBengio وCourville السلسلة على الصورة f(x) = f⁽³⁾(f⁽²⁾(f⁽¹⁾(x)))، ويسمّون f⁽¹⁾ الطبقة الأولى وf⁽²⁾ الثانية، ويعرّفون العمق (depth) بأنه طول هذه السلسلة. ويضيف Jurafsky وMartin تحذيرًا بشأن الاسم: الشبكات الحديثة متعددة الطبقات تسمى perceptrons لأسباب تاريخية فقط، إذ إن وحداتها ليست perceptrons بالمعنى الدقيق. استخدم الاسم، لكن لا تستنتج منه شيئًا.

Y1=XW1T+b1,Z=f(Y1) W2T+b2\mathbf{Y}_1 = \mathbf{X}\mathbf{W}_1^{T} + \mathbf{b}_1,\qquad \mathbf{Z} = f(\mathbf{Y}_1)\,\mathbf{W}_2^{T} + \mathbf{b}_2
Two layers chained. W1 is (3, 5), W2 is (2, 3); with a batch, X is (n, 5), Y1 is (n, 3) and Z is (n, 2)طبقتان متتاليتان. W1 أبعادها (3, 5) وW2 أبعادها (2, 3)؛ ومع دفعة تكون X بأبعاد (n, 5) وY1 بأبعاد (n, 3) وZ بأبعاد (n, 2)

The shapes must click together: the output channels of layer k are the input channels of layer k + 1. Here c_o = 3 of the first layer becomes c_i = 3 of the second, and the inner dimension of the second product matches. The batch dimension n rides through unchanged, just as it did in one layer.

يجب أن تتطابق الأشكال وتتعشّق: قنوات الإخراج في الطبقة k هي قنوات الإدخال في الطبقة k + 1. هنا يصبح c_o = 3 في الطبقة الأولى هو c_i = 3 في الثانية، فيتطابق البُعد الداخلي لعملية الضرب الثانية. وبُعد الدفعة (batch dimension) n يمرّ عبرها دون تغيير، تمامًا كما في الطبقة الواحدة.

LayerالطبقةW shapeشكل WWeightsالأوزان (weights)Biasesالـ biasesParametersالـ parametersActivations per sampleالـ activations لكل عيّنة
FC1 (5 to 3)FC1 (من 5 إلى 3)(3, 5)(3, 5)151533181833
FC2 (3 to 2)FC2 (من 3 إلى 2)(2, 3)(2, 3)66228822
Totalالمجموع21215526265 computed, 10 with inputs5 محسوبة، و10 مع المدخلات
Parameter and activation budget of the slide 7 MLPميزانية الـ parameters والـ activations في الـ MLP الخاص بالشريحة 7

Once you see the MLP as a chain, counting any network becomes mechanical, and this is the skill the rest of the course leans on. For each fully connected layer the parameters are c_o × c_i + c_o; sum over the layers. The activations per sample are the sum of the layer output sizes, plus the input if you are budgeting memory for it. Multiply activations by n for a batch. Never multiply parameters by n.

ما إن ترى الـ MLP على أنه سلسلة، يصبح عدّ أي شبكة عملًا آليًّا، وهذه هي المهارة التي يعتمد عليها بقية المقرر. لكل طبقة كاملة الاتصال تساوي الـ parameters c_o × c_i + c_o؛ ثم اجمع على كل الطبقات. والـ activations لكل عيّنة تساوي مجموع أحجام مخرجات الطبقات، مضافًا إليه المُدخل إذا كنت تحسب له ذاكرة. اضرب الـ activations في n للدفعة. ولا تضرب الـ parameters في n أبدًا.

Quick checkاختبار سريع

A 5 to 3 fully connected layer is followed by a 3 to 2 layer. How many learnable parameters does the pair contain?طبقة كاملة الاتصال من 5 إلى 3 تتبعها طبقة من 3 إلى 2. كم parameter قابلًا للتعلّم في الطبقتين معًا؟

Recall

How many parameters does the slide 7 MLP (5 to 3 to 2) have, and how many activations per sample does it compute?كم parameter في الـ MLP الخاص بالشريحة 7 (من 5 إلى 3 إلى 2)، وكم activation يحسب لكل عيّنة؟

FC1 has 15 + 3 = 18, FC2 has 6 + 2 = 8, total 26 parameters. It computes 3 + 2 = 5 activations per sample, 10 if the 5 inputs are counted.
في FC1 عدد 15 + 3 = 18، وفي FC2 عدد 6 + 2 = 8، والمجموع 26 parameter. ويحسب 3 + 2 = 5 قيم activations لكل عيّنة، أو 10 إذا احتُسبت المدخلات الـ 5.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • A neuron computes a weighted sum of its inputs plus one bias, then applies an activation function f.يحسب العصبون مجموعًا موزونًا (weighted sum) لمدخلاته مضافًا إليه bias واحد، ثم يطبّق دالة التنشيط (activation function) f.
  • Edges are synapses, weights or parameters; nodes are neurons, features or activations.الحواف (edges) هي synapses أو weights أو parameters؛ والعُقد (nodes) هي neurons أو features أو activations.
  • Layers are counted without the input; 5-4-3-2 is a 3-layer network with 2 hidden layers and 47 parameters.تُعدّ الطبقات دون طبقة الإدخال؛ فالشبكة 5-4-3-2 شبكة من 3 طبقات، منها طبقتان مخفيتان (hidden layers)، وفيها 47 parameter.
  • Width is the size of the hidden layers, depth is the number of layers in the chain.العرض (width) هو حجم الطبقات المخفية، والعمق (depth) هو عدد الطبقات في السلسلة.
  • An FC layer has W of shape (c_o, c_i) and b of shape (c_o,), and computes Y = X W^T + b.الطبقة FC لها W بالشكل (c_o, c_i) وb بالشكل (c_o,)، وتحسب Y = X W^T + b.
  • Batching adds rows to X and Y, giving (n, c_i) and (n, c_o); it never changes W or b.التجميع في دفعات (batching) يضيف صفوفًا إلى X وY فيصبح شكلاهما (n, c_i) و(n, c_o)؛ ولا يغيّر W ولا b أبدًا.
  • Parameters per FC layer = c_o x c_i + c_o; the slide 5 layer has 18, the slide 7 MLP has 26.عدد الـ parameters في كل طبقة FC يساوي c_o x c_i + c_o؛ طبقة الشريحة 5 فيها 18، والـ MLP في الشريحة 7 فيها 26.
  • Parameters determine flash (model size); activations determine SRAM (runtime memory).الـ parameters تحدد ذاكرة flash (حجم النموذج)؛ والـ activations تحدد ذاكرة SRAM (ذاكرة التشغيل).

Sourcesالمصادر