COE 592Lecture 02المحاضرة 02Full guideالشرح الكامل
Neural network fundamentalsأساسيات الشبكات العصبية
The whole lecture on one page, taught concept by concept. Work through the parts in order, mark each concept once you understand it, and open the slide chips when you want the original slides.المحاضرة كاملة في صفحة واحدة، مشروحة مفهومًا مفهومًا. امشِ على الأجزاء بالترتيب، وعلّم كل مفهوم بعد أن تفهمه، وافتح أزرار الشرائح عندما تريد الرجوع إلى الشرائح الأصلية.
- Partsالأجزاء
- 6
- Conceptsالمفاهيم
- 28
- Slidesالشرائح
- 45
- Readingمدة القراءة
- 168 min168 دقيقة
Part 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.مفردات الشبكات العصبية وأبسط طبقة، حيث يرى كل عصبون في المخرج كل عصبون في المدخل.
5 concepts5 مفاهيم, ، slides 1-7الشرائح 1-7
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)(يفتح في تبويب جديد)
Part 02: Convolution layers and their tensor shapesطبقات الالتفاف (Convolution) وأشكال موتراتها
Local receptive fields and weight sharing, from 1D to 2D convolution, the no-padding output size and a fully worked two-channel example.حقول الاستقبال المحلية ومشاركة الأوزان، من الالتفاف أحادي البعد إلى ثنائي البعد، وحجم المخرج بدون حشو (padding)، ومثال محلول بالكامل بقناتين.
4 concepts4 مفاهيم, ، slides 8-15الشرائح 8-15
Why this part mattersلماذا يهمّ هذا الجزء
Every efficiency technique in this course is an operation on the tensors of a convolution layer. Lecture 3 counts parameters and MACs from these shapes, pruning in lecture 4 removes entries of W, quantization shrinks the bits of W and Y, and the depthwise and grouped convolutions later in this lecture change the shape of W itself. Every model-size or MAC estimate for an embedded target starts from that shape and from the formula for the output height h_o.
Part 01 gave you the fully connected layer, whose weight matrix (c_o, c_i) connects every input to every output. This part crosses out that matrix and rebuilds it as a convolution: a small filter that sees only a local patch and is reused at every position. You will learn to read the four Conv2D tensors, to predict how much the feature map shrinks, to compute one output value by hand from a two-channel input, and to count parameters. These four skills are the most reliable exam material in the lecture, and they are the same arithmetic you will do when you size a model for a microcontroller or read a MobileNet table.
كل تقنية لرفع الكفاءة في هذا المقرر هي عملية على موتّرات (tensors) طبقة الالتفاف. المحاضرة 3 تعدّ المعاملات وعمليات الـ MACs انطلاقًا من هذه الأشكال، والـ pruning في المحاضرة 4 يحذف عناصر من W، والـ quantization يقلّص عدد بتات W وY، والالتفافات الـ depthwise والـ grouped التي تأتي لاحقًا في هذه المحاضرة تغيّر شكل W نفسه. وكل تقدير لحجم النموذج أو لعدد الـ MACs على جهاز مدمج يبدأ من هذا الشكل ومن صيغة ارتفاع المخرج h_o.
قدّم لك الجزء 01 الطبقة كاملة الاتصال (fully connected)، التي تربط مصفوفة أوزانها (c_o, c_i) كل مدخل بكل مخرج. هذا الجزء يشطب تلك المصفوفة ويعيد بناءها على شكل التفاف: مرشّح صغير لا يرى إلا رقعة محلية ويُعاد استخدامه في كل موضع. ستتعلم قراءة موتّرات Conv2D الأربعة، وتوقّع مقدار انكماش الـ feature map، وحساب قيمة مخرج واحدة يدويًا من مدخل ذي قناتين، وعدّ المعاملات. هذه المهارات الأربع هي أكثر مادة امتحانية موثوقة في المحاضرة، وهي الحساب نفسه الذي ستجريه عندما تقدّر حجم نموذج لمتحكم دقيق (microcontroller) أو تقرأ جدول MobileNet.
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain, with the receptive field and weight sharing, why one filter is reused at every position and why the parameter count does not depend on image size.تشرح، باستخدام المجال الاستقبالي (receptive field) ومشاركة الأوزان (weight sharing)، لماذا يُعاد استخدام المرشّح الواحد في كل موضع، ولماذا لا يعتمد عدد المعاملات على حجم الصورة.
- Write the shapes of X, Y, W and b for any Conv2D from its c_i, c_o, k_h, k_w and input size, in PyTorch order.تكتب أشكال X وY وW وb لأي Conv2D انطلاقًا من c_i وc_o وk_h وk_w وحجم المدخل، بترتيب PyTorch.
- Compute the output size without padding at stride 1, and with the general stride formula that slide 15 uses ahead of slide 18.تحسب حجم المخرج بدون padding عند stride يساوي 1، ثم بالصيغة العامة للـ stride التي تستخدمها الشريحة 15 قبل أن تعرّفها الشريحة 18.
- Compute one output value by hand from a multi-channel input, summing per-channel contributions and adding the bias.تحسب قيمة مخرج واحدة يدويًا من مدخل متعدد القنوات، بجمع مساهمة كل قناة على حدة ثم إضافة الـ bias.
- Count a Conv2D's parameters as c_o c_i k_h k_w + c_o and check it against the 1216 of the ConvNetJS demo.تعدّ معاملات Conv2D على أنها c_o c_i k_h k_w + c_o وتتحقق منها مقابل الرقم 1216 في عرض ConvNetJS.
Take a CIFAR-10 image: 32 x 32 pixels in three colour channels, so 3072 numbers. A single neuron of the Fully connected layer from Part 01 has one weight for each of those 3072 inputs, and CS231n uses exactly this count to show why the fully connected design does not scale to images. A neuron in a Convolution layer does something far more modest. It looks at a 5 x 5 patch of the image across all three channels, so 5 x 5 x 3 = 75 numbers, and ignores everything else. That patch is the neuron's Receptive field, and the slide states the whole idea in one sentence: the output neuron is connected to input neurons in the receptive field.
خذ صورة من CIFAR-10: 32 x 32 بكسلًا في ثلاث قنوات لونية (channels)، أي 3072 رقمًا. عصبون واحد في الـ fully connected layer من الجزء 01 له وزن واحد لكل مدخل من هذه المدخلات الـ 3072، ويستخدم CS231n هذا العدد تحديدًا ليبيّن لماذا لا يتوسع التصميم كامل الاتصال ليناسب الصور. أما العصبون في الـ convolution layer فيفعل شيئًا أكثر تواضعًا بكثير: ينظر إلى رقعة بحجم 5 x 5 من الصورة عبر القنوات الثلاث كلها، أي 5 x 5 x 3 = 75 رقمًا، ويتجاهل كل ما عداها. هذه الرقعة هي الـ receptive field (المجال الاستقبالي) للعصبون، والشريحة تلخّص الفكرة كلها في جملة واحدة: عصبون المخرج متصل بعصبونات المدخل الموجودة في المجال الاستقبالي.
That sentence hides two separate decisions, and Goodfellow, Bengio and Courville name them in section 9.2 of Deep Learning. The first is sparse interactions. With m inputs and n outputs, a fully connected layer stores m x n weights. If each output is allowed to touch only k inputs, the layer stores k x n. On the CIFAR image that is 75 per output instead of 3072.
تخفي هذه الجملة قرارين منفصلين، ويسمّيهما Goodfellow وBengio وCourville في القسم 9.2 من كتاب Deep Learning. الأول هو التفاعلات المتفرقة (sparse interactions). مع m مدخلًا وn مخرجًا، تخزّن الطبقة كاملة الاتصال m x n وزنًا. فإذا سُمح لكل مخرج بأن يلمس k مدخلًا فقط، تخزّن الطبقة k x n. وفي صورة CIFAR يعني ذلك 75 وزنًا لكل مخرج بدلًا من 3072.
The second decision is Weight sharing. Sparse connectivity alone would still give every output position its own private set of 75 weights. Convolution goes further and uses the same 75 numbers at every position. Goodfellow and colleagues put it plainly: rather than learning a separate set of parameters for every location, we learn only one set, so the storage of one filter drops to k parameters. They also give the synonym you will meet in older papers, tied weights. Slide 8 labels its figure Weight Sharing, and slides 12 and 13 show it in motion: the same green, yellow and cyan filters are applied wherever the red window sits on the blue input.
القرار الثاني هو الـ weight sharing (مشاركة الأوزان). الاتصال المتفرق وحده كان سيعطي كل موضع مخرج مجموعته الخاصة من 75 وزنًا. أما الالتفاف فيذهب أبعد ويستخدم الأرقام الـ 75 نفسها في كل موضع. ويقولها Goodfellow وزملاؤه بوضوح: بدلًا من تعلّم مجموعة منفصلة من المعاملات لكل موقع، نتعلم مجموعة واحدة فقط، فينخفض تخزين مرشّح (filter) واحد إلى k معاملًا (parameters). ويذكرون أيضًا المرادف الذي ستصادفه في الأوراق الأقدم: الأوزان المربوطة (tied weights). الشريحة 8 تعنون شكلها بـ Weight Sharing، والشريحتان 12 و13 تُظهرانه أثناء الحركة: المرشّحات الخضراء والصفراء والسماوية نفسها تُطبَّق أينما كانت النافذة الحمراء على المدخل الأزرق.
What the saving looks like in a real first layerكيف يبدو هذا التوفير في طبقة أولى حقيقية
CS231n works through AlexNet's first layer to show the size of the effect, and the numbers are worth holding in memory for the exam. The input is 227 x 227 x 3 (the AlexNet paper says 224 x 224; CS231n uses 227 so that (227 - 11)/4 + 1 = 55 comes out exact), the filters are 11 x 11 x 3, and the output is 55 x 55 x 96.
يمرّ CS231n على الطبقة الأولى من AlexNet ليبيّن حجم الأثر، والأرقام تستحق أن تحفظها للامتحان. المدخل 227 x 227 x 3 (ورقة AlexNet تقول 224 x 224؛ ويستخدم CS231n القيمة 227 لكي يخرج (227 - 11)/4 + 1 = 55 عددًا صحيحًا)، والمرشّحات 11 x 11 x 3، والمخرج 55 x 55 x 96.
Worked exampleمثال محلول
AlexNet conv1 with and without weight sharingالطبقة conv1 في AlexNet مع مشاركة الأوزان وبدونها
Count the output neuronsعُدّ عصبونات المخرج
55 x 55 x 96 = 290,400 outputs, one per position per output Channel.55 x 55 x 96 = 290,400 مخرجًا، واحد لكل موضع ولكل قناة مخرج.Weights per neuron with a local receptive field onlyالأوزان لكل عصبون مع مجال استقبالي محلي فقط
No sharingبدون مشاركة
290,400 x 364 = 105,705,600 parameters for one layer.290,400 x 364 = 105,705,600 معامل لطبقة واحدة.With sharingمع المشاركة
One filter per output channel: 96 x 363 = 34,848 weights plus 96 biases, so 34,944.مرشّح واحد لكل قناة مخرج: 96 x 363 = 34,848 وزنًا إضافة إلى 96 قيمة bias، أي 34,944.Reductionمقدار التخفيض
About 3025x fewer parameters, and 3025 = 55 x 55 is exactly the number of positions the filter is reused at.معاملات أقل بنحو 3025x مرة، و3025 = 55 x 55 هو بالضبط عدد المواضع التي يُعاد فيها استخدام المرشّح.
Crossing out the fully connected shapesشطب أشكال الطبقة كاملة الاتصال
The slide introduces the shapes by literally striking through the fully connected ones. In Part 01 the input was X (n, c_i): n samples in the batch (the Batch size), each a vector of c_i features. A 1D convolution adds a spatial axis of width w_i, so X becomes (n, c_i, w_i) and Y becomes (n, c_o, w_o). The weight matrix (c_o, c_i) gains a kernel width k_w and becomes (c_o, c_i, k_w). The bias does not change at all: still one number per output channel, (c_o,). The table already shows the 2D column, which adds a height axis in the same way; the next concept explains each of its axes.
تقدّم الشريحة الأشكال بشطب أشكال الطبقة كاملة الاتصال حرفيًا. في الجزء 01 كان المدخل X (n, c_i): n عينة في الدفعة (الـ batch size)، كل منها متجه من c_i خاصية. الالتفاف أحادي البعد (1D) يضيف محورًا مكانيًا عرضه w_i، فيصبح X بالشكل (n, c_i, w_i) ويصبح Y بالشكل (n, c_o, w_o). ومصفوفة الأوزان (c_o, c_i) تكتسب عرض نواة k_w فتصبح (c_o, c_i, k_w). أما الـ bias فلا يتغير إطلاقًا: لا يزال رقمًا واحدًا لكل قناة مخرج، (c_o,). والجدول يعرض مسبقًا عمود الحالة ثنائية البعد (2D)، الذي يضيف محور الارتفاع بالطريقة نفسها؛ والمفهوم التالي يشرح كل محور فيه.
| Tensorالموتّر | Fully connectedكاملة الاتصال | 1D convolutionالتفاف أحادي البعد | 2D convolutionالتفاف ثنائي البعد |
|---|---|---|---|
| Input Xالمدخل X | (n, c_i)(n, c_i) | (n, c_i, w_i)(n, c_i, w_i) | (n, c_i, h_i, w_i)(n, c_i, h_i, w_i) |
| Output Yالمخرج Y | (n, c_o)(n, c_o) | (n, c_o, w_o)(n, c_o, w_o) | (n, c_o, h_o, w_o)(n, c_o, h_o, w_o) |
| Weights Wالأوزان W | (c_o, c_i)(c_o, c_i) | (c_o, c_i, k_w)(c_o, c_i, k_w) | (c_o, c_i, k_h, k_w)(c_o, c_i, k_h, k_w) |
| Bias bالـ bias b | (c_o,)(c_o,) | (c_o,)(c_o,) | (c_o,)(c_o,) |
Recall
A 1D convolution has 8 input channels, 32 output channels and kernel width 5. Write the shapes of W and b, and say what happens to them if the input width doubles.التفاف أحادي البعد له 8 قنوات مدخل و32 قناة مخرج وعرض نواة 5. اكتب شكلي W وb، وقل ماذا يحدث لهما إذا تضاعف عرض المدخل.
Quick checkاختبار سريع
Why does a convolution layer on a 32x32x3 image need far fewer parameters than a fully connected layer?لماذا تحتاج طبقة الالتفاف على صورة 32x32x3 معاملات أقل بكثير من الطبقة كاملة الاتصال؟
Start with a layer you could type into PyTorch today: nn.Conv2d(3, 16, kernel_size=5), applied to a batch of 8 RGB images of 32 x 32. Four tensors take part, and every axis of each one has a name.
ابدأ بطبقة يمكنك كتابتها في PyTorch اليوم: nn.Conv2d(3, 16, kernel_size=5)، مطبّقة على دفعة من 8 صور RGB بحجم 32 x 32. تشارك أربعة موتّرات، ولكل محور في كل منها اسم.
nn.Conv2d(3, 16, kernel_size=5) on 8 images of 32x32, no paddingnn.Conv2d(3, 16, kernel_size=5) على 8 صور بحجم 32x32، بدون padding
- Input Xالمدخل X
- (8, 3, 32, 32): batch, input channels, height, width(8, 3, 32, 32): الدفعة (batch)، قنوات المدخل، الارتفاع، العرض
- Weights Wالأوزان W
- (16, 3, 5, 5): output channels, input channels, kernel height, kernel width(16, 3, 5, 5): قنوات المخرج، قنوات المدخل، ارتفاع النواة، عرض النواة
- Bias bالـ bias b
- (16,): one scalar per output channel(16,): قيمة عددية واحدة لكل قناة مخرج
- Output Yالمخرج Y
- (8, 16, 28, 28): batch, output channels, then 32 - 5 + 1 = 28 each way(8, 16, 28, 28): الدفعة، قنوات المخرج، ثم 32 - 5 + 1 = 28 في كل اتجاه
The general rule is the 2D column that the previous concept left open. The input X is (n, c_i, h_i, w_i), the output Y is (n, c_o, h_o, w_o), the weights W are (c_o, c_i, k_h, k_w) and the bias b is (c_o,). The PyTorch documentation for torch.nn.Conv2d confirms this exact ordering: the weight is stored as (out_channels, in_channels / groups, kernel_size[0], kernel_size[1]) and the bias as (out_channels). The groups divisor is 1 by default; it becomes important in Part 03 when grouped and depthwise convolutions arrive.
القاعدة العامة هي عمود الحالة ثنائية البعد الذي تركه المفهوم السابق مفتوحًا. المدخل X شكله (n, c_i, h_i, w_i)، والمخرج Y شكله (n, c_o, h_o, w_o)، والأوزان W شكلها (c_o, c_i, k_h, k_w)، والـ bias b شكله (c_o,). وتؤكد وثائق PyTorch الخاصة بـ torch.nn.Conv2d هذا الترتيب بالضبط: الوزن مخزّن بالشكل (out_channels, in_channels / groups, kernel_size[0], kernel_size[1]) والـ bias بالشكل (out_channels). القاسم groups قيمته الافتراضية 1؛ ويصبح مهمًا في الجزء 03 عندما تظهر الالتفافات الـ grouped والـ depthwise.
A filter is a block, not a squareالمرشّح كتلة، لا مربع
Slides 9 to 11 make one argument in three frames. The first frame draws a single filter as a green block with edges labelled k_h, k_w and c_i, sitting next to the red window on the input. The block is as deep as the input has channels. When it is applied at one position, it multiplies all c_i x k_h x k_w input values under the window by its own c_i x k_h x k_w weights, sums everything into a single number, and drops that number into one output channel. CS231n states the rule as a sentence to memorise: the extent of the connectivity along the depth axis is always equal to the depth of the input volume.
تبني الشرائح من 9 إلى 11 حجة واحدة في ثلاث لقطات. اللقطة الأولى ترسم مرشّحًا واحدًا على شكل كتلة خضراء أضلاعها موسومة بـ k_h وk_w وc_i، بجوار النافذة الحمراء على المدخل. عمق الكتلة يساوي عدد قنوات المدخل. وعندما تُطبَّق في موضع واحد، تضرب كل قيم المدخل الـ c_i x k_h x k_w الواقعة تحت النافذة في أوزانها الـ c_i x k_h x k_w، وتجمع كل شيء في رقم واحد، وتضع ذلك الرقم في قناة مخرج واحدة. ويصوغ CS231n القاعدة في جملة تستحق الحفظ: امتداد الاتصال على محور العمق يساوي دائمًا عمق حجم المدخل.
The second frame adds a yellow filter. It has the same depth c_i, sees the same window, and drops its number into a second output channel. The third frame shows the full stack of c_o filters with a bracket labelled c_o, and the output is now c_o channels deep. That bracket is literally the first axis of W. Choosing c_o is a design decision made by whoever builds the network; it is not derived from c_i.
اللقطة الثانية تضيف مرشّحًا أصفر. له العمق نفسه c_i، ويرى النافذة نفسها، ويضع رقمه في قناة مخرج ثانية. اللقطة الثالثة تعرض المكدّس الكامل من c_o مرشّحًا مع قوس موسوم بـ c_o، ويصبح عمق المخرج الآن c_o قناة. هذا القوس هو حرفيًا المحور الأول في W. اختيار c_o قرار تصميمي يتخذه من يبني الشبكة؛ وهو لا يُشتق من c_i.
Bias and batch, the two easy axesالـ bias والدفعة، المحوران السهلان
The Bias is added once per output channel, after the whole c_i-deep dot product, which is why its shape is (c_o,) exactly as in the fully connected layer of Part 01. The Batch size n appears in X and Y but never in W or b: the same weights process every sample in the batch. If you are ever unsure of a shape, check that n is absent from the parameters and that c_o leads W.
يُضاف الـ bias مرة واحدة لكل قناة مخرج، بعد حاصل الضرب النقطي الكامل الذي عمقه c_i، ولهذا شكله (c_o,) تمامًا كما في الطبقة كاملة الاتصال في الجزء 01. أما الـ batch size n فيظهر في X وY لكنه لا يظهر أبدًا في W أو b: الأوزان نفسها تعالج كل عينة في الدفعة. وإذا شككت يومًا في شكل ما، فتحقق من أن n غائب عن المعاملات وأن c_o يتصدّر W.
Recall
Write the shapes of X, W, b and Y for nn.Conv2d(3, 16, kernel_size=5) applied to a batch of 8 images of 32x32 with no padding.اكتب أشكال X وW وb وY للطبقة nn.Conv2d(3, 16, kernel_size=5) مطبّقة على دفعة من 8 صور بحجم 32x32 بدون padding.
Recall
A Conv2D has 64 input channels and 128 filters of 3x3. How many output channels does it produce, and what is the shape of W?طبقة Conv2D لها 64 قناة مدخل و128 مرشّحًا بحجم 3x3. كم قناة مخرج تنتج، وما شكل W؟
Quick checkاختبار سريع
A Conv2D layer has c_i = 3, c_o = 16 and 5x5 kernels. Which is the shape of its weight tensor W?طبقة Conv2D فيها c_i = 3 وc_o = 16 ونوى بحجم 5x5. ما شكل موتّر أوزانها W؟
Slide 14 gives the smallest example that shows the shrink. The input is 4 x 4, the Kernel (filter) is 3 x 3, there is no padding and the window moves one cell at a time. Along the top row the window can start at column 0 or column 1. Starting at column 2 would need columns 2, 3 and 4, and there is no column 4. Two starting positions per axis means the output is 2 x 2.
تعطي الشريحة 14 أصغر مثال يُظهر الانكماش. المدخل 4 x 4، والـ kernel بحجم 3 x 3، ولا يوجد padding، والنافذة تتحرك خلية واحدة في كل مرة. على الصف العلوي يمكن أن تبدأ النافذة عند العمود 0 أو العمود 1. البدء عند العمود 2 يحتاج الأعمدة 2 و3 و4، ولا يوجد عمود 4. موضعا بدء لكل محور يعنيان أن المخرج 2 x 2.
Worked exampleمثال محلول
Counting the valid positions on slide 14عدّ المواضع الصالحة في الشريحة 14
Last valid start indexآخر فهرس بدء صالح
The window of height k_h = 3 starting at row r covers rows r to r + 2. It fits while r + 2 <= 3, so the last start is r = h_i - k_h = 1.النافذة التي ارتفاعها k_h = 3 وتبدأ عند الصف r تغطي الصفوف من r إلى r + 2. وهي تتسع ما دام r + 2 <= 3، لذا فآخر بداية هي r = h_i - k_h = 1.Count the startsعُدّ مواضع البدء
Rows 0 and 1, which is h_i - k_h + 1 = 4 - 3 + 1 = 2 positions.الصفان 0 و1، أي h_i - k_h + 1 = 4 - 3 + 1 = 2 موضعين.Resultالنتيجة
h_o = w_o = 2. The feature map is 2 x 2, smaller than the input by k - 1 = 2 along each axis (one cell lost at each border).h_o = w_o = 2. الـ feature map حجمها 2 x 2، أصغر من المدخل بمقدار k - 1 = 2 على كل محور (خلية مفقودة عند كل حافة).
Slides 12 and 13 are two frames of the same motion, taken from the MIT 6.5940 lecture that this section of the deck reproduces. The red window moves one step at a time along a spatial dimension, and at every stop the same green, yellow and cyan filters are applied, which is why slide 12 carries the label Weight Sharing. Nothing about the filters changes as the window moves; this is where Weight sharing becomes visible rather than stated. Each stop adds one short stack of c_o values to the output, one value per filter, so the output has exactly one spatial cell per valid window position. The count of valid positions is the whole story of the output size.
الشريحتان 12 و13 لقطتان من الحركة نفسها، مأخوذتان من محاضرة MIT 6.5940 التي يعيد هذا القسم من العرض إنتاجها. تتحرك النافذة الحمراء خطوة واحدة في كل مرة على امتداد بُعد مكاني، وفي كل توقف تُطبَّق المرشّحات الخضراء والصفراء والسماوية نفسها، ولهذا تحمل الشريحة 12 عنوان Weight Sharing. لا شيء في المرشّحات يتغير مع حركة النافذة؛ وهنا يصبح الـ weight sharing مرئيًا بدلًا من أن يكون مجرد عبارة. كل توقف يضيف إلى المخرج مكدّسًا قصيرًا من c_o قيمة، قيمة واحدة لكل مرشّح، لذا فللمخرج خلية مكانية واحدة بالضبط لكل موضع صالح للنافذة. وعدد المواضع الصالحة هو القصة الكاملة لحجم المخرج.
The general formula, one part earlyالصيغة العامة، قبل موعدها بجزء واحد
Slide 15 asks you to use a Stride of 2 before slide 18 has defined stride, so you need the general rule now. With padding p on every border and stride s, Dumoulin and Visin's relationship 6 gives the output size, and the PyTorch documentation states the same formula with a dilation term that is 1 throughout this course.
تطلب منك الشريحة 15 استخدام stride قيمته 2 قبل أن تعرّف الشريحة 18 الـ stride، لذا تحتاج القاعدة العامة الآن. مع padding قدره p على كل حافة وstride قدره s، تعطي العلاقة 6 عند Dumoulin وVisin حجم المخرج، وتذكر وثائق PyTorch الصيغة نفسها مع حد dilation قيمته 1 طوال هذا المقرر.
| h_ih_i | kk | ss | pp | Arithmeticالحساب | h_oh_o |
|---|---|---|---|---|---|
| 44 | 33 | 11 | 00 | (4 - 3)/1 + 1(4 - 3)/1 + 1 | 22 |
| 44 | 22 | 22 | 00 | (4 - 2)/2 + 1(4 - 2)/2 + 1 | 22 |
| 3232 | 55 | 11 | 00 | (32 - 5)/1 + 1(32 - 5)/1 + 1 | 2828 |
| 3232 | 55 | 11 | 22 | (32 + 4 - 5)/1 + 1(32 + 4 - 5)/1 + 1 | 3232 |
| 224224 | 77 | 22 | 33 | floor((224 + 6 - 7)/2) + 1floor((224 + 6 - 7)/2) + 1 | 112112 |
Two rows deserve a second look. The 32, 5, 1, 2 row is the ConvNetJS demo on slide 25, which uses pad 2 precisely so that its 5 x 5 convolution keeps the 32 x 32 size, giving the 32 x 32 x 16 output printed there. The 224, 7, 2, 3 row is the first layer of a ResNet, which halves the spatial size in one step. Both are the same formula.
صفّان يستحقان نظرة ثانية. صف 32, 5, 1, 2 هو عرض ConvNetJS في الشريحة 25، الذي يستخدم pad قدره 2 تحديدًا لكي يحافظ التفافه 5 x 5 على الحجم 32 x 32، فيعطي المخرج 32 x 32 x 16 المطبوع هناك. وصف 224, 7, 2, 3 هو الطبقة الأولى في ResNet، التي تنصّف الحجم المكاني في خطوة واحدة. وكلاهما الصيغة نفسها.
Recall
A 32x32 input meets a 5x5 kernel at stride 1. What is the output size with no padding, and with padding 2?مدخل بحجم 32x32 يلتقي بـ kernel بحجم 5x5 عند stride يساوي 1. ما حجم المخرج بدون padding، ومع padding قدره 2؟
Quick checkاختبار سريع
An input of 4x4 is convolved with a 3x3 kernel, stride 1, no padding. What is the output spatial size?مدخل بحجم 4x4 يُطبَّق عليه التفاف بـ kernel بحجم 3x3، وstride يساوي 1، بدون padding. ما الحجم المكاني للمخرج؟
Slide 15 is the exam question in miniature: two input channels of 4 x 4, one output channel, a 2 x 2 filter, Stride 2, no padding and no Bias. Input channel 1 has every row equal to 1 2 3 4; input channel 2 has every row equal to 5 6 7 8. The filter's first slab is [[2, 1], [1, 2]] and its second slab is [[1, 2], [2, 1]]. In the shapes of the previous concepts, X is (1, 2, 4, 4) and W is (1, 2, 2, 2).
الشريحة 15 هي سؤال الامتحان في صورة مصغّرة: قناتا مدخل بحجم 4 x 4، وقناة مخرج واحدة، ومرشّح (filter) بحجم 2 x 2، وstride قدره 2، بلا padding وبلا bias. كل صف في قناة المدخل الأولى يساوي 1 2 3 4؛ وكل صف في قناة المدخل الثانية يساوي 5 6 7 8. الشريحة الأولى من المرشّح هي [[2, 1], [1, 2]] والثانية [[1, 2], [2, 1]]. وبأشكال المفاهيم السابقة، X شكله (1, 2, 4, 4) وW شكله (1, 2, 2, 2).
The formula says what the filter-is-a-block idea implied: the sum over c folds all input channels into one number. There is no separate output per input channel. With that in mind the four values of the slide are four applications of the same recipe. Strictly, this is cross-correlation: mathematical convolution flips the kernel first, and this formula does not. Deep learning libraries, PyTorch and this deck all call the unflipped version convolution (Goodfellow et al., section 9.1), and this course follows that convention.
تقول الصيغة ما كانت فكرة أن المرشّح كتلة تتضمنه: الجمع على c يطوي كل قنوات المدخل في رقم واحد. لا يوجد مخرج منفصل لكل قناة مدخل. ومع وضع ذلك في الذهن، فالقيم الأربع في الشريحة هي أربعة تطبيقات للوصفة نفسها. وبدقة، هذا ارتباط متقاطع (cross-correlation): الالتفاف الرياضي يقلب النواة أولًا، وهذه الصيغة لا تفعل ذلك. مكتبات التعلم العميق وPyTorch وهذا العرض كلها تسمّي النسخة غير المقلوبة التفافًا (Goodfellow et al.، القسم 9.1)، وهذا المقرر يتبع هذا العرف.
Worked exampleمثال محلول
Slide 15, worked from the shapes downالشريحة 15، محلولة انطلاقًا من الأشكال
Output sizeحجم المخرج
Stride 2 with no padding: (4 - 2)/2 + 1 = 2, so the output is 2 x 2 and the window visits four positions, starting at rows and columns 0 and 2.stride قدره 2 بلا padding: (4 - 2)/2 + 1 = 2، لذا فالمخرج 2 x 2 والنافذة تزور أربعة مواضع، تبدأ عند الصفين والعمودين 0 و2.Top-left value, window at rows 0 to 1, columns 0 to 1القيمة العلوية اليسرى، النافذة على الصفين 0 إلى 1 والعمودين 0 إلى 1
Per-channel contributions at position (0, 0)مساهمة كل قناة عند الموضع (0, 0)
- Channel 1القناة 1
- 2·1 + 1·2 + 1·1 + 2·2 = 92·1 + 1·2 + 1·1 + 2·2 = 9
- Channel 2القناة 2
- 1·5 + 2·6 + 2·5 + 1·6 = 331·5 + 2·6 + 2·5 + 1·6 = 33
- Biasالـ bias
- 00
- Sumالمجموع
- 9 + 33 = 429 + 33 = 42
Top-right value, window at rows 0 to 1, columns 2 to 3القيمة العلوية اليمنى، النافذة على الصفين 0 إلى 1 والعمودين 2 إلى 3
Per-channel contributions at position (0, 1)مساهمة كل قناة عند الموضع (0, 1)
- Channel 1القناة 1
- 2·3 + 1·4 + 1·3 + 2·4 = 212·3 + 1·4 + 1·3 + 2·4 = 21
- Channel 2القناة 2
- 1·7 + 2·8 + 2·7 + 1·8 = 451·7 + 2·8 + 2·7 + 1·8 = 45
- Biasالـ bias
- 00
- Sumالمجموع
- 21 + 45 = 6621 + 45 = 66
Bottom rowالصف السفلي
Every row of both inputs is identical, so the windows at rows 2 to 3 see the same numbers and give 42 and 66 again.كل صفوف المدخلين متطابقة، لذا فالنوافذ على الصفين 2 إلى 3 ترى الأرقام نفسها وتعطي 42 و66 مرة أخرى.Output feature mapالـ feature map الناتجة
[[42, 66], [42, 66]], exactly as printed on the slide.[[42, 66], [42, 66]]، تمامًا كما هي مطبوعة على الشريحة.
Now do it with your hands. The stepper below holds the slide 15 numbers, highlights the current window on both input channels, lists the products channel by channel and fills the output one cell at a time. Switch the stride to 1 to see the 3 x 3 output, then change a few cells and predict the result before you press Next.
الآن جرّبها بيدك. الأداة التفاعلية أدناه تحمل أرقام الشريحة 15، وتبرز النافذة الحالية على قناتي المدخل كلتيهما، وتسرد حواصل الضرب قناة بقناة، وتملأ المخرج خلية خلية. غيّر الـ stride إلى 1 لترى المخرج 3 x 3، ثم غيّر بعض الخلايا وتوقّع النتيجة قبل أن تضغط Next.
- This position
- press Next to place the windowاضغط التالي لوضع النافذة
Every input and filter cell is editable, negative weights included; a blank cell counts as 0. Rows and columns are numbered from 0, as in the formula above. Each channel of the filter multiplies the matching input channel inside the highlighted window, the two channel sums are added into one value, and the bias is added last.Channel 1 and channel 2 keep their colours throughout.كل خلية في الإدخال وفي المرشح قابلة للتعديل، بما في ذلك الأوزان السالبة، والخلية الفارغة تُحسب 0. تُرقَّم الصفوف والأعمدة بدءًا من 0، كما في الصيغة أعلاه. تضرب كل قناة من قنوات المرشح قناة الإدخال المقابلة لها داخل النافذة المظللة، ثم يُجمع مجموعا القناتين في قيمة واحدة، ويُضاف الانحياز في النهاية. تحتفظ القناة 1 والقناة 2 بلونيهما طوال الوقت.
| Stride 2 (the slide)stride يساوي 2 (الشريحة) | Stride 1stride يساوي 1 | |
|---|---|---|
| Output sizeحجم المخرج | (4 - 2)/2 + 1 = 2, so 2x2(4 - 2)/2 + 1 = 2، إذن 2x2 | (4 - 2)/1 + 1 = 3, so 3x3(4 - 2)/1 + 1 = 3، إذن 3x3 |
| Channel 1 partials, one rowالمجاميع الجزئية للقناة 1، صف واحد | 9, 219, 21 | 9, 15, 219, 15, 21 |
| Channel 2 partials, one rowالمجاميع الجزئية للقناة 2، صف واحد | 33, 4533, 45 | 33, 39, 4533, 39, 45 |
| One output rowصف مخرج واحد | 42, 6642, 66 | 42, 54, 6642, 54, 66 |
| Windows visitedعدد النوافذ التي تمت زيارتها | 44 | 99 |
The stride-1 column shows what the middle window adds. At columns 1 to 2, channel 1 gives 2·2 + 1·3 + 1·2 + 2·3 = 15 and channel 2 gives 1·6 + 2·7 + 2·6 + 1·7 = 39, so 54 sits between 42 and 66. Stride 2 skips that window entirely, which is why the output is smaller and the compute drops from 9 windows to 4. On a large input the compute falls to about a quarter of the stride-1 cost, since both axes are halved.
يُظهر عمود stride يساوي 1 ما تضيفه النافذة الوسطى. عند العمودين 1 إلى 2 تعطي القناة الأولى 2·2 + 1·3 + 1·2 + 2·3 = 15 وتعطي القناة الثانية 1·6 + 2·7 + 2·6 + 1·7 = 39، لذا تقع 54 بين 42 و66. الـ stride الذي يساوي 2 يتخطى تلك النافذة كليًا، ولهذا يكون المخرج أصغر وينخفض الحساب من 9 نوافذ إلى 4. وعلى مدخل كبير ينخفض الحساب إلى نحو ربع تكلفة stride يساوي 1، لأن المحورين كليهما يُنصَّفان.
Recall
In the slide 15 example, what are the per-channel contributions to the top-right output value?في مثال الشريحة 15، ما مساهمة كل قناة في قيمة المخرج العلوية اليمنى؟
Recall
Same data with stride 1: what is the output size, and what is the middle column?البيانات نفسها مع stride يساوي 1: ما حجم المخرج، وما العمود الأوسط؟
Quick checkاختبار سريع
In the slide 15 example (two input channels, one filter with a 2x2 slab per input channel, stride 2, no bias), what is the top-left output value?في مثال الشريحة 15 (قناتا مدخل، ومرشّح واحد بشريحة 2x2 لكل قناة مدخل، وstride يساوي 2، بدون bias)، ما قيمة المخرج العلوية اليسرى؟
Counting parametersعدّ المعاملات
The shapes of W and b give the parameter count directly, and that count is what decides whether a model fits in a microcontroller's flash. The weights number c_o x c_i x k_h x k_w, the size of W, and there is one bias per output channel.
شكلا W وb يعطيان عدد المعاملات مباشرة، وهذا العدد هو ما يقرر هل يتسع النموذج في ذاكرة الـ flash لمتحكم دقيق. عدد الأوزان (weights) هو c_o x c_i x k_h x k_w، أي حجم W، ويوجد bias واحد لكل قناة مخرج.
Slide 25 later in this lecture shows the ConvNetJS CIFAR-10 demo, whose first convolution has 16 filters of 5 x 5 over the 3 colour channels. The demo prints 16 x 5 x 5 x 3 + 16 = 1216 parameters, and the formula reproduces it: 1200 weights and 16 biases. Use that number as a retrieval hook; if your formula does not give 1216 for that layer, you have forgotten the biases or swapped a factor.
تعرض الشريحة 25 لاحقًا في هذه المحاضرة عرض ConvNetJS على CIFAR-10، الذي فيه أول التفاف من 16 مرشّحًا بحجم 5 x 5 على القنوات اللونية الثلاث 3. يطبع العرض 16 x 5 x 5 x 3 + 16 = 1216 معاملًا، والصيغة تعيد إنتاج هذا الرقم: 1200 وزن و16 قيمة bias. استخدم هذا الرقم كخطاف للتذكّر؛ إذا لم تعطك صيغتك 1216 لهذه الطبقة، فقد نسيت الـ biases أو بدّلت أحد العوامل.
| Layerالطبقة | Arithmeticالحساب | Parametersالمعاملات | Depends on h_i, w_i?هل يعتمد على h_i وw_i؟ |
|---|---|---|---|
| Conv2D, 16 filters of 5x5, 3 to 16 channelsConv2D، 16 مرشّحًا بحجم 5x5، من 3 قنوات إلى 16 | 16 x 3 x 5 x 5 + 1616 x 3 x 5 x 5 + 16 | 12161216 | Noلا |
| Fully connected, 32x32x3 = 3072 inputs to 16 outputsطبقة كاملة الاتصال، 32x32x3 = 3072 مدخلًا إلى 16 مخرجًا | 3072 x 16 + 163072 x 16 + 16 | 49,16849,168 | Yesنعم |
| AlexNet conv1, 96 filters of 11x11, 3 to 96 channelsالطبقة conv1 في AlexNet، 96 مرشّحًا بحجم 11x11، من 3 قنوات إلى 96 | 96 x 3 x 11 x 11 + 9696 x 3 x 11 x 11 + 96 | 34,94434,944 | Noلا |
| Changeالتغيير | Arithmeticالحساب | Parametersالمعاملات |
|---|---|---|
| Input 64x64x3, convمدخل 64x64x3، التفاف | 16 x 3 x 5 x 5 + 1616 x 3 x 5 x 5 + 16 | 12161216 |
| Input 64x64x3, fully connectedمدخل 64x64x3، طبقة كاملة الاتصال | 12288 x 16 + 1612288 x 16 + 16 | 196,624196,624 |
| Kernels 3x3نوى بحجم 3x3 | 16 x 3 x 3 x 3 + 1616 x 3 x 3 x 3 + 16 | 448448 |
| Filters 3232 مرشّحًا | 32 x 3 x 5 x 5 + 3232 x 3 x 5 x 5 + 32 | 24322432 |
Only the filter geometry and the channel counts move the number.
وحدهما هندسة المرشّح وعدد القنوات يحرّكان الرقم.
Recall
How many parameters does nn.Conv2d(3, 16, kernel_size=5) have, and what changes if the images become 64x64?كم معاملًا في nn.Conv2d(3, 16, kernel_size=5)، وماذا يتغير إذا أصبحت الصور 64x64؟
Quick checkاختبار سريع
A Conv2D with 16 filters of 5x5x3 plus biases (the ConvNetJS first layer) has how many parameters?طبقة Conv2D فيها 16 مرشّحًا بحجم 5x5x3 مع الـ biases (الطبقة الأولى في ConvNetJS)، كم معاملًا فيها؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- A conv output neuron is connected only to the input neurons in its receptive field, and the same filter is reused at every position (weight sharing).عصبون المخرج في طبقة الالتفاف متصل فقط بعصبونات المدخل الواقعة في مجاله الاستقبالي (receptive field)، والمرشّح نفسه يُعاد استخدامه في كل موضع (weight sharing).
- Shapes: X (n, c_i, h_i, w_i), Y (n, c_o, h_o, w_o), W (c_o, c_i, k_h, k_w), b (c_o,). The batch size n never appears in W or b.الأشكال: X (n, c_i, h_i, w_i)، وY (n, c_o, h_o, w_o)، وW (c_o, c_i, k_h, k_w)، وb (c_o,). حجم الدفعة n لا يظهر أبدًا في W أو b.
- A filter is not 2D: it is c_i deep and produces exactly one output channel, so c_o filters give c_o channels regardless of c_i.المرشّح ليس ثنائي الأبعاد: عمقه c_i وينتج قناة مخرج واحدة بالضبط، لذا فإن c_o مرشّحًا تعطي c_o قناة مهما كانت قيمة c_i.
- Without padding at stride 1, h_o = h_i - k_h + 1: a 4x4 input with a 3x3 kernel gives 2x2; in general h_o = floor((h_i + 2p - k_h)/s) + 1.بدون padding وعند stride يساوي 1 يكون h_o = h_i - k_h + 1: مدخل 4x4 مع kernel بحجم 3x3 يعطي 2x2؛ وبشكل عام h_o = floor((h_i + 2p - k_h)/s) + 1.
- Slide 15: output size (4 - 2)/2 + 1 = 2; top-left 9 + 33 = 42, top-right 21 + 45 = 66; output [[42, 66], [42, 66]]; bias is ignored on the slide.الشريحة 15: حجم المخرج (4 - 2)/2 + 1 = 2؛ القيمة العلوية اليسرى 9 + 33 = 42، والعلوية اليمنى 21 + 45 = 66؛ المخرج [[42, 66], [42, 66]]؛ والشريحة تتجاهل الـ bias.
- Parameters = c_o c_i k_h k_w + c_o; 16 filters of 5x5x3 plus biases give 1216; the count is independent of h_i and w_i, unlike an FC layer's 3072 x 16 + 16 = 49,168.عدد المعاملات = c_o c_i k_h k_w + c_o؛ 16 مرشّحًا بحجم 5x5x3 مع الـ biases تعطي 1216؛ والعدد لا يعتمد على h_i وw_i، بخلاف طبقة FC التي تحتاج 3072 x 16 + 16 = 49,168.
Sourcesالمصادر
- Deep Learning, chapter 9: Convolutional Networks, section 9.2 MotivationBookMIT Press (Goodfellow, Bengio and Courville)Sparse interactions (m x n against k x n), parameter sharing and tied weights, receptive field of one output unitالتفاعلات المتفرقة (m x n مقابل k x n)، ومشاركة المعاملات والأوزان المربوطة (tied weights)، والمجال الاستقبالي لوحدة مخرج واحدة(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 02: Basics of Deep LearningDocsMIT HAN Lab (Song Han), Fall 2023Original slides that slides 8 to 14 reproduce: receptive field, tensor shapes, weight sharing, h_o = h_i - k_h + 1الشرائح الأصلية التي تعيد الشرائح من 8 إلى 14 إنتاجها: المجال الاستقبالي، وأشكال الموتّرات، ومشاركة الأوزان، وh_o = h_i - k_h + 1(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.Conv2dDocsPyTorch documentationWeight (out_channels, in_channels / groups, kH, kW), bias (out_channels), output size formula with padding, dilation and strideالوزن (out_channels, in_channels / groups, kH, kW)، والـ bias (out_channels)، وصيغة حجم المخرج مع padding وdilation وstride(opens in a new tab)(يفتح في تبويب جديد)
- CS231n Convolutional Neural Networks for Visual Recognition: Convolutional NetworksDocsStanford University3072 weights for one FC neuron on CIFAR-10, depth of connectivity equals input depth, AlexNet conv1 parameter counts with and without sharing3072 وزنًا لعصبون FC واحد على CIFAR-10، وعمق الاتصال يساوي عمق المدخل، وعدد معاملات conv1 في AlexNet مع المشاركة وبدونها(opens in a new tab)(يفتح في تبويب جديد)
- A guide to convolution arithmetic for deep learningPaperarXiv (Dumoulin and Visin, 2016)Relationship 1: o = i - k + 1; relationship 6: o = floor((i + 2p - k)/s) + 1العلاقة 1: o = i - k + 1؛ والعلاقة 6: o = floor((i + 2p - k)/s) + 1(opens in a new tab)(يفتح في تبويب جديد)
- ConvNetJS CIFAR-10 demoDocsAndrej Karpathy, StanfordFirst conv layer: 16 filters of 5x5, stride 1, pad 2 on a 32x32x3 input, 1216 parameters as shown on slide 25أول طبقة التفاف: 16 مرشّحًا بحجم 5x5، وstride يساوي 1، وpad يساوي 2 على مدخل 32x32x3، و1216 معاملًا كما يظهر في الشريحة 25(opens in a new tab)(يفتح في تبويب جديد)
- Gradient-based learning applied to document recognitionPaperProceedings of the IEEE (LeCun, Bottou, Bengio and Haffner, 1998)Local receptive fields and shared weights as the architectural ideas of LeNet-5; section II.A credits earlier work by Fukushima (1980) and LeCun et al. (1989)المجالات الاستقبالية المحلية والأوزان المشتركة بوصفها الفكرتين المعماريتين في LeNet-5؛ ويَنسب القسم II.A الفضل إلى أعمال سابقة لـ Fukushima (1980) وLeCun et al. (1989)(opens in a new tab)(يفتح في تبويب جديد)
Part 03: Padding, stride, receptive field and grouped convolutionالحشو والخطوة وحقل الاستقبال والالتفاف المجمّع
How padding and stride control the output size, how the receptive field grows with depth, and how grouped and depthwise convolution cut parameters.كيف يتحكم الحشو (padding) والخطوة (stride) في حجم المخرج، وكيف ينمو حقل الاستقبال (receptive field) مع العمق، وكيف يقلل الالتفاف المجمّع (grouped) والعمقي (depthwise) عدد المعاملات.
4 concepts4 مفاهيم, ، slides 16-20الشرائح 16-20
Why this part mattersلماذا يهمّ هذا الجزء
Part 02 gave you a convolution that slides one filter over one input. Every embedded CNN you will profile or design in this course, from MobileNet to the TinyML backbones in your project, is built by turning four extra knobs on that layer: padding, stride, depth and grouping.
Those knobs decide the three numbers an embedded engineer lives by. The output size sets how much activation memory a layer needs. The receptive field sets how many layers it takes before one output can see the whole image. The parameter count sets how much flash the weights occupy and, with the output size, how many multiply-accumulates each inference costs. The exam asks for all three directly, and the storage and MAC budgets in your research project come out of the same formulas.
أعطاك الجزء 02 التفافًا (convolution) يمرّر مرشّحًا واحدًا فوق مدخل واحد. كل شبكة CNN مدمجة ستقيس أداءها أو تصممها في هذا المقرر، من MobileNet إلى الشبكات الأساسية (backbones) الخاصة بـ TinyML في مشروعك، تُبنى بتحريك أربعة مفاتيح إضافية في تلك الطبقة: الحشو (padding) والخطوة (stride) والعمق (depth) والتجميع (grouping).
هذه المفاتيح تحدد الأرقام الثلاثة التي يعيش عليها مهندس الأنظمة المدمجة. حجم المخرج يحدد مقدار ذاكرة التنشيطات (activation memory) التي تحتاجها الطبقة. ومجال الاستقبال (receptive field) يحدد كم طبقة نحتاج قبل أن يرى مخرج واحد الصورة كاملة. وعدد المعاملات (parameters) يحدد كم تشغل الأوزان من ذاكرة flash، ومعه حجم المخرج يحدد كم عملية ضرب وتجميع (multiply-accumulate) يكلّف كل استدلال (inference). الامتحان يسأل عن الثلاثة مباشرة، وميزانيات التخزين وعدد MAC في مشروعك البحثي تخرج من الصيغ نفسها.
By the end you canفي نهاية هذا الجزء ستستطيع
- Compute the output size of any Conv2D from h_i, k, p and s, including the floor, and pick p for a same-size output.تحسب حجم المخرج لأي طبقة Conv2D من h_i و k و p و s مع دالة floor، وتختار p بحيث يبقى المخرج بنفس حجم المدخل.
- Explain what each padding mode puts in the border and why none of them adds a parameter.تشرح ماذا يضع كل نوع من أنواع الحشو (padding) في الإطار، ولماذا لا يضيف أيٌّ منها أي معامل (parameter).
- Compute the receptive field after L layers, with and without stride, and explain why networks downsample.تحسب مجال الاستقبال (receptive field) بعد L طبقة مع الخطوة (stride) وبدونها، وتشرح لماذا تقلّص الشبكات الدقة (downsample).
- Count weights and multiply-accumulates for standard, grouped and depthwise convolutions, and check that g divides c_i and c_o.تعدّ الأوزان وعمليات الضرب والتجميع (MACs) للالتفاف العادي والمجمّع (grouped) والعمقي (depthwise)، وتتحقق من أن g يقسم c_i و c_o.
- Argue when a depthwise separable block is the right trade for an embedded device and what it gives up.تبرهن متى تكون كتلة depthwise separable هي المقايضة الصحيحة على جهاز مدمج (embedded)، وما الذي تتنازل عنه.
Take the 5 x 5 input and 3 x 3 Kernel (filter) from Part 02. The kernel can only sit where it fits entirely inside the image, so it has three horizontal and three vertical positions and the output Feature map is 3 x 3. Stack a second such layer and you get 1 x 1. A third layer cannot run at all. Every layer eats two rows and two columns, and Goodfellow, Bengio and Courville put it plainly: without padding the representation shrinks by one pixel less than the kernel width at every layer.
خذ المدخل 5 x 5 والنواة kernel بحجم 3 x 3 من الجزء 02. لا يمكن للنواة أن تقف إلا حيث تتسع بالكامل داخل الصورة، فلها ثلاثة مواضع أفقية وثلاثة رأسية، ويكون feature map الناتج بحجم 3 x 3. أضف طبقة ثانية مثلها فتحصل على 1 x 1. أما الطبقة الثالثة فلا يمكن تشغيلها أصلًا. كل طبقة تأكل صفّين وعمودين، ويقولها Goodfellow وBengio وCourville بوضوح: بدون حشو يتقلص التمثيل في كل طبقة بمقدار عرض النواة ناقص بكسل واحد.
Padding is the fix. Add a ring of p extra cells around the input before sliding the kernel. With p = 1 the 5 x 5 image becomes 7 x 7, the kernel now has five positions in each direction, and the output is 5 x 5 again. The same 27 weights per filter (3 x 3 over three input channels) do the work; the only thing that changed is where the kernel is allowed to stand.
الحل هو الحشو padding. أضف حلقة من p خلية إضافية حول المدخل قبل تمرير النواة. مع p = 1 تصبح الصورة 5 x 5 بحجم 7 x 7، وتصبح للنواة خمسة مواضع في كل اتجاه، ويعود المخرج إلى 5 x 5. الأوزان نفسها (weights) وعددها 27 لكل مرشّح (3 x 3 على ثلاث قنوات مدخلة) هي التي تقوم بالعمل؛ الشيء الوحيد الذي تغيّر هو المكان المسموح للنواة أن تقف فيه.
Read the formula as a story. The padded input is h_i + 2p rows tall. A window of height k_h can start on any row except the last k_h - 1, which leaves h_i + 2p - k_h + 1 starting rows, one per output row. Set h_o = h_i and the equation solves to p = (k - 1)/2, which is a whole number only for odd kernels. That is why 3 x 3, 5 x 5 and 7 x 7 dominate practice: Zhang et al. note that an odd kernel lets the same amount of padding go on both sides and keeps the output pixel centred on its window. Dumoulin and Visin call this half padding, and the deep learning book calls it same convolution, after the MATLAB option of that name.
اقرأ الصيغة كقصة. المدخل بعد الحشو ارتفاعه h_i + 2p صفًّا. نافذة ارتفاعها k_h يمكن أن تبدأ من أي صف ما عدا آخر k_h - 1 صفوف، فيبقى h_i + 2p - k_h + 1 صف بداية، صف مخرج لكل واحد منها. ضع h_o = h_i فتُحل المعادلة إلى p = (k - 1)/2، وهو عدد صحيح فقط عندما تكون النواة فردية. لهذا تسيطر 3 x 3 و5 x 5 و7 x 7 على الممارسة العملية: يلاحظ Zhang et al. أن النواة الفردية تسمح بوضع مقدار الحشو نفسه على الجانبين وتبقي بكسل المخرج في مركز نافذته. يسمي Dumoulin وVisin هذا بالحشو النصفي (half padding)، ويسميه كتاب التعلم العميق الالتفاف المتساوي (same convolution)، نسبةً إلى خيار MATLAB الذي يحمل هذا الاسم.
Worked exampleمثال محلول
Same, valid and a strided stemحشو same وحشو valid وبداية شبكة بخطوة
CIFAR-10 input, no paddingمدخل CIFAR-10 بدون حشو
32 x 32 image, k = 5, p = 0: 32 - 5 + 1 = 28. The map shrinks by four.صورة 32 x 32، k = 5، p = 0: 32 - 5 + 1 = 28. تتقلص الخريطة بمقدار أربعة.Same padding for k = 5حشو same عندما k = 5
p = (5 - 1)/2 = 2: 32 + 4 - 5 + 1 = 32. The map keeps its size.p = (5 - 1)/2 = 2: 32 + 4 - 5 + 1 = 32. تحافظ الخريطة على حجمها.The slide 16 exampleمثال الشريحة 16
h_i = 5, k = 3, p = 1: 5 + 2 - 3 + 1 = 5.h_i = 5، k = 3، p = 1: 5 + 2 - 3 + 1 = 5.Parameters in every caseالمعاملات في كل الحالات
Identical. A 3 x 3 filter over three input channels has 27 weights whether p is 0, 1 or 10. Padding changes the output size and therefore the multiply-accumulates, and nothing else.متطابقة. مرشّح 3 x 3 على ثلاث قنوات مدخلة له 27 وزنًا سواء كان p يساوي 0 أو 1 أو 10. الحشو يغيّر حجم المخرج وبالتالي عدد عمليات الضرب والتجميع، ولا شيء غير ذلك.
What goes in the ringماذا نضع في الحلقة
The formula does not care what the new cells contain, but the values do affect what the network learns near the border. The slide pads a 3 x 3 image holding 1 to 9 by p = 2 on every side, which is a good specimen because 2 is the largest reflection padding a 3 x 3 image can take. Zero padding writes zeros, and PyTorch uses it unless told otherwise. Reflection padding mirrors the image across its edge without repeating the edge pixel, so the row above 1 2 3 reads 4 5 6 and the row above that reads 7 8 9. Replication padding stretches each edge pixel outward, so the corner 1 fills the whole top-left 3 x 3 block. Constant padding is zero padding with a value of your choice.
الصيغة لا تهتم بما تحتويه الخلايا الجديدة، لكن القيم تؤثر فعلًا على ما تتعلمه الشبكة قرب الإطار. تحشو الشريحة صورة 3 x 3 تحمل القيم من 1 إلى 9 بمقدار p = 2 من كل جانب، وهي عينة جيدة لأن 2 هو أكبر حشو انعكاسي تتحمله صورة 3 x 3. الحشو الصفري zero padding يكتب أصفارًا، وتستخدمه PyTorch ما لم تطلب غيره. الحشو الانعكاسي reflection padding يعكس الصورة عبر حافتها دون تكرار بكسل الحافة نفسه، فالصف فوق 1 2 3 يُقرأ 4 5 6 والصف فوقه يُقرأ 7 8 9. الحشو التكراري replication padding يمدّ كل بكسل حافة إلى الخارج، فالركن 1 يملأ كتلة 3 x 3 العلوية اليسرى كاملة. والحشو الثابت (constant) هو حشو صفري لكن بقيمة تختارها أنت.
| Modeالنمط | Ruleالقاعدة | Row 0الصف 0 | PyTorchPyTorch |
|---|---|---|---|
| Zeroصفري (Zero) | Fill the border with 0املأ الإطار بالقيمة 0 | 0 0 0 0 0 0 00 0 0 0 0 0 0 | padding_mode='zeros' (default)padding_mode='zeros' (default) |
| Reflectionانعكاسي (Reflection) | Mirror across the edge; the edge value itself is not repeatedاعكس الصورة عبر الحافة؛ قيمة الحافة نفسها لا تتكرر | 9 8 7 8 9 8 79 8 7 8 9 8 7 | ReflectionPad2d, padding_mode='reflect'ReflectionPad2d, padding_mode='reflect' |
| Replicationتكراري (Replication) | Repeat the nearest edge value outwardكرّر أقرب قيمة على الحافة نحو الخارج | 1 1 1 2 3 3 31 1 1 2 3 3 3 | ReplicationPad2d, padding_mode='replicate'ReplicationPad2d, padding_mode='replicate' |
| Constantثابت (Constant) | Fill the border with one chosen value vاملأ الإطار بقيمة واحدة تختارها v | v v v v v v vv v v v v v v | ConstantPad2d(padding, value)ConstantPad2d(padding, value) |
The index rule behind the visual is short. For a padded coordinate j in 0 to 6, subtract p to get an offset into the original image. Replication clamps that offset into 0 to 2. Reflection folds it: -1 becomes 1, -2 becomes 2, 3 becomes 1 and 4 becomes 0. Fold both the row and the column offset and you have the source pixel. The PyTorch documentation for ReflectionPad2d walks through exactly this example with the values 0 to 8. PyTorch also offers a 'circular' mode that wraps around to the opposite edge; the slide leaves it out.
قاعدة الفهارس وراء الشكل قصيرة. لإحداثي محشو j من 0 إلى 6، اطرح p لتحصل على إزاحة داخل الصورة الأصلية. التكرار يحصر هذه الإزاحة داخل المدى 0 إلى 2. أما الانعكاس فيطويها: -1 تصبح 1، و-2 تصبح 2، و3 تصبح 1، و4 تصبح 0. اطوِ إزاحة الصف وإزاحة العمود معًا فتحصل على البكسل المصدر. توثيق PyTorch الخاص بـ ReflectionPad2d يشرح هذا المثال بالضبط بالقيم من 0 إلى 8. وتوفر PyTorch أيضًا نمط 'circular' الذي يلتف إلى الحافة المقابلة؛ والشريحة لا تذكره.
Recall
Input 5 x 5, kernel 3 x 3, stride 1. What padding keeps the output 5 x 5, and how many parameters does that padding add?مدخل 5 x 5، ونواة 3 x 3، وخطوة 1. ما الحشو الذي يبقي المخرج 5 x 5، وكم معاملًا يضيفه هذا الحشو؟
One output pixel of a 3 x 3 Convolution layer is a weighted sum of a 3 x 3 patch of its input. That patch is its Receptive field. Now feed that layer's output into a second 3 x 3 layer. One pixel of the second layer reads a 3 x 3 block of first-layer outputs, and each of those was itself computed from a 3 x 3 patch. The patches of neighbouring pixels overlap and slide by one cell, so the union of all nine is a 5 x 5 region of the original image. A third layer makes it 7 x 7.
بكسل مخرج واحد من طبقة التفاف convolution layer بحجم 3 x 3 هو مجموع موزون لرقعة 3 x 3 من مدخلها. هذه الرقعة هي مجال استقباله receptive field. الآن أدخل مخرج تلك الطبقة إلى طبقة ثانية 3 x 3. بكسل واحد من الطبقة الثانية يقرأ كتلة 3 x 3 من مخرجات الطبقة الأولى، وكل واحد منها حُسب بدوره من رقعة 3 x 3. رقع البكسلات المتجاورة تتداخل وتنزاح بخلية واحدة، فيكون اتحاد الرقع التسع منطقة 5 x 5 من الصورة الأصلية. والطبقة الثالثة تجعلها 7 x 7.
Each stride-1 layer adds k - 1 to the side of the region, because the outermost cells of the new window reach (k - 1)/2 further in each direction. Start from a single pixel and add k - 1 once per layer.
كل طبقة بخطوة 1 تضيف k - 1 إلى ضلع المنطقة، لأن الخلايا الطرفية في النافذة الجديدة تمتد (k - 1)/2 أبعد في كل اتجاه. ابدأ من بكسل واحد وأضف k - 1 مرة لكل طبقة.
With k = 3 this gives 3, 5, 7, 9, 11 for L = 1 to 5. The deep learning book's figure 9.4 makes the same point and adds the hint that matters next: the effect increases if the network includes strided convolution or pooling.
مع k = 3 تعطي هذه الصيغة 3, 5, 7, 9, 11 للقيم من L = 1 إلى 5. الشكل 9.4 في كتاب التعلم العميق يوضح النقطة نفسها ويضيف التلميح المهم لما يأتي: يزداد هذا الأثر إذا احتوت الشبكة على التفاف بخطوة (strided convolution) أو تجميع (pooling).
The problem with large imagesمشكلة الصور الكبيرة
A classifier must eventually let one output depend on the whole picture. For a 224 x 224 image and stride-1 3 x 3 layers, solve 2L + 1 >= 224: L = 112 layers before a single output pixel sees everything. Each of those layers works at full resolution, so the activation memory and the multiply-accumulates are enormous. The slide states the problem and the cure in one breath: downsample inside the network.
المصنّف (classifier) يجب في النهاية أن يجعل مخرجًا واحدًا يعتمد على الصورة كلها. لصورة 224 x 224 وطبقات 3 x 3 بخطوة 1، حُلّ 2L + 1 >= 224: L = 112 طبقة قبل أن يرى بكسل مخرج واحد كل شيء. كل طبقة منها تعمل بالدقة الكاملة، فتكون ذاكرة التنشيطات وعمليات الضرب والتجميع هائلة. الشريحة تذكر المشكلة والعلاج في نفس واحد: قلّص الدقة (downsample) داخل الشبكة.
Stride is the cheapest way to do it. Instead of moving the kernel one cell at a time, move it s cells. The number of window positions drops by about a factor of s in each direction, and so does the output size. Goodfellow, Bengio and Courville describe stride s as mathematically equivalent to a stride-1 convolution followed by keeping every s-th output, only without wasting work on the outputs you would throw away.
الخطوة stride هي أرخص طريقة لذلك. بدلًا من تحريك النواة خلية واحدة في كل مرة، حرّكها s خلايا. ينخفض عدد مواضع النافذة بمعامل s تقريبًا في كل اتجاه، وكذلك حجم المخرج. يصف Goodfellow وBengio وCourville الخطوة s بأنها مكافئة رياضيًا لالتفاف بخطوة 1 يتبعه الاحتفاظ بمخرج واحد من كل s مخرجات، لكن دون إهدار حساب على المخرجات التي كنت ستتخلص منها.
The floor is not decoration. The padded input has h_i + 2p - k_h rows on which a window can start after the first, and the kernel visits every s-th of them. If that span is not a multiple of s, the last window would run past the edge and is dropped. Try h_i = 8, k = 3, p = 0, s = 2: the slide's version gives 3.5, which is not a size; the true answer is floor(5/2) + 1 = 3. Dumoulin and Visin state the relationship with the floor, and the PyTorch Conv2d documentation computes the output shape the same way.
دالة floor ليست زينة. المدخل المحشو فيه h_i + 2p - k_h صفًّا يمكن أن تبدأ منها النافذة بعد الصف الأول، والنواة تزور واحدًا من كل s منها. إذا لم يكن هذا المدى من مضاعفات s، فإن النافذة الأخيرة ستتجاوز الحافة فتُحذف. جرّب h_i = 8، k = 3، p = 0، s = 2: صيغة الشريحة تعطي 3.5، وهذا ليس حجمًا؛ الجواب الصحيح هو floor(5/2) + 1 = 3. يكتب Dumoulin وVisin العلاقة مع floor، وتوثيق Conv2d في PyTorch يحسب شكل المخرج بالطريقة نفسها.
Change p or s and watch the output size and the MACs move while the weight count stays still. Only c_i, c_o, k and g touch the parameter count.غيّر p أو s وراقب حجم الإخراج وعدد عمليات MAC يتغيران بينما يبقى عدد الأوزان ثابتًا. وحدها c_i وc_o وk وg تؤثر في عدد المعاملات.
How stride changes the receptive field ruleكيف تغيّر الخطوة قاعدة مجال الاستقبال
Stride does more than shrink maps. Once a layer has stride 2, two neighbouring outputs of that layer sit two input pixels apart, so every layer after it takes steps of two in input coordinates. Its 3 x 3 window no longer adds 2 pixels to the receptive field; it adds 2 x 2 = 4. The slide 18 top row shows the payoff: a stride-2 first layer followed by one stride-1 layer already reaches 7, where stride 1 needed three layers.
الخطوة تفعل أكثر من تصغير الخرائط. بمجرد أن تكون لطبقة خطوة 2، يصبح المخرجان المتجاوران لتلك الطبقة على بعد بكسلين في المدخل، فتمشي كل طبقة بعدها بخطوات من اثنين في إحداثيات المدخل. نافذتها 3 x 3 لم تعد تضيف 2 بكسل إلى مجال الاستقبال؛ بل تضيف 2 x 2 = 4. الصف العلوي في الشريحة 18 يُظهر المكسب: طبقة أولى بخطوة 2 تتبعها طبقة واحدة بخطوة 1 تصل بالفعل إلى 7، بينما احتاجت الخطوة 1 إلى ثلاث طبقات.
Araujo, Norris and Sim give the general closed form. Call the product of the strides before layer l the jump (some authors say effective stride). Each layer adds (k_l - 1) times its jump.
يعطي Araujo وNorris وSim الصيغة المغلقة العامة. سمِّ حاصل ضرب الخطوات قبل الطبقة l بالقفزة jump (وبعض المؤلفين يسمونها الخطوة الفعلية effective stride). كل طبقة تضيف (k_l - 1) مضروبًا في قفزتها.
Worked exampleمثال محلول
Slide 18, top row, and one step furtherالشريحة 18، الصف العلوي، وخطوة أبعد
Layer 1: k = 3, s = 2الطبقة 1: k = 3، s = 2
Jump before it is 1, so RF = 1 + 2 x 1 = 3. Jump after it is 2.القفزة قبلها 1، إذن RF = 1 + 2 x 1 = 3. والقفزة بعدها 2.Layer 2: k = 3, s = 1الطبقة 2: k = 3، s = 1
RF = 3 + 2 x 2 = 7. Two layers reach what three stride-1 layers reached.RF = 3 + 2 x 2 = 7. طبقتان تصلان إلى ما وصلت إليه ثلاث طبقات بخطوة 1.Add a third stride-1 layerأضف طبقة ثالثة بخطوة 1
Jump is still 2: RF = 7 + 2 x 2 = 11.القفزة ما زالت 2: RF = 7 + 2 x 2 = 11.Or make layers 1 and 2 both stride 2أو اجعل الطبقتين 1 و 2 كلتيهما بخطوة 2
Jumps 1, 2, 4: RF = 1 + 2 + 4 + 8 = 15 after three 3 x 3 layers.القفزات 1, 2, 4: RF = 1 + 2 + 4 + 8 = 15 بعد ثلاث طبقات 3 x 3.The shortcut only holds for stride 1الاختصار يصح فقط مع خطوة 1
L(k - 1) + 1 would predict 7 for all three-layer cases. The general formula gives 7, 11 or 15 depending on where the strides sit.الصيغة L(k - 1) + 1 كانت ستتنبأ بـ 7 في كل حالات الطبقات الثلاث. أما الصيغة العامة فتعطي 7 أو 11 أو 15 بحسب مواضع الخطوات.
| Layersعدد الطبقات | All stride 1كل الخطوات 1 | First layer stride 2الطبقة الأولى بخطوة 2 | Every layer stride 2كل طبقة بخطوة 2 |
|---|---|---|---|
| 11 | 33 | 33 | 33 |
| 22 | 55 | 77 | 77 |
| 33 | 77 | 1111 | 1515 |
| 44 | 99 | 1515 | 3131 |
The jump column is the jump before each layer: the product of the strides of the layers above it. The layer adds (k - 1) times that jump. A stride of 2 in layer 1 doubles how much every later layer adds, which is why the general value pulls away from L(k - 1) + 1.عمود jump هو القفزة قبل كل طبقة: حاصل ضرب خطوات الطبقات التي تسبقها. تضيف الطبقة (k - 1) مضروبًا في تلك القفزة. الخطوة 2 في الطبقة 1 تضاعف ما تضيفه كل طبقة بعدها، ولهذا تبتعد القيمة العامة عن L(k - 1) + 1.
Real networks live at the strided end of that table. Araujo, Norris and Sim compute receptive fields of 195 for AlexNet, 212 for VGG-16, 483 for ResNet-50 and 1,311 for Inception-v3, all with a total downsampling of 32, and remark that the receptive field usually covers the entire input image. The stride in the ResNet stem you computed in the calculator is the first of those doublings: 224 to 112 in one layer.
الشبكات الحقيقية تعيش في طرف الخطوات من ذلك الجدول. يحسب Araujo وNorris وSim مجالات استقبال قدرها 195 لـ AlexNet، و212 لـ VGG-16، و483 لـ ResNet-50، و1,311 لـ Inception-v3، وكلها بتقليص كلي للدقة قدره 32، ويلاحظون أن مجال الاستقبال يغطي عادةً صورة المدخل بأكملها. الخطوة في بداية ResNet (stem) التي حسبتها في الحاسبة هي أول هذه المضاعفات: من 224 إلى 112 في طبقة واحدة.
Quick checkاختبار سريع
A 32 x 32 input passes through a 5 x 5 kernel with padding 2 and stride 2. What is the output height?يمر مدخل 32 x 32 عبر نواة 5 x 5 بحشو 2 وخطوة 2. ما ارتفاع المخرج؟
Quick checkاختبار سريع
Three 3 x 3 convolutions have strides 2, 1 and 1 in that order. What is the receptive field of one output pixel?ثلاثة التفافات 3 x 3 خطواتها 2 ثم 1 ثم 1 بهذا الترتيب. ما مجال الاستقبال لبكسل مخرج واحد؟
Recall
Write the output size formula with stride and say why the floor is needed.اكتب صيغة حجم المخرج مع الخطوة، وقل لماذا نحتاج floor.
Recall
Three 3 x 3 layers with strides 2, 2, 1: what is the receptive field?ثلاث طبقات 3 x 3 بخطوات 2, 2, 1: ما مجال الاستقبال؟
Padding and stride change the shape of the computation. The last two concepts change its wiring. Take a layer with c_i = c_o = 64 channels and 3 x 3 kernels. Each of the 64 filters reads all 64 input channels, so the layer holds 64 x 64 x 9 = 36,864 weights. Now split the channels into two halves and run two separate convolutions, each mapping 32 inputs to 32 outputs. Each half holds 32 x 32 x 9 = 9,216 weights, so the pair holds 18,432: the same output shape at half the cost.
الحشو والخطوة يغيّران شكل الحساب. أما المفهومان الأخيران فيغيّران توصيلاته. خذ طبقة فيها c_i = c_o = 64 قناة (channels) ونوى 3 x 3. كل مرشّح من المرشّحات الـ 64 يقرأ كل القنوات المدخلة الـ 64، فتحمل الطبقة 64 x 64 x 9 = 36,864 وزنًا. الآن قسّم القنوات إلى نصفين وشغّل التفافين منفصلين، كل واحد يحوّل 32 مدخلًا إلى 32 مخرجًا. كل نصف يحمل 32 x 32 x 9 = 9,216 وزنًا، فيحمل الزوج 18,432: شكل المخرج نفسه بنصف التكلفة.
That is a Grouped convolution with g = 2, or in the slide's words a group of narrower convolutions. In general, split c_i into g blocks of c_i/g channels and c_o into g blocks of c_o/g. Output block j reads only input block j. The slide writes the new Weight shape as (g · c_o/g, c_i/g, k_h, k_w), and the first factor simplifies to c_o: there are still c_o filters, each just reads c_i/g channels instead of all c_i. The Bias stays one per output channel.
هذا التفاف مجمّع grouped convolution مع g = 2، أو بتعبير الشريحة: مجموعة من الالتفافات الأضيق. بشكل عام، قسّم c_i إلى g كتل كل منها c_i/g قناة، وقسّم c_o إلى g كتل كل منها c_o/g. كتلة المخرج j تقرأ كتلة المدخل j فقط. تكتب الشريحة الشكل الجديد لموتّر الأوزان weight هكذا: (g · c_o/g, c_i/g, k_h, k_w)، والعامل الأول يُختصر إلى c_o: ما زال هناك c_o مرشّحًا، لكن كل واحد يقرأ c_i/g قناة بدل كل القنوات c_i. ويبقى الانحياز bias واحدًا لكل قناة مخرجة.
| Groupsالمجموعات | c_o x (c_i/g) x k x kc_o x (c_i/g) x k x k | Weightsالأوزان | Savingالتوفير |
|---|---|---|---|
| g = 1g = 1 | 64 x 64 x 964 x 64 x 9 | 36,86436,864 | 1x1x |
| g = 2g = 2 | 64 x 32 x 964 x 32 x 9 | 18,43218,432 | 2x2x |
| g = 4g = 4 | 64 x 16 x 964 x 16 x 9 | 9,2169,216 | 4x4x |
| g = 8g = 8 | 64 x 8 x 964 x 8 x 9 | 4,6084,608 | 8x8x |
| g = 64g = 64 | 64 x 1 x 964 x 1 x 9 | 576576 | 64x64x |
Each drawn lane stands for 8 real channels. 32 of 64 lane pairs stay connected.كل مسار مرسوم يمثل 8 قناة حقيقية. يبقى 32 من أصل 64 زوجًا من المسارات متصلًا.
The divisibility ruleقاعدة قابلية القسمة
The split only works if both channel counts divide evenly. PyTorch says so directly: in_channels and out_channels must both be divisible by groups. With c_i = 64 and c_o = 96, g = 4 is fine (16 inputs and 24 outputs per group) but g = 5 is not, because 64/5 is not an integer. Output channel j belongs to group floor(j / (c_o/g)) and reads only the input channels of that group. The calculator earlier in this part flags any pair that fails the rule.
التقسيم لا ينجح إلا إذا انقسم عددا القنوات كلاهما بالتساوي. توثيق PyTorch يقولها صراحة: يجب أن يقبل كلٌّ من in_channels وout_channels القسمة على groups. مع c_i = 64 وc_o = 96، القيمة g = 4 مقبولة (16 مدخلًا و24 مخرجًا لكل مجموعة) لكن g = 5 غير مقبولة، لأن 64/5 ليس عددًا صحيحًا. قناة المخرج j تنتمي إلى المجموعة floor(j / (c_o/g)) ولا تقرأ إلا قنوات المدخل في تلك المجموعة. الحاسبة السابقة في هذا الجزء تنبّه على أي زوج يخالف القاعدة.
The idea is older than its name. Krizhevsky, Sutskever and Hinton trained AlexNet on two GTX 580 GPUs with 3 GB each, so they put half of the kernels on each GPU and let the kernels of layer 4 take input only from the kernel maps of layer 3 that lived on the same GPU. That is g = 2, chosen for memory rather than elegance. ResNeXt (Xie et al., 2017) turned the group count into a design axis they named cardinality and found that raising it was more effective than going deeper or wider at the same parameter budget.
الفكرة أقدم من اسمها. درّب Krizhevsky وSutskever وHinton شبكة AlexNet على معالجَي رسوميات GTX 580 في كلٍّ منهما 3 GB، فوضعوا نصف النوى على كل معالج، وجعلوا نوى الطبقة 4 تأخذ مدخلها فقط من خرائط الطبقة 3 الموجودة على المعالج نفسه. هذا هو g = 2، واختير بسبب الذاكرة لا بسبب الأناقة. ثم حوّلت ResNeXt (Xie et al., 2017) عدد المجموعات إلى محور تصميم سمّوه cardinality، ووجدوا أن زيادته أكثر فاعلية من زيادة العمق أو العرض بنفس ميزانية المعاملات.
That last clause is the cost. Inside a grouped layer, no information crosses a group boundary; the faint grey lines in the visual are exactly the missing connections. Networks that use grouped layers alternate them with something that mixes channels again, most often a 1 x 1 convolution. The next concept pushes g to its limit and meets that need head on.
هذه الجملة الأخيرة هي التكلفة. داخل الطبقة المجمّعة لا تعبر أي معلومة حدود المجموعة؛ والخطوط الرمادية الباهتة في الشكل هي بالضبط الوصلات المفقودة. الشبكات التي تستخدم طبقات مجمّعة تبادل بينها وبين شيء يخلط القنوات من جديد، وغالبًا ما يكون التفافًا 1 x 1. المفهوم التالي يدفع g إلى أقصى حد ويواجه هذه الحاجة مباشرة.
Quick checkاختبار سريع
A layer has c_i = c_o = 128, k = 3, g = 8 and no bias. How many weights does it hold?طبقة فيها c_i = c_o = 128 و k = 3 و g = 8 وبدون انحياز. كم وزنًا تحمل؟
Recall
c_i = c_o = 64, k = 3. How many weights for g = 1, g = 4 and depthwise (g = 64)?c_i = c_o = 64، k = 3. كم وزنًا عندما g = 1، و g = 4، وفي الحالة العمقية (g = 64)؟
Push the group count as far as it goes: g = c_i = c_o. Every group is now one Channel in and one channel out, so each channel gets its own k x k filter and nothing else. For 64 channels and 3 x 3 kernels that is 64 x 9 = 576 weights, 64 times fewer than the 36,864 of the standard layer. The slide 20 diagram says it in one picture: eight straight arrows from input strip to output strip, and not one of them crosses.
ادفع عدد المجموعات إلى أقصى ما يمكن: g = c_i = c_o. كل مجموعة الآن قناة channel واحدة تدخل وقناة واحدة تخرج، فتحصل كل قناة على مرشّح k x k خاص بها ولا شيء غيره. لـ 64 قناة ونوى 3 x 3 يكون ذلك 64 x 9 = 576 وزنًا، أي أقل بـ 64 مرة من 36,864 في الطبقة العادية. مخطط الشريحة 20 يقولها في صورة واحدة: ثمانية أسهم مستقيمة من شريط المدخل إلى شريط المخرج، ولا يتقاطع أيٌّ منها.
This is a Depthwise convolution. The slide writes its Weight tensor as (c, k_h, k_w), one k_h x k_w filter per channel. Frameworks keep the four-dimensional layout: PyTorch stores (out_channels, in_channels/groups, kH, kW), which with groups = in_channels is (c, 1, k_h, k_w), the same numbers with a singleton axis. PyTorch also allows out_channels = K · in_channels with groups = in_channels, a depthwise layer with depth multiplier K; the slide's g = c_i = c_o is the case K = 1.
هذا التفاف عمقي depthwise convolution. تكتب الشريحة موتّر أوزانه weight بالشكل (c, k_h, k_w)، أي مرشّح k_h x k_w واحد لكل قناة. لكن أطر العمل تحتفظ بالتخطيط رباعي الأبعاد: PyTorch تخزّن (out_channels, in_channels/groups, kH, kW)، وهو مع groups = in_channels يصبح (c, 1, k_h, k_w)، الأرقام نفسها مع محور طوله واحد. وتسمح PyTorch أيضًا بـ out_channels = K · in_channels مع groups = in_channels، وهي طبقة عمقية بمضاعِف عمق (depth multiplier) قدره K؛ وحالة الشريحة g = c_i = c_o هي الحالة K = 1.
Is this reduction in the number of weights really good?هل هذا التخفيض في عدد الأوزان جيد فعلًا؟
The slide asks the question in red and leaves it open. The honest answer has two halves. For storage and arithmetic, yes: 576 weights fit in 2.3 KB of fp32 flash, and the multiply-accumulates fall by the same factor of 64. For what the layer can learn, no. A depthwise layer filters each channel in isolation. Sandler et al. describe it as lightweight filtering that applies a single convolutional filter per input channel, and Howard et al. point out that it does not combine input channels, so it cannot build a feature that depends on two channels at once. A standard layer does that at every position; a depthwise layer never does.
تطرح الشريحة السؤال باللون الأحمر وتتركه مفتوحًا. الجواب الصادق له نصفان. من ناحية التخزين والحساب، نعم: 576 وزنًا تتسع في 2.3 KB من ذاكرة flash بصيغة fp32، وعمليات الضرب والتجميع تنخفض بالمعامل نفسه 64. أما من ناحية ما تستطيع الطبقة تعلّمه، فلا. الطبقة العمقية ترشّح كل قناة بمعزل عن غيرها. يصفها Sandler et al. بأنها ترشيح خفيف يطبّق مرشّحًا التفافيًا واحدًا لكل قناة مدخلة، ويشير Howard et al. إلى أنها لا تدمج القنوات المدخلة، فلا تستطيع بناء ميزة (feature) تعتمد على قناتين معًا. الطبقة العادية تفعل ذلك في كل موضع؛ والطبقة العمقية لا تفعله أبدًا.
The repair is a 1 x 1 convolution, called a pointwise convolution, placed right after it. With k = 1 it looks at one position and mixes all c_i channels into c_o outputs: W of shape (c_o, c_i, 1, 1), c_o · c_i weights, and in Sandler et al.'s words it is responsible for building new features through computing linear combinations of the input channels. The depthwise layer handles space, the pointwise layer handles channels, and the pair is called a depthwise separable convolution. It is the building block of MobileNet and, wrapped in an expand and project pair of 1 x 1 layers, of MobileNetV2.
الإصلاح هو التفاف 1 x 1، يسمى الالتفاف النقطي (pointwise convolution)، يوضع بعدها مباشرة. مع k = 1 ينظر إلى موضع واحد ويخلط كل القنوات c_i في c_o مخرجًا: W بالشكل (c_o, c_i, 1, 1)، وc_o · c_i وزنًا، وبتعبير Sandler et al. هو المسؤول عن بناء ميزات جديدة بحساب تراكيب خطية من القنوات المدخلة. الطبقة العمقية تتولى المكان، والطبقة النقطية تتولى القنوات، ويسمى الزوج التفافًا عمقيًا قابلًا للفصل depthwise separable convolution. وهو حجر البناء في MobileNet، وفي MobileNetV2 بعد إحاطته بزوج من طبقات 1 x 1 للتوسيع والإسقاط (expand وproject).
| Layerالطبقة | Weightsالأوزان | Biasesالانحيازات | Weights + biasesالأوزان + الانحيازات | Fewer weights than standardعدد أوزان أقل من الطبقة العادية بـ |
|---|---|---|---|---|
| Standard 3 x 3 (g = 1)عادية 3 x 3 (g = 1) | 36,86436,864 | 6464 | 36,92836,928 | 1x1x |
| Grouped, g = 2مجمّعة، g = 2 | 18,43218,432 | 6464 | 18,49618,496 | 2x2x |
| Grouped, g = 4مجمّعة، g = 4 | 9,2169,216 | 6464 | 9,2809,280 | 4x4x |
| Depthwise, g = 64عمقية، g = 64 | 576576 | 6464 | 640640 | 64x64x |
| Depthwise + 1 x 1 pointwiseعمقية + نقطية 1 x 1 | 576 + 4,096 = 4,672576 + 4,096 = 4,672 | 128128 | 4,8004,800 | 7.9x7.9x |
The last row is the one to remember. Howard et al. derive the cost ratio of a separable block to a standard layer as 1/N + 1/D_K², where N is the output channel count and D_K the kernel size. With N = 64 and D_K = 3 that is 1/64 + 1/9 ≈ 0.127, so the block is about 7.9 times cheaper, matching the table. For wide layers the 1/N term fades and the saving approaches D_K² = 9; Howard et al. report 8 to 9 times less computation for 3 x 3 kernels at a small accuracy cost.
الصف الأخير هو الذي يجب أن تتذكره. يشتق Howard et al. نسبة تكلفة الكتلة القابلة للفصل إلى الطبقة العادية بأنها 1/N + 1/D_K²، حيث N عدد قنوات المخرج وD_K حجم النواة. مع N = 64 وD_K = 3 تكون 1/64 + 1/9 ≈ 0.127، فالكتلة أرخص بنحو 7.9 مرة، وهذا يطابق الجدول. في الطبقات العريضة يتلاشى الحد 1/N ويقترب التوفير من D_K² = 9؛ ويذكر Howard et al. حسابًا أقل بـ 8 إلى 9 مرات لنوى 3 x 3 مقابل خسارة صغيرة في الدقة.
Multiply-accumulates on a 56 x 56 map (h_o · w_o = 3,136)عمليات الضرب والتجميع على خريطة 56 x 56 (h_o · w_o = 3,136)
- Standard 3 x 3, 64 to 64عادية 3 x 3، من 64 إلى 64
- 36,864 x 3,136 = 115,605,504 (about 115.6 M)36,864 x 3,136 = 115,605,504 (نحو 115.6 M)
- Depthwise 3 x 3 plus 1 x 1 pointwiseعمقية 3 x 3 مع نقطية 1 x 1
- 4,672 x 3,136 = 14,651,392 (about 14.7 M)4,672 x 3,136 = 14,651,392 (نحو 14.7 M)
- Ratioالنسبة
- about 7.9xنحو 7.9x
What this means on an embedded deviceماذا يعني هذا على جهاز مدمج
- Fewer weights means less flash and less weight traffic from memory, which on a microcontroller is often the whole budget.أوزان أقل تعني ذاكرة flash أقل ونقلًا أقل للأوزان من الذاكرة، وهذا على المتحكم الدقيق (microcontroller) يكون غالبًا هو الميزانية كلها.
- Fewer multiply-accumulates means lower latency and lower energy per inference on the same core.عمليات ضرب وتجميع أقل تعني زمن تأخير (latency) أقل وطاقة أقل لكل استدلال على النواة الحسابية نفسها.
- The depthwise layer does only k² multiply-accumulates per output value, against c_i · k² for a standard layer, yet still reads a k x k window per output, so it moves a lot of activation data for little arithmetic and on many accelerators is bound by memory bandwidth rather than compute. Treat the parameter saving as an upper bound on the speedup, not a promise.الطبقة العمقية لا تنفّذ إلا k² عملية ضرب وتجميع لكل قيمة مخرجة، مقابل c_i · k² في الطبقة العادية، ومع ذلك ما زالت تقرأ نافذة k x k لكل مخرج، فهي تنقل الكثير من بيانات التنشيطات مقابل حساب قليل، وعلى كثير من المسرّعات يكون أداؤها محدودًا بعرض نطاق الذاكرة (memory bandwidth) لا بالحساب. اعتبر توفير المعاملات حدًّا أعلى للتسريع، لا وعدًا به.
- Accuracy drops a little relative to a standard layer of the same shape, which is the price of removing the cross-channel terms.تنخفض الدقة (accuracy) قليلًا مقارنة بطبقة عادية بالشكل نفسه، وهذا هو ثمن إزالة الحدود التي تربط القنوات ببعضها.
Quick checkاختبار سريع
Why does MobileNet follow each depthwise convolution with a 1 x 1 pointwise convolution?لماذا تُتبع MobileNet كل التفاف عمقي (depthwise) بالتفاف نقطي (pointwise) بحجم 1 x 1؟
Recall
Why is a depthwise convolution alone a weak layer on an embedded device, and what fixes it?لماذا يكون الالتفاف العمقي وحده طبقة ضعيفة على جهاز مدمج، وما الذي يصلحه؟
Recall
c_i = c_o = 64, k = 3. How many weights does a depthwise 3 x 3 plus 1 x 1 pointwise block hold, and how does it compare with the standard layer?c_i = c_o = 64، k = 3. كم وزنًا تحمل كتلة depthwise بحجم 3 x 3 مع pointwise بحجم 1 x 1، وكيف تقارن بالطبقة العادية؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- h_o = floor((h_i + 2p - k_h)/s) + 1. With s = 1 and p = (k - 1)/2 the output keeps the input size.h_o = floor((h_i + 2p - k_h)/s) + 1. عندما s = 1 و p = (k - 1)/2 يحافظ المخرج على حجم المدخل.
- Zero, reflection, replication and constant padding change only the border cells. W is unchanged.الحشو الصفري والانعكاسي والتكراري والثابت (zero و reflection و replication و constant) يغيّر خلايا الإطار فقط. أما W فلا يتغير.
- One 3 x 3 layer sees 3 x 3. Each stride-1 layer adds k - 1, so L layers give L(k - 1) + 1.طبقة 3 x 3 واحدة ترى 3 x 3. كل طبقة بخطوة 1 تضيف k - 1، لذلك L طبقة تعطي L(k - 1) + 1.
- With stride, RF_L = RF_(L-1) + (k - 1) times the product of the earlier strides. Stride 2 in layer 1 gives 7 after two 3 x 3 layers.مع الخطوة: RF_L = RF_(L-1) + (k - 1) مضروبًا في حاصل ضرب خطوات الطبقات السابقة. خطوة 2 في الطبقة الأولى تعطي 7 بعد طبقتين 3 x 3.
- Stride and padding change activations and MACs, never the parameter count.الخطوة والحشو يغيّران التنشيطات (activations) وعدد MACs، ولا يغيّران عدد المعاملات أبدًا.
- Grouped convolution: W is (c_o, c_i/g, k_h, k_w), g times fewer weights, and c_i and c_o must both be divisible by g.الالتفاف المجمّع (grouped): شكل W هو (c_o, c_i/g, k_h, k_w)، أي أوزان أقل بـ g مرة، ويجب أن يقبل كلٌّ من c_i و c_o القسمة على g.
- Depthwise convolution is g = c_i = c_o: one k x k filter per channel and no mixing across channels.الالتفاف العمقي (depthwise) هو الحالة g = c_i = c_o: مرشّح k x k واحد لكل قناة ولا خلط بين القنوات.
- 64 to 64 channels, 3 x 3: 36,864 weights standard, 576 depthwise, 4,672 depthwise plus 1 x 1, about 7.9x fewer.من 64 إلى 64 قناة بنواة 3 x 3: الطبقة العادية 36,864 وزنًا، والعمقية 576، والعمقية مع 1 x 1 تساوي 4,672، أي أقل بنحو 7.9 مرة.
- The 1 x 1 pointwise convolution restores cross-channel mixing. MobileNets are built on this pair.الالتفاف النقطي (pointwise) 1 x 1 يعيد الخلط بين القنوات. شبكات MobileNet مبنية على هذا الزوج.
Sourcesالمصادر
- torch.nn.Conv2dDocsPyTorch documentationOutput shape with floor, groups divisibility, weight shape (out, in/groups, kH, kW), padding modes, padding='same' only for stride 1, depthwise as groups = in_channels.شكل المخرج مع floor، وقابلية القسمة على groups، وشكل الأوزان (out, in/groups, kH, kW)، وأنماط الحشو، و padding='same' للخطوة 1 فقط، والالتفاف العمقي على أنه groups = in_channels.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.ReflectionPad2dDocsPyTorch documentationWorked 3 x 3 to 7 x 7 reflection example.مثال محلول للحشو الانعكاسي من 3 x 3 إلى 7 x 7.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.ReplicationPad2dDocsPyTorch documentationEdge replication example.مثال على تكرار الحافة.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.ConstantPad2dDocsPyTorch documentationConstant padding with a chosen value.حشو ثابت بقيمة تختارها.(opens in a new tab)(يفتح في تبويب جديد)
- A guide to convolution arithmetic for deep learningPaperDumoulin and Visin, arXiv, 2016 (revised 2018)Relationship 6: o = floor((i + 2p - k)/s) + 1; half padding p = floor(k/2).العلاقة 6: o = floor((i + 2p - k)/s) + 1؛ والحشو النصفي p = floor(k/2).(opens in a new tab)(يفتح في تبويب جديد)
- Computing Receptive Fields of Convolutional Neural NetworksPaperAraujo, Norris and Sim, Distill, 2019Closed-form receptive field with strides; AlexNet 195, VGG-16 212, ResNet-50 483, Inception-v3 1311.صيغة مغلقة لمجال الاستقبال مع الخطوات؛ AlexNet 195، و VGG-16 212، و ResNet-50 483، و Inception-v3 1311.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 9: Convolutional NetworksBookGoodfellow, Bengio and Courville, MIT Press, 2016Figure 9.4 receptive field growth; section 9.5 on zero padding (valid, same, full) and stride as convolution plus downsampling.الشكل 9.4 عن نمو مجال الاستقبال؛ والقسم 9.5 عن الحشو الصفري (valid و same و full) والخطوة بوصفها التفافًا يتبعه تقليص للدقة.(opens in a new tab)(يفتح في تبويب جديد)
- Dive into Deep Learning, section 7.3: Padding and StrideBookZhang, Lipton, Li and SmolaPadding and stride formulas with floor; odd kernels for symmetric padding.صيغ الحشو والخطوة مع floor؛ والنوى الفردية من أجل حشو متماثل.(opens in a new tab)(يفتح في تبويب جديد)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperHoward et al., arXiv, 2017Depthwise separable cost ratio 1/N + 1/D_K^2; 8 to 9 times less computation for 3 x 3; depthwise does not combine channels.نسبة تكلفة depthwise separable هي 1/N + 1/D_K^2؛ وحساب أقل بـ 8 إلى 9 مرات لنوى 3 x 3؛ والالتفاف العمقي لا يدمج القنوات.(opens in a new tab)(يفتح في تبويب جديد)
- MobileNetV2: Inverted Residuals and Linear BottlenecksPaperSandler et al., CVPR 2018Section 3.1: single filter per input channel; pointwise builds features as linear combinations of channels; expand, depthwise, project block.القسم 3.1: مرشّح واحد لكل قناة مدخلة؛ والطبقة النقطية تبني الميزات كتراكيب خطية من القنوات؛ وكتلة expand ثم depthwise ثم project.(opens in a new tab)(يفتح في تبويب جديد)
- ImageNet Classification with Deep Convolutional Neural NetworksPaperKrizhevsky, Sutskever and Hinton, NeurIPS 2012Section 3.2: half the kernels on each GPU, layer 4 reads only same-GPU maps, the origin of grouped convolution.القسم 3.2: نصف النوى على كل GPU، والطبقة 4 تقرأ فقط الخرائط الموجودة على المعالج نفسه، وهذا أصل الالتفاف المجمّع.(opens in a new tab)(يفتح في تبويب جديد)
- Aggregated Residual Transformations for Deep Neural Networks (ResNeXt)PaperXie et al., CVPR 2017Cardinality via grouped convolutions; more effective than going deeper or wider at equal cost.الـ cardinality عبر الالتفافات المجمّعة؛ أكثر فاعلية من زيادة العمق أو العرض بالتكلفة نفسها.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, lecture 2DocsSong Han, MIT HAN LabThe deck these slides follow.العرض التقديمي الذي تتبعه شرائح هذه المحاضرة.(opens in a new tab)(يفتح في تبويب جديد)
Part 04: Pooling and what CNN filters learnالتجميع (Pooling) وما تتعلمه مرشحات CNN
Parameter-free downsampling with max and average pooling, the feature hierarchy a CNN learns, and two interactive demos.تقليل الأبعاد بدون معاملات عبر التجميع الأقصى (max) والمتوسط (average)، والتسلسل الهرمي للسمات الذي تتعلمه شبكة CNN، وعرضان تفاعليان.
4 concepts4 مفاهيم, ، slides 21-26الشرائح 21-26
Why this part mattersلماذا يهمّ هذا الجزء
Pooling is the cheapest layer you will ever deploy: zero parameters, four times fewer activations, and it is therefore the first tool for fitting a CNN into microcontroller SRAM. The feature hierarchy that follows it explains why pruning, quantization and transfer learning treat early and late layers differently.
The two skills this part drills are pure counting: the output shape of a pooling layer and the parameter count of a convolution layer. That same counting is the first step of every embedded model budget, because parameters set flash and activation shapes set SRAM. The two browser demos at the end give you the numbers layer by layer so you can check yourself.
الـ pooling هو أرخص طبقة ستنشرها على الإطلاق: صفر معاملات، وعدد activations أقل بأربع مرات، ولذلك فهو الأداة الأولى لإدخال شبكة CNN في ذاكرة SRAM الخاصة بالمتحكم الدقيق. والتسلسل الهرمي للخصائص الذي يأتي بعده يشرح لماذا تتعامل تقنيات pruning وquantization وtransfer learning مع الطبقات الأولى والأخيرة بطرق مختلفة.
المهارتان اللتان يدرّبك عليهما هذا الجزء عدٌّ خالص: شكل مخرج طبقة pooling، وعدد معاملات طبقة convolution. وهذا العدّ نفسه هو الخطوة الأولى في كل ميزانية لنموذج مدمج، لأن المعاملات تحدد ذاكرة flash وأشكال الـ activations تحدد ذاكرة SRAM. والعرضان التفاعليان في المتصفح في نهاية الجزء يعطيانك الأرقام طبقةً طبقة لتتحقق من نفسك.
By the end you canفي نهاية هذا الجزء ستستطيع
- Compute the output shape of any pooling layer from W1, H1, C, F and S, and state why its parameter count is zero.احسب شكل مخرج أي طبقة pooling انطلاقًا من W1 وH1 وC وF وS، واذكر لماذا يساوي عدد معاملاتها صفرًا.
- Evaluate max and average pooling by hand on a small slice and explain the small translation invariance max pooling brings.طبّق max pooling وaverage pooling يدويًا على شريحة صغيرة، واشرح الثبات التقريبي أمام الإزاحات الصغيرة (translation invariance) الذي يضيفه max pooling.
- Describe the edges, parts, objects feature hierarchy and tie it to receptive-field growth through CONV-RELU-POOL blocks.صِف تسلسل الخصائص الهرمي: حواف ثم أجزاء ثم أجسام كاملة، واربطه بنمو الحقل الاستقبالي (receptive field) عبر كتل CONV-RELU-POOL.
- Count the parameters of a conv layer from its filter shape, including biases, as in the ConvNetJS first layer.احسب عدد معاملات طبقة conv من شكل الـ filter مع إضافة الـ biases، كما في الطبقة الأولى من ConvNetJS.
- Chain shapes through a real small network (CNN Explainer) and separate the layers that cost parameters from those that cost only activations.تتبّع الأشكال عبر شبكة صغيرة حقيقية (CNN Explainer)، وافصل بين الطبقات التي تكلّف معاملات والطبقات التي لا تكلّف إلا activations.
A VGG-style block hands the next layer a volume of 224 x 224 x 64, which is 3,211,264 activations. A 2 x 2 pool with stride 2 returns 112 x 112 x 64, which is 802,816. Nothing was learned, nothing was mixed across channels, and every one of the 64 maps was simply shrunk on its own. That is the whole job of a Pooling layer.
تسلّم كتلة على نمط VGG الطبقة التالية حجمًا قدره 224 x 224 x 64، أي 3,211,264 قيمة activation. وطبقة pool بنافذة 2 x 2 وخطوة 2 تُرجع 112 x 112 x 64، أي 802,816. لم يُتعلَّم شيء، ولم يُمزج شيء عبر القنوات، وكل خريطة من الخرائط الـ 64 صُغّرت ببساطة وحدها. هذه هي وظيفة pooling layer كاملة.
The rule behind the example: a pooling layer applies a fixed summary over a small window of each activation map, one map at a time. Two summaries are common. Max pooling keeps the largest value in the window, and Average pooling keeps the mean. Because the summary is computed inside one Channel and never across channels, the depth of the volume is preserved while the spatial size falls. Pooling is therefore the second Downsampling tool of this lecture, next to Stride, and its output is a smaller Feature map per channel.
القاعدة وراء المثال: طبقة الـ pooling تطبّق تلخيصًا ثابتًا على نافذة صغيرة من كل خريطة activation، خريطةً واحدة في كل مرة. وهناك تلخيصان شائعان: max pooling يحتفظ بأكبر قيمة في النافذة، وaverage pooling يحتفظ بالمتوسط. ولأن التلخيص يُحسب داخل channel واحدة ولا يُحسب أبدًا عبر القنوات، يبقى عمق الحجم محفوظًا بينما يصغر حجمه المكاني. لذلك فالـ pooling هو الأداة الثانية للـ downsampling في هذه المحاضرة إلى جانب stride، ومخرجه feature map أصغر لكل قناة.
Slide 21 pinned as shapesالشريحة 21 مثبّتة على شكل أبعاد
- Inputالمدخل
- 224 x 224 x 64224 x 224 x 64
- Spatial extent F, stride Sالامتداد المكاني F، والخطوة S
- 2, 22, 2
- Outputالمخرج
- 112 x 112 x 64112 x 112 x 64
- Channelsالقنوات
- Unchanged, 64 in and 64 outدون تغيير، 64 تدخل و64 تخرج
- Activationsالـ activations
- 3,211,264 to 802,816 (75% discarded)من 3,211,264 إلى 802,816 (يُستبعد 75%)
- Learned parametersالمعاملات المتعلَّمة
- 00
Why max pooling is the default, and why that is a heuristicلماذا يُعدّ max pooling الخيار الافتراضي، ولماذا هذه قاعدة تجريبية
Pooling of any kind brings an approximate translation invariance, and max pooling shows it most sharply, which is why slide 21 attaches "Introduces spatial invariance" to max pooling. Goodfellow, Bengio and Courville describe pooling in all its forms as making the representation "approximately invariant to small translations of the input": if the input shifts by a pixel, the values of most pooled outputs do not change, because the maximum is still inside its window. Their example is face detection, where the network need not know the location of the eyes with pixel-perfect accuracy, only that there is an eye on each side. They also point out that pooling regions spaced k pixels apart give the next layer roughly k times fewer inputs to process, which is the memory saving your microcontroller will feel.
أي نوع من الـ pooling يضيف ثباتًا تقريبيًا أمام الإزاحة (translation invariance)، وmax pooling يُظهره بأوضح صورة، ولهذا تربط الشريحة 21 عبارة «Introduces spatial invariance» بالـ max pooling. يصف Goodfellow وBengio وCourville الـ pooling بجميع أشكاله بأنه يجعل التمثيل «approximately invariant to small translations of the input»، أي ثابتًا تقريبًا أمام الإزاحات الصغيرة للمدخل: إذا انزاح المدخل بكسلًا واحدًا، فإن قيم معظم مخرجات الـ pooling لا تتغير، لأن القيمة العظمى ما زالت داخل نافذتها. ومثالهم هو كشف الوجوه، حيث لا تحتاج الشبكة إلى معرفة موضع العينين بدقة البكسل، بل يكفيها أن تعرف أن هناك عينًا في كل جانب. ويشيرون أيضًا إلى أن مناطق pooling تفصل بينها k بكسلات تعطي الطبقة التالية مدخلات أقل بنحو k مرة لتعالجها، وهذا هو التوفير في الذاكرة الذي سيشعر به المتحكم الدقيق لديك.
| Max poolingMax pooling | Average poolingAverage pooling | |
|---|---|---|
| What it returnsما يُرجعه | The largest value in the windowأكبر قيمة في النافذة | The mean of all values in the windowمتوسط كل القيم في النافذة |
| What it is sensitive toما يتأثر به | One strong activation anywhere in the windowactivation واحدة قوية في أي مكان داخل النافذة | Every value equally, so a strong spike is dilutedكل قيمة بالتساوي، فتُخفَّف القمة القوية |
| Effect of a one-cell shiftأثر إزاحة بمقدار خلية واحدة | Usually none, if the maximum stays inside the windowعادةً لا أثر، إذا بقيت القيمة العظمى داخل النافذة | Small change, the average moves slightlyتغيّر صغير، إذ يتحرك المتوسط قليلًا |
| Gradient in the backward passالـ gradient في الـ backward pass | Routed to the argmax cell onlyيُوجَّه إلى خلية الـ argmax فقط | Spread equally, 1/F² to every cellيُوزَّع بالتساوي، 1/F² لكل خلية |
| Typical place in a networkموضعه المعتاد في الشبكة | Inside the trunk after a conv blockداخل الجذع (trunk) بعد كتلة conv | Global average pooling as the classifier headglobal average pooling كرأس للتصنيف |
Slide 21 goes further and says max pooling "performs a lot better" because it discards noisy activations. Treat that as the slide's heuristic, not a law. Boureau, Ponce and LeCun analysed the two in 2010 and found that which one wins depends on how sparse the features are and how large the pool is; they note that earlier comparisons had been "purely empirical". CS231n states more cautiously that max pooling "has been shown to work better in practice". And the field has partly moved on: Springenberg et al. showed in 2014 that max pooling "can simply be replaced by a convolutional layer with increased stride without loss in accuracy", and Lin, Chen and Yan's Network in Network replaced the fully connected head with global average pooling, which they found "less prone to overfitting". ResNet and MobileNet families follow that pattern: strided convolutions inside, a single global average pool at the end.
تذهب الشريحة 21 أبعد من ذلك وتقول إن max pooling «performs a lot better»، أي أفضل بكثير، لأنه يتخلص من الـ activations المشوَّشة. تعامل مع هذا على أنه قاعدة تجريبية من الشريحة، لا قانون. فقد حلّل Boureau وPonce وLeCun النوعين عام 2010 ووجدوا أن الفائز يعتمد على مدى تناثر (sparsity) الخصائص وعلى حجم الـ pool؛ ولاحظوا أن المقارنات السابقة كانت «purely empirical»، أي تجريبية بحتة. ويقول CS231n بحذر أكبر إن max pooling «has been shown to work better in practice»، أي ثبت أنه يعمل أفضل عمليًا. وقد تجاوز المجال ذلك جزئيًا: أظهر Springenberg et al. عام 2014 أن max pooling «can simply be replaced by a convolutional layer with increased stride without loss in accuracy»، أي يمكن ببساطة استبداله بطبقة convolution بخطوة أكبر دون خسارة في الدقة، واستبدلت ورقة Network in Network لـ Lin وChen وYan الرأس المتصل بالكامل (fully connected head) بـ global average pooling، ووجدوه «less prone to overfitting»، أي أقل عرضة للـ overfitting. وعائلتا ResNet وMobileNet تتبعان هذا النمط: strided convolutions في الداخل، وglobal average pool واحد في النهاية.
Recall
A 224x224x64 volume passes through max pooling with F = 2 and S = 2. What are the output shape and the number of learned parameters?يمرّ حجم 224x224x64 عبر max pooling مع F = 2 وS = 2. ما شكل المخرج، وما عدد المعاملات المتعلَّمة؟
Quick checkاختبار سريع
A 56x56x128 volume passes through max pooling with a 2x2 window and stride 2. What does it output?يمرّ حجم 56x56x128 عبر max pooling بنافذة 2x2 وخطوة 2. ما مخرجه؟
Take the single depth slice from slide 22, a 4 x 4 grid, and pool it with a 2 x 2 window and Stride 2. The window lands in four places and never overlaps itself, so the four coloured quadrants of the slide are the four windows. Each quadrant becomes one output cell.
خذ شريحة العمق الواحدة من الشريحة 22، وهي شبكة 4 x 4، وطبّق عليها pooling بنافذة 2 x 2 وstride قيمتها 2. تقع النافذة في أربعة مواضع ولا تتداخل مع نفسها أبدًا، لذا فالأرباع الأربعة الملوّنة في الشريحة هي النوافذ الأربع. وكل ربع يصبح خلية واحدة في المخرج.
| Windowالنافذة | Valuesالقيم | MaxMax | MeanMean |
|---|---|---|---|
| Top left (pink on the slide)أعلى اليسار (الوردي في الشريحة) | {1, 1, 5, 6}{1, 1, 5, 6} | 66 | 13 / 4 = 3.2513 / 4 = 3.25 |
| Top right (green)أعلى اليمين (الأخضر) | {2, 4, 7, 8}{2, 4, 7, 8} | 88 | 21 / 4 = 5.2521 / 4 = 5.25 |
| Bottom left (yellow)أسفل اليسار (الأصفر) | {3, 2, 1, 2}{3, 2, 1, 2} | 33 | 8 / 4 = 28 / 4 = 2 |
| Bottom right (blue)أسفل اليمين (الأزرق) | {1, 0, 3, 4}{1, 0, 3, 4} | 44 | 8 / 4 = 28 / 4 = 2 |
Read the two results as grids. Max pooling gives [[6, 8], [3, 4]] and Average pooling gives [[3.25, 5.25], [2, 2]]. Notice what each one threw away. Max kept only the argmax cell of each window (the 6, the 8, the 3, the 4) and forgot the other three values entirely. Mean kept a trace of all four but flattened the 8 down to 5.25.
اقرأ النتيجتين كشبكتين. يعطي max pooling النتيجة [[6, 8], [3, 4]]، ويعطي average pooling النتيجة [[3.25, 5.25], [2, 2]]. لاحظ ما تخلّص منه كل واحد منهما. الـ max احتفظ فقط بخلية الـ argmax في كل نافذة (الـ 6 والـ 8 والـ 3 والـ 4) ونسي القيم الثلاث الأخرى تمامًا. أما الـ mean فاحتفظ بأثر من القيم الأربع كلها لكنه خفّض الـ 8 إلى 5.25.
The general ruleالقاعدة العامة
Let the input be W1 x H1 x C. A Pooling layer needs exactly two hyperparameters, the spatial extent F of the window and the stride S, and it produces W2 x H2 x C.
ليكن المدخل W1 x H1 x C. تحتاج pooling layer إلى معاملين فائقين (hyperparameters) فقط: الامتداد المكاني للنافذة F، والخطوة S، وتُنتج W2 x H2 x C.
The shape formula is the convolution formula from part 03 with p = 0 and the kernel size renamed to F. PyTorch writes the same rule in its general form, H_out = floor((H_in + 2p - d(F - 1) - 1)/S + 1), with the stride defaulting to the kernel size, so MaxPool2d(2) means F = 2, S = 2 (PyTorch docs). The floor matters when the division is not exact, and the second worked example shows why.
صيغة الشكل هي نفسها صيغة الـ convolution من الجزء 03 مع p = 0 وتغيير اسم حجم الـ kernel إلى F. ويكتب PyTorch القاعدة نفسها بصيغتها العامة H_out = floor((H_in + 2p - d(F - 1) - 1)/S + 1)، والخطوة فيه تساوي حجم الـ kernel افتراضيًا، لذا فإن MaxPool2d(2) تعني F = 2, S = 2 (توثيق PyTorch). ويهمّ الـ floor عندما لا تكون القسمة صحيحة، والمثال المحلول الثاني يوضح السبب.
The zero comes from what pooling does in the backward pass. CS231n notes that a pooling layer "introduces zero parameters since it computes a fixed function of the input". Implementations keep the index of the maximum, the "switch", so the gradient is routed to exactly one cell per window in max pooling; in average pooling it is spread as 1/F² to each cell. Either way the layer has no Weight and no Bias, so it adds nothing to the Parameter count.
يأتي الصفر مما يفعله الـ pooling في الـ backward pass. يذكر CS231n أن طبقة الـ pooling «introduces zero parameters since it computes a fixed function of the input»، أي لا تضيف أي معاملات لأنها تحسب دالة ثابتة من المدخل. وتحتفظ التطبيقات بموضع القيمة العظمى، ويُسمّى «switch»، فيُوجَّه الـ gradient إلى خلية واحدة بالضبط في كل نافذة في max pooling؛ أما في average pooling فيُوزَّع بمقدار 1/F² على كل خلية. وفي الحالتين لا تملك الطبقة أي weight ولا أي bias، فلا تضيف شيئًا إلى عدد الـ parameters.
Worked exampleمثال محلول
224 to 112, the slide 21 numbersمن 224 إلى 112، أرقام الشريحة 21
Substitute into the width formulaعوّض في صيغة العرض
W2 = (224 - 2)/2 + 1 = 111 + 1 = 112. The height is identical.W2 = (224 - 2)/2 + 1 = 111 + 1 = 112. والارتفاع مطابق.Carry the depth acrossانقل العمق كما هو
C2 = C1 = 64, because the window only ever looks inside one channel.C2 = C1 = 64، لأن النافذة لا تنظر أبدًا إلا داخل قناة واحدة.Count the activationsاحسب الـ activations
Before: 224 x 224 x 64 = 3,211,264. After: 112 x 112 x 64 = 802,816. Exactly one quarter survives; CS231n phrases it as "discards exactly 75% of the activations in an input volume".قبل: 224 x 224 x 64 = 3,211,264. بعد: 112 x 112 x 64 = 802,816. يبقى الربع بالضبط؛ ويصوغها CS231n بعبارة «discards exactly 75% of the activations in an input volume»، أي يتخلص من 75% بالضبط من الـ activations في حجم المدخل.Resultالنتيجة
Output 112 x 112 x 64, 0 parameters, 75% of runtime memory for that tensor gone.المخرج 112 x 112 x 64، و0 معاملات، وزوال 75% من ذاكرة وقت التشغيل الخاصة بذلك الـ tensor.
Worked exampleمثال محلول
Why F = 3, S = 2 needs careلماذا تحتاج F = 3, S = 2 إلى انتباه
Overlapping pool on 224pool متداخل على 224
(224 - 3)/2 + 1 = 111.5. Not an integer: the last window would hang off the edge. PyTorch floors to 111 by default and rounds up to 112 only with ceil_mode=True.(224 - 3)/2 + 1 = 111.5. ليس عددًا صحيحًا: النافذة الأخيرة ستتدلّى خارج الحافة. يقرّب PyTorch للأسفل إلى 111 افتراضيًا، ولا يقرّب للأعلى إلى 112 إلا مع ceil_mode=True.The AlexNet case that does divideحالة AlexNet التي تنقسم فعلًا
AlexNet pools 55 x 55 maps with F = 3, S = 2: (55 - 3)/2 + 1 = 27, a clean integer, which is why that configuration is famous.يطبّق AlexNet الـ pooling على خرائط 55 x 55 مع F = 3, S = 2: (55 - 3)/2 + 1 = 27، وهو عدد صحيح تمامًا، ولهذا اشتهر هذا الإعداد.Rule of thumbقاعدة عملية
Check that (W1 - F) divides by S. If it does not, state the floor explicitly.تحقّق من أن (W1 - F) يقبل القسمة على S. وإذا لم يكن كذلك، فاذكر التقريب للأسفل (floor) صراحةً.
Changing F and S on the same 4 x 4 slice shows how strongly the two hyperparameters shape the result. The values below are max pooling; the simulator lets you check the means.
تغيير F وS على شريحة 4 x 4 نفسها يُظهر مدى قوة تأثير هذين المعاملين الفائقين في النتيجة. القيم أدناه هي لـ max pooling؛ والمحاكي يتيح لك التحقق من المتوسطات.
| F, SF, S | W2W2 | Outputالمخرج | Valuesالقيم | What happenedماذا حدث |
|---|---|---|---|---|
| F = 2, S = 2F = 2, S = 2 | (4 - 2)/2 + 1 = 2(4 - 2)/2 + 1 = 2 | 2 x 22 x 2 | [[6, 8], [3, 4]][[6, 8], [3, 4]] | Windows tile the input, 75% of activations droppedالنوافذ تغطي المدخل دون تداخل، ويُستبعد 75% من الـ activations |
| F = 2, S = 1F = 2, S = 1 | (4 - 2)/1 + 1 = 3(4 - 2)/1 + 1 = 3 | 3 x 33 x 3 | [[6, 7, 8], [6, 7, 8], [3, 3, 4]][[6, 7, 8], [6, 7, 8], [3, 3, 4]] | Windows overlap, the row 6, 7, 8 appears twiceالنوافذ متداخلة، فيظهر الصف 6، 7، 8 مرتين |
| F = 3, S = 1F = 3, S = 1 | (4 - 3)/1 + 1 = 2(4 - 3)/1 + 1 = 2 | 2 x 22 x 2 | [[7, 8], [7, 8]][[7, 8], [7, 8]] | Nine cells per window, the small values vanishتسع خلايا في كل نافذة، فتختفي القيم الصغيرة |
| F = 3, S = 2F = 3, S = 2 | (4 - 3)/2 + 1 = 1.5(4 - 3)/2 + 1 = 1.5 | 1 x 1 (floored)1 x 1 (مقرّب للأسفل) | [[7]][[7]] | Not an integer, PyTorch floors and drops the last row and columnليس عددًا صحيحًا، فيقرّب PyTorch للأسفل ويُسقط الصف والعمود الأخيرين |
- This windowهذه النافذة
- hover an output cellمرّر المؤشر فوق خلية إخراج
- Input W1 × H1 × C
- 4 × 4 × 1
- Output W2 × H2 × C
- 2 × 2 × 1
- Learned parametersالمعاملات المتعلَّمة
- 0
- Activations in, outالتنشيطات داخلًا وخارجًا
- 16 to 4 (75% discarded)من 16 إلى 4 (يُهمَل 75%)
Recall
Compute max and mean pooling with F = 2, S = 2 on [[1,1,2,4],[5,6,7,8],[3,2,1,0],[1,2,3,4]].احسب max pooling وmean pooling مع F = 2 وS = 2 على [[1,1,2,4],[5,6,7,8],[3,2,1,0],[1,2,3,4]].
Quick checkاختبار سريع
Why does a pooling layer add nothing to a network's parameter count?لماذا لا تضيف طبقة الـ pooling شيئًا إلى عدد معاملات الشبكة؟
The grid on slide 23 comes from Lee, Grosse, Ranganath and Ng (ICML 2009), who learned a first layer of edge detectors from natural images once, then trained the second and third layers of a convolutional deep belief network separately on unlabeled Caltech-101 images of faces, cars, elephants and chairs, and visualized what each layer responds to. Their own summary: the first, second and third layers "learn edge detectors, object parts, and objects respectively". Look at the bottom row of the slide. It is the same across all four columns because that first layer was learned once from natural images: small oriented edges like Gabor patches. The middle row diverges into eyes and noses, wheels and bumpers, tusks and ears, chair legs and backs. The top row shows entire faces, cars, elephants and chairs.
الشبكة في الشريحة 23 مأخوذة من Lee وGrosse وRanganath وNg (ICML 2009)، الذين تعلّموا طبقة أولى من كاشفات الحواف من صور طبيعية مرة واحدة، ثم درّبوا الطبقتين الثانية والثالثة من convolutional deep belief network بشكل منفصل على صور غير معنونة من Caltech-101 للوجوه والسيارات والفيلة والكراسي، وعرضوا بصريًا ما تستجيب له كل طبقة. وملخّصهم هم: الطبقات الأولى والثانية والثالثة «learn edge detectors, object parts, and objects respectively»، أي تتعلم كاشفات الحواف، ثم أجزاء الأجسام، ثم الأجسام، على الترتيب. انظر إلى الصف السفلي في الشريحة. إنه متطابق في الأعمدة الأربعة لأن تلك الطبقة الأولى جرى تعلّمها مرة واحدة من صور طبيعية: حواف صغيرة موجّهة تشبه رقع Gabor. ثم يتفرّع الصف الأوسط إلى عيون وأنوف، وعجلات ومصدّات، وأنياب وآذان، وأرجل كراسٍ وظهورها. ويُظهر الصف العلوي وجوهًا وسيارات وفيلة وكراسي كاملة.
This ladder is the Feature hierarchy, and it is not designed by hand. Every Convolution layer runs the same operation with a small Kernel (filter). What changes with depth is the Receptive field: a first-layer unit sees only its k x k patch of pixels, so the most it can detect is an edge. A second-layer unit sees a k x k patch of first-layer responses, which covers a larger region of the image (L(k - 1) + 1 pixels after L stride-1 layers, faster with stride or pooling), so it can combine edges into a curve or an eye. Deep units cover most of the image and can match a whole face. Zeiler and Fergus confirmed in 2013 that the same ladder appears inside a supervised ImageNet CNN, using a deconvolutional visualization of intermediate layers.
هذا السلّم هو feature hierarchy، ولا يُصمَّم يدويًا. كل convolution layer تنفّذ العملية نفسها بـ kernel صغير. ما يتغير مع العمق هو receptive field: وحدة في الطبقة الأولى لا ترى إلا رقعتها k x k من البكسلات، فأقصى ما يمكنها كشفه حافة. ووحدة في الطبقة الثانية ترى رقعة k x k من استجابات الطبقة الأولى، وهذه تغطي منطقة أكبر من الصورة (L(k - 1) + 1 بكسلًا بعد L طبقات بخطوة 1، وأسرع مع الـ stride أو الـ pooling)، فتستطيع جمع الحواف في منحنى أو عين. أما الوحدات العميقة فتغطي معظم الصورة ويمكنها مطابقة وجه كامل. وقد أكّد Zeiler وFergus عام 2013 أن السلّم نفسه يظهر داخل CNN مُشرَف عليها مدرَّبة على ImageNet، باستخدام عرض بصري بالـ deconvolution للطبقات الوسيطة.
| Tierالمستوى | Layersالطبقات | What the filters respond toما تستجيب له الـ filters | Receptive field, roughlyالحقل الاستقبالي تقريبًا |
|---|---|---|---|
| Low levelالمستوى المنخفض | First convolution layersطبقات الـ convolution الأولى | Oriented edges, bars, blobs, colour opponentsحواف موجّهة، وقضبان، وبقع، وتضادّات لونية | 3 x 3 to 11 x 11 pixels (the first kernel size)من 3 x 3 إلى 11 x 11 بكسلًا (حجم الـ kernel الأول) |
| Mid levelالمستوى المتوسط | Middle layersالطبقات الوسطى | Curves, corners, eyes, wheels, tusks, chair legsمنحنيات، وزوايا، وعيون، وعجلات، وأنياب، وأرجل كراسٍ | tens of pixelsعشرات البكسلات |
| High levelالمستوى العالي | Deepest layersأعمق الطبقات | Whole faces, cars, elephants, chairsوجوه، وسيارات، وفيلة، وكراسٍ كاملة | most of the imageمعظم الصورة |
The mechanism in action: slide 24الآلية أثناء العمل: الشريحة 24
Slide 24 shows a network of the pattern [CONV, RELU, CONV, RELU, POOL] repeated three times and then a fully connected layer, run on a photo of a car. Each column is the activation maps of one layer. Read them left to right and the hierarchy appears live: the first columns look like edge-filtered copies of the car, the middle columns are sparse and blotchy, and the last columns are tiny grids that no longer resemble the photo at all.
تعرض الشريحة 24 شبكة من النمط [CONV, RELU, CONV, RELU, POOL] مكررًا ثلاث مرات ثم طبقة متصلة بالكامل (fully connected)، تعمل على صورة سيارة. كل عمود هو الـ activation maps لطبقة واحدة. اقرأها من اليسار إلى اليمين وسيظهر التسلسل الهرمي حيًّا: الأعمدة الأولى تبدو نسخًا من السيارة مرّت عبر فلتر حواف، والأعمدة الوسطى متناثرة وبقعية، والأعمدة الأخيرة شبكات صغيرة لم تعد تشبه الصورة إطلاقًا.
Two convs with ReLU, then pool: edges and colour blobs.
طبقتا conv مع ReLU، ثم pool: حواف وبقع لونية.
Same pattern on the smaller maps: corners, wheels, parts.
النمط نفسه على الخرائط الأصغر: زوايا وعجلات وأجزاء.
Coarse maps whose units see most of the image.
خرائط خشنة ترى وحداتها معظم الصورة.
car, truck, airplane, ship, horse; car wins.
car وtruck وairplane وship وhorse، والفائز car.
The ReLU after each conv keeps only positive responses, which is why the columns after it are darker. Each POOL halves the Feature map and, because the next block's k x k window now covers twice as many original pixels, pushes the receptive field outward. Two pools have already halved the maps twice, so the third block works on maps one quarter the width of the input, and the third pool leaves the fully connected layer maps one eighth the width; that is why the last units can see a whole car. The Fully connected layer at the end reads every unit of the final maps at once and produces one score per class. On the slide, car scores highest, well above truck, then airplane, ship and horse.
الـ ReLU بعد كل conv لا تُبقي إلا الاستجابات الموجبة، ولهذا تكون الأعمدة التي تليها أغمق. وكل POOL يقسم feature map على اثنين، ولأن نافذة k x k في الكتلة التالية تغطي الآن ضعف عدد البكسلات الأصلية، فإنه يدفع الحقل الاستقبالي إلى الخارج. وقد قسم الـ pool الأول والثاني الخرائط على اثنين مرتين، فتعمل الكتلة الثالثة على خرائط عرضها ربع عرض المدخل، ويترك الـ pool الثالث للطبقة المتصلة بالكامل خرائط عرضها ثُمن عرض المدخل؛ ولهذا تستطيع الوحدات الأخيرة رؤية سيارة كاملة. وتقرأ fully connected layer في النهاية كل وحدة من الخرائط الأخيرة دفعة واحدة وتُنتج درجة واحدة لكل فئة. وفي الشريحة تحصل car على أعلى درجة، أعلى بكثير من truck، ثم airplane وship وhorse.
Why this matters on an embedded deviceلماذا يهمّ هذا على جهاز مدمج
The hierarchy explains a pattern you will meet throughout this course. Early layers have few parameters (a 5 x 5 x 3 filter is 75 weights) but large activation maps; late layers have many parameters but tiny maps. So SRAM pressure comes from the front of the network and flash pressure from the back, and pruning or quantizing them calls for different budgets. Transfer learning on the edge uses the hierarchy directly: the edge and part detectors are generic, so you freeze the low tiers and retrain only the top.
يشرح التسلسل الهرمي نمطًا ستقابله طوال هذا المقرر. الطبقات الأولى لها معاملات قليلة (filter بحجم 5 x 5 x 3 يساوي 75 وزنًا) لكن activation maps كبيرة؛ والطبقات الأخيرة لها معاملات كثيرة لكن خرائط صغيرة جدًا. لذا يأتي الضغط على SRAM من مقدمة الشبكة، ويأتي الضغط على flash من مؤخرتها، ويحتاج الـ pruning أو الـ quantization لكل منهما إلى ميزانية مختلفة. ويستخدم الـ transfer learning على الحافة (edge) هذا التسلسل الهرمي مباشرة: كاشفات الحواف والأجزاء عامة، فتجمّد الطبقات الدنيا وتعيد تدريب العليا فقط.
Recall
Name the three tiers of the feature hierarchy with one example each, and say what property of deep layers makes the top tier possible.سمِّ المستويات الثلاثة للتسلسل الهرمي للخصائص مع مثال واحد لكل منها، واذكر ما خاصية الطبقات العميقة التي تجعل المستوى الأعلى ممكنًا.
Quick checkاختبار سريع
In the feature hierarchy, what do the deepest convolution layers respond to?في التسلسل الهرمي للخصائص، ماذا تستجيب له أعمق طبقات الـ convolution؟
Open the ConvNetJS CIFAR-10 demo and read its first conv block. It prints "conv (32x32x16), filter size 5x5x3, stride 1, parameters: 16x5x5x3+16 = 1216". That one line is a complete exam answer, and it is worth rebuilding by hand.
افتح عرض ConvNetJS CIFAR-10 واقرأ أول كتلة conv فيه. إنه يطبع «conv (32x32x16), filter size 5x5x3, stride 1, parameters: 16x5x5x3+16 = 1216». هذا السطر الواحد إجابة امتحان كاملة، ويستحق أن تعيد بناءه يدويًا.
Worked exampleمثال محلول
The ConvNetJS first conv layerأول طبقة conv في ConvNetJS
Weightsالأوزان
16 filters, each 5 x 5 and 3 deep to match the RGB input: 16 x 5 x 5 x 3 = 1200 weights.16 filter، كل منها 5 x 5 وعمقه 3 ليطابق مدخل RGB: 16 x 5 x 5 x 3 = 1200 من الـ weights.Biasesالـ biases
One Bias per filter, so 16.bias واحد لكل filter، أي 16.Why the map stays 32 x 32لماذا تبقى الخريطة 32 x 32
The demo's layer definition uses pad 2: (32 + 2·2 - 5)/1 + 1 = 32. With Padding equal to (F - 1)/2, a stride-1 conv keeps its size.تعريف الطبقة في العرض يستخدم pad 2: (32 + 2·2 - 5)/1 + 1 = 32. فعندما يساوي الـ padding القيمة (F - 1)/2، تحافظ conv بخطوة 1 على حجمها.Resultالنتيجة
1200 + 16 = 1216 parameters, output 32 x 32 x 16, which is 16,384 activations for one image.1200 + 16 = 1216 معاملًا، والمخرج 32 x 32 x 16، أي 16,384 قيمة activation لصورة واحدة.
The ConvNetJS CIFAR-10 demoعرض ConvNetJS على CIFAR-10
- Datasetمجموعة البيانات
- CIFAR-10: 60,000 colour images of 32 x 32, 10 classes, 50,000 train and 10,000 test (Krizhevsky)CIFAR-10: 60,000 صورة ملوّنة بحجم 32 x 32، و10 فئات، و50,000 للتدريب و10,000 للاختبار (Krizhevsky)
- Classesالفئات
- airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truckairplane (طائرة)، automobile (سيارة)، bird (طائر)، cat (قطة)، deer (غزال)، dog (كلب)، frog (ضفدع)، horse (حصان)، ship (سفينة)، truck (شاحنة)
- Accuracy quoted on the pageالدقة المذكورة في الصفحة
- state of the art "about 90%", humans "about 94%" (2014 figures; current models exceed 99%)أفضل نتيجة وقتها «نحو 90%»، والبشر «نحو 94%» (أرقام 2014؛ والنماذج الحالية تتجاوز 99%)
- Augmentationزيادة البيانات (augmentation)
- random flips and random shifts of up to 2 pxقلب عشوائي (flips) وإزاحات عشوائية حتى 2 px
- Optimizerالمُحسِّن (optimizer)
- Adadelta, batch size 4, L2 decay 0.0001Adadelta، وحجم الدفعة (batch size) 4، وL2 decay قدره 0.0001
The whole demo network is small enough to count in full. Two things to watch: every conv filter is as deep as the channels feeding it, and the three pooling layers contribute nothing to the total while cutting the activations by four each time.
شبكة العرض كلها صغيرة بما يكفي لعدّها كاملة. انتبه لأمرين: كل filter في conv عمقه يساوي عدد القنوات الداخلة إليه، وطبقات الـ pooling الثلاث لا تضيف شيئًا إلى المجموع بينما تقلّص الـ activations بأربع مرات في كل مرة.
| Layerالطبقة | Outputالمخرج | Parametersالمعاملات | Noteملاحظة |
|---|---|---|---|
| inputالمدخل (input) | 32 x 32 x 332 x 32 x 3 | 00 | Raw CIFAR-10 imageصورة CIFAR-10 خام |
| conv, 16 filters 5x5x3, pad 2conv، 16 filter بحجم 5x5x3، pad 2 | 32 x 32 x 1632 x 32 x 16 | 16 x 5 x 5 x 3 + 16 = 121616 x 5 x 5 x 3 + 16 = 1216 | The count printed on slide 25العدد المطبوع في الشريحة 25 |
| max pool 2x2, stride 2max pool 2x2، stride 2 | 16 x 16 x 1616 x 16 x 16 | 00 | Activations cut by 4xتنقص الـ activations بأربع مرات |
| conv, 20 filters 5x5x16, pad 2conv، 20 filter بحجم 5x5x16، pad 2 | 16 x 16 x 2016 x 16 x 20 | 20 x 5 x 5 x 16 + 20 = 802020 x 5 x 5 x 16 + 20 = 8020 | Depth of the filter equals the input channelsعمق الـ filter يساوي عدد قنوات المدخل |
| max pool 2x2, stride 2max pool 2x2، stride 2 | 8 x 8 x 208 x 8 x 20 | 00 | |
| conv, 20 filters 5x5x20, pad 2conv، 20 filter بحجم 5x5x20، pad 2 | 8 x 8 x 208 x 8 x 20 | 20 x 5 x 5 x 20 + 20 = 1002020 x 5 x 5 x 20 + 20 = 10020 | |
| max pool 2x2, stride 2max pool 2x2، stride 2 | 4 x 4 x 204 x 4 x 20 | 00 | |
| softmax, 10 classessoftmax، 10 فئات | 1010 | 320 x 10 + 10 = 3210320 x 10 + 10 = 3210 | The only layer whose count depends on image sizeالطبقة الوحيدة التي يعتمد عدد معاملاتها على حجم الصورة |
| Totalالمجموع | 22,46622,466 | Three pools contribute nothingطبقات الـ pool الثلاث لا تساهم بشيء |
The total is 22,466 parameters. The softmax layer is the only one whose count depends on the image size, because it reads the flattened 4 x 4 x 20 = 320 activations; every conv layer's count depends only on its filter shape, which is Weight sharing at work.
المجموع 22,466 معاملًا. وطبقة الـ softmax هي الوحيدة التي يعتمد عدد معاملاتها على حجم الصورة، لأنها تقرأ الـ activations المسطّحة وعددها 4 x 4 x 20 = 320؛ أما عدد معاملات كل طبقة conv فيعتمد على شكل الـ filter فقط، وهذا هو weight sharing أثناء العمل.
CNN Explainer as retrieval practiceCNN Explainer كتمرين استرجاع
CNN Explainer (Wang et al., IEEE VIS 2020) runs a network the authors call Tiny VGG on ten classes: lifeboat, ladybug, pizza, bell pepper, school bus, koala, espresso, red panda, orange and sport car. Its training code uses 3 x 3 convolutions with 10 filters and no padding ("valid"), and 2 x 2 max pools with stride 2. The input is 64 x 64 x 3. Before you look at the table, chain the shapes yourself: a valid conv subtracts 2, a pool halves.
يشغّل CNN Explainer (Wang et al., IEEE VIS 2020) شبكة يسمّيها مؤلفوها Tiny VGG على عشر فئات: lifeboat وladybug وpizza وbell pepper وschool bus وkoala وespresso وred panda وorange وsport car. ويستخدم كود التدريب الخاص به convolutions بحجم 3 x 3 مع 10 filters ودون padding («valid»)، وmax pools بحجم 2 x 2 وخطوة 2. والمدخل 64 x 64 x 3. قبل أن تنظر إلى الجدول، تتبّع الأشكال بنفسك: الـ conv من نوع valid تطرح 2، والـ pool يقسم على اثنين.
Recall
Chain the CNN Explainer shapes from 64x64x3 to the second pool.تتبّع أشكال CNN Explainer من 64x64x3 حتى الـ pool الثاني.
| Layerالطبقة | Shape formulaصيغة الشكل | Outputالمخرج | Parametersالمعاملات |
|---|---|---|---|
| inputالمدخل (input) | 64 x 64 x 364 x 64 x 3 | 00 | |
| conv_1_1, 10 filters 3x3, validconv_1_1، 10 filters بحجم 3x3، valid | 64 - 3 + 164 - 3 + 1 | 62 x 62 x 1062 x 62 x 10 | 10 x 3 x 3 x 3 + 10 = 28010 x 3 x 3 x 3 + 10 = 280 |
| conv_1_2, 10 filters 3x3, validconv_1_2، 10 filters بحجم 3x3، valid | 62 - 3 + 162 - 3 + 1 | 60 x 60 x 1060 x 60 x 10 | 10 x 3 x 3 x 10 + 10 = 91010 x 3 x 3 x 10 + 10 = 910 |
| max_pool_1, 2x2, stride 2max_pool_1، 2x2، stride 2 | (60 - 2)/2 + 1(60 - 2)/2 + 1 | 30 x 30 x 1030 x 30 x 10 | 00 |
| conv_2_1, 10 filters 3x3, validconv_2_1، 10 filters بحجم 3x3، valid | 30 - 3 + 130 - 3 + 1 | 28 x 28 x 1028 x 28 x 10 | 910910 |
| conv_2_2, 10 filters 3x3, validconv_2_2، 10 filters بحجم 3x3، valid | 28 - 3 + 128 - 3 + 1 | 26 x 26 x 1026 x 26 x 10 | 910910 |
| max_pool_2, 2x2, stride 2max_pool_2، 2x2، stride 2 | (26 - 2)/2 + 1(26 - 2)/2 + 1 | 13 x 13 x 1013 x 13 x 10 | 00 |
| flatten, dense 10, softmaxflatten، ثم dense 10، ثم softmax | 13 x 13 x 10 = 169013 x 13 x 10 = 1690 | 1010 | 1690 x 10 + 10 = 16,9101690 x 10 + 10 = 16,910 |
| Totalالمجموع | 19,92019,920 |
The pattern repeats: 19,920 parameters, of which the two pools contribute 0 while each removes three quarters of the activations. The dense layer holds 16,910 of the total because it multiplies the image-dependent 1690 by the ten classes. Click any activation map in the live page and it animates the window that produced it, which is the pooling and convolution arithmetic of this lecture rendered one cell at a time.
النمط يتكرر: 19,920 معاملًا، يساهم فيها الـ pool الاثنان بـ 0، بينما يزيل كل منهما ثلاثة أرباع الـ activations. وتحمل الطبقة الكثيفة (dense) 16,910 من المجموع لأنها تضرب العدد 1690 المعتمد على حجم الصورة في الفئات العشر. انقر أي activation map في الصفحة الحية وستعرض حركيًا النافذة التي أنتجتها، وهذا هو حساب الـ pooling والـ convolution في هذه المحاضرة معروضًا خليةً خلية.
Recall
How many parameters does the ConvNetJS first conv layer hold, and why does the map stay 32x32?كم معاملًا تحمل أول طبقة conv في ConvNetJS، ولماذا تبقى الخريطة 32x32؟
Quick checkاختبار سريع
In the ConvNetJS CIFAR-10 demo the first conv layer has 16 filters of 5x5x3. How many parameters does it hold?في عرض ConvNetJS على CIFAR-10، أول طبقة conv فيها 16 filter بحجم 5x5x3. كم معاملًا تحمل؟
Quick checkاختبار سريع
In CNN Explainer, conv_1_2 outputs 60x60x10. What does max_pool_1 with a 2x2 window and stride 2 output?في CNN Explainer، مخرج conv_1_2 هو 60x60x10. ما مخرج max_pool_1 بنافذة 2x2 وخطوة 2؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Pooling summarizes a window in each activation map independently: spatial size drops, channel count stays, 224x224x64 becomes 112x112x64.يلخّص الـ pooling نافذة داخل كل activation map على حدة: يصغر الحجم المكاني ويبقى عدد القنوات كما هو، فيصبح 224x224x64 هو 112x112x64.
- W2 = (W1 - F)/S + 1, H2 = (H1 - F)/S + 1, depth C unchanged, parameters 0.W2 = (W1 - F)/S + 1 وH2 = (H1 - F)/S + 1، والعمق C لا يتغير، وعدد المعاملات 0.
- On the slide's 4x4 slice, F = 2 and S = 2 give max [[6,8],[3,4]] and mean [[3.25,5.25],[2,2]].على شريحة 4x4 في الشريحة 22، مع F = 2 وS = 2 يعطي max النتيجة [[6,8],[3,4]] ويعطي mean النتيجة [[3.25,5.25],[2,2]].
- Max pooling adds approximate invariance to small shifts; 'max is always better' is a heuristic, and many modern networks use strided convolutions or global average pooling instead.يضيف max pooling ثباتًا تقريبيًا أمام الإزاحات الصغيرة؛ وعبارة «max أفضل دائمًا» قاعدة تجريبية لا قانون، وكثير من الشبكات الحديثة تستخدم بدلًا منه strided convolutions أو global average pooling.
- Filters form a hierarchy: edges in early layers, object parts in the middle, whole objects deep, because receptive fields grow with depth and pooling.تشكّل الـ filters تسلسلًا هرميًا: حواف في الطبقات الأولى، وأجزاء أجسام في الوسط، وأجسام كاملة في العمق، لأن الحقل الاستقبالي يكبر مع العمق ومع الـ pooling.
- CONV-RELU-CONV-RELU-POOL repeated three times then FC turns an image into class scores; car beats truck, airplane, ship, horse.تكرار CONV-RELU-CONV-RELU-POOL ثلاث مرات ثم طبقة FC يحوّل الصورة إلى درجات للفئات؛ وتتفوق car على truck وairplane وship وhorse.
- ConvNetJS first layer: 16 x 5 x 5 x 3 + 16 = 1216 parameters; CNN Explainer shapes: 64, 62, 60, 30, 28, 26, 13.الطبقة الأولى في ConvNetJS: 16 x 5 x 5 x 3 + 16 = 1216 معاملًا؛ وأشكال CNN Explainer هي: 64، 62، 60، 30، 28، 26، 13.
Sourcesالمصادر
- CS231n Convolutional Neural Networks for Visual Recognition, Pooling LayerDocsStanford UniversityPooling formulas, zero parameters, 75% discard, the argmax switch, and getting rid of pooling.صيغ الـ pooling، وصفر معاملات، واستبعاد 75%، ومفتاح الـ argmax (switch)، والاستغناء عن الـ pooling.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 9.3 PoolingBookGoodfellow, Bengio and Courville, MIT PressApproximate translation invariance and k times fewer inputs for the next layer, pp. 335 to 337.الثبات التقريبي أمام الإزاحة، ومدخلات أقل بـ k مرة للطبقة التالية، الصفحات 335 إلى 337.(opens in a new tab)(يفتح في تبويب جديد)
- Convolutional Deep Belief Networks for Scalable Unsupervised Learning of Hierarchical RepresentationsPaperLee, Grosse, Ranganath and Ng, ICML 2009Source of the faces, cars, elephants, chairs figure on slide 23: edges, object parts, objects.مصدر شكل الوجوه والسيارات والفيلة والكراسي في الشريحة 23: حواف، ثم أجزاء أجسام، ثم أجسام.(opens in a new tab)(يفتح في تبويب جديد)
- Visualizing and Understanding Convolutional NetworksPaperZeiler and Fergus, 2013Deconvolutional visualization showing the same hierarchy in a supervised ImageNet CNN.عرض بصري بالـ deconvolution يُظهر التسلسل الهرمي نفسه في CNN مُشرَف عليها مدرَّبة على ImageNet.(opens in a new tab)(يفتح في تبويب جديد)
- Striving for Simplicity: The All Convolutional NetPaperSpringenberg, Dosovitskiy, Brox and Riedmiller, 2014Max pooling replaced by strided convolution with no loss in accuracy.استبدال max pooling بـ strided convolution دون خسارة في الدقة.(opens in a new tab)(يفتح في تبويب جديد)
- Network In NetworkPaperLin, Chen and Yan, 2013Global average pooling as a classifier head, less prone to overfitting than fully connected layers.global average pooling كرأس للتصنيف، وهو أقل عرضة للـ overfitting من الطبقات المتصلة بالكامل.(opens in a new tab)(يفتح في تبويب جديد)
- A Theoretical Analysis of Feature Pooling in Visual RecognitionPaperBoureau, Ponce and LeCun, ICML 2010When max beats average depends on feature sparsity and pool size.تفوّق max على average يعتمد على تناثر الخصائص (sparsity) وحجم الـ pool.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.MaxPool2dDocsPyTorch documentationFloor formula, stride defaulting to kernel size, ceil_mode, and C unchanged from input to output.صيغة الـ floor، والخطوة التي تساوي حجم الـ kernel افتراضيًا، وceil_mode، وبقاء C دون تغيير من المدخل إلى المخرج.(opens in a new tab)(يفتح في تبويب جديد)
- ConvNetJS CIFAR-10 demoDocsAndrej Karpathy, StanfordAbout 90% state of the art, 94% human, 2 px shifts, Adadelta; layer parameters printed live.أفضل نتيجة نحو 90%، والبشر 94%، وإزاحات 2 px، وAdadelta؛ ومعاملات كل طبقة تُطبع مباشرة.(opens in a new tab)(يفتح في تبويب جديد)
- ConvNetJS CIFAR-10 demo sourceDocsGitHub, karpathy/convnetjslayer_defs with pad 2 and 16, 20, 20 filters; trainer with batch size 4 and L2 decay 0.0001.layer_defs مع pad 2 و16 و20 و20 filter؛ والـ trainer بحجم دفعة 4 وL2 decay قدره 0.0001.(opens in a new tab)(يفتح في تبويب جديد)
- The CIFAR-10 datasetDocsAlex Krizhevsky, University of Toronto60,000 colour 32x32 images in 10 classes, 50,000 train and 10,000 test.60,000 صورة ملوّنة بحجم 32x32 في 10 فئات، 50,000 للتدريب و10,000 للاختبار.(opens in a new tab)(يفتح في تبويب جديد)
- CNN ExplainerDocsPolo Club of Data Science, Georgia TechInteractive Tiny VGG with layer shapes 64, 62, 60, 30, 28, 26, 13.Tiny VGG تفاعلية بأشكال طبقات 64، 62، 60، 30، 28، 26، 13.(opens in a new tab)(يفتح في تبويب جديد)
- Tiny VGG training scriptDocsGitHub, poloclub/cnn-explainer3x3 valid convolutions with 10 filters, 2x2 max pools, dense 10 with softmax.convolutions من نوع valid بحجم 3x3 مع 10 filters، وmax pools بحجم 2x2، وطبقة dense 10 مع softmax.(opens in a new tab)(يفتح في تبويب جديد)
- CNN Explainer: Learning Convolutional Neural Networks with Interactive VisualizationPaperWang et al., IEEE TVCG (IEEE VIS 2020)The paper behind the demo on slide 26.الورقة التي يقوم عليها العرض في الشريحة 26.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 2DocsSong Han, MITThe course whose Basics of Deep Learning lecture these slides follow.المقرر الذي تتبع هذه الشرائح محاضرته Basics of Deep Learning.(opens in a new tab)(يفتح في تبويب جديد)
Part 05: Batch, layer, instance and group normalizationتطبيع الدفعة والطبقة والمثيل والمجموعة
Why nicely scaled activations make optimization easier, how batch normalization computes, scales and shifts, how it changes at inference, and the axes each normalization variant averages over.لماذا تجعل التنشيطات ذات المقياس المناسب التحسين أسهل، وكيف يحسب تطبيع الدفعة (batch normalization) ثم يقيس ويزيح، وكيف يتغير أثناء الاستدلال (inference)، والمحاور التي يحسب كل نوع من التطبيع المتوسط عليها.
5 concepts5 مفاهيم, ، slides 27-38الشرائح 27-38
Why this part mattersلماذا يهمّ هذا الجزء
Every network you will compress, quantize or deploy in this course carries normalization layers. Batch normalization is a large part of why deep CNNs train at all, and it is also the first optimization you get for free on a microcontroller: at inference it folds into the layer before it and costs nothing.
This part builds the normalization family from one question: which numbers are averaged together to make one mean? Answer it for the batch axis and you have batch norm; answer it for the feature axis and you have layer norm, the block inside every transformer in part 06. Along the way you meet the exam staples of this lecture: tensor shapes, the train versus test difference, fusing BN into a linear layer, and computing BN on a tiny batch by hand.
كل شبكة ستضغطها أو تجري لها quantization أو تنشرها في هذا المقرر تحتوي على طبقات normalization. الـ batch normalization سبب رئيسي في أن شبكات الـ CNN العميقة تتدرب أصلًا، وهو أيضًا أول تحسين تحصل عليه مجانًا على الـ microcontroller: ففي الاستدلال (inference) يُدمج في الطبقة التي قبله ولا يكلّف شيئًا.
يبني هذا الجزء عائلة الـ normalization كلها من سؤال واحد: أي الأرقام تُجمع معًا لتكوين متوسط واحد؟ أجب عنه على محور الـ batch تحصل على الـ batch norm؛ وأجب عنه على محور الـ features تحصل على الـ layer norm، وهي الكتلة الموجودة داخل كل transformer في الجزء 06. وفي الطريق تقابل أساسيات الامتحان في هذه المحاضرة: أشكال الـ tensors، والفرق بين التدريب والاختبار، ودمج الـ BN في طبقة خطية، وحساب الـ BN يدويًا على batch صغيرة.
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain why uncentered or unevenly scaled inputs make y = Wx hard to optimize.تشرح لماذا تجعل المدخلات غير المتمركزة حول الصفر أو المتفاوتة في المقياس تحسين y = Wx صعبًا.
- Write the batch norm transform with the shape of every tensor and say which parts are learned.تكتب تحويل الـ batch norm مع شكل (shape) كل tensor، وتحدد أي أجزائه يُتعلَّم.
- State why BN differs between training and inference and derive the fused W' and b'.تبيّن لماذا يختلف الـ BN بين التدريب والاستدلال (inference)، وتشتق W' و b' بعد الدمج (fusion).
- Name the axes each of BN, LN, IN and GN averages over and read off the resulting mu shape.تسمّي المحاور التي يحسب كلٌّ من BN و LN و IN و GN المتوسط عليها، وتستنتج منها شكل mu الناتج.
- Compute BN on a small mini-batch by hand and self-check with mean = beta and variance = gamma squared.تحسب الـ BN يدويًا على mini-batch صغيرة، وتتحقق بنفسك من أن المتوسط = beta والتباين = gamma تربيع.
Feed a linear layer y = Wx two raw features: a pixel intensity somewhere in 0 to 255 and a binary flag in 0 to 1. For both features to have a similar influence on y, the two corresponding entries of W must differ by about two orders of magnitude. And because neither feature is centered at zero, the layer needs a large Bias just to bring its outputs near zero, where the next activation function does useful work.
أدخِل إلى طبقة خطية y = Wx خاصيتين (features) خامتين: شدة بكسل في مكان ما بين 0 to 255 وعلامة ثنائية بين 0 to 1. لكي يكون للخاصيتين تأثير متقارب على y، يجب أن يختلف المدخلان المقابلان لهما في W بنحو رتبتين عشريتين (مئة ضعف تقريبًا). ولأن أيًّا من الخاصيتين ليست متمركزة حول الصفر، تحتاج الطبقة إلى bias كبير فقط لتقريب مخرجاتها من الصفر، حيث تؤدي دالة التنشيط (activation function) التالية عملًا مفيدًا.
Those are the two troubles every layer faces. Inputs that are not centered force a large bias, and inputs with different scales per element force the entries of W to vary a lot. Both make gradient descent slow, because one learning rate has to serve directions of very different steepness, so the update zig-zags along the steep direction while creeping along the shallow one. Goodfellow, Bengio and Courville motivate the same problem with a deep chain of layers: every update to an early layer changes the statistics that every later layer sees, so the layers keep re-adapting to each other.
هاتان هما المشكلتان اللتان تواجههما كل طبقة. المدخلات غير المتمركزة تفرض bias كبيرًا، والمدخلات ذات المقاييس المختلفة من عنصر لآخر تفرض أن تتفاوت قيم W كثيرًا. وكلتاهما تبطئ الـ gradient descent، لأن learning rate واحدًا يجب أن يخدم اتجاهات شديدة الاختلاف في انحدارها، فيتعرّج التحديث (zig-zag) على الاتجاه شديد الانحدار بينما يزحف ببطء على الاتجاه المنبسط. ويعلّل Goodfellow و Bengio و Courville المشكلة نفسها بسلسلة عميقة من الطبقات: كل تحديث لطبقة مبكرة يغيّر الإحصاءات التي تراها كل طبقة لاحقة، فتظل الطبقات تعيد التكيّف بعضها مع بعض.
The idea, due to Ioffe and Szegedy in 2015, is to stop hoping the inputs are nice and to force them to be. For each dimension k, subtract the mean and divide by the standard deviation, so that the dimension has zero mean and unit variance:
الفكرة، التي تعود إلى Ioffe و Szegedy عام 2015، هي أن نكفّ عن الأمل في أن تكون المدخلات جيدة وأن نجبرها على ذلك. لكل بُعد k، اطرح المتوسط واقسم على الانحراف المعياري، فيصبح لهذا البعد متوسط صفري وتباين يساوي واحدًا:
The part that makes this a layer rather than a preprocessing step is that the map is differentiable. It sits inside the network, at any hidden layer, and gradients flow through it exactly as through a convolution. The CS231n notes call it differentiable preprocessing built into the network itself. On slide 27 this is why Normalization appears as the fifth component of a CNN, beside convolution, pooling, fully connected layers and activations, and slide 39 later returns to the same map with the activation function circled instead.
ما يجعل هذا طبقةً لا خطوة معالجة مسبقة (preprocessing) هو أن هذه الدالة قابلة للاشتقاق. فهي تقع داخل الشبكة، عند أي طبقة مخفية، وتمر الـ gradients خلالها تمامًا كما تمر خلال convolution. وتسمّيها ملاحظات CS231n معالجة مسبقة قابلة للاشتقاق مبنية داخل الشبكة نفسها. ولهذا يظهر Normalization في الشريحة 27 كمكوّن خامس للـ CNN، بجانب الـ convolution والـ pooling والطبقات الـ fully connected والـ activations، ثم تعود الشريحة 39 لاحقًا إلى الخريطة نفسها مع وضع دائرة حول دالة التنشيط بدلًا منه.
Recall
Give the two properties of an input x that make y = Wx hard to optimize, and what each forces the layer to do.اذكر الخاصيتين في المدخل x اللتين تجعلان تحسين y = Wx صعبًا، وما الذي تفرضه كل منهما على الطبقة.
Start with one column of numbers. Take the mini-batch from slide 38 after its ReLU, and look only at feature 1 across the four samples: (3, 1, 3, 2). Standardizing that column is the entire batch norm computation in miniature.
ابدأ بعمود واحد من الأرقام. خذ الـ mini-batch من الشريحة 38 بعد الـ ReLU، وانظر فقط إلى الخاصية 1 عبر العينات الأربع: (3, 1, 3, 2). توحيد مقياس هذا العمود هو حساب الـ batch norm كله في صورة مصغّرة.
Worked exampleمثال محلول
One feature, four samplesخاصية واحدة، أربع عينات
Mean over the batchالمتوسط على امتداد الـ batch
mu = (3 + 1 + 3 + 2) / 4 = 2.25mu = (3 + 1 + 3 + 2) / 4 = 2.25Variance over the batchالتباين على امتداد الـ batch
sigma squared = ((0.75) squared + (-1.25) squared + (0.75) squared + (-0.25) squared) / 4 = 0.6875sigma squared = ((0.75) squared + (-1.25) squared + (0.75) squared + (-0.25) squared) / 4 = 0.6875Normalize each valueوحّد مقياس كل قيمة
Divide each deviation by sqrt(0.6875 + eps), about 0.829: x_hat = (0.905, -1.508, 0.905, -0.302)اقسم كل انحراف عن المتوسط على sqrt(0.6875 + eps), about 0.829 (أي نحو 0.829): x_hat = (0.905, -1.508, 0.905, -0.302)Resultالنتيجة
One mean and one variance for the feature, computed across the samples. Every feature gets its own pair.متوسط واحد وتباين واحد للخاصية، يُحسبان عبر العينات. وكل خاصية تحصل على زوجها الخاص.
Now the general rule. The input to a batch norm layer after a fully connected layer is a matrix x of shape N x D: N samples in the mini-batch (the Batch size) by D features. Every average runs down the N axis, and that is the whole story of the axes: one statistic per feature column, averaged over the sample rows.
الآن القاعدة العامة. المدخل إلى طبقة batch norm بعد طبقة fully connected هو مصفوفة x شكلها N x D: عدد N من العينات في الـ mini-batch (أي الـ batch size) في عدد D من الخاصيات. كل متوسط يُحسب نزولًا على محور N، وهذه هي قصة المحاور كلها: إحصائية واحدة لكل عمود خاصية، محسوبة بالمتوسط على صفوف العينات.
Shape of every tensor in batch norm for a fully connected layerشكل كل tensor في الـ batch norm لطبقة fully connected
- x
- N x DN x D
- mu_j
- DD
- sigma_j squared
- DD
- x_hat
- N x DN x D
- gamma, beta
- DD
- y
- N x DN x D
The epsilon under the root is a tiny constant, 1e-5 by default in PyTorch, that prevents division by zero when a feature happens to be constant across the batch. It changes nothing numerically otherwise, but it appears in every formula and in every fusion derivation, so keep it.
الـ epsilon تحت الجذر ثابت صغير جدًا، قيمته 1e-5 افتراضيًا في PyTorch، يمنع القسمة على صفر حين تكون خاصية ما ثابتة عبر الـ batch. لا يغيّر شيئًا عدديًا فيما عدا ذلك، لكنه يظهر في كل معادلة وفي كل اشتقاق للدمج (fusion)، فاحتفظ به.
Why gamma and beta existلماذا توجد gamma و beta
Is forcing zero mean and unit variance always good? It is a hard constraint, and a network is not always best served by activations pinned to a standard distribution. So the layer gets two learnable vectors of shape D, gamma and beta, and outputs y = gamma x_hat + beta. Ioffe and Szegedy point out that setting gamma = sqrt(Var[x]) and beta = E[x] recovers the original activations, if that were the optimal thing to do. The network can therefore undo normalization entirely, which means normalization can never make the model less expressive; it can only change what is easy to learn.
هل فرض متوسط صفري وتباين يساوي واحدًا جيد دائمًا؟ إنه قيد صارم، والشبكة لا تُخدم دائمًا على أفضل وجه بتنشيطات مثبتة على توزيع معياري. لذلك تحصل الطبقة على متجهين قابلين للتعلّم شكلهما D، هما gamma و beta، وتُخرج y = gamma x_hat + beta. ويشير Ioffe و Szegedy إلى أن وضع gamma = sqrt(Var[x]) و beta = E[x] يستعيد التنشيطات الأصلية، إن كان ذلك هو الأمثل. فالشبكة إذن تستطيع إلغاء الـ normalization تمامًا، وهذا يعني أنه لا يمكن أن يجعل النموذج أقل قدرة على التعبير؛ كل ما يفعله أنه يغيّر ما يسهل تعلّمه.
Goodfellow, Bengio and Courville explain why the new parametrization learns more easily even though it can represent the same functions. In the original network the mean of a hidden unit is set by a long chain of interacting weights in all the layers below. After BN the mean is set by beta alone and the scale by gamma alone, two parameters with a direct, local effect on the quantity gradient descent needs to move.
يشرح Goodfellow و Bengio و Courville لماذا تتعلّم الصياغة الجديدة (parametrization) بسهولة أكبر رغم أنها تمثّل الدوال نفسها. في الشبكة الأصلية يتحدد متوسط الوحدة المخفية بسلسلة طويلة من الأوزان المتفاعلة في كل الطبقات التي تحتها. وبعد الـ BN يتحدد المتوسط بـ beta وحدها والمقياس بـ gamma وحدها، وهما معاملان لهما أثر مباشر ومحلي على الكمية التي يحتاج الـ gradient descent إلى تحريكها.
The tanh plot on slide 31 makes the point concrete. The derivative 1 - tanh squared (x) peaks at 1 at the origin and falls below 0.1 once |x| exceeds about 1.8. If normalized values feed a tanh, then gamma decides how far they reach into the flat, saturated tails and beta decides where the center of the band sits. A small gamma keeps the layer almost linear; a large one lets it saturate on purpose. The model chooses the amount of saturation instead of inheriting it from whatever scale the previous layer produced.
يجعل منحنى الـ tanh في الشريحة 31 الفكرة ملموسة. المشتقة 1 - tanh squared (x) تبلغ ذروتها 1 عند نقطة الأصل وتنخفض تحت 0.1 بمجرد أن تتجاوز |x| نحو 1.8. إذا دخلت القيم الموحّدة المقياس إلى tanh، فإن gamma تحدد إلى أي مدى تصل إلى الأطراف المسطّحة المشبعة، و beta تحدد أين يقع مركز النطاق. قيمة gamma صغيرة تُبقي الطبقة شبه خطية؛ وقيمة كبيرة تتيح لها التشبّع عن قصد. فالنموذج يختار مقدار التشبّع بدل أن يرثه من أي مقياس أنتجته الطبقة السابقة.
Recall
For x of shape N x D, write the four BN equations and the shape of each of mu, sigma squared, x_hat, gamma, beta and y.لمدخل x شكله N x D، اكتب معادلات الـ BN الأربع وشكل كل من mu و sigma squared و x_hat و gamma و beta و y.
Quick checkاختبار سريع
For a fully connected layer with input of shape N x D, over which axis does batch normalization average to get one mean?لطبقة fully connected مدخلها بشكل N x D، على أي محور يحسب الـ batch normalization المتوسط للحصول على متوسط واحد؟
A keyword-spotting model on a microcontroller hears one audio clip at a time. A batch of one has zero variance, so the statistics of the "batch" are meaningless, and even a batch of ten would make the answer for one clip depend on which nine clips happened to arrive with it. Ioffe and Szegedy state the requirement plainly: at inference the output should depend only on the input, deterministically.
نموذج لاكتشاف الكلمات المفتاحية (keyword spotting) على microcontroller يسمع مقطعًا صوتيًا واحدًا في كل مرة. والـ batch المكوّنة من عينة واحدة تباينها صفر، فتكون إحصاءات هذه "الـ batch" بلا معنى، وحتى batch من عشرة مقاطع ستجعل إجابة مقطع واحد تعتمد على المقاطع التسعة التي صادف وصولها معه. ويصوغ Ioffe و Szegedy الشرط بوضوح: في الاستدلال يجب أن يعتمد المخرج على المدخل وحده، وبشكل حتمي (deterministic).
So the layer changes its source of statistics. During training it keeps a running average of the mini-batch means and variances it has seen. At inference it uses those stored values and ignores the batch entirely. PyTorch updates the buffers as running = (1 - momentum) running + momentum x_batch with momentum 0.1, normalizes with the biased variance during training, and stores the unbiased variance (divide by N - 1) in running_var, following the paper's m/(m - 1) correction. Goodfellow, Bengio and Courville note the payoff: with running averages the model can be evaluated on a single example.
لذلك تغيّر الطبقة مصدر إحصاءاتها. أثناء التدريب تحتفظ بـ متوسط متحرك (running average) لمتوسطات وتباينات الـ mini-batches التي رأتها. وفي الاستدلال تستخدم تلك القيم المخزنة وتتجاهل الـ batch تمامًا. يحدّث PyTorch هذه الـ buffers بالقاعدة running = (1 - momentum) running + momentum x_batch مع momentum قيمته 0.1، ويوحّد المقياس بالتباين المتحيز (biased) أثناء التدريب، ويخزّن التباين غير المتحيز (القسمة على N - 1) في running_var، تبعًا لتصحيح الورقة m/(m - 1). ويلاحظ Goodfellow و Bengio و Courville المكسب: مع المتوسطات المتحركة يمكن تقييم النموذج على مثال واحد.
Frozen statistics turn BN into an affine mapالإحصاءات المجمّدة تحوّل الـ BN إلى تحويل أفيني (affine map)
Once mu and sigma squared are constants, batch norm on feature j is nothing more than multiply by one number and add another. And if the layer before it is a linear layer z = Wx + b, two affine maps in a row are one affine map. Write it out:
بمجرد أن تصبح mu و sigma squared ثوابت، لا يعدو الـ batch norm على الخاصية j كونه ضربًا في عدد وإضافة عدد آخر. وإذا كانت الطبقة التي قبله طبقة خطية z = Wx + b، فإن تحويلين أفينيين متتاليين يساويان تحويلًا أفينيًا واحدًا. اكتبه صراحة:
Read the first formula per output row j: the whole row of W is multiplied by the scalar gamma_j / sqrt(sigma_j squared + eps), and the bias becomes that scalar times (b_j - mu_j), plus beta_j. The same holds for a convolution: each output filter is scaled by its channel's factor. Ioffe and Szegedy already say this in the paper: the normalization is a linear transform that may be composed with the scaling by gamma and shift by beta into a single linear transform. PyTorch ships it as torch.nn.utils.fusion.fuse_conv_bn_eval, which requires both modules in eval mode with their running buffers computed. This is Batch norm fusion.
اقرأ المعادلة الأولى لكل صف مخرجات j: الصف كله من W يُضرب في العدد gamma_j / sqrt(sigma_j squared + eps)، ويصبح الـ bias هو هذا العدد مضروبًا في (b_j - mu_j) مضافًا إليه beta_j. والأمر نفسه ينطبق على الـ convolution: كل filter مخرجات يُحجَّم بمعامل قناته. وقد قال Ioffe و Szegedy هذا في الورقة نفسها: الـ normalization تحويل خطي يمكن تركيبه مع التحجيم بـ gamma والإزاحة بـ beta في تحويل خطي واحد. ويوفّره PyTorch باسم torch.nn.utils.fusion.fuse_conv_bn_eval، الذي يتطلب أن تكون الوحدتان في وضع eval وأن تكون الـ running buffers محسوبة. هذا هو BN fusion.
Worked exampleمثال محلول
Fusing BN into the slide 38 linear layerدمج الـ BN في الطبقة الخطية من الشريحة 38
Take the linear layer and frozen statisticsخذ الطبقة الخطية والإحصاءات المجمّدة
W = [[1, 0, 1], [1, 1, 0], [0, 2, -1]], b = (0, -1, 0), gamma = (2, 3, -1), beta = (0, 0, 1). For frozen statistics use the batch statistics of z = Wx + b itself (BN sits directly on the linear output, no ReLU in between): mu = (2.25, 1.25, 1.25), sigma squared = (0.6875, 1.6875, 6.6875).W = [[1, 0, 1], [1, 1, 0], [0, 2, -1]]، b = (0, -1, 0)، gamma = (2, 3, -1)، beta = (0, 0, 1). وكإحصاءات مجمّدة استخدم إحصاءات الـ batch لـ z = Wx + b نفسها (الـ BN يقع مباشرة على المخرج الخطي، دون ReLU بينهما): mu = (2.25, 1.25, 1.25)، sigma squared = (0.6875, 1.6875, 6.6875).Compute one scale factor per output rowاحسب معامل تحجيم واحدًا لكل صف مخرجات
s_j = gamma_j / sqrt(sigma_j squared + eps), so s = (2.412, 2.309, -0.387).s_j = gamma_j / sqrt(sigma_j squared + eps)، إذن s = (2.412, 2.309, -0.387).Scale each row of W and rebuild the biasحجّم كل صف من W وأعد بناء الـ bias
Row jالصف j gamma_j / sqrt(var_j + eps)gamma_j / sqrt(var_j + eps) Row j of W'الصف j من W' b'_jb'_j 11 2 / 0.829 = 2.4122 / 0.829 = 2.412 (2.412, 0, 2.412)(2.412, 0, 2.412) 2.412 (0 - 2.25) + 0 = -5.4272.412 (0 - 2.25) + 0 = -5.427 22 3 / 1.299 = 2.3093 / 1.299 = 2.309 (2.309, 2.309, 0)(2.309, 2.309, 0) 2.309 (-1 - 1.25) + 0 = -5.1962.309 (-1 - 1.25) + 0 = -5.196 33 -1 / 2.586 = -0.387-1 / 2.586 = -0.387 (0, -0.773, 0.387)(0, -0.773, 0.387) -0.387 (0 - 1.25) + 1 = 1.483-0.387 (0 - 1.25) + 1 = 1.483 Row jالصف j gamma_j / sqrt(var_j + eps)gamma_j / sqrt(var_j + eps) Row j of W'الصف j من W' b'_jb'_j 11 2 / 0.829 = 2.4122 / 0.829 = 2.412 (2.412, 0, 2.412)(2.412, 0, 2.412) 2.412 (0 - 2.25) + 0 = -5.4272.412 (0 - 2.25) + 0 = -5.427 22 3 / 1.299 = 2.3093 / 1.299 = 2.309 (2.309, 2.309, 0)(2.309, 2.309, 0) 2.309 (-1 - 1.25) + 0 = -5.1962.309 (-1 - 1.25) + 0 = -5.196 33 -1 / 2.586 = -0.387-1 / 2.586 = -0.387 (0, -0.773, 0.387)(0, -0.773, 0.387) -0.387 (0 - 1.25) + 1 = 1.483-0.387 (0 - 1.25) + 1 = 1.483 One linear layer replaces twoطبقة خطية واحدة تحل محل طبقتين
W'x + b' agrees with BN(Wx + b) to floating-point precision on all four slide 38 samples, which you will meet in full in the last concept. The deployed model holds only W' and b'; gamma, beta, the running mean and the running variance vanish from the binary.W'x + b' يطابق BN(Wx + b) حتى دقة الفاصلة العائمة على عينات الشريحة 38 الأربع كلها، والتي ستقابلها كاملة في المفهوم الأخير. النموذج المنشور يحمل فقط W' و b'؛ أما gamma و beta والمتوسط المتحرك والتباين المتحرك فتختفي من الملف التنفيذي.
For embedded deployment this is the reason the slide says zero overhead at test time. A fused network runs no extra multiply-accumulate operations, moves no extra activations through memory, launches one fewer kernel per layer, and stores four fewer vectors per layer in flash. In later lectures on pruning, the very gamma factors that get fused away are also what network slimming reads to decide which channels matter.
في النشر على الأنظمة المدمجة، هذا هو سبب قول الشريحة "صفر تكلفة إضافية وقت الاختبار". الشبكة المدموجة لا تنفّذ أي عمليات multiply-accumulate إضافية، ولا تنقل أي تنشيطات إضافية عبر الذاكرة، وتطلق kernel أقل بواحد في كل طبقة، وتخزّن أربعة متجهات أقل في كل طبقة في ذاكرة الـ flash. وفي محاضرات لاحقة عن الـ pruning، فإن معاملات gamma نفسها التي تُدمج وتختفي هنا هي ما يقرؤه الـ network slimming ليقرر أي القنوات مهمة.
What BN buys during training, and the one way it bitesما يقدّمه الـ BN أثناء التدريب، والطريقة الوحيدة التي يؤذيك بها
- Much easier training and higher learning rates: normalizing each layer's input stops small parameter changes from amplifying into large, suboptimal changes in activations and gradients as they pass through many layers (Ioffe and Szegedy, section 3.3). On ImageNet the paper reaches the same accuracy as its baseline in 14 times fewer training steps.تدريب أسهل بكثير ومعدلات تعلّم أعلى: توحيد مقياس مدخل كل طبقة يمنع التغييرات الصغيرة في المعاملات من أن تتضخم إلى تغييرات كبيرة وغير مثلى في التنشيطات والـ gradients أثناء مرورها عبر طبقات كثيرة (Ioffe و Szegedy، القسم 3.3). وعلى ImageNet تصل الورقة إلى دقة خط الأساس نفسها بعدد خطوات تدريب أقل بـ 14 مرة.
- Better gradient flow: the tanh argument from the previous concept applies at every layer. Activations are kept out of the saturated tails unless the model wants them there, so derivatives stay usable.تدفق أفضل للـ gradient: حجة الـ tanh من المفهوم السابق تنطبق عند كل طبقة. تُبقى التنشيطات خارج الأطراف المشبعة ما لم يرد النموذج وجودها هناك، فتظل المشتقات قابلة للاستخدام.
- Robustness to initialization: scaling the weights of a layer by a constant scales its output by the same constant, and normalization divides that constant back out. The scale of the initial weights largely stops mattering.متانة أمام الـ initialization: ضرب أوزان طبقة في ثابت يضرب مخرجها في الثابت نفسه، والـ normalization يقسم هذا الثابت ويلغيه. فيتوقف مقياس الأوزان الابتدائية عن أن يكون مهمًا إلى حد كبير.
- Regularization during training, the "How?" on slide 33: each sample is normalized with statistics that depend on the other random samples in its mini-batch, so the network no longer produces deterministic values for a given training example (section 3.4). That injected noise acts like a mild regularizer, and the paper reports that dropout can be removed or reduced in strength. Dive into Deep Learning notes that batch sizes around 50 to 100 inject roughly the right amount of noise.تنظيم (regularization) أثناء التدريب، وهو "How?" في الشريحة 33: كل عينة يُوحَّد مقياسها بإحصاءات تعتمد على العينات العشوائية الأخرى في الـ mini-batch الخاصة بها، فلا تعود الشبكة تنتج قيمًا حتمية لمثال تدريب معين (القسم 3.4). هذه الضوضاء المحقونة تعمل كمنظِّم خفيف، وتذكر الورقة أنه يمكن إزالة الـ dropout أو تخفيف قوته. ويلاحظ كتاب Dive into Deep Learning أن أحجام batch في حدود 50 to 100 تحقن تقريبًا المقدار المناسب من الضوضاء.
The red line on slide 33 is the hazard that follows from everything above. The layer computes something different in training mode (batch statistics) and evaluation mode (running statistics). Forget to call model.eval() and your deployed model's answer for one input depends on the other inputs in the batch and degrades silently; for a batch of one, x_hat is 0 by construction so every feature collapses to beta (PyTorch's BatchNorm1d refuses to run at all in that case). It is one of the most common bugs in deep learning code precisely because, for any larger batch, nothing crashes.
السطر الأحمر في الشريحة 33 هو الخطر الناتج عن كل ما سبق. الطبقة تحسب شيئًا مختلفًا في وضع التدريب (إحصاءات الـ batch) عنه في وضع التقييم (الإحصاءات المتحركة). انسَ استدعاء model.eval() وستصبح إجابة نموذجك المنشور لمدخل واحد معتمدة على المدخلات الأخرى في الـ batch، وتتدهور بصمت؛ وفي batch من عينة واحدة تكون x_hat مساوية لـ 0 بحكم التعريف، فتنهار كل خاصية إلى beta (والـ BatchNorm1d في PyTorch يرفض العمل أصلًا في هذه الحالة). إنه من أكثر الأخطاء شيوعًا في كود التعلّم العميق تحديدًا لأنه، مع أي batch أكبر، لا ينهار شيء.
| Varianceالتباين | Feature 1الخاصية 1 | Feature 2الخاصية 2 | Feature 3الخاصية 3 |
|---|---|---|---|
| Biased (training)متحيز (biased، أثناء التدريب) | 0.68750.6875 | 1.68751.6875 | 4.18754.1875 |
| Unbiased (running_var)غير متحيز (running_var) | 0.9170.917 | 2.252.25 | 5.5835.583 |
Recall
Why does BN behave differently at train and test time, and what does PyTorch store to make test time work?لماذا يتصرف الـ BN بشكل مختلف بين التدريب والاختبار، وما الذي يخزّنه PyTorch ليعمل وقت الاختبار؟
Recall
Derive W' and b' when BN with frozen statistics directly follows y = Wx + b.اشتق W' و b' عندما يأتي BN بإحصاءات مجمّدة مباشرة بعد y = Wx + b.
Quick checkاختبار سريع
During inference, which mean does a batch normalization layer subtract from its input?أثناء الاستدلال (inference)، أي متوسط تطرحه طبقة الـ batch normalization من مدخلها؟
Take one activation tensor from a convolution layer, N x C x H x W = 8 x 64 x 32 x 32. It holds about half a million numbers. Every normalization in this part does the same arithmetic, subtract a mean and divide by a standard deviation, then scale and shift. The only thing that distinguishes them is the answer to one question: which of those numbers are averaged together to make one mean?
خذ tensor تنشيطات واحدًا من طبقة convolution، N x C x H x W = 8 x 64 x 32 x 32. إنه يحمل نحو نصف مليون رقم. كل normalization في هذا الجزء يجري الحساب نفسه: اطرح متوسطًا واقسم على انحراف معياري، ثم حجّم وأزح. والشيء الوحيد الذي يميّز بعضها عن بعض هو الإجابة عن سؤال واحد: أي هذه الأرقام تُجمع معًا لتكوين متوسط واحد؟
| Normalizationالـ normalization | Values per meanعدد القيم لكل متوسط | Number of meansعدد المتوسطات |
|---|---|---|
| Batch norm for convolutionsالـ batch norm للـ convolutions | 8 x 32 x 32 = 81928 x 32 x 32 = 8192 | 6464 |
| Layer normالـ layer norm | 64 x 32 x 32 = 6553664 x 32 x 32 = 65536 | 88 |
| Instance norm, per Feature map per sampleالـ instance norm، لكل feature map لكل عينة | 32 x 32 = 102432 x 32 = 1024 | 512512 |
| Group normالـ group norm | 2 x 1024 = 20482 x 1024 = 2048 | 256256 |
Wu and He write all four as one formula. Each value x_i is normalized with a mean and standard deviation computed over a set S_i, and different normalizations simply use different definitions of that set:
يكتب Wu و He الأربعة كلها في معادلة واحدة. كل قيمة x_i يُوحَّد مقياسها بمتوسط وانحراف معياري محسوبين على مجموعة S_i، وأنواع الـ normalization المختلفة تستخدم ببساطة تعريفات مختلفة لهذه المجموعة:
| Normalizationالـ normalization | Averages overيحسب المتوسط على | mu, sigma shapeشكل mu و sigma | gamma, beta shapeشكل gamma و beta | Same at train and test?متطابق في التدريب والاختبار؟ |
|---|---|---|---|---|
| BN, fully connectedالـ BN، طبقة fully connected | NN | 1 x D1 x D | 1 x D1 x D | Noلا |
| BN, convolution (BatchNorm2d)الـ BN، convolution (BatchNorm2d) | N, H, WN, H, W | 1 x C x 1 x 11 x C x 1 x 1 | 1 x C x 1 x 11 x C x 1 x 1 | Noلا |
| Layer normLayer norm | D (or C, H, W)D (أو C, H, W) | N x 1 x 1 x 1 (N x 1 on slide 35)N x 1 x 1 x 1 (N x 1 في الشريحة 35) | 1 x D on slide 35; 1 x C x 1 x 1 in Wu and He1 x D في الشريحة 35؛ و 1 x C x 1 x 1 عند Wu و He | Yesنعم |
| Instance normInstance norm | H, WH, W | N x C x 1 x 1N x C x 1 x 1 | 1 x C x 1 x 11 x C x 1 x 1 | Yesنعم |
| Group normGroup norm | H, W and C/G channelsH, W و C/G من القنوات | N x G x 1 x 1N x G x 1 x 1 | 1 x C x 1 x 11 x C x 1 x 1 | Yesنعم |
Two patterns in that table carry most of the marks. First, the shape of mu is the tensor shape with a 1 wherever you averaged and the full size everywhere else. Batch norm averages over N, H, W and leaves 1 x C x 1 x 1; instance norm averages over H, W only and leaves N x C x 1 x 1. Second, gamma and beta do not follow the mu shape: they are per channel for BN, IN and GN. Ioffe and Szegedy learn a pair per feature map rather than per activation, and Wu and He write that BN, LN, IN and GN all learn a per-channel linear transform, the convention the table and the explorer below use. A BatchNorm2d over 64 channels therefore holds 128 learnable values, whatever the image size.
نمطان في هذا الجدول يحملان معظم الدرجات. الأول: شكل mu هو شكل الـ tensor مع 1 في كل محور حسبت عليه المتوسط، والحجم الكامل في كل محور آخر. الـ batch norm يحسب المتوسط على N, H, W ويترك 1 x C x 1 x 1؛ والـ instance norm يحسبه على H, W فقط ويترك N x C x 1 x 1. الثاني: gamma و beta لا تتبعان شكل mu: فهما لكل قناة في BN و IN و GN. يتعلّم Ioffe و Szegedy زوجًا لكل feature map لا لكل تنشيط، ويكتب Wu و He أن BN و LN و IN و GN كلها تتعلّم تحويلًا خطيًا لكل قناة، وهو الاصطلاح الذي يستخدمه الجدول والمستكشف أدناه. لذلك فإن BatchNorm2d على 64 قناة يحمل 128 قيمة قابلة للتعلّم، أيًّا كان حجم الصورة.
C/G = 1 (G = C) is instance normC/G = 1 (G = C) هو instance norm and و C/G = C (G = 1) is layer normC/G = C (G = 1) هو layer norm
Only the three visible faces are drawn. Lit cells share one mean with the anchor cell (teal column). Sample index runs into the depth, channel runs along the front edge.لا تُرسم إلا الأوجه الثلاثة الظاهرة. الخلايا المضاءة تشترك في متوسط واحد مع خلية المرجع (العمود الفيروزي). يمتد رقم العينة نحو العمق، وتمتد القناة على طول الحافة الأمامية.
Why the batch-free variants existلماذا توجد الأنواع التي لا تحتاج batch
Layer norm was introduced by Ba, Kiros and Hinton for exactly the cases where a batch is awkward. Its statistics come from all of the summed inputs to the neurons in a layer on a single training case, so it performs exactly the same computation at training and test time and is straightforward to apply to recurrent networks by computing the statistics separately at each time step. Slide 35 lists both facts. Because it never looks at another sample, it also works at batch size one, and the Transformer block in part 06 uses it in every Add and Norm step: Vaswani and colleagues define each sub-layer output as LayerNorm(x + Sublayer(x)).
قدّم Ba و Kiros و Hinton الـ layer norm للحالات التي يكون فيها الـ batch غير ملائم تحديدًا. إحصاءاته تأتي من كل المدخلات المجمّعة للعصبونات في طبقة واحدة على حالة تدريب واحدة، فيجري الحساب نفسه تمامًا في التدريب والاختبار، ويسهل تطبيقه على الشبكات التكرارية (recurrent networks) بحساب الإحصاءات منفصلة عند كل خطوة زمنية. وتذكر الشريحة 35 الحقيقتين. ولأنه لا ينظر أبدًا إلى عينة أخرى، فهو يعمل أيضًا مع batch size يساوي واحدًا، وكتلة الـ transformer في الجزء 06 تستخدمه في كل خطوة Add and Norm: إذ يعرّف Vaswani وزملاؤه مخرج كل طبقة فرعية بأنه LayerNorm(x + Sublayer(x)).
Instance norm came from style transfer. Ulyanov, Vedaldi and Lempitsky found that replacing batch norm with instance norm, applied both at training and testing, sharply improved stylization, because the per-image contrast statistics are exactly what a style transfer network needs to discard. PyTorch's InstanceNorm2d defaults to no affine parameters and no running statistics.
جاء الـ instance norm من نقل الأسلوب (style transfer). وجد Ulyanov و Vedaldi و Lempitsky أن استبدال الـ batch norm بالـ instance norm، مطبَّقًا في التدريب والاختبار معًا، حسّن الأسلبة بشكل كبير، لأن إحصاءات التباين (contrast) الخاصة بكل صورة هي بالضبط ما تحتاج شبكة نقل الأسلوب إلى التخلص منه. والـ InstanceNorm2d في PyTorch يكون افتراضيًا بلا معاملات affine وبلا إحصاءات متحركة.
Group norm is the embedded and detection story. Wu and He show that batch norm's error increases rapidly when the batch size becomes smaller: a ResNet-50 on ImageNet at batch size 2 reaches 34.7 percent error with BN and 24.1 percent with GN, while GN's computation is independent of batch size. With G = 32 as the default, GN becomes LN when G = 1 and IN when G = C, which is what the explorer's two slider ends show: one channel per group is instance norm, all channels in one group is layer norm. When you fine-tune a model on a device with a memory budget that allows one or two samples per step, a batch-free normalization is the difference between a model that trains and one that does not. Wu and He note that dropping the batch size constraint can free considerably more memory, sixteen times or more.
الـ group norm هو قصة الأنظمة المدمجة والكشف عن الأجسام (detection). يُظهر Wu و He أن خطأ الـ batch norm يزداد بسرعة حين يصغر الـ batch size: نموذج ResNet-50 على ImageNet عند batch size يساوي 2 يصل إلى خطأ 34.7 percent مع BN و 24.1 percent مع GN، بينما حساب GN مستقل عن الـ batch size. ومع G = 32 كقيمة افتراضية، يصبح GN هو LN عندما G = 1 ويصبح IN عندما G = C، وهذا ما يُظهره طرفا شريط التمرير في المستكشف: قناة واحدة لكل مجموعة هي instance norm، وكل القنوات في مجموعة واحدة هي layer norm. حين تعيد ضبط (fine-tune) نموذج على جهاز بميزانية ذاكرة تسمح بعينة أو عينتين في كل خطوة، يكون الـ normalization الذي لا يحتاج batch هو الفرق بين نموذج يتدرب ونموذج لا يتدرب. ويلاحظ Wu و He أن التخلص من قيد الـ batch size قد يحرّر ذاكرة أكثر بكثير، ستة عشر ضعفًا أو أكثر.
Recall
A conv output is 8 x 64 x 32 x 32. Give the mu shape for BN, LN, IN and GN with 32 groups, and how many values each mean averages.مخرج conv شكله 8 x 64 x 32 x 32. أعطِ شكل mu لكل من BN و LN و IN و GN بـ 32 مجموعة، وعدد القيم التي يجمعها كل متوسط.
Quick checkاختبار سريع
A BatchNorm2d layer follows a convolution with 64 output channels of size 32 x 32. How many learnable parameters does it hold?طبقة BatchNorm2d تأتي بعد convolution لها 64 قناة مخرجات حجم كل منها 32 x 32. كم معاملًا قابلًا للتعلّم تحمل؟
Quick checkاختبار سريع
Which normalization gives identical outputs at training and test time and never depends on other samples in the batch?أي نوع normalization يعطي مخرجات متطابقة في التدريب والاختبار ولا يعتمد أبدًا على العينات الأخرى في الـ batch؟
Tom Yeh's batch norm worksheet runs a mini-batch of four samples with three features each passes through a linear layer, a ReLU, then batch norm. Filling in every cell is the single best preparation for the computational question on this topic, so here is every number, recomputed rather than copied from the heavily rounded original.
في ورقة عمل الـ batch norm من Tom Yeh تمر mini-batch من أربع عينات، لكل منها ثلاث خاصيات، عبر طبقة خطية، ثم ReLU، ثم batch norm. ملء كل خلية فيها هو أفضل تحضير على الإطلاق للسؤال الحسابي في هذا الموضوع، لذلك إليك كل رقم، مُعاد حسابه لا منقولًا من الأصل كثير التقريب.
The layout hides two conventions. The mini-batch is written as columns x1 = (1, 0, 2), x2 = (0, 3, 1), x3 = (3, 1, 0), x4 = (0, 1, 2) with a row of ones appended, so the red 3 x 4 "Linear Layer" block is [W | b] with W = [[1, 0, 1], [1, 1, 0], [0, 2, -1]] and b = (0, -1, 0). Likewise the "Scale and Shift" block is [diag(gamma) | beta] with gamma = (2, 3, -1) and beta = (0, 0, 1), applied to the normalized matrix with its own row of ones. The small two-column "Trainable Parameters" box (drawn as a 2 x 2 grid in the blank template) stands for the columns [gamma | beta]; for this three-feature layer it holds a 3 x 2 matrix.
يخفي التخطيط اصطلاحين. الـ mini-batch مكتوبة كأعمدة x1 = (1, 0, 2)، x2 = (0, 3, 1)، x3 = (3, 1, 0)، x4 = (0, 1, 2) مع صف من الآحاد مضاف إليها، فتكون الكتلة الحمراء 3 x 4 المسماة "Linear Layer" هي [W | b] حيث W = [[1, 0, 1], [1, 1, 0], [0, 2, -1]] و b = (0, -1, 0). وبالمثل فإن كتلة "Scale and Shift" هي [diag(gamma) | beta] حيث gamma = (2, 3, -1) و beta = (0, 0, 1)، مطبّقة على المصفوفة الموحّدة المقياس مع صف الآحاد الخاص بها. والمربع الصغير ذو العمودين "Trainable Parameters" (المرسوم كشبكة 2 x 2 في القالب الفارغ) يمثّل العمودين [gamma | beta]؛ ولهذه الطبقة ذات الخاصيات الثلاث يحمل مصفوفة 3 x 2.
Worked exampleمثال محلول
Batch norm on the slide 38 mini-batchالـ batch norm على mini-batch الشريحة 38
Linear layer z = Wx + bالطبقة الخطية z = Wx + b
Row 1 of W is (1, 0, 1) with bias 0, so for x1 it gives 1 + 2 = 3. Doing this for every row and sample: row 1 is (3, 1, 3, 2), row 2 is (0, 2, 3, 0), row 3 is (-2, 5, 2, 0).الصف 1 من W هو (1, 0, 1) مع bias يساوي 0، فيعطي لـ x1 القيمة 1 + 2 = 3. وبتكرار ذلك لكل صف وعينة: الصف 1 هو (3, 1, 3, 2)، والصف 2 هو (0, 2, 3, 0)، والصف 3 هو (-2, 5, 2, 0).ReLUReLU
Only one value is negative. The -2 in row 3 becomes 0, so the batch norm input is (3, 1, 3, 2), (0, 2, 3, 0), (0, 5, 2, 0).قيمة واحدة فقط سالبة. الـ -2 في الصف 3 تصبح 0، فيكون مدخل الـ batch norm هو (3, 1, 3, 2)، (0, 2, 3, 0)، (0, 5, 2, 0).Batch statistics, one per feature rowإحصاءات الـ batch، واحدة لكل صف خاصية
Quantityالكمية Feature 1الخاصية 1 Feature 2الخاصية 2 Feature 3الخاصية 3 After ReLUبعد الـ ReLU 3, 1, 3, 23, 1, 3, 2 0, 2, 3, 00, 2, 3, 0 0, 5, 2, 00, 5, 2, 0 Sumالمجموع 99 55 77 Mean (divide by N = 4)المتوسط (القسمة على N = 4) 2.252.25 1.251.25 1.751.75 Variance (biased, divide by N)التباين (متحيز، القسمة على N) 0.68750.6875 1.68751.6875 4.18754.1875 Standard deviationالانحراف المعياري about 0.829نحو 0.829 about 1.299نحو 1.299 about 2.046نحو 2.046 Quantityالكمية Feature 1الخاصية 1 Feature 2الخاصية 2 Feature 3الخاصية 3 After ReLUبعد الـ ReLU 3, 1, 3, 23, 1, 3, 2 0, 2, 3, 00, 2, 3, 0 0, 5, 2, 00, 5, 2, 0 Sumالمجموع 99 55 77 Mean (divide by N = 4)المتوسط (القسمة على N = 4) 2.252.25 1.251.25 1.751.75 Variance (biased, divide by N)التباين (متحيز، القسمة على N) 0.68750.6875 1.68751.6875 4.18754.1875 Standard deviationالانحراف المعياري about 0.829نحو 0.829 about 1.299نحو 1.299 about 2.046نحو 2.046 Normalizeوحّد المقياس
Subtract the row mean, divide by the row standard deviation (with eps = 1e-5 under the root). For feature 1: (3 - 2.25) / 0.829 = 0.905, (1 - 2.25) / 0.829 = -1.508, and so on.اطرح متوسط الصف، واقسم على الانحراف المعياري للصف (مع eps = 1e-5 تحت الجذر). للخاصية 1: (3 - 2.25) / 0.829 = 0.905، (1 - 2.25) / 0.829 = -1.508، وهكذا.Scale and shift with gamma = (2, 3, -1), beta = (0, 0, 1)حجّم وأزح باستخدام gamma = (2, 3, -1) و beta = (0, 0, 1)
Rowالصف Feature 1الخاصية 1 Feature 2الخاصية 2 Feature 3الخاصية 3 x_hatx_hat 0.905, -1.508, 0.905, -0.3020.905, -1.508, 0.905, -0.302 -0.962, 0.577, 1.347, -0.962-0.962, 0.577, 1.347, -0.962 -0.855, 1.588, 0.122, -0.855-0.855, 1.588, 0.122, -0.855 gamma, betagamma, beta 2, 02, 0 3, 03, 0 -1, 1-1, 1 y = gamma x_hat + betay = gamma x_hat + beta 1.81, -3.02, 1.81, -0.601.81, -3.02, 1.81, -0.60 -2.89, 1.73, 4.04, -2.89-2.89, 1.73, 4.04, -2.89 1.86, -0.59, 0.88, 1.861.86, -0.59, 0.88, 1.86 Mean of y (should be beta)متوسط y (يجب أن يساوي beta) 00 00 11 Variance of y (should be gamma squared)تباين y (يجب أن يساوي gamma تربيع) 44 99 11 Rowالصف Feature 1الخاصية 1 Feature 2الخاصية 2 Feature 3الخاصية 3 x_hatx_hat 0.905, -1.508, 0.905, -0.3020.905, -1.508, 0.905, -0.302 -0.962, 0.577, 1.347, -0.962-0.962, 0.577, 1.347, -0.962 -0.855, 1.588, 0.122, -0.855-0.855, 1.588, 0.122, -0.855 gamma, betagamma, beta 2, 02, 0 3, 03, 0 -1, 1-1, 1 y = gamma x_hat + betay = gamma x_hat + beta 1.81, -3.02, 1.81, -0.601.81, -3.02, 1.81, -0.60 -2.89, 1.73, 4.04, -2.89-2.89, 1.73, 4.04, -2.89 1.86, -0.59, 0.88, 1.861.86, -0.59, 0.88, 1.86 Mean of y (should be beta)متوسط y (يجب أن يساوي beta) 00 00 11 Variance of y (should be gamma squared)تباين y (يجب أن يساوي gamma تربيع) 44 99 11 Self-check in ten secondsتحقق ذاتي في عشر ثوانٍ
Each output row has mean beta_j and variance gamma_j squared: means 0, 0, 1 and variances 4, 9, 1. If your rows do not satisfy this, the arithmetic slipped somewhere. Output values are rounded to two decimals, so treat them as approximate.كل صف من المخرجات متوسطه beta_j وتباينه gamma_j squared: المتوسطات 0, 0, 1 والتباينات 4, 9, 1. إن لم تحقق صفوفك ذلك، فقد انزلق الحساب في مكان ما. قيم المخرجات مقرّبة إلى منزلتين عشريتين، فاعتبرها تقريبية.
| x1 | x2 | x3 | x4 | |
|---|---|---|---|---|
| f1 | 1 | 0 | 3 | 0 |
| f2 | 0 | 3 | 1 | 1 |
| f3 | 2 | 1 | 0 | 2 |
| w1 | w2 | w3 | b | |
|---|---|---|---|---|
| f1 | 1 | 0 | 1 | 0 |
| f2 | 1 | 1 | 0 | -1 |
| f3 | 0 | 2 | -1 | 0 |
| x1 | x2 | x3 | x4 | |
|---|---|---|---|---|
| f1 | 3 | 1 | 3 | 2 |
| f2 | 0 | 2 | 3 | 0 |
| f3 | -2 | 5 | 2 | 0 |
| x1 | x2 | x3 | x4 | |
|---|---|---|---|---|
| f1 | 3 | 1 | 3 | 2 |
| f2 | 0 | 2 | 3 | 0 |
| f3 | 0 | 5 | 2 | 0 |
| f1 | f2 | f3 | |
|---|---|---|---|
| sum | 9 | 5 | 7 |
| mean | 2.2500 | 1.2500 | 1.7500 |
| var | 0.6875 | 1.6875 | 4.1875 |
| std | 0.829 | 1.299 | 2.046 |
| x1 | x2 | x3 | x4 | |
|---|---|---|---|---|
| f1 | 0.905 | -1.508 | 0.905 | -0.302 |
| f2 | -0.962 | 0.577 | 1.347 | -0.962 |
| f3 | -0.855 | 1.588 | 0.122 | -0.855 |
| x1 | x2 | x3 | x4 | |
|---|---|---|---|---|
| f1 | 1.809 | -3.015 | 1.809 | -0.603 |
| f2 | -2.887 | 1.732 | 4.041 | -2.887 |
| f3 | 1.855 | -0.588 | 0.878 | 1.855 |
| x1 | x2 | x3 | x4 | |
|---|---|---|---|---|
| train | 1.809 | -3.015 | 1.809 | -0.603 |
| eval | 4.000 | 0.000 | 4.000 | 2.000 |
Sample x1 gives 1.81 with batch statistics and 4.00 with the running ones. A model left in train mode at deployment produces the first number and its value depends on which other samples share the batch.تعطي العينة x1 القيمة 1.81 بإحصاءات الدفعة، و4.00 بالإحصاءات الجارية. النموذج الذي يُترك في وضع التدريب عند النشر يُنتج الرقم الأول، وتتوقف قيمته على العينات الأخرى التي تشاركه الدفعة.
Switch the simulator to eval mode and the same four inputs produce different outputs with the simulator's placeholder running mean and variance of one (PyTorch itself starts running_mean at 0 and running_var at 1): sample 1, feature 1 gives 4.00 instead of 1.81. That gap is the slide 33 bug made visible. To reproduce the fusion table of the previous concept, switch the placement to the paper order, press "load this batch's statistics" so the running mean becomes 2.25, 1.25, 1.25 and the running variance 0.6875, 1.6875, 6.6875, then press fuse: W' and b' appear computed live, with the check that they reproduce BN(Wx + b). With the placeholder running values of one you get a different, equally valid fused layer with scales 2, 3, -1, because the fused weights depend on whatever statistics training froze.
حوّل المحاكي إلى وضع eval وستنتج المدخلات الأربعة نفسها مخرجات مختلفة مع المتوسط والتباين المتحركين المؤقتين في المحاكي اللذين يساويان واحدًا (أما PyTorch نفسه فيبدأ running_mean عند 0 و running_var عند 1): العينة 1، الخاصية 1 تعطي 4.00 بدلًا من 1.81. هذه الفجوة هي خطأ الشريحة 33 وقد صار مرئيًا. ولإعادة إنتاج جدول الدمج من المفهوم السابق، حوّل موضع الطبقة إلى ترتيب الورقة، واضغط "load this batch's statistics" فيصبح المتوسط المتحرك 2.25, 1.25, 1.25 والتباين المتحرك 0.6875, 1.6875, 6.6875، ثم اضغط fuse: يظهر W' و b' محسوبين مباشرة، مع التحقق من أنهما يعيدان إنتاج BN(Wx + b). ومع القيم المتحركة المؤقتة التي تساوي واحدًا تحصل على طبقة مدموجة مختلفة وصحيحة بالقدر نفسه بمعاملات تحجيم 2, 3, -1، لأن الأوزان المدموجة تعتمد على أي إحصاءات جمّدها التدريب.
Recall
Feature values after ReLU are (0, 2, 3, 0). Compute mu, sigma squared and x_hat with epsilon negligible.قيم خاصية بعد الـ ReLU هي (0, 2, 3, 0). احسب mu و sigma squared و x_hat مع إهمال epsilon.
Recall
Which of mu, sigma squared, gamma and beta are learned by backpropagation, and which are computed?أيٌّ من mu و sigma squared و gamma و beta يُتعلَّم بالـ backpropagation، وأيها يُحسب؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Normalization exists because y = Wx is hard to optimize when inputs are off-center (a large bias is needed) or differently scaled (W entries must span very different magnitudes).وُجد الـ normalization لأن تحسين y = Wx صعب حين تكون المدخلات بعيدة عن المركز (فنحتاج bias كبيرًا) أو مختلفة المقياس (فيجب أن تمتد قيم W على مقادير متباعدة جدًا).
- BN works per feature over the batch: mu and sigma squared have shape D, x_hat = (x - mu)/sqrt(sigma squared + eps), y = gamma x_hat + beta. gamma and beta are learned, mu and sigma are computed.يعمل الـ BN لكل feature على امتداد الـ batch: شكل mu و sigma تربيع هو D، و x_hat = (x - mu)/sqrt(sigma squared + eps)، و y = gamma x_hat + beta. الـ gamma و beta تُتعلَّم، أما mu و sigma فتُحسب.
- gamma = sqrt(sigma squared + eps) and beta = mu recover the identity; before tanh they decide how far activations reach into the saturated tails.القيمتان gamma = sqrt(sigma squared + eps) و beta = mu تستعيدان دالة الهوية (identity)؛ وقبل tanh تحددان إلى أي مدى تصل الـ activations إلى الأطراف المشبعة (saturated tails).
- At inference mu and sigma squared are running averages, so BN is an affine map that folds into the previous FC or conv: W' = diag(gamma/sqrt(sigma squared + eps)) W, b' = gamma (b - mu)/sqrt(sigma squared + eps) + beta, at zero test-time cost.في الاستدلال تكون mu و sigma تربيع متوسطات متحركة (running averages)، فيصبح الـ BN تحويلًا أفينيًا (affine) يُدمج في طبقة الـ FC أو الـ conv السابقة: W' = diag(gamma/sqrt(sigma squared + eps)) W و b' = gamma (b - mu)/sqrt(sigma squared + eps) + beta، دون أي تكلفة وقت الاختبار.
- Benefits: easier training, better gradient flow, higher learning rates, robustness to initialization, regularization from batch noise. Hazard: train and eval mode behave differently.الفوائد: تدريب أسهل، وتدفق أفضل للـ gradient، ومعدلات تعلّم أعلى، ومتانة أمام الـ initialization، وتنظيم (regularization) ناتج عن ضوضاء الـ batch. الخطر: وضع التدريب ووضع التقييم (train و eval) يتصرفان بشكل مختلف.
- BN for conv averages over N, H, W (mu is 1 x C x 1 x 1); LN over features per sample (N x 1); IN over H, W per sample and channel (N x C x 1 x 1); GN over H, W and C/G channels. gamma and beta are per channel (1 x C x 1 x 1) for BN, IN and GN; LN on an N x D input has them 1 x D on slide 35, per channel in Wu and He.الـ BN للـ conv يحسب المتوسط على N و H و W (شكل mu هو 1 x C x 1 x 1)؛ والـ LN على الـ features لكل عينة (N x 1)؛ والـ IN على H و W لكل عينة وقناة (N x C x 1 x 1)؛ والـ GN على H و W و C/G من القنوات. الـ gamma و beta لكل قناة (1 x C x 1 x 1) في BN و IN و GN؛ أما LN على مدخل N x D فشكلهما 1 x D في الشريحة 35، ولكل قناة عند Wu و He.
- LN, IN and GN need no batch and behave the same at train and test, which is why LN runs in RNNs and transformers and GN wins at batch size 2.الـ LN و IN و GN لا تحتاج batch وتتصرف بالطريقة نفسها في التدريب والاختبار، ولهذا يُستخدم LN في الـ RNNs والـ transformers ويتفوق GN عند batch size يساوي 2.
- Slide 38 batch: means 2.25, 1.25, 1.75; variances 0.6875, 1.6875, 4.1875; every output row has mean beta and variance gamma squared.batch الشريحة 38: المتوسطات 2.25 و 1.25 و 1.75؛ والتباينات 0.6875 و 1.6875 و 4.1875؛ وكل صف من المخرجات متوسطه beta وتباينه gamma تربيع.
Sourcesالمصادر
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate ShiftPaperIoffe and Szegedy, ICML 2015Algorithm 1, identity recovery via gamma and beta, inference with population statistics, regularization (3.4), higher learning rates (3.3), per-feature-map parameters, 14x fewer steps.الخوارزمية 1، واستعادة دالة الهوية عبر gamma و beta، والاستدلال بإحصاءات المجتمع، والـ regularization (3.4)، ومعدلات التعلّم الأعلى (3.3)، والمعاملات لكل feature map، وخطوات أقل بـ 14 مرة.(opens in a new tab)(يفتح في تبويب جديد)
- Layer NormalizationPaperBa, Kiros and Hinton, 2016Statistics from a single training case, same computation at train and test, application to recurrent networks.إحصاءات من حالة تدريب واحدة، والحساب نفسه في التدريب والاختبار، والتطبيق على الشبكات التكرارية (recurrent networks).(opens in a new tab)(يفتح في تبويب جديد)
- Instance Normalization: The Missing Ingredient for Fast StylizationPaperUlyanov, Vedaldi and Lempitsky, 2016Replacing batch norm with instance norm at training and test time for style transfer.استبدال الـ batch norm بالـ instance norm في التدريب والاختبار لنقل الأسلوب (style transfer).(opens in a new tab)(يفتح في تبويب جديد)
- Improved Texture Networks: Maximizing Quality and Diversity in Feed-forward Stylization and Texture SynthesisPaperUlyanov, Vedaldi and Lempitsky, CVPR 2017The instance normalization reference cited on slide 36.مرجع الـ instance normalization المذكور في الشريحة 36.(opens in a new tab)(يفتح في تبويب جديد)
- Group NormalizationPaperWu and He, ECCV 2018Unified S_i formulation, G = 32 default, LN at G = 1 and IN at G = C, per-channel gamma and beta, 24.1 vs 34.7 percent error at batch size 2, memory remark.صياغة S_i الموحّدة، و G = 32 كقيمة افتراضية، و LN عند G = 1 و IN عند G = C، و gamma و beta لكل قناة، وخطأ 24.1 مقابل 34.7 في المئة عند batch size يساوي 2، وملاحظة الذاكرة.(opens in a new tab)(يفتح في تبويب جديد)
- Attention Is All You NeedPaperVaswani et al., NeurIPS 2017Section 3.1: each sub-layer output is LayerNorm(x + Sublayer(x)).القسم 3.1: مخرج كل طبقة فرعية هو LayerNorm(x + Sublayer(x)).(opens in a new tab)(يفتح في تبويب جديد)
- How Does Batch Normalization Help Optimization?PaperSanturkar, Tsipras, Ilyas and Madry, NeurIPS 2018BN smooths the optimization landscape; internal covariate shift is not the cause of its success.الـ BN يجعل سطح التحسين أنعم؛ والـ internal covariate shift ليس سبب نجاحه.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 8.7.1: Batch NormalizationBookGoodfellow, Bengio and Courville, MIT PressAdaptive reparametrization, why beta and gamma make the mean and scale easy to learn, running averages at test time.إعادة الصياغة التكيّفية (adaptive reparametrization)، ولماذا تجعل beta و gamma تعلّم المتوسط والمقياس سهلًا، والمتوسطات المتحركة وقت الاختبار.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 2DocsMIT HAN LabSlide 41: normalization makes optimization faster, unified S_i definition, per-channel linear transform.الشريحة 41: الـ normalization يسرّع التحسين، وتعريف S_i الموحّد، والتحويل الخطي لكل قناة.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.BatchNorm2dDocsPyTorch documentationeps 1e-5, momentum 0.1, biased variance in training and unbiased running variance, running estimates in eval mode, gamma and beta of size C.eps يساوي 1e-5، و momentum يساوي 0.1، والتباين المتحيز في التدريب والتباين المتحرك غير المتحيز، والتقديرات المتحركة في وضع eval، و gamma و beta بحجم C.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.LayerNormDocsPyTorch documentationStatistics over the last D dimensions, identical in training and evaluation modes.الإحصاءات على آخر D من الأبعاد، متطابقة في وضعي التدريب والتقييم.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.GroupNormDocsPyTorch documentationEach group holds num_channels / num_groups channels; same statistics at train and eval.كل مجموعة تحمل num_channels / num_groups من القنوات؛ والإحصاءات نفسها في التدريب والتقييم.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.InstanceNorm2dDocsPyTorch documentationInstance statistics at train and eval, affine and track_running_stats off by default.إحصاءات لكل instance في التدريب والتقييم، و affine و track_running_stats مطفأة افتراضيًا.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.utils.fusion.fuse_conv_bn_evalDocsPyTorch documentationFuses a convolution and a batch norm in eval mode into a single convolution.يدمج convolution و batch norm في وضع eval في convolution واحدة.(opens in a new tab)(يفتح في تبويب جديد)
- CS231n notes: Setting up the data and the modelDocsStanford UniversityBatch norm as differentiable preprocessing, inserted after FC or conv layers and before nonlinearities.الـ batch norm كمعالجة مسبقة قابلة للاشتقاق، تُدرج بعد طبقات الـ FC أو الـ conv وقبل الدوال غير الخطية.(opens in a new tab)(يفتح في تبويب جديد)
- Dive into Deep Learning, section 8.5: Batch NormalizationBookZhang, Lipton, Li and SmolaTraining versus prediction mode, batch noise as regularization at batch sizes of 50 to 100, epsilon, per-channel BN for convolutions.وضع التدريب مقابل وضع التنبؤ، وضوضاء الـ batch كـ regularization عند أحجام batch من 50 إلى 100، و epsilon، والـ BN لكل قناة في الـ convolutions.(opens in a new tab)(يفتح في تبويب جديد)
- Batch Normalization by HandArticleTom Yeh, byhand.aiSource of the slide 38 worksheet. The full sheet is paywalled; values here were recomputed.مصدر ورقة عمل الشريحة 38. الورقة الكاملة خلف اشتراك مدفوع؛ والقيم هنا معاد حسابها.(opens in a new tab)(يفتح في تبويب جديد)
Part 06: Activation functions and the transformerدوال التنشيط والمحوّل (Transformer)
The non-linearities used in embedded networks, then the transformer block: scaled dot-product attention worked by hand and the position-wise feed-forward network.اللاخطيات المستخدمة في الشبكات المضمّنة، ثم كتلة المحوّل: انتباه الضرب النقطي المقيّس (scaled dot-product attention) محلولًا يدويًا، وشبكة التغذية الأمامية الموضعية (position-wise feed-forward).
6 concepts6 مفاهيم, ، slides 39-45الشرائح 39-45
Why this part mattersلماذا يهمّ هذا الجزء
Every model you will compress, quantize or deploy on a microcontroller in this course has an activation function between its layers, and that one choice decides whether int8 inference is exact and cheap or approximate and slow. MobileNetV2 and V3 picked ReLU6 and hard swish for exactly this reason.
The second half of the part opens the transformer, now the model family you are most likely to be asked to shrink for the edge. Its attention block has a cost that grows with the square of the sequence length, which is the first number an embedded designer must reason about. The exam asks for the attention formula with shapes, the sqrt(d_k) argument, and a small attention computed by hand, so the part ends with two worked slides checked line by line, including two arithmetic slips on the slides themselves.
كل نموذج ستضغطه أو تكمّمه (quantize) أو تنشره على متحكم دقيق (microcontroller) في هذا المقرر توجد بين طبقاته دالة تفعيل، وهذا الاختيار الواحد يحدد هل سيكون الاستدلال بـ int8 دقيقًا ورخيصًا أم تقريبيًا وبطيئًا. ولهذا السبب تحديدًا اختارت MobileNetV2 وV3 الدالتين ReLU6 وhard swish.
النصف الثاني من هذا الجزء يفتح الـ transformer، وهو الآن عائلة النماذج التي يُرجَّح أن يُطلب منك تصغيرها للأجهزة الطرفية (edge). كتلة الانتباه فيه لها كلفة تنمو مع مربع طول التسلسل، وهذا أول رقم يجب أن يفكّر فيه مصمم الأنظمة المضمّنة. والاختبار يطلب صيغة الانتباه مع الأشكال، وحجة القسمة على sqrt(d_k)، وحساب انتباه صغير يدويًا، لذلك ينتهي الجزء بشريحتين محلولتين مُراجعتين سطرًا بسطر، بما في ذلك خطآن حسابيان في الشرائح نفسها.
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain why activations must be non-linear, and write sigmoid, ReLU, ReLU6, leaky ReLU, swish and hard swish with their ranges and derivatives.تشرح لماذا يجب أن تكون دوال التفعيل غير خطية، وتكتب sigmoid وReLU وReLU6 وleaky ReLU وswish وhard swish مع مداها ومشتقاتها.
- Argue which activations suit quantized embedded models and why: a bounded range and no exponential.تحتج لأي دوال التفعيل تناسب النماذج المضمّنة المكمّمة (quantized) ولماذا: مدى محدود وعدم وجود دالة أُسّية.
- Draw the transformer block and place multi-head attention, Add and Norm, the feed-forward network and positional encoding.ترسم كتلة الـ transformer وتحدد موضع multi-head attention وAdd and Norm والشبكة الأمامية (feed-forward network) والترميز الموضعي (positional encoding).
- State Attention(Q, K, V) with every matrix shape, explain the query, key and value roles, and justify the sqrt(d_k) scaling.تكتب Attention(Q, K, V) مع شكل كل مصفوفة، وتشرح أدوار الاستعلام (query) والمفتاح (key) والقيمة (value)، وتبرّر القسمة على sqrt(d_k).
- Compute a small self-attention and a position-wise FFN by hand, and explain why the N x N attention matrix is the embedded bottleneck.تحسب self-attention صغيرًا وشبكة FFN تعمل على كل موضع (position-wise) يدويًا، وتشرح لماذا مصفوفة الانتباه N x N هي عنق الزجاجة في الأنظمة المضمّنة.
Start with the Neuron from the beginning of the lecture: three inputs, three weights, a Bias, and then y = f(w0 x0 + w1 x1 + w2 x2 + b). Now ask what happens if f is the identity and you stack two such layers. The second layer computes W2 (W1 x + b1) + b2, which multiplies out to (W2 W1) x + (W2 b1 + b2): one matrix, one bias, one linear layer. Ten layers would collapse the same way. Depth buys nothing.
ابدأ بـ neuron (العصبون) من بداية المحاضرة: ثلاثة مدخلات، وثلاثة weights (أوزان)، وbias (انحياز)، ثم y = f(w0 x0 + w1 x1 + w2 x2 + b). الآن اسأل: ماذا يحدث لو كانت f هي الدالة المحايدة (identity) وكدّست طبقتين من هذا النوع؟ الطبقة الثانية تحسب W2 (W1 x + b1) + b2، وبفك الأقواس تصبح (W2 W1) x + (W2 b1 + b2): مصفوفة واحدة، وانحياز واحد، أي طبقة خطية واحدة. وعشر طبقات ستنهار بالطريقة نفسها. العمق لا يضيف شيئًا.
Worked exampleمثال محلول
Two linear layers collapse into oneطبقتان خطيتان تنهاران إلى طبقة واحدة
Write the second layer in terms of the firstاكتب الطبقة الثانية بدلالة الأولى
y = W2 h + b2 with h = W1 x + b1, so y = W2 W1 x + W2 b1 + b2.y = W2 h + b2 حيث h = W1 x + b1، إذن y = W2 W1 x + W2 b1 + b2.Name the productsسمِّ حواصل الضرب
Set W = W2 W1 and b = W2 b1 + b2. Both are constants once training stops.ضع W = W2 W1 وb = W2 b1 + b2. كلاهما ثابت بمجرد انتهاء التدريب.A single fully connected layerطبقة واحدة متصلة بالكامل
y = W x + b. The stacked network can only draw straight decision boundaries, exactly like the one layer it collapsed into. Goodfellow, Bengio and Courville open their chapter on feedforward networks with XOR for this reason: no single line separates its four points, but a hidden layer of rectified units does.y = W x + b. الشبكة المكدّسة لا تستطيع رسم إلا حدود قرار مستقيمة، تمامًا مثل الطبقة الواحدة التي انهارت إليها. ولهذا السبب يفتتح Goodfellow وBengio وCourville فصلهم عن الشبكات الأمامية (feedforward) بمسألة XOR: لا يوجد خط واحد يفصل نقاطها الأربع، لكن طبقة مخفية من الوحدات المقوِّمة (rectified units) تفعل ذلك.
That is why the slide says activation functions are typically non-linear. The Activation function is the bend that stops the collapse, and it sits after every weighted sum in every Hidden layer. Slide 39 places it on the CNN component map beside convolution, pooling, fully connected layers and the normalization block of the previous part: the only component with no parameters and no shape arithmetic, just a pointwise bend applied to every value. Which bend to use is a real design decision, and on embedded hardware it is decided by two questions the slide leaves implicit: does the function need an exponential, and is its output bounded?
لهذا تقول الشريحة إن دوال التفعيل تكون عادةً غير خطية. فـ activation function (دالة التفعيل) هي الانحناء الذي يمنع الانهيار، وتأتي بعد كل مجموع موزون في كل hidden layer (طبقة مخفية). تضعها الشريحة 39 على خريطة مكونات الـ CNN بجانب الالتفاف (convolution) والتجميع (pooling) والطبقات المتصلة بالكامل وكتلة التطبيع (normalization) من الجزء السابق: إنها المكوّن الوحيد الذي لا يملك معاملات ولا حسابات أشكال، بل مجرد انحناء نقطي يُطبَّق على كل قيمة. واختيار الانحناء قرار تصميمي حقيقي، وفي العتاد المضمّن يحسمه سؤالان تتركهما الشريحة ضمنيين: هل تحتاج الدالة إلى دالة أُسّية، وهل مخرجها محدود؟
The six on the slide, and what each costs a microcontrollerالدوال الست في الشريحة، وكلفة كل منها على المتحكم الدقيق
Sigmoid, 1 / (1 + e^-x), squashes any input into (0, 1). Its derivative is s(1 - s), at most 0.25 at zero and already 0.0066 at x = 5. A sigmoid unit therefore saturates across most of its domain, which is why Goodfellow and colleagues say its use as a hidden unit is now discouraged. It also needs an exponential per element. ReLU, max(0, x), has derivative 0 or 1, needs a single comparison, and is the same textbook's recommended default; AlexNet is the network that made it standard.
Sigmoid، أي 1 / (1 + e^-x)، تضغط أي مدخل إلى المجال (0, 1). مشتقتها s(1 - s)، وأقصاها 0.25 عند الصفر، وتصبح 0.0066 فقط عند x = 5. لذلك تتشبّع وحدة sigmoid على معظم مجالها، وهذا سبب قول Goodfellow وزملائه إن استخدامها كوحدة مخفية لم يعد مُستحسَنًا. وهي تحتاج أيضًا إلى دالة أُسّية لكل عنصر. أما ReLU، أي max(0, x)، فمشتقتها 0 أو 1، وتحتاج مقارنة واحدة فقط، وهي الخيار الافتراضي الذي يوصي به الكتاب نفسه؛ وAlexNet هي الشبكة التي جعلتها معيارًا.
ReLU6, min(max(0, x), 6), is ReLU with a ceiling. Krizhevsky introduced the cap in 2010 to encourage sparse features; MobileNetV2 kept it for a different reason, robustness when used with low-precision computation. Leaky ReLU, max(alpha x, x), replaces the flat negative side with a small slope so a unit that is off still passes a gradient; Maas, Hannun and Ng used alpha = 0.01, which is still PyTorch's default. Swish, x / (1 + e^-x), is x times its own sigmoid (the general form is x sigmoid(beta x), PyTorch calls it SiLU). It dips to -0.278 at x = -1.28 before rising, and Ramachandran, Zoph and Le found it beats ReLU on deeper models. It costs an exponential, which is the whole problem on a Cortex-M.
ReLU6، أي min(max(0, x), 6)، هي ReLU مع سقف. قدّم Krizhevsky هذا السقف عام 2010 لتشجيع الميزات المتناثرة (sparse features)؛ واحتفظت به MobileNetV2 لسبب مختلف، وهو المتانة عند استخدامها مع حسابات منخفضة الدقة. Leaky ReLU، أي max(alpha x, x)، تستبدل الجانب السالب المسطّح بميل صغير بحيث تظل الوحدة المطفأة تمرّر تدرّجًا؛ استخدم Maas وHannun وNg القيمة alpha = 0.01، وما زالت القيمة الافتراضية في PyTorch. أما Swish، أي x / (1 + e^-x)، فهي x مضروبة في الـ sigmoid الخاصة بها (الصيغة العامة x sigmoid(beta x)، وتسميها PyTorch باسم SiLU). تنخفض إلى -0.278 عند x = -1.28 قبل أن ترتفع، ووجد Ramachandran وZoph وLe أنها تتفوق على ReLU في النماذج الأعمق. لكنها تكلّف دالة أُسّية، وهذه هي المشكلة كلها على معالج Cortex-M.
Hard swish is MobileNetV3's answer: x ReLU6(x + 3) / 6. Expand it and you get the slide's three cases, 0 for x <= -3, x for x >= 3, and x(x + 3) / 6 between. Howard and colleagues give three reasons: optimized ReLU6 kernels exist on virtually every framework and chip, the piecewise form removes the precision loss that different approximate sigmoids introduce in quantized mode, and it can run with fewer memory accesses. They saw no discernible accuracy difference against real swish, and an optimized h-swish added about 1 ms over ReLU on a Pixel 1.
Hard swish هي إجابة MobileNetV3: x ReLU6(x + 3) / 6. بفكّها تحصل على الحالات الثلاث في الشريحة: 0 عندما x <= -3، وx عندما x >= 3، وx(x + 3) / 6 بينهما. يذكر Howard وزملاؤه ثلاثة أسباب: أنوية (kernels) ReLU6 المحسّنة موجودة تقريبًا على كل إطار عمل وكل شريحة، والصيغة المجزّأة تزيل فقدان الدقة الذي تسببه تقريبات sigmoid المختلفة في الوضع المكمَّم، ويمكن تشغيلها بعدد أقل من الوصول إلى الذاكرة. ولم يلاحظوا أي فرق ملموس في الدقة مقارنة بـ swish الحقيقية، وأضافت h-swish المحسّنة حوالي 1 ms فقط فوق ReLU على هاتف Pixel 1.
| xx | swishswish | hard swishhard swish | gapالفجوة |
|---|---|---|---|
| -3-3 | -0.142-0.142 | 00 | 0.1420.142 |
| -1-1 | -0.269-0.269 | -0.333-0.333 | 0.0640.064 |
| 00 | 00 | 00 | 00 |
| 11 | 0.7310.731 | 0.6670.667 | 0.0640.064 |
| 33 | 2.8582.858 | 33 | 0.1420.142 |
| 44 | 3.9283.928 | 44 | 0.0720.072 |
The largest gap on [-6, 6] is 0.142, at the two knots x = -3 and x = 3. The derivative also lines up: swish has slope 0.5 at zero and hard swish has (2x + 3) / 6 = 0.5 there too.
أكبر فجوة على المجال [-6, 6] هي 0.142، عند العقدتين x = -3 وx = 3. والمشتقة تتطابق أيضًا: ميل swish عند الصفر 0.5، وميل hard swish هناك (2x + 3) / 6 = 0.5 كذلك.
| Functionالدالة | Formulaالصيغة | Rangeالمدى | Derivativeالمشتقة | Needsتحتاج | Embedded noteملاحظة للأنظمة المضمّنة |
|---|---|---|---|---|---|
| SigmoidSigmoid | 1 / (1 + e^-x)1 / (1 + e^-x) | (0, 1)(0, 1) | s(1 - s), at most 0.25s(1 - s)، و0.25 على الأكثر | expexp | Saturates both sides; output gates and probabilities onlyتتشبّع من الجانبين؛ لبوابات المخرج والاحتمالات فقط |
| ReLUReLU | max(0, x)max(0, x) | [0, inf)[0, inf) | 0 or 10 أو 1 | max onlymax فقط | Default hidden unit; unbounded, so the int8 range must be calibratedالوحدة المخفية الافتراضية؛ غير محدودة، لذا يجب معايرة مدى int8 |
| ReLU6ReLU6 | min(max(0, x), 6)min(max(0, x), 6) | [0, 6][0, 6] | 1 on (0, 6), else 01 على (0, 6)، و0 فيما عدا ذلك | max and minmax وmin | Bounded: fixed quantization grid; MobileNetV2's choiceمحدودة: شبكة تكميم ثابتة؛ اختيار MobileNetV2 |
| Leaky ReLULeaky ReLU | max(alpha x, x)max(alpha x, x) | (-inf, inf)(-inf, inf) | alpha or 1alpha أو 1 | max onlymax فقط | Keeps a small gradient for negative inputs, alpha = 0.01 by defaultتُبقي تدرّجًا صغيرًا للمدخلات السالبة، والقيمة الافتراضية alpha = 0.01 |
| SwishSwish | x / (1 + e^-x)x / (1 + e^-x) | [-0.278, inf)[-0.278, inf) | s + x s(1 - s)s + x s(1 - s) | expexp | Smooth and non-monotonic; better than ReLU on deep models, costly on MCUsناعمة وغير رتيبة (non-monotonic)؛ أفضل من ReLU في النماذج العميقة، ومكلفة على المتحكمات الدقيقة (MCUs) |
| Hard swishHard swish | x ReLU6(x + 3) / 6x ReLU6(x + 3) / 6 | [-0.375, inf)[-0.375, inf) | 0, (2x + 3)/6, or 10 أو (2x + 3)/6 أو 1 | multiply and clampضرب وقصّ (clamp) | Swish traced with a ruler; MobileNetV3's replacement for swishSwish مرسومة بالمسطرة؛ البديل الذي اعتمدته MobileNetV3 عن swish |
| tanhtanh | 2 s(2x) - 12 s(2x) - 1 | (-1, 1)(-1, 1) | 1 - tanh^21 - tanh^2 | expexp | Zero-centred sigmoid; still saturatessigmoid متمركزة حول الصفر؛ وما زالت تتشبّع |
| GELUGELU | x Phi(x)x Phi(x) | [-0.17, inf)[-0.17, inf) | Phi(x) + x phi(x)Phi(x) + x phi(x) | erf or tanherf أو tanh | Default in BERT and GPT style transformersالافتراضية في نماذج الـ transformer من نمط BERT وGPT |
| ELUELU | x if x > 0 else e^x - 1x if x > 0 else e^x - 1 | (-1, inf)(-1, inf) | 1 or e^x1 أو e^x | expexp | Negative saturation at -1 pushes the mean toward zeroالتشبّع السالب عند -1 يدفع المتوسط نحو الصفر |
| MishMish | x tanh(softplus(x))x tanh(softplus(x)) | [-0.31, inf)[-0.31, inf) | numeric in practiceتُحسب عدديًا في التطبيق | exp, log, tanhexp وlog وtanh | Smooth like swish, even more expensiveناعمة مثل swish، وأغلى منها كلفة |
Why bounded is what int8 wantsلماذا المحدودية هي ما يريده int8
Quantizing an Activation tensor to 8 bits means choosing a range and dividing it into 255 steps. With ReLU6 the range is known before you see a single input: [0, 6], so each step is 6 / 255 = 0.0235 and every layer shares the same grid. With plain ReLU the range depends on the data. If one activation channel happens to reach 60, the step becomes 60 / 255 = 0.235, ten times coarser, for every value in that tensor, including the many small ones. MobileNetV2's sentence about low-precision robustness is the citation; the arithmetic is the reason.
تكميم موتّر activation (تفعيلات) إلى 8 بتات يعني اختيار مدى وتقسيمه إلى 255 خطوة. مع ReLU6 يكون المدى معروفًا قبل أن ترى أي مدخل: [0, 6]، فكل خطوة 6 / 255 = 0.0235 وتتشارك كل الطبقات الشبكة نفسها. أما مع ReLU العادية فالمدى يعتمد على البيانات. فإذا وصلت قناة تفعيل واحدة إلى 60، تصبح الخطوة 60 / 255 = 0.235، أي أخشن بعشر مرات، لكل قيمة في ذلك الموتّر، بما فيها القيم الصغيرة الكثيرة. جملة MobileNetV2 عن المتانة مع الدقة المنخفضة هي المرجع؛ والحساب هو السبب.
Worked exampleمثال محلول
uint8 grid for ReLU6 versus an unbounded ReLUشبكة uint8 لـ ReLU6 مقابل ReLU غير محدودة
Boundedمحدودة
Range [0, 6], 255 steps, step size 6 / 255 = 0.0235. Fixed at design time.المدى [0, 6]، و255 خطوة، وحجم الخطوة 6 / 255 = 0.0235. ثابت منذ وقت التصميم.Unbounded, calibrated on dataغير محدودة، ومعايَرة على البيانات
Observed range [0, 60], step size 60 / 255 = 0.235. One outlier channel sets the grid for everyone.المدى الملاحَظ [0, 60]، وحجم الخطوة 60 / 255 = 0.235. قناة شاذة واحدة تحدد الشبكة للجميع.Resultالنتيجة
Same 8 bits, ten times the rounding error per value. A bounded activation removes the calibration risk entirely.نفس الـ 8 بتات، لكن خطأ التقريب لكل قيمة عشرة أضعاف. التفعيل المحدود يزيل خطر المعايرة (calibration) تمامًا.
- cost on an MCUالكلفة على MCU
- needs an exponentialتحتاج دالة أُسّية
- boundedمحدودة
- no, range [-0.278, inf)لا، المدى [-0.278, inf)
Quick checkاختبار سريع
Which activation removes the exponential so a quantized microcontroller can run it cheaply?أي دالة تفعيل تُلغي الدالة الأُسّية بحيث يستطيع متحكم دقيق مكمَّم تشغيلها بكلفة رخيصة؟
Recall
Which activation would you pick for an int8 MCU model and why, and what would you use instead of swish?أي دالة تفعيل ستختار لنموذج int8 على MCU ولماذا، وماذا ستستخدم بدل swish؟
Feed a four-word sentence into the model of Vaswani and colleagues. Each word becomes an embedding of d_model = 512 numbers, a positional encoding of the same size is added to it, and the four vectors enter the first encoder layer. That layer does exactly two things: a Multi-head attention step that lets each word read the others, then a position-wise feed-forward network that processes each word on its own. Around each of the two sits an Add and Norm. Repeat the layer N = 6 times and you have the encoder of the original Transformer.
أدخل جملة من أربع كلمات إلى نموذج Vaswani وزملائه. تصبح كل كلمة تضمينًا (embedding) من d_model = 512 رقمًا، ويُضاف إليه positional encoding (ترميز موضعي) بالحجم نفسه، ثم تدخل المتجهات الأربعة إلى طبقة المُرمِّز (encoder) الأولى. هذه الطبقة تفعل شيئين فقط: خطوة multi-head attention تسمح لكل كلمة بقراءة الكلمات الأخرى، ثم position-wise feed-forward network (شبكة أمامية تعمل على كل موضع) تعالج كل كلمة وحدها. وحول كل واحدة من الاثنتين توجد خطوة Add and Norm. كرّر الطبقة N = 6 مرات وستحصل على مُرمِّز الـ transformer الأصلي.
Token vector plus a sinusoidal position vector.
متجه الرمز مضافًا إليه متجه موضع جيبي.
Every token reads every other token.
كل رمز يقرأ كل رمز آخر.
LayerNorm(x + Sublayer(x)).
Same two-layer MLP on each position.
شبكة MLP نفسها ذات الطبقتين على كل موضع.
Output feeds the next identical layer.
يغذّي الإخراجُ الطبقةَ المطابقة التالية.
The Add and Norm box is the link back to the normalization part. Each sub-layer's output is added to its own input (a residual connection, the ResNet idea) and the sum goes through Layer normalization: LayerNorm(x + Sublayer(x)). LayerNorm rather than BatchNorm because it normalizes each token's 512 features on their own, so it behaves identically at training and inference and needs no batch statistics, which is what you want for variable-length sequences. Jurafsky and Martin describe the whole stack as a residual stream that each component reads from and adds back to.
صندوق Add and Norm هو الرابط مع جزء التطبيع (normalization). مخرج كل طبقة فرعية يُضاف إلى مدخلها نفسه (residual connection، أي الاتصال المتبقي، وهي فكرة ResNet) ثم يمر المجموع عبر layer normalization: LayerNorm(x + Sublayer(x)). نستخدم LayerNorm بدل BatchNorm لأنها تطبّع ميزات كل token البالغة 512 وحدها، فتتصرف بالطريقة نفسها في التدريب والاستدلال ولا تحتاج إحصاءات الدفعة (batch statistics)، وهذا ما تريده للتسلسلات متغيرة الطول. ويصف Jurafsky وMartin المكدّس كله بأنه تيار متبقٍّ (residual stream) يقرأ منه كل مكوّن ثم يضيف إليه.
Base model hyperparameters (Vaswani et al. 2017)المعاملات الفائقة للنموذج الأساسي (Vaswani وزملاؤه 2017)
- Layers per stack Nعدد الطبقات في كل مكدّس N
- 66
- Model width d_modelعرض النموذج d_model
- 512512
- Heads hعدد الرؤوس h
- 88
- Head size d_k = d_vحجم الرأس d_k = d_v
- 512 / 8 = 64512 / 8 = 64
- FFN hidden width d_ffالعرض المخفي للـ FFN d_ff
- 20482048
Inside the two sub-layersداخل الطبقتين الفرعيتين
Multi-head attention runs h = 8 copies of Scaled dot-product attention in parallel. Each head first projects the queries, keys and values with its own small linear layers down to d_k = 64, computes attention, and the eight 64-wide results are concatenated back to 512 and passed through one more linear layer. Because each head works at one eighth of the width, the total cost is about that of a single head at full width, and each head is free to attend to a different relationship.
يشغّل multi-head attention عدد h = 8 نسخ من scaled dot-product attention على التوازي. كل رأس (head) يُسقط أولًا الاستعلامات والمفاتيح والقيم بطبقاته الخطية الصغيرة الخاصة إلى d_k = 64، ثم يحسب الانتباه، ثم تُضمّ (concatenate) النتائج الثماني ذات العرض 64 لتعود إلى 512 وتمر عبر طبقة خطية إضافية واحدة. ولأن كل رأس يعمل بثُمن العرض، فالكلفة الكلية تقارب كلفة رأس واحد بالعرض الكامل، وكل رأس حرّ في الانتباه إلى علاقة مختلفة.
The feed-forward sub-layer is a two-layer MLP with a ReLU between, widening from 512 to 2048 and back. It is applied to each position separately and identically, which the paper notes is the same as two convolutions with kernel size 1. This is where the block's non-linearity lives: once the attention weights are fixed, attention itself is a linear mix of the values.
الطبقة الفرعية الأمامية هي MLP من طبقتين بينهما ReLU، تتسع من 512 إلى 2048 ثم تعود. تُطبَّق على كل موضع بشكل منفصل ومتطابق، وتلاحظ الورقة أن هذا يعادل التفافين (convolutions) بحجم نواة 1. هنا يسكن اللاخطّية في الكتلة: فبمجرد تثبيت أوزان الانتباه، يكون الانتباه نفسه مزيجًا خطيًا من القيم.
Position, the decoder, and the outputالموضع، والمُفكِّك (decoder)، والمخرج
Attention has no notion of order: swap two tokens and the same weights come out swapped. So before the first layer, a positional encoding is added to each embedding. The paper uses sinusoids of different wavelengths, PE(pos, 2i) = sin(pos / 10000^(2i / d_model)) and cosine for odd indices, so that every position gets a distinct 512-vector and relative offsets are easy to express.
الانتباه لا يعرف مفهوم الترتيب: بدّل موضعي token وستخرج الأوزان نفسها مبدّلة. لذلك يُضاف ترميز موضعي إلى كل تضمين قبل الطبقة الأولى. تستخدم الورقة موجات جيبية بأطوال موجية مختلفة، PE(pos, 2i) = sin(pos / 10000^(2i / d_model)) وجيب التمام (cosine) للمؤشرات الفردية، بحيث يحصل كل موضع على متجه مميّز من 512 بعدًا ويسهل التعبير عن الإزاحات النسبية.
The decoder stack (right side of the slide) repeats the same layer with two changes. Its self-attention is masked so that position i cannot see positions after i, which together with the outputs being shifted right by one keeps training honest about what is known when. And a third sub-layer, encoder-decoder attention, takes queries from the decoder and keys and values from the encoder output. A final linear layer and softmax turn the last vector into next-token probabilities.
مكدّس المُفكِّك (decoder، الجانب الأيمن من الشريحة) يكرر الطبقة نفسها مع تغييرين. الأول أن self-attention فيه مُقنَّع (masked) بحيث لا يرى الموضع i المواضع التي بعد i، وهذا مع إزاحة المخرجات إلى اليمين بمقدار واحد يُبقي التدريب صادقًا بشأن ما هو معروف في كل لحظة. والثاني طبقة فرعية ثالثة، هي انتباه المُرمِّز والمُفكِّك (encoder-decoder attention)، تأخذ الاستعلامات من المُفكِّك والمفاتيح والقيم من مخرج المُرمِّز. وفي النهاية تحوّل طبقة خطية ثم softmax المتجه الأخير إلى احتمالات الـ token التالي.
Recall
What does the position-wise FFN do that attention does not, and vice versa?ما الذي تفعله الـ FFN التي تعمل على كل موضع ولا يفعله الانتباه، والعكس؟
Type a phrase into YouTube's search bar. The phrase is your query. Every video carries a title and description, which is its key. The video itself is the value. Search scores your query against every key, ranks the matches, and hands you back videos. Attention does the same three things with vectors, with one twist: instead of returning the single best video it returns a blend of all the values, weighted by how well each key matched. That is the Query, key, value design of the slide.
اكتب عبارة في شريط البحث في YouTube. العبارة هي استعلامك (query). كل فيديو يحمل عنوانًا ووصفًا، وهذا هو مفتاحه (key). والفيديو نفسه هو القيمة (value). يقارن البحث استعلامك بكل مفتاح، ويرتّب المطابقات، ويعيد لك الفيديوهات. والانتباه يفعل الأشياء الثلاثة نفسها بالمتجهات، مع اختلاف واحد: بدل أن يعيد أفضل فيديو واحد، يعيد مزيجًا من كل القيم، موزونًا بمدى تطابق كل مفتاح. هذا هو تصميم query-key-value في الشريحة.
In self-attention all three come from the same input matrix X of N tokens, each through its own learned projection: Q = X W_Q, K = X W_K, V = X W_V. Jurafsky and Martin give the roles precisely: the query is the current element being compared, a key is a preceding input being compared to it to produce a similarity weight, and a value is what gets weighted and summed. The comparison is a dot product, so the whole score table is one matrix multiply, and the paper names the result Scaled dot-product attention.
في self-attention تأتي الثلاثة كلها من مصفوفة المدخل نفسها X المكوّنة من N token، كل منها عبر إسقاط متعلَّم خاص به: Q = X W_Q، K = X W_K، V = X W_V. ويحدد Jurafsky وMartin الأدوار بدقة: الاستعلام هو العنصر الحالي الذي تجري مقارنته، والمفتاح مدخل سابق يُقارَن به لإنتاج وزن تشابه، والقيمة هي ما يُوزَن ويُجمع. المقارنة حاصل ضرب نقطي (dot product)، فجدول الدرجات كله عملية ضرب مصفوفات واحدة، وتسمّي الورقة الناتج scaled dot-product attention.
Shapes for N tokens with head size d_k and value size d_vالأشكال لعدد N من الـ tokens بحجم رأس d_k وحجم قيمة d_v
- Q, K
- N x d_kN x d_k
- V
- N x d_vN x d_v
- Q K^T (scores) and softmax outputQ K^T (الدرجات) ومخرج softmax
- N x NN x N
- Outputالمخرج
- N x d_vN x d_v
Read the slide's diagram bottom to top with those shapes: Q and K of shape N x d multiply into an N x N table of scores, the optional mask sets forbidden entries to minus infinity, softmax turns each row into a probability distribution, and multiplying by V (N x d) gives one output row per token. The masked version is what the decoder uses so that a token cannot attend to what comes after it.
اقرأ مخطط الشريحة من الأسفل إلى الأعلى بهذه الأشكال: Q وK بشكل N x d يُضربان لينتجا جدول درجات بشكل N x N، ويضع القناع الاختياري (mask) الخانات الممنوعة عند سالب ما لا نهاية، وتحوّل softmax كل صف إلى توزيع احتمالي، والضرب في V (N x d) يعطي صف مخرج واحدًا لكل token. والنسخة المقنّعة هي ما يستخدمه المُفكِّك حتى لا يستطيع token أن ينتبه إلى ما يأتي بعده.
Why divide by the square root of d_kلماذا نقسم على الجذر التربيعي لـ d_k
If the components of a query and a key have mean zero and variance one, their dot product has variance d_k: it is a sum of d_k products. So raw scores grow with the head size, and Vaswani and colleagues observe that large dot products push the softmax into regions where it has extremely small gradients. Goodfellow and colleagues say the same about softmax in general: its outputs saturate when the differences between inputs become extreme. Dividing by sqrt(d_k) brings the variance back to one, whatever the head size.
إذا كان متوسط مكونات الاستعلام والمفتاح صفرًا وتباينها واحدًا، فإن تباين حاصل ضربهما النقطي يساوي d_k: لأنه مجموع d_k حاصل ضرب. إذن تكبر الدرجات الخام مع حجم الرأس، ويلاحظ Vaswani وزملاؤه أن حواصل الضرب النقطي الكبيرة تدفع softmax إلى مناطق تكون فيها تدرّجاتها صغيرة جدًا. ويقول Goodfellow وزملاؤه الشيء نفسه عن softmax عمومًا: تتشبّع مخرجاتها عندما تصبح الفروق بين المدخلات متطرفة. والقسمة على sqrt(d_k) تعيد التباين إلى واحد، مهما كان حجم الرأس.
| d_kd_k | Unscaled scoresالدرجات دون قسمة | softmax unscaledsoftmax دون قسمة | softmax after / sqrt(d_k)softmax بعد القسمة على sqrt(d_k) |
|---|---|---|---|
| 44 | (2, 0, -2)(2, 0, -2) | (0.867, 0.117, 0.016)(0.867, 0.117, 0.016) | (0.665, 0.245, 0.090)(0.665, 0.245, 0.090) |
| 6464 | (8, 0, -8)(8, 0, -8) | (1.000, 0.000, 0.000)(1.000, 0.000, 0.000) | (0.665, 0.245, 0.090)(0.665, 0.245, 0.090) |
| 512512 | (22.6, 0, -22.6)(22.6, 0, -22.6) | (1.000, 0.000, 0.000)(1.000, 0.000, 0.000) | (0.665, 0.245, 0.090)(0.665, 0.245, 0.090) |
Without the division, a head of size 64 already turns a modest spread into a one-hot answer of (1.000, 0.000, 0.000), and the gradient through those zeros is essentially zero. With it, every head size sees the same healthy distribution. Softmax is also shift-invariant, softmax(z) = softmax(z + c), so only differences between scores matter, which is what implementations exploit for numerical stability.
بدون القسمة، رأس بحجم 64 يحوّل انتشارًا متواضعًا إلى إجابة أحادية (one-hot) هي (1.000, 0.000, 0.000)، والتدرّج عبر تلك الأصفار يكاد يكون صفرًا. ومع القسمة، يرى كل حجم رأس التوزيع الصحي نفسه. كذلك softmax لا تتأثر بالإزاحة، softmax(z) = softmax(z + c)، فالفروق بين الدرجات وحدها هي المهمة، وهذا ما تستغله التطبيقات البرمجية لتحقيق الاستقرار العددي.
The N x N matrix is the embedded constraintمصفوفة N x N هي القيد في الأنظمة المضمّنة
Every query is scored against every key, so one head at one layer produces N^2 scores, whatever the feature size. Jurafsky and Martin state it plainly: attention is quadratic in the length of the input. Vaswani's complexity table writes the per-layer cost as O(n^2 d). Double the sequence and you quadruple the score matrix; multiply by heads and layers and it is the dominant activation memory of the model.
كل استعلام يُقارَن بكل مفتاح، فرأس واحد في طبقة واحدة ينتج N^2 درجة، مهما كان حجم الميزات. ويقولها Jurafsky وMartin بوضوح: الانتباه تربيعي في طول المدخل. وجدول التعقيد عند Vaswani يكتب كلفة الطبقة على أنها O(n^2 d). ضاعف التسلسل وستضاعف مصفوفة الدرجات أربع مرات؛ واضربها في عدد الرؤوس والطبقات وستجدها أكبر ذاكرة تفعيلات في النموذج.
| Tokens Nعدد الـ tokens N | Entries N^2عدد الخانات N^2 | Memoryالذاكرة |
|---|---|---|
| 1616 | 256256 | 1 KB1 KB |
| 6464 | 4,0964,096 | 16 KB16 KB |
| 256256 | 65,53665,536 | 256 KB256 KB |
| 1,0241,024 | 1,048,5761,048,576 | 4 MB4 MB |
| 4,0964,096 | 16,777,21616,777,216 | 64 MB64 MB |
Quick checkاختبار سريع
What is the shape of the softmax output in single-head attention over N tokens with head size d_k?ما شكل مخرج softmax في انتباه برأس واحد على N من الـ tokens بحجم رأس d_k؟
Quick checkاختبار سريع
Why does Vaswani et al. divide Q K^T by the square root of d_k?لماذا يقسم Vaswani وزملاؤه Q K^T على الجذر التربيعي لـ d_k؟
Recall
Write the attention formula and give the shape of every matrix for N tokens and head size d_k.اكتب صيغة الانتباه وأعطِ شكل كل مصفوفة لعدد N من الـ tokens وحجم رأس d_k.
Recall
Where does the N^2 come from and why does it matter on embedded hardware?من أين يأتي N^2 ولماذا يهم على العتاد المضمّن؟
Tom Yeh's drawing makes Scaled dot-product attention concrete with numbers you can check at a desk. Four tokens, each with six features, are the columns x1 = (2, 0, 0, 0, 2, 1), x2 = (0, 1, 2, 0, 0, 0), x3 = (0, 0, 1, 1, 0, 1) and x4 = (2, 0, 0, 1, 0, 1). Three 3 x 6 matrices W_Q, W_K and W_V project them to the queries, keys and values of head size d_k = 3. Those 54 numbers are the only learned parameters; everything else is computed from the input.
رسم Tom Yeh يجعل scaled dot-product attention ملموسًا بأرقام يمكنك التحقق منها على مكتبك. أربعة tokens، لكل منها ست ميزات، هي الأعمدة x1 = (2, 0, 0, 0, 2, 1) وx2 = (0, 1, 2, 0, 0, 0) وx3 = (0, 0, 1, 1, 0, 1) وx4 = (2, 0, 0, 1, 0, 1). ثلاث مصفوفات بشكل 3 x 6 هي W_Q وW_K وW_V تُسقطها إلى queries, keys and values (الاستعلامات والمفاتيح والقيم) بحجم رأس d_k = 3. هذه الأرقام الـ 54 هي المعاملات المتعلَّمة الوحيدة؛ وكل ما عداها يُحسب من المدخل.
Two shortcuts keep the arithmetic in your head. Dividing by sqrt(3) = 1.732 is replaced by dividing by 2 and dropping the fraction (rounding toward zero, so -1 / 2 becomes 0), and e^x is replaced by 3^x, which has the same shape and integer values for non-negative exponents. The shortcuts preserve the ranking for three of the four queries; the comparison table below shows the one query where halving squashes the gap away.
اختصاران يُبقيان الحساب في رأسك. القسمة على sqrt(3) = 1.732 تُستبدل بالقسمة على 2 وحذف الكسر (التقريب نحو الصفر، فتصبح -1 / 2 صفرًا 0)، وe^x تُستبدل بـ 3^x، التي لها الشكل نفسه وقيم صحيحة للأسس غير السالبة. يحافظ الاختصاران على الترتيب في ثلاثة من الاستعلامات الأربعة؛ وجدول المقارنة أدناه يُظهر الاستعلام الوحيد الذي يسحق فيه التنصيف الفرق.
Worked exampleمثال محلول
Column 1: what token 1 attends toالعمود 1: إلى ماذا ينتبه الـ token رقم 1
Projectالإسقاط
q1 = W_Q x1 = (2, 0, 3). The keys are k1 = (0, 0, 1), k2 = (2, 1, 0), k3 = (1, 0, -1), k4 = (0, 0, 1), and the values v1 = (20, 0, 0), v2 = (0, 0, 10), v3 = (0, 10, 0), v4 = (20, 10, 0).q1 = W_Q x1 = (2, 0, 3). المفاتيح هي k1 = (0, 0, 1)، k2 = (2, 1, 0)، k3 = (1, 0, -1)، k4 = (0, 0, 1)، والقيم v1 = (20, 0, 0)، v2 = (0, 0, 10)، v3 = (0, 10, 0)، v4 = (20, 10, 0).Scoreالدرجات
Dot products of q1 with each key: 3, 4, -1, 3. Key 2 is the best match.حواصل الضرب النقطي لـ q1 مع كل مفتاح: 3, 4, -1, 3. المفتاح 2 هو أفضل تطابق.Scaleالقسمة (التحجيم)
Divide by 2 and truncate: 1, 2, 0, 1. (Exact: divide by 1.732, giving 1.73, 2.31, -0.58, 1.73.)اقسم على 2 واحذف الكسر: 1, 2, 0, 1. (القيمة الدقيقة: القسمة على 1.732 تعطي 1.73, 2.31, -0.58, 1.73.)Softmax with powers of threeSoftmax بقوى العدد ثلاثة
3^1, 3^2, 3^0, 3^1 = 3, 9, 1, 3, sum 16, weights 0.19, 0.56, 0.06, 0.19, drawn as .2, .6, 0, .2.3^1, 3^2, 3^0, 3^1 = 3, 9, 1, 3، والمجموع 16، والأوزان 0.19, 0.56, 0.06, 0.19، وتُرسم .2, .6, 0, .2.Weighted sum of valuesالمجموع الموزون للقيم
z1 = 0.2 v1 + 0.6 v2 + 0 v3 + 0.2 v4 = (4, 0, 0) + (0, 0, 6) + (4, 2, 0).z1 = 0.2 v1 + 0.6 v2 + 0 v3 + 0.2 v4 = (4, 0, 0) + (0, 0, 6) + (4, 2, 0).z1 = (8, 2, 6)z1 = (8, 2, 6)
Token 1's new representation is mostly value 2 (weight 0.6), with a fifth each of values 1 and 4. The exact computation gives (10.3, 2.8, 4.6) with the same ranking.التمثيل الجديد للـ token رقم 1 هو في معظمه القيمة 2 (بوزن 0.6)، مع خُمس من كل من القيمتين 1 و4. والحساب الدقيق يعطي (10.3, 2.8, 4.6) بالترتيب نفسه.
| d1 | d2 | d3 | |
|---|---|---|---|
| q1 | |||
| q2 | |||
| q3 | |||
| q4 |
| d1 | d2 | d3 | |
|---|---|---|---|
| k1 | |||
| k2 | |||
| k3 | |||
| k4 |
| d1 | d2 | d3 | |
|---|---|---|---|
| v1 | |||
| v2 | |||
| v3 | |||
| v4 |
| k1 | k2 | k3 | k4 | |
|---|---|---|---|---|
| q1 | 0.20 | 0.60 | 0.10 | 0.20 |
| q2 | 0.30 | 0.30 | 0.10 | 0.30 |
| q3 | 0.40 | 0.10 | 0.00 | 0.40 |
| q4 | 0.10 | 0.80 | 0.10 | 0.10 |
| q . k | trunc(s / 2) | 3^s | weight | |
|---|---|---|---|---|
| k1 | 3 | 1 | 3.00 | 0.200 |
| k2 | 4 | 2 | 9.00 | 0.600 |
| k3 | -1 | 0 | 1.00 | 0.100 |
| k4 | 3 | 1 | 3.00 | 0.200 |
Sum of the exponentials 16.00. Each weight is its power divided by this sum, rounded to one decimal as on the slide.مجموع الأُسّيات 16.00. كل وزن هو قوّته مقسومة على هذا المجموع، مقرّبة إلى منزلة عشرية واحدة كما في الشريحة.
0.20 (20, 0, 0) + 0.60 (0, 0, 10) + 0.10 (0, 10, 0) + 0.20 (20, 10, 0)
(8.0, 3.0, 6.0)
Head size d_k = 3, N = 4 tokens, so the score matrix has 16 entries whatever the feature size. The highlighted row is the selected query; the teal entries are the keys that win (all of them when tied). Switch between the two arithmetics: the weights move and the winner rarely does. Try q2 to see the one default row where halving erases the gap.حجم الرأس d_k = 3، وعدد الرموز N = 4، لذلك تحتوي مصفوفة الدرجات على 16 مدخلًا مهما كان حجم الخصائص. الصف المظلل هو الاستعلام المختار، والمدخلات الفيروزية هي المفاتيح الفائزة (كلها عند التعادل). بدّل بين طريقتي الحساب: تتغير الأوزان ونادرًا ما يتغير الفائز. جرّب q2 لترى الصف الافتراضي الوحيد الذي تمحو فيه القسمة على اثنين الفارق.
| Queryالاستعلام | Scores q . kالدرجات q . k | Slide weightsأوزان الشريحة | Exact weightsالأوزان الدقيقة | Slide zz في الشريحة | Exact zz الدقيقة |
|---|---|---|---|---|---|
| q1 = (2, 0, 3)q1 = (2, 0, 3) | 3, 4, -1, 33, 4, -1, 3 | .2, .6, 0, .2.2, .6, 0, .2 | .258, .459, .026, .258.258, .459, .026, .258 | (8, 2, 6)(8, 2, 6) | (10.3, 2.8, 4.6)(10.3, 2.8, 4.6) |
| q2 = (1, 1, 2)q2 = (1, 1, 2) | 2, 3, -1, 22, 3, -1, 2 | .3, .3, .1, .3.3, .3, .1, .3 | .253, .450, .045, .253.253, .450, .045, .253 | (12, 4, 3)(12, 4, 3) | (10.1, 3.0, 4.5)(10.1, 3.0, 4.5) |
| q3 = (0, 1, 2)q3 = (0, 1, 2) | 2, 1, -2, 22, 1, -2, 2 | .4, .2, 0, .4.4, .2, 0, .4 | .376, .211, .037, .376.376, .211, .037, .376 | (16, 4, 2)(16, 4, 2) | (15.0, 4.1, 2.1)(15.0, 4.1, 2.1) |
| q4 = (2, 1, 1)q4 = (2, 1, 1) | 1, 5, 1, 11, 5, 1, 1 | .1, .7, .1, .1.1, .7, .1, .1 | .077, .770, .077, .077.077, .770, .077, .077 | (4, 2, 7)(4, 2, 7) | (3.1, 1.5, 7.7)(3.1, 1.5, 7.7) |
For q1, q3 and q4 the same key wins under both arithmetics, and the second-best key is the same too: softmax cares about differences between scores, and a monotone squashing of those differences preserves the order as long as the differences survive. The numbers drift (a weight of 0.6 becomes 0.46), the decision does not. The corrected q2 is the exception that shows how coarse the shortcut is: its scores 2, 3, -1, 2 halve to 1, 1, 0, 1, so the by-hand softmax hands keys 1, 2 and 4 the same 0.3 while the exact softmax still separates key 2 at 0.45. Integer halving erased a difference of one, which is exactly the kind of information a real quantizer must be careful not to lose.
بالنسبة لـ q1 وq3 وq4 يفوز المفتاح نفسه في الحسابين، والمفتاح الثاني هو نفسه أيضًا: softmax تهتم بالفروق بين الدرجات، وأي ضغط رتيب (monotone) لتلك الفروق يحافظ على الترتيب ما دامت الفروق باقية. الأرقام تنحرف (وزن 0.6 يصبح 0.46)، لكن القرار لا يتغير. والـ q2 المصحّحة هي الاستثناء الذي يُظهر مدى خشونة الاختصار: درجاتها 2, 3, -1, 2 تتنصّف إلى 1, 1, 0, 1، فتعطي softmax اليدوية المفاتيح 1 و2 و4 الوزن نفسه 0.3، بينما ما زالت softmax الدقيقة تميّز المفتاح 2 عند 0.45. التنصيف الصحيح محا فرقًا مقداره واحد، وهذا بالضبط نوع المعلومات الذي يجب أن يحرص المكمِّم الحقيقي على عدم فقدانه.
Quick checkاختبار سريع
On slide 43 with query q1 = (2, 0, 3), which key receives the largest attention weight?في الشريحة 43 مع الاستعلام q1 = (2, 0, 3)، أي مفتاح يحصل على أكبر وزن انتباه؟
Recall
With q = (2, 0, 3) and keys (0, 0, 1), (2, 1, 0), (1, 0, -1), (0, 0, 1), which key wins and what are the raw scores?مع q = (2, 0, 3) والمفاتيح (0, 0, 1) و(2, 1, 0) و(1, 0, -1) و(0, 0, 1)، أي مفتاح يفوز وما الدرجات الخام؟
The second by-hand slide picks up where the first stopped. Five tokens with three features arrive from the previous block, x1 = (5, 0, 1), x2 = (6, 2, 0), x3 = (0, 4, 1), x4 = (7, 0, 1), x5 = (0, 3, 0). The attention weight matrix A is given rather than computed, and it is deliberately simple: each column has exactly two ones, so each token attends equally to itself and to one other token (the next neighbour for tokens 1 to 4, and token 1 for token 5). The attention-weighted feature of token 1 is then a plain sum, z1 = x1 + x2 = (11, 2, 1), and likewise z2 = x2 + x3, z3 = x3 + x4, z4 = x4 + x5 and z5 = x1 + x5.
الشريحة اليدوية الثانية تكمل من حيث توقفت الأولى. خمسة tokens بثلاث ميزات تصل من الكتلة السابقة: x1 = (5, 0, 1)، x2 = (6, 2, 0)، x3 = (0, 4, 1)، x4 = (7, 0, 1)، x5 = (0, 3, 0). مصفوفة أوزان الانتباه A معطاة وليست محسوبة، وهي بسيطة عمدًا: في كل عمود واحدان بالضبط، فكل token ينتبه بالتساوي إلى نفسه وإلى token واحد آخر (الجار التالي للـ tokens من 1 إلى 4، والـ token رقم 1 للـ token رقم 5). عندها تكون الميزة الموزونة بالانتباه للـ token رقم 1 مجموعًا بسيطًا، z1 = x1 + x2 = (11, 2, 1)، وبالمثل z2 = x2 + x3، وz3 = x3 + x4، وz4 = x4 + x5، وz5 = x1 + x5.
Now the position-wise feed-forward network takes each z_j on its own. The first Fully connected layer has weights W1 of shape 4 x 3 with rows (1, -1, 0), (1, 1, 0), (0, 1, 1), (-1, 1, 1) and biases (1, 0, 1, 0). A ReLU follows. The second layer has W2 of shape 3 x 4 with rows (1, 0, 0, -1), (0, 1, 1, 0), (0, 0, 1, -1) and biases (0, 0, 1). Widen from three to four, bend, narrow back to three: the same expand-and-contract shape as the paper's 512 to 2048 to 512.
الآن تأخذ position-wise feed-forward network كل z_j وحده. الطبقة الأولى fully connected layer (متصلة بالكامل) أوزانها W1 بشكل 4 x 3 وصفوفها (1, -1, 0)، (1, 1, 0)، (0, 1, 1)، (-1, 1, 1) وانحيازاتها (1, 0, 1, 0). تليها ReLU. والطبقة الثانية لها W2 بشكل 3 x 4 وصفوفها (1, 0, 0, -1)، (0, 1, 1, 0)، (0, 0, 1, -1) وانحيازاتها (0, 0, 1). توسيع من ثلاثة إلى أربعة، ثم انحناء، ثم تضييق إلى ثلاثة: شكل التوسيع والتضييق نفسه في الورقة من 512 إلى 2048 إلى 512.
Worked exampleمثال محلول
Token 1 through the FFNالـ token رقم 1 عبر الـ FFN
First layerالطبقة الأولى
W1 z1 + b1 with z1 = (11, 2, 1): row 1 gives 11 - 2 + 0 + 1 = 10, row 2 11 + 2 + 0 = 13, row 3 0 + 2 + 1 + 1 = 4, row 4 -11 + 2 + 1 + 0 = -8. Hidden vector (10, 13, 4, -8).W1 z1 + b1 مع z1 = (11, 2, 1): الصف 1 يعطي 11 - 2 + 0 + 1 = 10، والصف 2 11 + 2 + 0 = 13، والصف 3 0 + 2 + 1 + 1 = 4، والصف 4 -11 + 2 + 1 + 0 = -8. المتجه المخفي (10, 13, 4, -8).ReLUReLU
max(0, h) = (10, 13, 4, 0). The fourth unit is off for this token.max(0, h) = (10, 13, 4, 0). الوحدة الرابعة مطفأة لهذا الـ token.Second layerالطبقة الثانية
W2 h + b2: row 1 10 - 0 + 0 = 10, row 2 13 + 4 + 0 = 17, row 3 4 - 0 + 1 = 5.W2 h + b2: الصف 1 10 - 0 + 0 = 10، والصف 2 13 + 4 + 0 = 17، والصف 3 4 - 0 + 1 = 5.out1 = (10, 17, 5)out1 = (10, 17, 5)
This vector goes to the next block. Run the same three steps with the same W1, b1, W2, b2 on the other four tokens and you get the slide's five outputs.هذا المتجه يذهب إلى الكتلة التالية. نفّذ الخطوات الثلاث نفسها بنفس W1, b1, W2, b2 على الـ tokens الأربعة الأخرى وستحصل على المخرجات الخمسة في الشريحة.
| Tokenالـ token | zz | W1 z + b1W1 z + b1 | after ReLUبعد ReLU | W2 h + b2W2 h + b2 |
|---|---|---|---|---|
| z1 = x1 + x2z1 = x1 + x2 | (11, 2, 1)(11, 2, 1) | (10, 13, 4, -8)(10, 13, 4, -8) | (10, 13, 4, 0)(10, 13, 4, 0) | (10, 17, 5)(10, 17, 5) |
| z2 = x2 + x3z2 = x2 + x3 | (6, 6, 1)(6, 6, 1) | (1, 12, 8, 1)(1, 12, 8, 1) | (1, 12, 8, 1)(1, 12, 8, 1) | (0, 20, 8)(0, 20, 8) |
| z3 = x3 + x4z3 = x3 + x4 | (7, 4, 2)(7, 4, 2) | (4, 11, 7, -1)(4, 11, 7, -1) | (4, 11, 7, 0)(4, 11, 7, 0) | (4, 18, 8)(4, 18, 8) |
| z4 = x4 + x5z4 = x4 + x5 | (7, 3, 1)(7, 3, 1) | (5, 10, 5, -3)(5, 10, 5, -3) | (5, 10, 5, 0)(5, 10, 5, 0) | (5, 15, 6)(5, 15, 6) |
| z5 = x1 + x5z5 = x1 + x5 | (5, 3, 1)(5, 3, 1) | (3, 8, 5, -1)(3, 8, 5, -1) | (3, 8, 5, 0)(3, 8, 5, 0) | (3, 13, 6)(3, 13, 6) |
Where the parameters sitأين توجد المعاملات
The FFN is small on the slide and large in the real model. Derive it from the base hyperparameters: the four attention projections W_Q, W_K, W_V, W_O are each 512 x 512, about 1.05 M weights per layer. The FFN's two matrices are 512 x 2048 and 2048 x 512, about 2.1 M. Two thirds of a layer's weights live in the FFN, so pruning and quantization work in later lectures will spend most of its effort there, while the N^2 activation cost of the previous concept lives in attention.
الـ FFN صغيرة في الشريحة وكبيرة في النموذج الحقيقي. استنتج ذلك من معاملات النموذج الأساسي: إسقاطات الانتباه الأربعة W_Q, W_K, W_V, W_O كل منها 512 x 512، أي حوالي 1.05 M وزن لكل طبقة. ومصفوفتا الـ FFN هما 512 x 2048 و2048 x 512، أي حوالي 2.1 M. ثلثا أوزان الطبقة تعيش في الـ FFN، لذلك سيصرف عمل التقليم (pruning) والتكميم في المحاضرات اللاحقة معظم جهده هناك، بينما تعيش كلفة التفعيلات N^2 من المفهوم السابق في الانتباه.
Weights in one base encoder layer, derived from d_model = 512 and d_ff = 2048 (biases omitted)الأوزان في طبقة مُرمِّز أساسية واحدة، مشتقة من d_model = 512 وd_ff = 2048 (دون الانحيازات)
- Attention projections (4 x 512 x 512)إسقاطات الانتباه (4 x 512 x 512)
- 1,048,5761,048,576
- FFN (2 x 512 x 2048)الـ FFN (2 x 512 x 2048)
- 2,097,1522,097,152
- FFN share of the layerحصة الـ FFN من الطبقة
- about 67%حوالي 67%
Quick checkاختبار سريع
In the position-wise feed-forward network, which statement is true?في الشبكة الأمامية التي تعمل على كل موضع (position-wise FFN)، أي عبارة صحيحة؟
Recall
Slide 44: compute the FFN output for token 4, whose attention-weighted feature is z4 = (7, 3, 1).الشريحة 44: احسب مخرج الـ FFN للـ token رقم 4، الذي ميزته الموزونة بالانتباه z4 = (7, 3, 1).
The closing slide lists the ten works the lecturer built this lecture from. Read them as a map of where each part came from: the convolution arithmetic animations and CS231n behind parts 2 to 4, the two normalization papers behind part 5, the four architecture papers whose design choices (ReLU in AlexNet, depth in VGG, residuals in ResNet, ReLU6 in MobileNetV2) run through this part, and the survey and course that frame the whole thing for embedded deployment.
الشريحة الختامية تسرد الأعمال العشرة التي بنى منها المحاضر هذه المحاضرة. اقرأها كخريطة لمصدر كل جزء: رسوم حساب الالتفاف المتحركة وCS231n وراء الأجزاء من 2 إلى 4، وورقتا التطبيع وراء الجزء 5، وأوراق المعماريات الأربع التي تسري خيارات تصميمها في هذا الجزء (ReLU في AlexNet، والعمق في VGG، والاتصالات المتبقية في ResNet، وReLU6 في MobileNetV2)، والمسح (survey) والمقرر اللذان يؤطّران الموضوع كله للنشر على الأنظمة المضمّنة.
Sourcesالمصادر
- Convolution arithmetic (animations)DocsDumoulin and Visin, GitHubPadding and stride animations used in parts 2 and 3رسوم متحركة للحشو (padding) والخطوة (stride) مستخدمة في الجزأين 2 و3(opens in a new tab)(يفتح في تبويب جديد)
- CS231n Lecture 5: Image Classification with CNNsDocsStanford UniversityConvolution, pooling and the CNN components mapالالتفاف (convolution) والتجميع (pooling) وخريطة مكونات الـ CNN(opens in a new tab)(يفتح في تبويب جديد)
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate ShiftPaperIoffe and Szegedy, ICML 2015Part 5الجزء 5(opens in a new tab)(يفتح في تبويب جديد)
- Group NormalizationPaperWu and He, ECCV 2018Part 5, the family of normalization axesالجزء 5، عائلة محاور التطبيع (normalization)(opens in a new tab)(يفتح في تبويب جديد)
- ImageNet Classification with Deep Convolutional Neural Networks (AlexNet)PaperKrizhevsky, Sutskever and Hinton, NeurIPS 2012ReLU as the standard hidden unitReLU بوصفها الوحدة المخفية المعيارية(opens in a new tab)(يفتح في تبويب جديد)
- Very Deep Convolutional Networks for Large-Scale Image Recognition (VGG)PaperSimonyan and Zisserman, ICLR 2015Depth from stacked 3 x 3 convolutionsالعمق عبر تكديس التفافات 3 x 3(opens in a new tab)(يفتح في تبويب جديد)
- Deep Residual Learning for Image Recognition (ResNet)PaperHe, Zhang, Ren and Sun, CVPR 2016The residual connection reused by Add and Normالاتصال المتبقي (residual connection) الذي تعيد Add and Norm استخدامه(opens in a new tab)(يفتح في تبويب جديد)
- MobileNetV2: Inverted Residuals and Linear BottlenecksPaperSandler et al., CVPR 2018ReLU6 for low-precision robustnessReLU6 للمتانة مع الدقة المنخفضة(opens in a new tab)(يفتح في تبويب جديد)
- Model Compression and Hardware Acceleration for Neural Networks: A Comprehensive SurveyPaperDeng, Li, Han, Shi and Xie, Proceedings of the IEEE 108(4), 2020The embedded framing of the whole lectureالإطار المضمّن (embedded) للمحاضرة كلها(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940: TinyML and Efficient Deep Learning Computing (Fall 2024)DocsSong Han, MIT HAN LabLecture 2, Basics of Deep Learning, which this lecture followsالمحاضرة 2، أساسيات التعلم العميق، التي تتبعها هذه المحاضرة(opens in a new tab)(يفتح في تبويب جديد)
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Without a non-linearity, stacked linear layers collapse into one linear layer, so depth buys nothing.بدون دالة غير خطية تنهار الطبقات الخطية المكدّسة إلى طبقة خطية واحدة، فلا يضيف العمق شيئًا.
- Sigmoid saturates on both sides with a derivative of at most 0.25. ReLU, max(0, x), is the default hidden unit.دالة sigmoid تتشبّع من الجانبين ومشتقتها 0.25 على الأكثر. ودالة ReLU، أي max(0, x)، هي الوحدة المخفية الافتراضية.
- ReLU6 = min(max(0, x), 6) is bounded, which hands int8 a fixed grid of 6/255 per step. MobileNetV2 chose it for low-precision robustness.ReLU6 = min(max(0, x), 6) محدودة، فتعطي الـ int8 شبكة ثابتة خطوتها 6/255. اختارتها MobileNetV2 لمتانتها مع الدقة المنخفضة (low precision).
- Hard swish = x ReLU6(x + 3)/6 stays within 0.15 of swish everywhere and needs no exponential.Hard swish = x ReLU6(x + 3)/6 تبقى ضمن 0.15 من swish في كل مكان ولا تحتاج دالة أُسّية.
- A transformer block is multi-head attention, Add and Norm (residual plus LayerNorm), position-wise FFN, Add and Norm. Base model: N = 6, d_model = 512, h = 8, d_k = 64, d_ff = 2048.كتلة الـ transformer هي multi-head attention، ثم Add and Norm (اتصال متبقٍّ residual مع LayerNorm)، ثم FFN تعمل على كل موضع، ثم Add and Norm. النموذج الأساسي: N = 6، d_model = 512، h = 8، d_k = 64، d_ff = 2048.
- Attention(Q, K, V) = softmax(Q K^T / sqrt(d_k)) V. Q and K are N x d_k, the weights are N x N, the output is N x d_v.Attention(Q, K, V) = softmax(Q K^T / sqrt(d_k)) V. المصفوفتان Q وK بشكل N x d_k، والأوزان بشكل N x N، والمخرج بشكل N x d_v.
- Dividing by sqrt(d_k) keeps the dot-product variance at 1 so softmax does not saturate and its gradients stay alive.القسمة على sqrt(d_k) تُبقي تباين حاصل الضرب النقطي عند 1 فلا تتشبّع softmax وتبقى تدرّجاتها حيّة.
- Attention memory and compute grow with N^2. At N = 1024 one fp32 head already holds 4 MB of scores.ذاكرة الانتباه وحسابه ينموان مع N^2. عند N = 1024 يحمل رأس واحد بدقة fp32 وحده 4 MB من الدرجات (scores).
- Slide 43 by hand: q1 = (2, 0, 3) scores 3, 4, -1, 3 against the keys, weights about 0.2, 0.6, 0, 0.2, z1 = (8, 2, 6). The slide's q2 should be (1, 1, 2).الشريحة 43 يدويًا: q1 = (2, 0, 3) تعطي الدرجات 3 و4 و-1 و3 مقابل المفاتيح، والأوزان تقريبًا 0.2 و0.6 و0 و0.2، وz1 = (8, 2, 6). والصحيح أن تكون q2 في الشريحة (1, 1, 2).
- The FFN applies the same linear, ReLU, linear to every token independently. Slide 44's two wrong hidden entries (-9 and -4 instead of -8 and -3) are zeroed by ReLU anyway.الـ FFN تطبّق نفس العمليات (خطية، ثم ReLU، ثم خطية) على كل token بشكل مستقل. والقيمتان المخفيتان الخاطئتان في الشريحة 44 (-9 و-4 بدل -8 و-3) تصفّرهما ReLU على أي حال.
Sourcesالمصادر
- Attention Is All You NeedPaperVaswani et al., NeurIPS 2017Sections 3.1 to 3.5: block, multi-head attention, sqrt(d_k), FFN, positional encoding; Table 1 for O(n^2 d)الأقسام من 3.1 إلى 3.5: الكتلة، وmulti-head attention، وsqrt(d_k)، والـ FFN، والترميز الموضعي (positional encoding)؛ والجدول 1 لـ O(n^2 d)(opens in a new tab)(يفتح في تبويب جديد)
- Speech and Language Processing, 3rd ed. draft, Chapter 7: Transformers and PretrainingBookJurafsky and Martin, StanfordQuery, key and value roles (7.1), quadratic cost, the residual stream and pre-norm (7.2)أدوار الاستعلام والمفتاح والقيمة (7.1)، والكلفة التربيعية، والتيار المتبقي (residual stream) وpre-norm (7.2)(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, Chapter 6: Deep Feedforward NetworksBookGoodfellow, Bengio and Courville, MIT PressXOR motivation (6.1), ReLU as default and leaky ReLU (6.3), sigmoid saturation (6.3.2), softmax saturation (6.2.2.3)دافع مسألة XOR (6.1)، وReLU بوصفها الافتراضية وleaky ReLU (6.3)، وتشبّع sigmoid (6.3.2)، وتشبّع softmax (6.2.2.3)(opens in a new tab)(يفتح في تبويب جديد)
- Searching for MobileNetV3PaperHoward et al., ICCV 2019Section 5.2: h-swish definition and the three reasons; Table 5 latencyالقسم 5.2: تعريف h-swish والأسباب الثلاثة؛ والجدول 5 لزمن الاستجابة (latency)(opens in a new tab)(يفتح في تبويب جديد)
- MobileNetV2: Inverted Residuals and Linear BottlenecksPaperSandler et al., CVPR 2018Section 4: ReLU6 chosen for robustness under low-precision computationالقسم 4: اختيار ReLU6 للمتانة مع الحسابات منخفضة الدقة(opens in a new tab)(يفتح في تبويب جديد)
- Searching for Activation FunctionsPaperRamachandran, Zoph and Le, 2017Swish, x sigmoid(beta x), and its gains over ReLU on deeper modelsSwish، أي x sigmoid(beta x)، ومكاسبها مقارنة بـ ReLU في النماذج الأعمق(opens in a new tab)(يفتح في تبويب جديد)
- Convolutional Deep Belief Networks on CIFAR-10PaperKrizhevsky, 2010Origin of the rectified unit capped at 6 (section 4.1)أصل الوحدة المقوِّمة (rectified unit) ذات السقف عند 6 (القسم 4.1)(opens in a new tab)(يفتح في تبويب جديد)
- Rectifier Nonlinearities Improve Neural Network Acoustic ModelsPaperMaas, Hannun and Ng, ICML 2013The leaky rectifier with slope 0.01المقوِّم المتسرّب (leaky rectifier) بميل 0.01(opens in a new tab)(يفتح في تبويب جديد)
- Gaussian Error Linear Units (GELUs)PaperHendrycks and Gimpel, 2016x Phi(x)x Phi(x)(opens in a new tab)(يفتح في تبويب جديد)
- Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs)PaperClevert, Unterthiner and Hochreiter, ICLR 2016(opens in a new tab)(يفتح في تبويب جديد)
- Mish: A Self Regularized Non-Monotonic Activation FunctionPaperMisra, BMVC 2020(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.HardswishDocsPyTorch 2.14 documentationThe three-case definition; see also ReLU6, LeakyReLU (negative_slope 0.01) and SiLUالتعريف ذو الحالات الثلاث؛ وانظر أيضًا ReLU6 وLeakyReLU (negative_slope 0.01) وSiLU(opens in a new tab)(يفتح في تبويب جديد)
- Self Attention by HandArticleTom Yeh, AI by HandSource of slide 43; the 4 x 6 setup is in the free partمصدر الشريحة 43؛ وإعداد 4 x 6 موجود في الجزء المجاني(opens in a new tab)(يفتح في تبويب جديد)
- Transformer by HandArticleTom Yeh, AI by HandSource of slide 44مصدر الشريحة 44(opens in a new tab)(يفتح في تبويب جديد)
- The Illustrated TransformerArticleJay AlammarLinked from slide 44مرتبط من الشريحة 44(opens in a new tab)(يفتح في تبويب جديد)
- Attention in transformers, step-by-stepVideo3Blue1BrownThe attention pattern has the square of the context size in entriesنمط الانتباه فيه عدد خانات يساوي مربع حجم السياق(opens in a new tab)(يفتح في تبويب جديد)
- Transformer Attention Explained By ExampleVideoYouTubeLinked from slide 42مرتبط من الشريحة 42(opens in a new tab)(يفتح في تبويب جديد)
- How Attention Mechanism Works in Transformer ArchitectureVideoYouTubeLinked from slide 42مرتبط من الشريحة 42(opens in a new tab)(يفتح في تبويب جديد)
- What exactly are keys, queries, and values in attention mechanisms?ArticleCross Validated (Stack Exchange)Content credit named on slide 42 for the search analogyمصدر المحتوى المذكور في الشريحة 42 لتشبيه البحث(opens in a new tab)(يفتح في تبويب جديد)