COE 592Lecture 03المحاضرة 03Part 02الجزء 02
Parameters and model sizeالمعاملات وحجم النموذج
Counting weights for linear, convolution, grouped and depthwise layers, totaling AlexNet to 61M parameters, and converting parameter count into storage at different bit widths.عدّ الأوزان في الطبقات الخطية والالتفافية والمجمّعة والعمقية، وجمعها في AlexNet لتبلغ 61M معاملًا، وتحويل عدد المعاملات إلى مساحة تخزين عند عروض بت مختلفة.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 10-17
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Every result you will meet later in this course, from pruning AlexNet "9x smaller" to Deep Compression's "35x" storage cut, is a ratio of two numbers computed the way this part teaches. Before you can judge pruning, quantization or an architecture search, you must be able to count what a network stores, and the same count decides whether a model fits into a microcontroller's flash.
Lecture 02 taught how linear, convolution, grouped and depthwise layers compute their outputs. This part does not repeat that. It asks one narrower question of each layer: how many numbers does it keep? Four short formulas answer it, AlexNet gives them a real workout, and one multiplication by the bit width turns the answer into megabytes. Along the way you will find a typo on the slide, a hidden value to compute yourself, and a simulator you can use to check every parameter and model-size number here and the MAC and FLOP numbers in part 04.
كل نتيجة ستقابلها لاحقًا في هذا المقرر، من تقليم AlexNet ليصبح "أصغر 9x" إلى خفض التخزين "35x" في Deep Compression، هي نسبة بين عددين يُحسبان بالطريقة التي يعلّمها هذا الجزء. قبل أن تستطيع الحكم على التقليم (pruning) أو التكميم (quantization) أو البحث عن المعمارية، يجب أن تعرف كيف تعدّ ما تخزّنه الشبكة، وهذا العدد نفسه هو ما يقرّر هل يتّسع النموذج في ذاكرة الفلاش (flash) لمتحكّم دقيق (microcontroller) أم لا.
علّمت المحاضرة 02 كيف تحسب الطبقات الخطية وطبقات الالتفاف والمجمّعة والعمقية مخرجاتها. هذا الجزء لا يكرّر ذلك، بل يطرح على كل طبقة سؤالًا أضيق: كم عددًا تحتفظ به؟ أربع صيغ قصيرة تجيب عن السؤال، و AlexNet يمنحها تمرينًا حقيقيًا، وضربة واحدة في عرض البت تحوّل الجواب إلى ميغابايت. وفي الطريق ستجد خطأ مطبعيًا في الشريحة، وقيمة مخفية عليك حسابها بنفسك، ومحاكيًا يمكنك استخدامه للتحقق من كل رقم للمعاملات ولحجم النموذج هنا، ومن أرقام MAC و FLOP في الجزء 04.
By the end you canفي نهاية هذا الجزء ستستطيع
- Define #Parameters as the element count of all weight tensors and explain why n, h and w never enter it.تعريف عدد المعاملات (#Parameters) بأنه عدد عناصر جميع موترات الأوزان، وشرح لماذا لا تدخل فيه n و h و w أبدًا.
- Write and derive the four counting formulas (linear, convolution, grouped, depthwise) and justify the single division by g.كتابة صيغ العدّ الأربع (الخطية، والالتفاف، والمجمّع، والعمقي) واشتقاقها، وتبرير القسمة الواحدة على g.
- Total AlexNet layer by layer, identify where 96 percent of the weights sit, and spot the slide's conv1 typo.جمع معاملات AlexNet طبقة بطبقة، وتحديد أين تقع 96 بالمئة من الأوزان، واكتشاف الخطأ المطبعي في conv1 على الشريحة.
- Convert a parameter count to model size at any bit width in bits, KB and MB, stating the decimal convention.تحويل عدد المعاملات إلى حجم نموذج عند أي عرض بت بالبت و KB و MB، مع ذكر الاصطلاح العشري المستخدم.
- Predict how architecture choices (groups, depthwise filters, linear layer width) move model size before touching hardware.توقّع كيف تغيّر قرارات المعمارية (المجموعات، والمرشّحات العمقية، وعرض الطبقة الخطية) حجم النموذج قبل لمس العتاد.
Start with the smallest network worth drawing: five inputs x0 to x4 and three outputs y0 to y2, every input wired to every output. Count the arrows. Each input sends one arrow to each output, so there are 3 × 5 = 15 arrows, labelled from w00 up to w42. Each arrow carries exactly one learned number, its weight. That is the whole idea of the number of parameters: it is the synapse count, the number of weights, and for this layer it is 15.
ابدأ بأصغر شبكة تستحق الرسم: خمسة مُدخلات من x0 إلى x4 وثلاثة مخرجات من y0 إلى y2، وكل مُدخل موصول بكل مخرج. عُدّ الأسهم. كل مُدخل يرسل سهمًا واحدًا إلى كل مخرج، فيكون عدد الأسهم 3 × 5 = 15، مُرقّمة من w00 حتى w42. كل سهم يحمل عددًا متعلَّمًا واحدًا بالضبط، هو وزنه. هذه هي الفكرة كلها في عدد المعاملات (number of parameters): إنه عدد المشابك (synapses)، أي عدد الأوزان، وفي هذه الطبقة يساوي 15.
Now look at the same layer the way a framework stores it. The inputs for a batch are a matrix X of shape n × c_i, the weights are a matrix W of shape c_o × c_i, and the outputs come out as Y = X · W^T of shape n × c_o. The fifteen arrows have become the fifteen elements of W. This is the second, equivalent definition the slides give: #Parameters is the number of elements in the network's weight tensors. For one linear layer that is the product of its two dimensions.
انظر الآن إلى الطبقة نفسها كما يخزّنها إطار العمل (framework). مُدخلات الدفعة مصفوفة X شكلها n × c_i، والأوزان مصفوفة W شكلها c_o × c_i، والمخرجات تخرج على شكل Y = X · W^T بالشكل n × c_o. الأسهم الخمسة عشر أصبحت العناصر الخمسة عشر في W. هذا هو التعريف الثاني المكافئ الذي تعطيه الشرائح: #Parameters هو عدد العناصر في موترات الأوزان (weight tensors) في الشبكة. وفي طبقة خطية (linear layer) واحدة يساوي ذلك حاصل ضرب بُعديها.
One thing in that matrix picture deserves a second look. The batch size n appears in X and in Y, but never in W. Feed the layer one image or a thousand and the weight matrix is the same c_o × c_i block. Parameter count is a property of the model, not of the data flowing through it. Hold on to this observation: it returns in the next concept for spatial size, and it is the reason parameters and MACs behave so differently in part 04.
شيء واحد في صورة المصفوفات هذه يستحق نظرة ثانية. حجم الدفعة (batch size) n يظهر في X وفي Y، لكنه لا يظهر أبدًا في W. أدخِل إلى الطبقة صورة واحدة أو ألف صورة، وستبقى مصفوفة الأوزان هي الكتلة نفسها c_o × c_i. عدد المعاملات خاصية للنموذج، لا للبيانات التي تمرّ عبره. احتفظ بهذه الملاحظة: ستعود في المفهوم التالي مع الحجم المكاني، وهي سبب اختلاف سلوك المعاملات عن MACs اختلافًا كبيرًا في الجزء 04.
The slides define the count for the "given" (slide 10) or "entire" (slide 11) neural network; both mean the same thing, the sum over every layer. They also say bias is ignored, and this lecture keeps that convention throughout. In PyTorch a linear layer stores a bias vector of length c_o and a convolution stores one bias per output channel, so the true count is larger by c_o per layer (PyTorch Conv2d documentation). Next to c_o · c_i that is a rounding error, which is why the lecture drops it.
تعرّف الشرائح العدد للشبكة العصبية "المعطاة" (given، الشريحة 10) أو "الكاملة" (entire، الشريحة 11)؛ والمعنى واحد في الحالتين، وهو المجموع على كل الطبقات. وتقول الشرائح أيضًا إن الانحياز (bias) مُهمَل، وهذه المحاضرة تلتزم بهذا الاصطلاح من أولها إلى آخرها. في PyTorch تخزّن الطبقة الخطية متجه انحياز طوله c_o، ويخزّن الالتفاف انحيازًا واحدًا لكل قناة مخرج، فيكون العدد الحقيقي أكبر بمقدار c_o لكل طبقة (توثيق Conv2d في PyTorch). وبجانب c_o · c_i هذا فرق تقريب لا يُذكر، ولهذا تُسقطه المحاضرة.
Notation used on slides 11 to 14 and in the rest of this lectureالرموز المستخدمة في الشرائح من 11 إلى 14 وفي بقية هذه المحاضرة
- n
- Batch size, the number of inputs processed togetherحجم الدفعة، أي عدد المُدخلات التي تُعالَج معًا
- c_i, c_o
- Input and output channels (for a linear layer, input and output features)قنوات المُدخل والمخرج (وفي الطبقة الخطية: خصائص features المُدخل والمخرج)
- h_i, h_o
- Input and output heightارتفاع المُدخل والمخرج
- w_i, w_o
- Input and output widthعرض المُدخل والمخرج
- k_h, k_w
- Kernel height and widthارتفاع النواة (kernel) وعرضها
- g
- Number of groups in a grouped convolutionعدد المجموعات في الالتفاف المجمّع
Layer mechanics live in lecture 02آلية عمل الطبقات موجودة في المحاضرة 02
How a linear layer, a convolution, padding and stride, grouped and depthwise convolution and pooling produce their outputs, including the output-shape arithmetic behind numbers like 96 × 55 × 55, was covered in lecture 02: neurons and linear layers, convolution layers, padding, stride and grouped convolution and pooling.
كيف تُنتج الطبقة الخطية، والالتفاف، والحشو (padding) والخطوة (stride)، والالتفاف المجمّع والعمقي، والتجميع (pooling) مخرجاتها، بما في ذلك حساب شكل المخرج الذي يقف خلف أرقام مثل 96 × 55 × 55، كل ذلك شُرح في المحاضرة 02: العصبونات والطبقات الخطية، طبقات الالتفاف، الحشو والخطوة والالتفاف المجمّع، و التجميع.
Recall
A linear layer maps 512 inputs to 256 outputs. How many parameters does it have, ignoring bias, and does the batch size change that number?طبقة خطية تحوّل 512 مُدخلًا إلى 256 مخرجًا. كم معاملًا فيها مع إهمال الانحياز، وهل يغيّر حجم الدفعة هذا العدد؟
Take AlexNet's third convolution: 384 filters, each looking at 256 input channels through a 3 × 3 window. One filter is a small brick of numbers, 256 deep and 3 × 3 across, so it holds 256 · 9 = 2,304 weights. There are 384 such bricks, so the layer holds 384 · 2,304 = 884,736 weights. That is the whole convolution formula for the #Parameters, read off the shape of the weight tensor.
خُذ الالتفاف الثالث في AlexNet: 384 مرشّحًا (filter)، كل منها ينظر إلى 256 قناة مُدخل عبر نافذة 3 × 3. المرشّح الواحد قالب صغير من الأعداد، عمقه 256 وعرضه 3 × 3، فيحمل 256 · 9 = 2,304 وزنًا. وهناك 384 قالبًا من هذا النوع، فتحمل الطبقة 384 · 2,304 = 884,736 وزنًا. هذه هي صيغة #Parameters كاملة لطبقة convolution (الالتفاف)، تُقرأ مباشرة من شكل موتّر الأوزان.
Notice what is missing. The input is 256 × 13 × 13 and the output is 384 × 13 × 13, yet 13 appears nowhere in the count. A convolution slides the same brick to every output position, so the weights are shared across space. Goodfellow, Bengio and Courville make the point with an edge detector: a two-element kernel applied to a 320 × 280 image stores two parameters, where a dense matrix doing the same job would store billions (Goodfellow et al., section 9.2). The kernel size and the channel counts set the parameter count; the output height and width do not. Part 04 will show that MACs do multiply by h_o · w_o, because the brick is reapplied at every position, and that is the single most important difference between the two metrics.
لاحظ ما هو غائب. المُدخل 256 × 13 × 13 والمخرج 384 × 13 × 13، ومع ذلك لا يظهر 13 في أي مكان من العدد. الالتفاف يُزلق القالب نفسه إلى كل موضع في المخرج، فالأوزان مشتركة عبر المكان. ويوضّح Goodfellow و Bengio و Courville هذه النقطة بكاشف حواف: نواة من عنصرين تُطبَّق على صورة 320 × 280 تخزّن معاملين فقط، بينما مصفوفة كثيفة تؤدي العمل نفسه ستخزّن مليارات (Goodfellow et al.، القسم 9.2). kernel size (حجم النواة) وأعداد القنوات هي التي تحدّد عدد المعاملات؛ أما ارتفاع المخرج وعرضه (output height and width) فلا. وسيُظهر الجزء 04 أن MACs تُضرب فعلًا في h_o · w_o، لأن القالب يُعاد تطبيقه في كل موضع، وهذا هو الفرق الأهم على الإطلاق بين المقياسين.
Grouped convolution: derive it, do not memorize itالالتفاف المجمّع: اشتقّه ولا تحفظه
A grouped convolution with g groups cuts the c_i input channels into g slices of c_i/g and the c_o output channels into g slices of c_o/g, then runs g ordinary convolutions side by side, each reading only its own input slice and writing only its own output slice. So count one group with the formula you already have: (c_o/g) · (c_i/g) · k_h · k_w. There are g groups, so multiply by g. One factor of g cancels.
grouped convolution (الالتفاف المجمّع) ذو g groups يقطع قنوات المُدخل c_i إلى g شرائح، في كل منها c_i/g، ويقطع قنوات المخرج c_o إلى g شرائح، في كل منها c_o/g، ثم يشغّل g التفافات عادية جنبًا إلى جنب، كل منها يقرأ شريحة المُدخل الخاصة به فقط ويكتب في شريحة المخرج الخاصة به فقط. إذن عُدّ مجموعة واحدة بالصيغة التي تعرفها: (c_o/g) · (c_i/g) · k_h · k_w. وهناك g مجموعات، فاضرب في g. فيُحذف أحد عاملَي g.
This matches what a framework actually stores. PyTorch gives Conv2d a weight of shape (out_channels, in_channels / groups, kH, kW) and requires that in_channels and out_channels are both divisible by groups (PyTorch documentation). The historical reason AlexNet has g = 2 in three of its layers is hardware, not elegance: the network was trained across two GTX 580 GPUs with 3 GB each, and "the kernels of the second, fourth, and fifth convolutional layers are connected only to those kernel maps in the previous layer which reside on the same GPU" (Krizhevsky et al., 2012, section 3.5). Grouping halved the weights each GPU had to hold.
هذا يطابق ما يخزّنه إطار العمل فعلًا. يعطي PyTorch الطبقة Conv2d وزنًا شكله (out_channels, in_channels / groups, kH, kW)، ويشترط أن يقبل كل من in_channels و out_channels القسمة على groups (توثيق PyTorch). والسبب التاريخي لوجود g = 2 في ثلاث من طبقات AlexNet هو العتاد، لا الأناقة: دُرّبت الشبكة موزّعة على معالجين رسوميين GTX 580 في كل منهما 3 GB، و"أنوية طبقات الالتفاف الثانية والرابعة والخامسة متصلة فقط بخرائط الأنوية في الطبقة السابقة الموجودة على المعالج الرسومي نفسه" (Krizhevsky et al., 2012، القسم 3.5). التجميع قسم الأوزان التي يجب أن يحملها كل معالج رسومي إلى النصف.
Depthwise convolution: the limit g = c_i = c_oالالتفاف العمقي: الحالة القصوى g = c_i = c_o
Push the grouping to its extreme, one channel per group, and you get a depthwise convolution. Now g = c_i = c_o and every channel has its own private k_h × k_w filter. In the MobileNets paper this is stated directly: "the depthwise convolution applies a single filter to each input channel" (Howard et al., 2017, section 3.1). Plug g = c_i into the grouped formula and the c_i cancels.
ادفع التجميع إلى أقصاه، قناة واحدة لكل مجموعة، فتحصل على depthwise convolution (الالتفاف العمقي). الآن g = c_i = c_o، ولكل قناة مرشّحها الخاص k_h × k_w. وورقة MobileNets تقول ذلك صراحة: "الالتفاف العمقي يطبّق مرشّحًا واحدًا على كل قناة مُدخل" (Howard et al., 2017، القسم 3.1). عوّض g = c_i في صيغة المجمّع فيُحذف c_i.
Worked exampleمثال محلول
One 3 × 3 layer with 256 input and 256 output channelsطبقة 3 × 3 واحدة فيها 256 قناة مُدخل و 256 قناة مخرج
Standard convolutionالالتفاف العادي
256 · 256 · 3 · 3 = 589,824 weights.256 · 256 · 3 · 3 = 589,824 وزنًا.Grouped with g = 2مجمّع مع g = 2
589,824 / 2 = 294,912. Each of the two groups is a 128 → 128 convolution with 147,456 weights.589,824 / 2 = 294,912. كل واحدة من المجموعتين التفاف 128 → 128 فيه 147,456 وزنًا.Grouped with g = 8مجمّع مع g = 8
589,824 / 8 = 73,728. Eight groups of 32 → 32.589,824 / 8 = 73,728. ثماني مجموعات، كل منها 32 → 32.Depthwise, g = 256عمقي، g = 256
256 · 9 = 2,304. Each channel keeps one 3 × 3 filter.256 · 9 = 2,304. كل قناة تحتفظ بمرشّح 3 × 3 واحد.Ratio to the standard layerالنسبة إلى الطبقة العادية
Layerالطبقة Countالعدّ #Parameters#Parameters Ratioالنسبة Standard, g = 1عادي، g = 1 256 · 256 · 9256 · 256 · 9 589,824589,824 11 Grouped, g = 2مجمّع، g = 2 256 · 256 · 9 / 2256 · 256 · 9 / 2 294,912294,912 1/21/2 Grouped, g = 8مجمّع، g = 8 256 · 256 · 9 / 8256 · 256 · 9 / 8 73,72873,728 1/81/8 Depthwise, g = 256عمقي، g = 256 256 · 9256 · 9 2,3042,304 1/2561/256 Layerالطبقة Countالعدّ #Parameters#Parameters Ratioالنسبة Standard, g = 1عادي، g = 1 256 · 256 · 9256 · 256 · 9 589,824589,824 11 Grouped, g = 2مجمّع، g = 2 256 · 256 · 9 / 2256 · 256 · 9 / 2 294,912294,912 1/21/2 Grouped, g = 8مجمّع، g = 8 256 · 256 · 9 / 8256 · 256 · 9 / 8 73,72873,728 1/81/8 Depthwise, g = 256عمقي، g = 256 256 · 9256 · 9 2,3042,304 1/2561/256
| Layerالطبقة | #Parameters#Parameters | Depends on h, w?يعتمد على h و w؟ | Depends on n?يعتمد على n؟ | Whyالسبب |
|---|---|---|---|---|
| Linearخطية (Linear) | c_o · c_ic_o · c_i | Only through c_i after a flattenفقط عبر c_i بعد التسطيح | Noلا | Every input feature is wired to every output feature; a flattened c_i carries h × wكل خاصية مُدخل موصولة بكل خاصية مخرج؛ و c_i المسطّحة تحمل h × w |
| Convolutionالتفاف (Convolution) | c_o · c_i · k_h · k_wc_o · c_i · k_h · k_w | Noلا | Noلا | c_o filters, each c_i deep and k_h × k_w widec_o مرشّحًا، عمق كل منها c_i وعرضه k_h × k_w |
| Grouped convolutionالتفاف مجمّع (Grouped convolution) | c_o · c_i · k_h · k_w / gc_o · c_i · k_h · k_w / g | Noلا | Noلا | g independent convolutions on c_i/g and c_o/g channelsg التفافات مستقلة على c_i/g و c_o/g قناة |
| Depthwise convolutionالتفاف عمقي (Depthwise convolution) | c_o · k_h · k_wc_o · k_h · k_w | Noلا | Noلا | Grouped with g = c_i = c_o, one filter per channelمجمّع مع g = c_i = c_o، مرشّح واحد لكل قناة |
The simulator collects all four formulas, adds the bit width from the final concept of this part and the MACs from part 04, and carries a preset for every parameterized AlexNet layer. Try conv3 now and confirm 884,736, then change h_o and watch which readouts move.
يجمع المحاكي الصيغ الأربع كلها، ويضيف عرض البت من المفهوم الأخير في هذا الجزء و MACs من الجزء 04، وفيه إعداد جاهز لكل طبقة ذات معاملات في AlexNet. جرّب conv3 الآن وتأكّد من 884,736، ثم غيّر h_o وراقب أي القراءات تتحرّك.
Change h_o or w_o and watch MACs move while the parameter count and model size stay still. Only c_i, c_o, k_h, k_w and g touch the weight count, and only the bit width turns that count into storage.غيّر h_o أو w_o وراقب تغيّر MACs بينما يبقى عدد المعاملات وحجم النموذج ثابتين. وحدهاc_i وc_o وk_h وk_w وg تؤثر في عدد الأوزان، ووحده عرض البت يحوّل هذا العدد إلى مساحة تخزين.
Quick checkاختبار سريع
A 3 × 3 convolution has c_i = 64 and c_o = 128. How many parameters does it have?التفاف 3 × 3 فيه c_i = 64 و c_o = 128. كم عدد معاملاته؟
Quick checkاختبار سريع
Doubling the input resolution of a CNN does what to a convolution layer's parameter count and MAC count?ماذا تفعل مضاعفة دقة المُدخل لشبكة CNN بعدد معاملات طبقة الالتفاف وبعدد MACs فيها؟
Recall
Write the grouped-convolution count two ways and say why the division is by g, not g squared.اكتب عدد معاملات الالتفاف المجمّع بطريقتين، وقل لماذا تكون القسمة على g وليس على g تربيع.
AlexNet is the convolutional network whose variant won the ILSVRC-2012 challenge with a top-5 test error of 15.3%, and its authors state up front that it has "60 million parameters and 650,000 neurons" (Krizhevsky et al., 2012). It is also the best possible exercise for this part: five convolutions, three of them grouped, three max-pools and three linear layers, with input 3 × 224 × 224. Walk it top to bottom and apply the right #Parameters formula at each layer. The output shapes come from lecture 02; here we only count.
AlexNet هي الشبكة الالتفافية التي فاز أحد إصداراتها بتحدي ILSVRC-2012 بخطأ اختبار top-5 قدره 15.3%، ويذكر مؤلفوها منذ البداية أن فيها "60 مليون معامل و 650,000 عصبون" (Krizhevsky et al., 2012). وهي أيضًا أفضل تمرين ممكن لهذا الجزء: خمسة التفافات، ثلاثة منها grouped (مجمّعة)، وثلاث طبقات max-pool، وثلاث linear layers (طبقات خطية)، والمُدخل 3 × 224 × 224. امشِ فيها من الأعلى إلى الأسفل وطبّق صيغة #Parameters المناسبة عند كل طبقة. أشكال المخرجات تأتي من المحاضرة 02؛ وهنا نعدّ فقط.
Worked exampleمثال محلول
Counting AlexNet, part one: the convolutionsعدّ معاملات AlexNet، الجزء الأول: طبقات الالتفاف
conv1: 11 × 11, 96 channels, stride 4, pad 2conv1: نواة 11 × 11، و 96 قناة، وخطوة 4، وحشو 2
Output 96 × 55 × 55. Weights 96 · 3 · 11 · 11 = 34,848. The stride and padding set the 55, but they do not touch the count.المخرج 96 × 55 × 55. الأوزان 96 · 3 · 11 · 11 = 34,848. الخطوة والحشو يحدّدان الـ 55، لكنهما لا يمسّان العدد.maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2
Output 96 × 27 × 27. Pooling takes a maximum; it learns nothing and stores nothing: 0 parameters.المخرج 96 × 27 × 27. التجميع يأخذ القيمة العظمى؛ فلا يتعلّم شيئًا ولا يخزّن شيئًا: 0 معاملات.conv2: 5 × 5, 256 channels, pad 2, groups 2conv2: نواة 5 × 5، و 256 قناة، وحشو 2، ومجموعتان
Output 256 × 27 × 27. Grouped, so 256 · 96 · 5 · 5 / 2 = 307,200.المخرج 256 × 27 × 27. مجمّع، إذن 256 · 96 · 5 · 5 / 2 = 307,200.maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2
Output 256 × 13 × 13, 0 parameters.المخرج 256 × 13 × 13، و 0 معاملات.conv3: 3 × 3, 384 channels, pad 1conv3: نواة 3 × 3، و 384 قناة، وحشو 1
Output 384 × 13 × 13. 384 · 256 · 3 · 3 = 884,736.المخرج 384 × 13 × 13. 384 · 256 · 3 · 3 = 884,736.conv4: 3 × 3, 384 channels, pad 1, groups 2conv4: نواة 3 × 3، و 384 قناة، وحشو 1، ومجموعتان
Output 384 × 13 × 13. 384 · 384 · 3 · 3 / 2 = 663,552.المخرج 384 × 13 × 13. 384 · 384 · 3 · 3 / 2 = 663,552.conv5: 3 × 3, 256 channels, pad 1, groups 2conv5: نواة 3 × 3، و 256 قناة، وحشو 1، ومجموعتان
Output 256 × 13 × 13. The slide hides this one behind a question mark. Compute it before you reveal the answer below.المخرج 256 × 13 × 13. الشريحة تُخفي هذه القيمة خلف علامة استفهام. احسبها قبل أن تكشف الجواب أدناه.
Recall
conv5: 3 × 3 kernels, 256 output channels, 384 input channels, groups 2. How many parameters?conv5: أنوية 3 × 3، و 256 قناة مخرج، و 384 قناة مُدخل، ومجموعتان. كم عدد المعاملات؟
Worked exampleمثال محلول
Counting AlexNet, part two: the linear layers and the totalعدّ معاملات AlexNet، الجزء الثاني: الطبقات الخطية والمجموع
maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2
Output 256 × 6 × 6, 0 parameters. This map is flattened into a vector of 256 · 6 · 6 = 9,216 values.المخرج 256 × 6 × 6، و 0 معاملات. هذه الخريطة تُسطَّح (flatten) إلى متجه فيه 256 · 6 · 6 = 9,216 قيمة.fc6: linear, 4096 outputsfc6: خطية، 4096 مخرجًا
4096 · 9,216 = 37,748,736. One layer, and already more than half of the network.4096 · 9,216 = 37,748,736. طبقة واحدة، وهي وحدها أكثر من نصف الشبكة.fc7: linear, 4096 outputsfc7: خطية، 4096 مخرجًا
4096 · 4096 = 16,777,216.4096 · 4096 = 16,777,216.fc8: linear, 1000 outputsfc8: خطية، 1000 مخرج
1000 · 4096 = 4,096,000, one output per ImageNet class.1000 · 4096 = 4,096,000، مخرج واحد لكل صنف في ImageNet.Totalالمجموع
2,332,704 in the convolutions plus 58,621,952 in the linear layers gives 60,954,656, about 61M, which is what the slide and the paper both round to.2,332,704 في طبقات الالتفاف زائد 58,621,952 في الطبقات الخطية يعطي 60,954,656، أي نحو 61M، وهو ما تقرّب إليه الشريحة والورقة كلتاهما.
| Layerالطبقة | Output C × H × Wالمخرج C × H × W | Countالعدّ | #Parameters#Parameters | Shareالنسبة |
|---|---|---|---|---|
| Imageالصورة | 3 × 224 × 2243 × 224 × 224 | 00 | ||
| conv1, 11 × 11, 96 ch, stride 4, pad 2conv1، نواة 11 × 11، و 96 قناة، وخطوة 4، وحشو 2 | 96 × 55 × 5596 × 55 × 55 | 96 · 3 · 11 · 1196 · 3 · 11 · 11 | 34,84834,848 | 0.06%0.06% |
| maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2 | 96 × 27 × 2796 × 27 × 27 | 00 | ||
| conv2, 5 × 5, 256 ch, pad 2, groups 2conv2، نواة 5 × 5، و 256 قناة، وحشو 2، ومجموعتان | 256 × 27 × 27256 × 27 × 27 | 256 · 96 · 5 · 5 / 2256 · 96 · 5 · 5 / 2 | 307,200307,200 | 0.50%0.50% |
| maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2 | 256 × 13 × 13256 × 13 × 13 | 00 | ||
| conv3, 3 × 3, 384 ch, pad 1conv3، نواة 3 × 3، و 384 قناة، وحشو 1 | 384 × 13 × 13384 × 13 × 13 | 384 · 256 · 3 · 3384 · 256 · 3 · 3 | 884,736884,736 | 1.45%1.45% |
| conv4, 3 × 3, 384 ch, pad 1, groups 2conv4، نواة 3 × 3، و 384 قناة، وحشو 1، ومجموعتان | 384 × 13 × 13384 × 13 × 13 | 384 · 384 · 3 · 3 / 2384 · 384 · 3 · 3 / 2 | 663,552663,552 | 1.09%1.09% |
| conv5, 3 × 3, 256 ch, pad 1, groups 2conv5، نواة 3 × 3، و 256 قناة، وحشو 1، ومجموعتان | 256 × 13 × 13256 × 13 × 13 | 256 · 384 · 3 · 3 / 2256 · 384 · 3 · 3 / 2 | 442,368442,368 | 0.73%0.73% |
| maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2 | 256 × 6 × 6256 × 6 × 6 | 00 | ||
| fc6, linear 4096fc6، خطية 4096 | 40964096 | 4096 · (256 · 6 · 6)4096 · (256 · 6 · 6) | 37,748,73637,748,736 | 61.93%61.93% |
| fc7, linear 4096fc7، خطية 4096 | 40964096 | 4096 · 40964096 · 4096 | 16,777,21616,777,216 | 27.52%27.52% |
| fc8, linear 1000fc8، خطية 1000 | 10001000 | 1000 · 40961000 · 4096 | 4,096,0004,096,000 | 6.72%6.72% |
| Five convolutionsطبقات الالتفاف الخمس | 2,332,7042,332,704 | 3.83%3.83% | ||
| Three linear layersالطبقات الخطية الثلاث | 58,621,95258,621,952 | 96.17%96.17% | ||
| Totalالمجموع | 60,954,65660,954,656 | 100%100% |
Where the weights actually areأين توجد الأوزان فعلًا
The share column is the real lesson. The five convolutions together hold 2.3M weights, under 4 percent. The three linear layers hold 58.6M, about 96 percent, and fc6 alone holds 62 percent. Krizhevsky and colleagues knew this: a footnote in the paper says that "most of the net's parameters are in the first fully-connected layer" (Krizhevsky et al., 2012). Every input of fc6 is wired to every one of its 4,096 outputs, and it has 9,216 inputs, so the dense wiring the convolutions avoided comes back in one enormous matrix.
عمود النسبة هو الدرس الحقيقي. طبقات الالتفاف الخمس مجتمعة تحمل 2.3M وزن، أي أقل من 4 بالمئة. والطبقات الخطية الثلاث تحمل 58.6M، أي نحو 96 بالمئة، و fc6 وحدها تحمل 62 بالمئة. وكان Krizhevsky وزملاؤه يعرفون ذلك: حاشية في الورقة تقول إن "معظم معاملات الشبكة موجودة في الطبقة الأولى الموصولة بالكامل" (Krizhevsky et al., 2012). كل مُدخل في fc6 موصول بكل واحد من مخرجاتها الـ 4,096، ولها 9,216 مُدخلًا، فيعود التوصيل الكثيف الذي تجنّبته طبقات الالتفاف في مصفوفة واحدة ضخمة.
There is a subtle exception hiding in that 9,216. The previous concept said parameter counts do not depend on input resolution, and for every convolution that is true. But fc6 takes the flattened 256 × 6 × 6 map as its c_i, and the 6 × 6 came from 224 × 224 through the strides and pools. Feed AlexNet a larger image and fc6 would need a larger weight matrix. The first linear layer after a flatten is the one place where resolution leaks into the parameter count. Global average pooling, introduced in Network in Network, removes that leak: it averages each channel to one number, and "there is no parameter to optimize" in it (Lin, Chen and Yan, 2014), so the classifier input no longer grows with the image.
هناك استثناء دقيق مختبئ في ذلك الرقم 9,216. قال المفهوم السابق إن عدد المعاملات لا يعتمد على دقة المُدخل (resolution)، وهذا صحيح لكل طبقة التفاف. لكن fc6 تأخذ الخريطة المسطّحة 256 × 6 × 6 على أنها c_i، والـ 6 × 6 جاءت من 224 × 224 عبر الخطوات وطبقات التجميع. أدخِل إلى AlexNet صورة أكبر وستحتاج fc6 إلى مصفوفة أوزان أكبر. الطبقة الخطية الأولى بعد التسطيح هي المكان الوحيد الذي تتسرّب فيه الدقة إلى عدد المعاملات. والتجميع المتوسط الشامل (global average pooling)، الذي قُدّم في Network in Network، يسدّ هذا التسرّب: فهو يحسب متوسط كل قناة في عدد واحد، و"لا يوجد فيه أي معامل يُحسَّن" (Lin, Chen and Yan, 2014)، فلا يعود مُدخل المصنِّف يكبر مع الصورة.
This table is the starting line for two results you will study later. Han and colleagues pruned AlexNet "from 61 million to 6.7 million" parameters with no loss of accuracy, a 9x reduction (Han et al., 2015), and Deep Compression then cut its storage "from 240MB to 6.9MB" (Han, Mao and Dally, 2016). Both papers attack exactly the layers this table flags: when 96 percent of the weights sit in three linear layers, that is where pruning pays.
هذا الجدول هو خط البداية لنتيجتين ستدرسهما لاحقًا. قلّم Han وزملاؤه AlexNet "من 61 مليونًا إلى 6.7 مليون" معامل دون أي خسارة في الدقة، أي تقليص 9x (Han et al., 2015)، ثم خفّض Deep Compression تخزينها "من 240MB إلى 6.9MB" (Han, Mao and Dally, 2016). الورقتان كلتاهما تهاجمان بالضبط الطبقات التي يشير إليها هذا الجدول: حين تقع 96 بالمئة من الأوزان في ثلاث طبقات خطية، فهناك يؤتي التقليم ثماره.
Quick checkاختبار سريع
In AlexNet, which group of layers holds most of the 61M parameters?في AlexNet، أي مجموعة من الطبقات تحمل معظم المعاملات البالغة 61M؟
Recall
Which AlexNet layers hold about 96 percent of the parameters, and why is fc6 so large?أي طبقات AlexNet تحمل نحو 96 بالمئة من المعاملات، ولماذا fc6 كبيرة إلى هذا الحد؟
A parameter count is a number of boxes. To know how much storage the boxes take, you need the size of each box. Store AlexNet's 61M weights as 32-bit floating point and each weight takes 4 bytes, so the whole network takes 61M × 4 B = 244 MB. Store the same weights as 8-bitintegers and the same network takes 61 MB. Nothing about the architecture changed; only the bit width did.
عدد المعاملات هو عدد صناديق. ولتعرف كم من التخزين تأخذ الصناديق، تحتاج إلى حجم كل صندوق. خزّن أوزان AlexNet البالغة 61M بصيغة الفاصلة العائمة 32-bit فيأخذ كل وزن 4 bytes، فتأخذ الشبكة كلها 61M × 4 B = 244 MB. وخزّن الأوزان نفسها أعدادًا صحيحة 8-bit فتأخذ الشبكة نفسها 61 MB. لم يتغيّر شيء في المعمارية؛ الذي تغيّر هو bit width (عرض البت) فقط.
That is the definition of model size: the storage needed for the weights of the network, the #Parameters times the bits each one occupies. The slide lists MB, KB and bits as the common units, and the formula as written produces bits. To reach bytes divide by 8, and to reach megabytes divide by another 10^6. The slide is explicit that its megabyte is decimal, 244 × 10^6 bytes. For a single layer, kilobytes are the natural unit: AlexNet's conv3 holds 884,736 weights, which at 8 bits is 884,736 bytes, or 884.7 KB (divide bytes by 10^3), and at 32 bits 3,538.9 KB.
هذا هو تعريف model size (حجم النموذج): التخزين المطلوب لأوزان الشبكة، أي #Parameters مضروبًا في عدد البتات التي يشغلها كل منها. تذكر الشريحة MB و KB والبت بوصفها الوحدات الشائعة، والصيغة كما هي مكتوبة تنتج بتات. للوصول إلى البايت اقسم على 8، وللوصول إلى الميغابايت اقسم مرة أخرى على 10^6. والشريحة صريحة في أن الميغابايت عندها عشري، أي 244 × 10^6 بايت. أما للطبقة الواحدة فالكيلوبايت هو الوحدة الطبيعية: الطبقة conv3 في AlexNet تحمل 884,736 وزنًا، وهذا عند 8 بت يساوي 884,736 بايت، أي 884.7 KB (اقسم البايتات على 10^3)، وعند 32 بت يساوي 3,538.9 KB.
| Data typeنوع البيانات | Size in bitsالحجم بالبت | Size (decimal MB)الحجم (MB عشرية) | Slide roundingتقريب الشريحة |
|---|---|---|---|
| 32-bit float (fp32)فاصلة عائمة 32-bit (fp32) | 1,950.5 Mbit1,950.5 Mbit | 243.8 MB243.8 MB | 244 MB244 MB |
| 16-bit (fp16 or bfloat16)16-bit (fp16 أو bfloat16) | 975.3 Mbit975.3 Mbit | 121.9 MB121.9 MB | 122 MB122 MB |
| 8-bit integer (int8)عدد صحيح 8-bit (int8) | 487.6 Mbit487.6 Mbit | 61.0 MB61.0 MB | 61 MB61 MB |
| 4-bit integer (int4)عدد صحيح 4-bit (int4) | 243.8 Mbit243.8 Mbit | 30.5 MB30.5 MB | 30.5 MB30.5 MB |
| 1-bit (binary weights)1-bit (أوزان ثنائية) | 61.0 Mbit61.0 Mbit | 7.6 MB7.6 MB | 7.6 MB7.6 MB |
The formula assumes one data type for the whole network. Mixed-precision models break that assumption, and then the size is a sum over layers, each layer's count times its own bit width. That is the form you need when, for example, the first and last layers stay at 8 bits while the middle is pushed to 4.
تفترض الصيغة نوع بيانات واحدًا للشبكة كلها. النماذج مختلطة الدقة (mixed-precision) تكسر هذا الافتراض، وعندها يكون الحجم مجموعًا على الطبقات، عدد كل طبقة مضروبًا في عرض البت الخاص بها. وهذه هي الصيغة التي تحتاجها حين تبقى الطبقتان الأولى والأخيرة مثلًا عند 8 bits بينما يُدفع الوسط إلى 4.
Why the multiplication matters for this courseلماذا يهمّ هذا الضرب في هذا المقرر
The parameter count is fixed once the architecture is chosen, but the bit width is a knob. The lecture on quantization turns that knob, and this formula says exactly what it buys: every halving of the bit width halves the model size with no change to the architecture. Deep Compression combined pruning, quantization and Huffman coding to take AlexNet "from 240MB to 6.9MB" (Han, Mao and Dally, 2016); their 240 MB starting point is, up to rounding, the 244 MB computed in this concept. Part 03 shows a microcontroller SRAM budget of 256 kB (the MCU constraint, a budget for activations). The slides do not give a flash size, but a typical MCU of this class, such as the STM32F746 used by MCUNet (Lin et al., NeurIPS 2020), has about 1 MB of flash, where the weights would live; even 8-bitAlexNet at 61 MB is about 60 times too large for that flash, which is why edge deployment starts with counting.
عدد المعاملات ثابت بمجرد اختيار المعمارية، لكن عرض البت مقبض قابل للتدوير. محاضرة التكميم تدير هذا المقبض، وهذه الصيغة تقول بالضبط ما الذي تشتريه: كل تنصيف لعرض البت ينصّف حجم النموذج دون أي تغيير في المعمارية. جمع Deep Compression بين التقليم والتكميم وترميز Huffman لينقل AlexNet "من 240MB إلى 6.9MB" (Han, Mao and Dally, 2016)؛ ونقطة بدايتهم 240 MB هي، بعد التقريب، الرقم 244 MB المحسوب في هذا المفهوم. يعرض الجزء 03 ميزانية SRAM لمتحكّم دقيق قدرها 256 kB (MCU constraint، وهي ميزانية للتنشيطات). الشرائح لا تعطي حجم ذاكرة الفلاش، لكن متحكّمًا نموذجيًا من هذه الفئة، مثل STM32F746 الذي يستخدمه MCUNet (Lin et al., NeurIPS 2020)، فيه نحو 1 MB من flash، وهي المكان الذي ستعيش فيه الأوزان؛ وحتى AlexNet بدقة 8-bit عند 61 MB أكبر من تلك الفلاش بنحو 60 مرة، ولهذا يبدأ النشر على الأطراف (edge deployment) بالعدّ.
Quick checkاختبار سريع
A 10M-parameter model stored entirely in 16-bit numbers occupies how much storage?نموذج فيه 10M معامل مخزّن بالكامل بأعداد 16-bit، كم يشغل من التخزين؟
Recall
61M parameters at 8 bits: what is the model size in MB, and in bits?61M معامل عند 8 بت: ما حجم النموذج بوحدة MB، وبالبت؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- #Parameters is the number of elements in the weight tensors of the whole network. Bias is ignored in this lecture.عدد المعاملات (#Parameters) هو عدد العناصر في موترات الأوزان للشبكة كاملة. الانحياز (bias) مُهمَل في هذه المحاضرة.
- Linear c_o · c_i. Convolution c_o · c_i · k_h · k_w. Grouped divides that by g. Depthwise is c_o · k_h · k_w.الخطية c_o · c_i. الالتفاف c_o · c_i · k_h · k_w. المجمّع يقسم ذلك على g. والعمقي c_o · k_h · k_w.
- Parameter count ignores batch size and spatial size. MACs (part 04) do not, because every filter is reapplied at every output position.عدد المعاملات لا يتأثر بحجم الدفعة ولا بالحجم المكاني. أما MACs (الجزء 04) فتتأثر، لأن كل مرشّح يُعاد تطبيقه عند كل موضع في المخرج.
- AlexNet has 60,954,656 parameters, about 61M. conv1 is 34,848 (the slide prints 24,848) and the hidden conv5 is 442,368.في AlexNet يوجد 60,954,656 معاملًا، أي نحو 61M. الطبقة conv1 فيها 34,848 (والشريحة تطبع 24,848)، والطبقة conv5 المخفية فيها 442,368.
- The three linear layers hold about 96 percent (58.6M). fc6 alone is 37.7M because its input is the flattened 256 × 6 × 6 map.الطبقات الخطية الثلاث تحمل نحو 96 بالمئة (58.6M). الطبقة fc6 وحدها 37.7M لأن مُدخلها هو الخريطة المسطّحة 256 × 6 × 6.
- Pooling layers have zero parameters.طبقات التجميع (pooling) معاملاتها صفر.
- Model size = #Parameters × bit width. 61M weights are 244 MB at 32-bit, 122 MB at 16-bit and 61 MB at 8-bit, in decimal MB.حجم النموذج = #Parameters × عرض البت. 61M وزن تساوي 244 MB عند 32-bit، و 122 MB عند 16-bit، و 61 MB عند 8-bit، بوحدات MB العشرية.
Sourcesالمصادر
- ImageNet Classification with Deep Convolutional Neural NetworksPaperNeurIPS 2012, Krizhevsky, Sutskever and Hinton60 million parameters, per-layer kernel shapes, same-GPU connectivity behind groups = 2, first fully connected layer holds most parameters.60 مليون معامل، وأشكال الأنوية لكل طبقة، والاتصال داخل المعالج الرسومي نفسه الذي يقف خلف groups = 2، والطبقة الأولى الموصولة بالكامل تحمل معظم المعاملات.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.Conv2dDocsPyTorch documentationWeight shape (out_channels, in_channels / groups, kH, kW), divisibility by groups, depthwise as groups = in_channels, bias shape.شكل الوزن (out_channels, in_channels / groups, kH, kW)، وقابلية القسمة على groups، والالتفاف العمقي بوصفه groups = in_channels، وشكل الانحياز.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Fall 2024DocsMIT HAN Lab, Song HanLecture 2, Basics of Deep Learning, is the source deck for slides 10 to 17 including the conv1 typo.المحاضرة 2، Basics of Deep Learning، هي مصدر الشرائح من 10 إلى 17، بما في ذلك الخطأ المطبعي في conv1.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman CodingPaperICLR 2016, Han, Mao and DallyAlexNet storage reduced 35x, from 240 MB to 6.9 MB.تخزين AlexNet خُفّض 35x، من 240 MB إلى 6.9 MB.(opens in a new tab)(يفتح في تبويب جديد)
- Learning both Weights and Connections for Efficient Neural NetworksPaperNeurIPS 2015, Han, Pool, Tran and DallyAlexNet pruned 9x, from 61 million to 6.7 million parameters.AlexNet قُلّمت 9x، من 61 مليون معامل إلى 6.7 مليون.(opens in a new tab)(يفتح في تبويب جديد)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperarXiv 2017, Howard et al.Depthwise convolution applies a single filter per input channel; Table 2 gives parameter shares of 1 x 1, depthwise and linear layers.الالتفاف العمقي يطبّق مرشّحًا واحدًا لكل قناة مُدخل؛ والجدول 2 يعطي نسب المعاملات لطبقات 1 x 1 والطبقات العمقية والخطية.(opens in a new tab)(يفتح في تبويب جديد)
- Prefixes for binary multiplesDocsNIST1 MB = 10^6 bytes, 1 MiB = 2^20 bytes.1 MB = 10^6 بايت، و 1 MiB = 2^20 بايت.(opens in a new tab)(يفتح في تبويب جديد)
- torchvision.models.alexnetDocsPyTorch documentation61,100,840 parameters; implementation follows the One weird trick variant, not the 2012 paper.61,100,840 معاملًا؛ والتنفيذ يتبع إصدار One weird trick، لا ورقة 2012.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 9: Convolutional NetworksBookMIT Press, Goodfellow, Bengio and CourvilleSection 9.2, parameter sharing: a two-parameter edge kernel versus a dense matrix with billions of entries.القسم 9.2، مشاركة المعاملات: نواة حواف بمعاملين مقابل مصفوفة كثيفة فيها مليارات العناصر.(opens in a new tab)(يفتح في تبويب جديد)
- MCUNet: Tiny Deep Learning on IoT DevicesPaperNeurIPS 2020, Lin et al.Runs TinyNAS networks on the STM32F746, a microcontroller with 320 kB SRAM and 1 MB flash.يشغّل شبكات TinyNAS على STM32F746، وهو متحكّم دقيق فيه 320 kB من SRAM و 1 MB من الفلاش.(opens in a new tab)(يفتح في تبويب جديد)
- Network In NetworkPaperICLR 2014, Lin, Chen and YanGlobal average pooling replaces fully connected classifier layers and has no parameters to optimize.التجميع المتوسط الشامل (global average pooling) يحلّ محل طبقات المصنِّف الموصولة بالكامل، ولا يحتوي أي معامل يُحسَّن.(opens in a new tab)(يفتح في تبويب جديد)