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 دقيقة
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في نهاية هذا الجزء ستستطيع
- 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)، وسمِّ ما يقابل كل رمز فيها في الصورة البيولوجية.
- Use the two synonym families (synapses = weights = parameters; neurons = features = activations) and count layers without counting the input.استخدم عائلتي المترادفات (synapses = weights = parameters؛ neurons = features = activations)، وعُدّ الطبقات دون احتساب طبقة الإدخال.
- 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.
- 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).
- 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عصبون واحد بثلاثة مدخلات
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.Sum and add the biasاجمع النواتج وأضف الـ bias
0.5 - 2 + 1 + 0.25 = -0.25.0.5 - 2 + 1 + 0.25 = -0.25.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.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:
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.
| 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 |
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).
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 قيم تحسبها لكل مُدخل. وكل ما تبقى في هذا المفهوم هو المصطلحات الخاصة بهذين العددين.
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الـ biases | Parametersالـ parameters | Neuronsالعصبونات |
|---|---|---|---|---|
| Layer 0, hidden (5 to 4)الطبقة 0، مخفية (من 5 إلى 4) | 5 × 4 = 205 × 4 = 20 | 44 | 2424 | 44 |
| Layer 1, hidden (4 to 3)الطبقة 1، مخفية (من 4 إلى 3) | 4 × 3 = 124 × 3 = 12 | 33 | 1515 | 33 |
| Layer 2, output (3 to 2)الطبقة 2، إخراج (من 3 إلى 2) | 3 × 2 = 63 × 2 = 6 | 22 | 88 | 22 |
| Totalالمجموع | 3838 | 99 | 4747 | 99 |
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 طبقات فيها طبقتان مخفيتان»، أي طبقة تُعدّ لكنها ليست مخفية، وأي صف لا يُعدّ إطلاقًا؟
Recall
What do width and depth of a model mean?ما معنى عرض النموذج (width) وعمقه (depth)؟
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 يعدّان قنوات الإدخال والإخراج؛ وفي الطبقة كاملة الاتصال تكون القناة ببساطة عصبونًا واحدًا.
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.
Worked exampleمثال محلول
The slide 5 layer with a batch of 32طبقة الشريحة 5 مع دفعة حجمها 32
Shapesالأشكال
X (32, 5), W (3, 5), b (3,), Y (32, 3).X (32, 5)، W (3, 5)، b (3,)، Y (32, 3).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.Parametersالـ parameters
Still 15 weights and 3 biases, 18 in total, identical to the single-sample case.ما زالت 15 وزنًا و3 biases، أي 18 في المجموع، مطابقة تمامًا لحالة العيّنة الواحدة.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 للدفعة كلها.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.
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
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 = 1 | Batch n = 8دفعة n = 8 | Memoryالذاكرة |
|---|---|---|---|
| Weightsالأوزان (weights) | 784 × 128 = 100,352784 × 128 = 100,352 | sameكما هو | flashflash |
| Biasesالـ biases | 128128 | sameكما هو | flashflash |
| Parametersالـ parameters | 100,480100,480 | sameكما هو | flashflash |
| Parameter bytes, fp32بايتات الـ parameters بصيغة fp32 | 401,920 B (about 392 KiB)401,920 B (نحو 392 KiB) | sameكما هو | flashflash |
| Parameter bytes, int8بايتات الـ parameters بصيغة int8 | 100,480 B100,480 B | sameكما هو | flashflash |
| Activationsالـ activations | 784 + 128 = 912 values784 + 128 = 912 قيمة | 8 × 912 = 7,296 values8 × 912 = 7,296 قيمة | SRAMSRAM |
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 مُخرجًا، والمعادلة التي تربطها.
Recall
Which quantity changes when the batch size doubles, and which microcontroller memory does each quantity map to?أي كمية تتغير عندما يتضاعف حجم الدفعة، وإلى أي ذاكرة في المتحكم الدقيق تنتمي كل كمية؟
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 بالمعنى الدقيق. استخدم الاسم، لكن لا تستنتج منه شيئًا.
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شكل W | Weightsالأوزان (weights) | Biasesالـ biases | Parametersالـ parameters | Activations per sampleالـ activations لكل عيّنة |
|---|---|---|---|---|---|
| FC1 (5 to 3)FC1 (من 5 إلى 3) | (3, 5)(3, 5) | 1515 | 33 | 1818 | 33 |
| FC2 (3 to 2)FC2 (من 3 إلى 2) | (2, 3)(2, 3) | 66 | 22 | 88 | 22 |
| Totalالمجموع | 2121 | 55 | 2626 | 5 computed, 10 with inputs5 محسوبة، و10 مع المدخلات |
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 يحسب لكل عيّنة؟
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المصادر
- Deep Learning, chapter 6: Deep Feedforward NetworksBookMIT Press, Goodfellow, Bengio and Courville, 2016Depth as the length of the chain of layers, width as the dimensionality of the hidden layers (the sentence quoted on slide 4).العمق بوصفه طول سلسلة الطبقات، والعرض بوصفه أبعاد الطبقات المخفية (الجملة المقتبسة في الشريحة 4).(opens in a new tab)(يفتح في تبويب جديد)
- Speech and Language Processing, chapter 6: Neural NetworksBookJurafsky and Martin, 3rd edition draftThe neural unit z = w·x + b, W in R^(n1 × n0), input layer not counted, fully connected definition, MLP naming, XOR.الوحدة العصبية z = w·x + b، وW في R^(n1 × n0)، وعدم احتساب طبقة الإدخال، وتعريف الاتصال الكامل (fully connected)، وسبب تسمية MLP، ومثال XOR.(opens in a new tab)(يفتح في تبويب جديد)
- Neural Networks Part 1: Setting up the ArchitectureDocsStanford CS231nBiological motivation, the N-layer convention that excludes the input, and the 41-parameter sizing example.الدافع البيولوجي، وعُرف N-layer الذي يستثني طبقة الإدخال، ومثال تحديد الحجم ذو الـ 41 parameter.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.LinearDocsPyTorch documentationy = x A^T + b with weight (out_features, in_features), bias (out_features), input (*, H_in) and output (*, H_out).y = x A^T + b بوزن شكله (out_features, in_features)، وbias شكله (out_features)، ومُدخل (*, H_in)، ومُخرج (*, H_out).(opens in a new tab)(يفتح في تبويب جديد)
- Efficient Processing of Deep Neural Networks: A Tutorial and SurveyPaperProceedings of the IEEE, Sze, Chen, Yang and Emer, 2017Reproduces the neuron and synapse figure (adapted from CS231n), synapse as a scaling factor, and fixes the activation and weight nomenclature.يعيد رسم شكل العصبون والـ synapse (مقتبسًا من CS231n)، ويصف الـ synapse بأنه معامل ضرب، ويثبّت تسميتي activation وweight.(opens in a new tab)(يفتح في تبويب جديد)
- MCUNet: Tiny Deep Learning on IoT DevicesPaperNeurIPS 2020, Lin, Chen, Lin, Cohn, Gan and HanSTM32F746 with 320 kB SRAM and 1 MB flash; SRAM constrains activations, flash constrains model size.STM32F746 فيه 320 kB من SRAM و1 MB من flash؛ ذاكرة SRAM تقيّد الـ activations، وذاكرة flash تقيّد حجم النموذج.(opens in a new tab)(يفتح في تبويب جديد)
- torchvision.models.resnet50DocsPyTorch documentationLists 25.6 million parameters for ResNet-50, used to size the model that does not fit a microcontroller.يذكر أن ResNet-50 فيه 25.6 مليون parameter، واستُخدم هذا الرقم لتقدير حجم النموذج الذي لا يتسع في متحكم دقيق.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 2: Basics of Deep LearningDocsMIT HAN Lab, Song Han, fall 2024The source deck that slides 2 to 7 reproduce, including the Layer 0 and w42 labels.الشرائح الأصلية التي تعيد إنتاجها الشرائح من 2 إلى 7، بما فيها التسميتان Layer 0 وw42.(opens in a new tab)(يفتح في تبويب جديد)