Lecture 03 · NN Efficiency Metrics
المحاضرة 03 · مقاييس كفاءة الشبكات العصبية
#Parameters: linear, conv, grouped, depthwise
عدد المعاملات (#Parameters): linear وconv وgrouped وdepthwise
P1 MUSTFour formulas, one rule: count the elements of the weight tensor. Batch size and image size never appear.
أربع صيغ وقاعدة واحدة: عُدّ عناصر weight tensor. حجم batch وحجم الصورة لا يظهران أبداً.
The definition
التعريف
Slide 10: #Parameters is the parameter (synapse/weight) count of the given neural network, i.e., the number of elements in the weight tensors. So the method is always the same: write down the shape of the weight tensor and multiply its dimensions. Slides 11 to 14 fill a table one row at a time, and the table header says "bias is ignored".
الشريحة 10: #Parameters هو عدد المعاملات (synapse/weight) في الشبكة، أي عدد العناصر في weight tensors. فالطريقة دائماً واحدة: اكتب شكل weight tensor واضرب أبعاده. الشرائح من 11 إلى 14 تملأ جدولاً صفاً صفاً، ورأس الجدول يقول "bias is ignored".
| Layerالطبقة | Weight tensor shapeشكل موتر الأوزان | #Parameters (bias ignored)#Parameters (مع تجاهل bias) |
|---|---|---|
| Linearخطية (linear) | co × ci | co · ci |
| Convolutionالتفاف (convolution) | co × ci × kh × kw | co · ci · kh · kw |
| Grouped convolutionالتفاف مجمّع (grouped convolution) | g groups of (co/g) × (ci/g) × kh × kwg مجموعات، كل منها (co/g) × (ci/g) × kh × kw | co/g · ci/g · kh · kw · g = co · ci · kh · kw / g |
| Depthwise convolutionالتفاف عمقي (depthwise convolution) | co × 1 × kh × kw (g = ci = co) | co · kh · kw |
هذا هو جدول الشرائح 11 إلى 14 كاملاً. ما يجب ملاحظته: كل صيغة هي حاصل ضرب أبعاد weight tensor فقط. الـ convolution هو linear مضروباً في حجم kernel، والـ grouped هو الـ convolution مقسوماً على g مرة واحدة، والـ depthwise هو حالة خاصة من grouped حيث g = ci = co، فيختفي ci من الصيغة.
Notation from the slides: n batch size, ci / co input / output channels, hi, ho, wi, wo input / output height and width, kh, kw kernel height and width, g groups. Of these, n, h and w never appear in a parameter count: the same kernel slides over every position and every image in the batch.
رموز الشرائح: n حجم batch، وci / co قنوات المدخل والمخرج، وhi, ho, wi, wo ارتفاع وعرض المدخل والمخرج، وkh, kw ارتفاع وعرض kernel، وg عدد المجموعات. من بين هذه، n وh وw لا تظهر أبداً في عدّ المعاملات: نفس kernel ينزلق على كل موضع وعلى كل صورة في الـ batch.
Why grouped divides by g exactly once
لماذا يقسم الالتفاف المجمّع على g مرة واحدة بالضبط
Slide 13 writes the grouped formula in two forms on purpose. Each of the g groups is an ordinary convolution with ci/g inputs and co/g outputs, so it has (co/g)(ci/g) khkw weights. There are g such groups, so multiply by g. Two divisions and one multiplication by g leave a single division:
تكتب الشريحة 13 صيغة grouped بشكلين عن قصد. كل مجموعة من المجموعات g هي convolution عادي بـ ci/g مدخلات وco/g مخرجات، فلها (co/g)(ci/g) khkw وزناً. وهناك g مجموعة، فنضرب في g. قسمتان وضرب واحد في g تتركان قسمة واحدة:
cog · cig · kh · kw · g = co · ci · kh · kwg
Depthwise is the extreme case g = ci = co: each group has one input and one output channel. Substitute g = ci and the ci cancels: co ci kh kw / ci = co kh kw. That is why slide 14's depthwise row has no ci.
الـ depthwise هو الحالة القصوى g = ci = co: كل مجموعة لها قناة مدخل واحدة وقناة مخرج واحدة. عوّض g = ci فيختصر ci: co ci kh kw / ci = co kh kw. ولهذا لا يحتوي صف depthwise في الشريحة 14 على ci.
Layer: ci = 64, co = 128, 3 × 3 kernel, input 56 × 56, batch 32. Count each of the four layer types (bias ignored).
الطبقة: ci = 64، co = 128، kernel بحجم 3 × 3، مدخل 56 × 56، batch 32. عُدّ المعاملات لأنواع الطبقات الأربعة (مع تجاهل bias).
Linear 64 -> 128 c_o c_i = 128 x 64 = 8,192 Convolution 3x3, 64 -> 128 c_o c_i k_h k_w = 128 x 64 x 3 x 3 = 73,728 Grouped convolution, g = 4 per group: (128/4) x (64/4) x 9 = 32 x 16 x 9 = 4,608 4 groups : 4,608 x 4 = 18,432 check : 73,728 / 4 = 18,432 Depthwise 3x3 (needs c_i = c_o, so use 128 -> 128, g = 128) c_o k_h k_w = 128 x 3 x 3 = 1,152 check as grouped: 128 x 128 x 9 / 128 = 1,152 standard 128 -> 128 conv: 128 x 128 x 9 = 147,456 (128x more = c_i) 56 x 56 and batch 32 were never used.
If the course follows lecture 02's convention and includes bias, add one bias per output channel: linear 8,192 + 128 = 8,320; conv 73,728 + 128 = 73,856. State which convention you use.
إذا اتبعت اصطلاح المحاضرة 02 وأدخلت bias، فأضف bias واحداً لكل قناة مخرج: linear 8,192 + 128 = 8,320؛ conv 73,728 + 128 = 73,856. اذكر الاصطلاح الذي تستخدمه.
"Linear, times kernel, over g, drop ci." Read the four rows top to bottom as four edits of one formula.
"Linear، ثم اضرب في kernel، ثم اقسم على g، ثم احذف ci." اقرأ الصفوف الأربعة من الأعلى إلى الأسفل كأربع تعديلات على صيغة واحدة.
c_o c_i -> c_o c_i k_h k_w -> c_o c_i k_h k_w / g -> c_o k_h k_w
Bias convention changes between lectures. Lecture 03's table says "bias is ignored". Lecture 02's counting added co biases. If a question does not say, write your assumption next to the answer.
اصطلاح bias يتغير بين المحاضرات. جدول المحاضرة 03 يقول "bias is ignored". أما العدّ في المحاضرة 02 فأضاف co من biases. إذا لم يحدد السؤال، فاكتب افتراضك بجانب الإجابة.
Depthwise has no ci. Writing co ci kh kw for depthwise overcounts by a factor of ci.
الـ depthwise لا يحتوي على ci. كتابة co ci kh kw للـ depthwise تضخّم العدد بمعامل ci.
Grouped divides once, not twice. co ci kh kw / g2 is the count for a single group; you forgot to multiply by the g groups.
الـ grouped يقسم مرة واحدة وليس مرتين. co ci kh kw / g2 هو عدد مجموعة واحدة فقط؛ نسيت أن تضرب في عدد المجموعات g.
Batch and spatial size never enter. They change activations and MACs (later pages), not parameters.
حجم batch والحجم المكاني لا يدخلان أبداً. هما يغيّران activations وMACs (في الصفحات اللاحقة)، وليس المعاملات.
Check yourself
اختبر نفسك
A 3 × 3 grouped convolution has ci = 32, co = 64 and g = 8. How many weights (bias ignored)?
Grouped convolution بحجم 3 × 3 فيه ci = 32 وco = 64 وg = 8. كم عدد الأوزان (مع تجاهل bias)؟
B. 64 × 32 × 9 / 8 = 18,432 / 8 = 2,304. Check per group: (64/8)(32/8)(9) = 8 × 4 × 9 = 288, times 8 groups = 2,304. A is the ungrouped count. C is a single group, the result of dividing by g twice. D is 64 × 9, the depthwise formula applied wrongly.
B. 64 × 32 × 9 / 8 = 18,432 / 8 = 2,304. تحقق لكل مجموعة: (64/8)(32/8)(9) = 8 × 4 × 9 = 288، مضروبة في 8 مجموعات تعطي 2,304. الخيار A هو العدد دون تجميع. والخيار C هو مجموعة واحدة، أي نتيجة القسمة على g مرتين. والخيار D هو 64 × 9، أي صيغة depthwise مطبّقة خطأً.
A 5 × 5 depthwise convolution on 96 channels processes a batch of 16 images of size 112 × 112. How many weights?
Depthwise convolution بحجم 5 × 5 على 96 قناة يعالج batch من 16 صورة بحجم 112 × 112. كم عدد الأوزان؟
C. co kh kw = 96 × 25 = 2,400. A is the standard conv 96 × 96 × 25, which keeps ci. D multiplies by the batch, which never enters a parameter count; neither does 112 × 112. B has no basis.
C. co kh kw = 96 × 25 = 2,400. الخيار A هو الالتفاف العادي 96 × 96 × 25 الذي يُبقي ci. والخيار D يضرب في الـ batch، وهو لا يدخل أبداً في عدّ المعاملات، وكذلك 112 × 112. والخيار B لا أساس له.
Why does the depthwise formula co kh kw contain no ci?
لماذا لا تحتوي صيغة depthwise وهي co kh kw على ci؟
A. Substitute g = ci into co ci kh kw / g and ci cancels. Each filter is kh × kw × 1 and touches only its own channel. B and D are false: the layer has ci = co channels. C confuses the bias convention, which is ignored here anyway.
A. عوّض g = ci في co ci kh kw / g فيُختصر ci. كل filter حجمه kh × kw × 1 ولا يلمس إلا قناته. الخياران B وD خاطئان: للطبقة ci = co قناة. والخيار C يخلط باصطلاح الـ bias، وهو مُتجاهَل هنا على أي حال.
A linear layer maps 512 features to 10 classes. Under lecture 03's convention, and under lecture 02's bias-included convention, the counts are:
طبقة linear تحوّل 512 خاصية إلى 10 أصناف. ما العدد باصطلاح المحاضرة 03، وباصطلاح المحاضرة 02 الذي يدخل فيه bias؟
D. co ci = 10 × 512 = 5,120 with bias ignored; adding one bias per output gives 5,120 + 10 = 5,130. A adds ci biases instead of co. C adds one bias for the whole layer. B adds dimensions instead of multiplying.
D. co ci = 10 × 512 = 5,120 مع تجاهل bias، وبإضافة bias واحد لكل مخرج نحصل على 5,120 + 10 = 5,130. الخيار A يضيف ci من الـ biases بدلاً من co. والخيار C يضيف bias واحداً للطبقة كلها. والخيار B يجمع الأبعاد بدلاً من ضربها.