COE 592Lecture 4.1المحاضرة 4.1Part 05الجزء 05
Pruning criteria: magnitude and scaling factorsمعايير التقليم: المقدار ومعاملات القياس
What makes a parameter less important, magnitude-based importance at element and row level with L1, L2 and general Lp norms, and scaling-based filter pruning that reuses batch normalization gamma factors.ما الذي يجعل المعامل أقل أهمية، والأهمية القائمة على المقدار (magnitude) على مستوى العنصر والصف باستخدام معايير L1 وL2 وLp العامة، وتقليم المرشحات القائم على معاملات القياس الذي يعيد استخدام معاملات gamma في تطبيع الدفعة.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 26-33
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Parts 03 and 04 settled the shape of what gets removed: a single weight, a pattern, a row, a whole channel. They never said which one. This part answers that question with the two criteria that run most real pruning: the magnitude of a weight or a group of weights, and a learned scaling factor per channel.
The same computations come back in three places. In a COE 592 exam you will be handed a small matrix and asked for its element-wise and row-wise importances and the pruned result. In code, every call to PyTorch's prune.l1_unstructured or prune.ln_structured does exactly what this part derives by hand. And in your research project, when a channel-pruned model has to justify its accuracy on an embedded board, the batch normalization gamma criterion at the end of this part is the one most practical channel-pruning pipelines actually use.
حسم الجزءان 03 و04 شكل ما يُحذف: وزن واحد، أو نمط، أو صف، أو قناة كاملة. لكنهما لم يحددا أيّها نحذف. هذا الجزء يجيب عن هذا السؤال بالمعيارين اللذين تقوم عليهما معظم عمليات التقليم الفعلية: مقدار الوزن أو مقدار مجموعة من الأوزان، ومعامل تحجيم (scaling factor) متعلَّم لكل قناة.
الحسابات نفسها تعود في ثلاثة مواضع. في امتحان COE 592 ستُعطى مصفوفة صغيرة ويُطلب منك حساب أهميتها على مستوى العنصر وعلى مستوى الصف ثم النتيجة بعد التقليم. وفي الكود، كل استدعاء لـ prune.l1_unstructured أو prune.ln_structured في PyTorch يفعل بالضبط ما يشتقه هذا الجزء يدويًا. وفي مشروعك البحثي، حين يحتاج نموذج مقلَّم القنوات إلى تبرير دقته على لوحة مدمجة، فإن معيار gamma في batch normalization الذي يأتي في نهاية هذا الجزء هو المعيار الذي تستخدمه فعلًا معظم خطوط تقليم القنوات العملية.
By the end you canفي نهاية هذا الجزء ستستطيع
- State the principle behind every pruning criterion and apply it to a single neuron.تذكر المبدأ الذي يقوم عليه كل معيار تقليم وتطبّقه على عصبون واحد.
- Compute element-wise importances |W| and produce the pruned matrix for a target sparsity.تحسب الأهمية على مستوى العنصر |W| وتُخرج المصفوفة المقلَّمة لنسبة تناثر مستهدفة.
- Score rows or channels with L1, L2 and general Lp norms and say which row is pruned.تقيّم الصفوف أو القنوات بالمعايير L1 وL2 والمعيار العام Lp وتحدد أي صف يُقلَّم.
- Show with numbers when L1 and L2 rank structural sets differently.تبيّن بالأرقام متى يرتّب L1 وL2 المجموعات الهيكلية ترتيبًا مختلفًا.
- Explain scaling-based pruning and why batch norm gamma is a free channel importance.تشرح التقليم القائم على التحجيم ولماذا تمثّل gamma في batch norm أهمية مجانية للقناة.
Start with one neuron and three inputs. Its output is y = ReLU(10 x_0 - 8 x_1 + 0.1 x_2), and the budget says one of the three connections has to go. Removing the 10 changes the pre-activation by up to 10 |x_0|. Removing the -8 changes it by up to 8 |x_1|. Removing the 0.1 changes it by at most 0.1 |x_2|. If the three inputs are of similar size, the third cut is a hundred times gentler than the first, and that is the one to make.
ابدأ بعصبون واحد له ثلاثة مدخلات. خرجه هو y = ReLU(10 x_0 - 8 x_1 + 0.1 x_2)، والميزانية تقول إن واحدًا من الاتصالات الثلاثة يجب أن يُحذف. حذف الوزن 10 يغيّر ما قبل التنشيط (pre-activation) بما يصل إلى 10 |x_0|. وحذف الوزن -8 يغيّره بما يصل إلى 8 |x_1|. أما حذف الوزن 0.1 فيغيّره بما لا يزيد على 0.1 |x_2|. إذا كانت المدخلات الثلاثة متقاربة في الحجم، فالحذف الثالث ألطف بمئة مرة من الأول، وهو الحذف الذي ينبغي أن نختاره.
That small decision contains the whole idea of a Pruning criterion. Pruning asks the network to give up parameters, and the principle the lecture states is simple: the less important the parameters being removed are, the better the performance of the pruned network is. Every criterion in this lecture is a different way of estimating importance, and importance always means the same thing underneath: how much the loss would change if this parameter, or this group, were set to zero. Magnitude, the subject of this part, is the cheapest such estimate. Second-order methods in the next part spend far more compute to estimate the same quantity more carefully (Second-order pruning).
هذا القرار الصغير يحمل الفكرة الكاملة لمعيار التقليم (pruning criterion). التقليم (pruning) يطلب من الشبكة أن تتخلى عن معاملات، والمبدأ الذي تنص عليه المحاضرة بسيط: كلما قلّت أهمية المعاملات المحذوفة كان أداء الشبكة المقلَّمة أفضل. كل معيار في هذه المحاضرة هو طريقة مختلفة لتقدير الأهمية، والأهمية تعني في جوهرها الشيء نفسه دائمًا: كم ستتغير الخسارة (loss) لو جُعل هذا المعامل، أو هذه المجموعة، صفرًا. المقدار (magnitude)، وهو موضوع هذا الجزء، أرخص تقدير من هذا النوع. أما الطرق من الدرجة الثانية في الجزء التالي فتنفق حسابًا أكبر بكثير لتقدير الكمية نفسها بعناية أكبر (second-order pruning).
Worked exampleمثال محلول
Three candidate deletionsثلاثة حذوفات مرشّحة
Cut the 10احذف الوزن 10
The pre-activation loses 10 x_0. For a typical input this is the largest term in the sum, so the output can flip from firing to silent.يخسر ما قبل التنشيط الحدّ 10 x_0. لمدخل نموذجي يكون هذا أكبر حدّ في المجموع، فقد ينقلب الخرج من عصبون نشِط إلى عصبون صامت.Cut the -8احذف الوزن -8
The pre-activation loses -8 x_1. The sign is negative, but the size of the change is 8 |x_1|, almost as large as the first case.يخسر ما قبل التنشيط الحدّ -8 x_1. الإشارة سالبة، لكن حجم التغيير هو 8 |x_1|، وهو يكاد يساوي الحالة الأولى.Cut the 0.1احذف الوزن 0.1
The pre-activation loses 0.1 x_2. With inputs of similar scale, this is a rounding error next to the other two terms.يخسر ما قبل التنشيط الحدّ 0.1 x_2. ومع مدخلات متقاربة في المقياس، يكون هذا مجرد خطأ تقريب مقارنة بالحدّين الآخرين.Remove the 0.1احذف الوزن 0.1
The connection with the smallest absolute weight is removed, and y = ReLU(10 x_0 - 8 x_1) is almost the same function as before.يُحذف الاتصال ذو أصغر وزن بالقيمة المطلقة، فتصبح y = ReLU(10 x_0 - 8 x_1) تقريبًا الدالة نفسها التي كانت من قبل.
Notice what the example needed to make the decision: nothing but the weights. No data, no gradients, no second run through the training set. That is the appeal of magnitude criteria and also their limit. They answer the Pruning formulation only approximately, because the formulation asks for the smallest increase in loss under a budget on nonzeros, and magnitude is a proxy for loss, not the loss itself. The next three sections make the proxy precise for single weights, for rows, and for whole channels.
لاحظ ما احتاجه المثال لاتخاذ القرار: الأوزان فقط. لا بيانات، ولا تدرجات (gradients)، ولا مرور ثانٍ على مجموعة التدريب. هذه هي جاذبية معايير المقدار وهي أيضًا حدّها. فهي تجيب عن صياغة التقليم (pruning formulation) تقريبيًا فقط، لأن الصياغة تطلب أصغر زيادة في الخسارة تحت ميزانية على عدد القيم غير الصفرية، والمقدار بديل تقريبي (proxy) للخسارة وليس الخسارة نفسها. الأقسام الثلاثة التالية تجعل هذا البديل دقيقًا للأوزان المفردة، وللصفوف، وللقنوات الكاملة.
Recall
W = [10, -8, 0.1] feeds a ReLU neuron. Which weight goes first, and what assumption makes that answer safe?المتجه W = [10, -8, 0.1] يغذّي عصبون ReLU. أي وزن يُحذف أولًا، وما الافتراض الذي يجعل هذه الإجابة آمنة؟
Quick checkاختبار سريع
With W = [10, -8, 0.1] and inputs of similar scale, which weight does magnitude pruning remove first?مع W = [10, -8, 0.1] ومدخلات متقاربة في المقياس، أي وزن يحذفه تقليم المقدار أولًا؟
Now scale the neuron up to a weight matrix. Take W = [[3, -2], [1, -5]] and a budget of 50 percent Sparsity, which on four weights means exactly two zeros. Write the absolute value of every entry, keep the two largest, zero the rest. The importances are [[3, 2], [1, 5]], the survivors are 3 and -5, and the pruned matrix is [[3, 0], [0, -5]].
الآن وسّع العصبون إلى مصفوفة أوزان. خذ W = [[3, -2], [1, -5]] وميزانية تناثر (sparsity) بنسبة 50 بالمئة، وهذا يعني على أربعة أوزان صفرين بالضبط. اكتب القيمة المطلقة لكل عنصر، واحتفظ بأكبر قيمتين، واجعل الباقي صفرًا. الأهميات هي [[3, 2], [1, 5]]، والناجيان هما 3 و-5، والمصفوفة المقلَّمة هي [[3, 0], [0, -5]].
This is Magnitude-based pruning at its finest granularity, the fine-grained case from part 03, and it is the criterion of the paper the slides cite. Han, Pool, Tran and Dally train a network, prune every connection whose weight falls below a threshold, and retrain the survivors. Done once, that took AlexNet to 5x fewer parameters; done iteratively, prune then retrain then prune again, it reached 9x on AlexNet (61M to 6.7M) and 13x on VGG-16 (138M to 10.3M) with no loss of accuracy. They also tried the obvious alternative, removing weights at random with probability tied to their magnitude, and report that it gave worse results. Hard thresholding on |w| won.
هذا هو التقليم القائم على المقدار (magnitude-based pruning) في أدق مستويات الحبيبية، أي الحالة الدقيقة (fine-grained) من الجزء 03، وهو معيار الورقة التي تستشهد بها الشرائح. يقوم Han وPool وTran وDally بتدريب شبكة، ثم يقلّمون كل اتصال يقع وزنه تحت عتبة (threshold)، ثم يعيدون تدريب الأوزان الناجية. حين طُبّق ذلك مرة واحدة خفّض AlexNet إلى معاملات أقل بـ 5x؛ وحين طُبّق تكراريًا، تقليم ثم إعادة تدريب ثم تقليم مرة أخرى، وصل إلى 9x على AlexNet (من 61M إلى 6.7M) وإلى 13x على VGG-16 (من 138M إلى 10.3M) دون أي خسارة في الدقة. وجرّبوا أيضًا البديل البديهي، أي حذف الأوزان عشوائيًا باحتمال مرتبط بمقدارها، وأفادوا بأنه أعطى نتائج أسوأ. فاز التحديد الصارم بعتبة على |w|.
Worked exampleمثال محلول
From W to the pruned matrix at 50 percent sparsityمن W إلى المصفوفة المقلَّمة عند تناثر 50 بالمئة
Take absolute valuesخذ القيم المطلقة
|W| = [[3, 2], [1, 5]]. The -2 becomes 2 and the -5 becomes 5.|W| = [[3, 2], [1, 5]]. تصبح -2 هي 2 وتصبح -5 هي 5.Count how many surviveاحسب عدد الناجين
Four weights at 50 percent sparsity means 4 × (1 - 0.5) = 2 survivors.أربعة أوزان عند تناثر 50 بالمئة تعني 4 × (1 - 0.5) = 2 من الأوزان الناجية.Rank and keepرتّب واحتفظ
Sorted importances are 5, 3, 2, 1. Keep the entries scoring 5 and 3, which are the original -5 and 3.الأهميات مرتبةً هي 5, 3, 2, 1. احتفظ بالعنصرين اللذين حصلا على 5 و3، وهما القيمتان الأصليتان -5 و3.Pruned weightالوزن المقلَّم
[[3, 0], [0, -5]]. The kept weights keep their signs; only the zeros are new.[[3, 0], [0, -5]]. الأوزان المحتفَظ بها تحتفظ بإشاراتها؛ الجديد فقط هو الأصفار.
In code, this is one call. PyTorch's prune.l1_unstructured(module, name, amount) prunes a tensor by removing the units with the lowest L1 norm, and amount can be a fraction such as 0.5 or an absolute count. The name is exact: on a single element the L1 norm is just its absolute value, so element-wise L1 and |W| are the same criterion, and the slide's label "L1-norm, element-wise" is the same statement as Importance = |W|. Any other p would give the same ranking on single elements, which is why the choice of norm only starts to matter in the next section.
في الكود، هذا استدعاء واحد. الدالة prune.l1_unstructured(module, name, amount) في PyTorch تقلّم موترًا (tensor) بحذف الوحدات ذات أدنى معيار L1، ويمكن أن يكون amount نسبة مثل 0.5 أو عددًا مطلقًا. الاسم دقيق: على عنصر مفرد يكون معيار L1 مجرد قيمته المطلقة، لذا فإن L1 على مستوى العنصر و|W| هما المعيار نفسه، وتسمية الشريحة "L1-norm, element-wise" هي العبارة نفسها Importance = |W|. وأي قيمة أخرى لـ p ستعطي الترتيب نفسه على العناصر المفردة، ولهذا لا يبدأ اختيار المعيار في أن يُحدث فرقًا إلا في القسم التالي.
Recall
Compute the element-wise importances of [[3, -2], [1, -5]] and the result at 50 percent sparsity.احسب الأهميات على مستوى العنصر للمصفوفة [[3, -2], [1, -5]] والنتيجة عند تناثر 50 بالمئة.
Keep the same matrix, [[3, -2], [1, -5]], but change the unit of removal. Instead of two individual entries, remove one whole row. A row of a linear layer's weight matrix is one output neuron's incoming connections, so deleting it is Neuron pruning, and the result is a smaller dense matrix that any library runs faster (Coarse-grained (structured) pruning). The question is how to score a row when it holds several weights of different sizes and signs.
احتفظ بالمصفوفة نفسها [[3, -2], [1, -5]]، لكن غيّر وحدة الحذف. بدلًا من عنصرين منفردين، احذف صفًا كاملًا. صف مصفوفة أوزان الطبقة الخطية هو الاتصالات الداخلة إلى عصبون خرج واحد، فحذفه هو تقليم العصبونات (neuron pruning)، والنتيجة مصفوفة كثيفة أصغر تشغّلها أي مكتبة بسرعة أكبر (coarse-grained pruning). السؤال هو كيف نقيّم صفًا يحتوي على عدة أوزان مختلفة في الحجم والإشارة.
The lecture's answer is to treat the row as a Structural set, written W^(S) for the set S of parameters it contains, and to collapse its absolute values into one number. Two collapses are standard. Add them: the L1-norm importance. Or square them, add, and take the root: the L2-norm importance. Row 0 scores |3| + |-2| = 5 under L1 and sqrt(9 + 4) = sqrt(13) = 3.61 under L2. Row 1 scores |1| + |-5| = 6 and sqrt(1 + 25) = sqrt(26) = 5.10. Both norms rank row 1 higher, so both prune row 0, and the pruned matrix is [[0, 0], [1, -5]].
جواب المحاضرة هو أن نعامل الصف بوصفه مجموعة هيكلية (structural set)، تُكتب W^(S) للمجموعة S من المعاملات التي يحتويها، وأن نختزل قيمها المطلقة في رقم واحد. هناك طريقتان معياريتان للاختزال. اجمعها: فتحصل على أهمية معيار L1 (L1-norm importance). أو ربّعها ثم اجمعها ثم خذ الجذر: فتحصل على أهمية معيار L2 (L2-norm importance). الصف 0 يحصل على |3| + |-2| = 5 بمعيار L1 وعلى sqrt(9 + 4) = sqrt(13) = 3.61 بمعيار L2. والصف 1 يحصل على |1| + |-5| = 6 وعلى sqrt(1 + 25) = sqrt(26) = 5.10. كلا المعيارين يضع الصف 1 في مرتبة أعلى، فكلاهما يقلّم الصف 0، والمصفوفة المقلَّمة هي [[0, 0], [1, -5]].
Worked exampleمثال محلول
Row-wise scores, both normsدرجات الصفوف بالمعيارين
Absolute values firstالقيم المطلقة أولًا
Row 0 is [3, 2] in magnitude, row 1 is [1, 5].الصف 0 مقداره [3, 2]، والصف 1 مقداره [1, 5].L1: addالمعيار L1: اجمع
Row 0: 3 + 2 = 5. Row 1: 1 + 5 = 6.الصف 0: 3 + 2 = 5. الصف 1: 1 + 5 = 6.L2: square, add, rootالمعيار L2: ربّع، اجمع، خذ الجذر
Row 0: sqrt(9 + 4) = sqrt(13) = 3.61. Row 1: sqrt(1 + 25) = sqrt(26) = 5.10.الصف 0: sqrt(9 + 4) = sqrt(13) = 3.61. الصف 1: sqrt(1 + 25) = sqrt(26) = 5.10.Compare and zero the loserقارن واجعل الخاسر صفرًا
Under both norms row 0 scores lower, so every entry of row 0 becomes zero.بكلا المعيارين يحصل الصف 0 على درجة أقل، فيصبح كل عنصر في الصف 0 صفرًا.Pruned weightالوزن المقلَّم
[[0, 0], [1, -5]]. Compare with the element-wise result [[3, 0], [0, -5]]: the same Sparsity, a different pattern, because the unit of removal changed.[[0, 0], [1, -5]]. قارنها بنتيجة مستوى العنصر [[3, 0], [0, -5]]: نسبة التناثر (sparsity) نفسها، لكن بنمط مختلف، لأن وحدة الحذف تغيّرت.
| Criterionالمعيار | Row 0 scoreدرجة الصف 0 | Row 1 scoreدرجة الصف 1 | Row prunedالصف المقلَّم | Pruned matrixالمصفوفة المقلَّمة |
|---|---|---|---|---|
| Element-wise |w||w| على مستوى العنصر | not a row scoreليست درجة صف | not a row scoreليست درجة صف | none (two smallest elements)لا شيء (أصغر عنصرين) | [[3, 0], [0, -5]][[3, 0], [0, -5]] |
| Row-wise L1L1 على مستوى الصف | 55 | 66 | row 0الصف 0 | [[0, 0], [1, -5]][[0, 0], [1, -5]] |
| Row-wise L2L2 على مستوى الصف | sqrt(13) = 3.61sqrt(13) = 3.61 | sqrt(26) = 5.10sqrt(26) = 5.10 | row 0الصف 0 | [[0, 0], [1, -5]][[0, 0], [1, -5]] |
One formula for every pصيغة واحدة لكل قيمة p
L1 and L2 are two settings of one dial. Goodfellow, Bengio and Courville define the Lp norm of a vector for any p ≥ 1 as the p-th root of the sum of p-th powers of the absolute values. Set p = 1 and the root and the power vanish, leaving slide 29. Set p = 2 and you get the Euclidean length of slide 30. Slide 31 is the same example under the general formula, not a new method.
المعياران L1 وL2 وضعيتان لقرص واحد. يعرّف Goodfellow وBengio وCourville معيار Lp (Lp norm) لمتجه لأي p ≥ 1 بأنه الجذر من الرتبة p لمجموع القوى من الرتبة p للقيم المطلقة. ضع p = 1 فيختفي الجذر والقوة، ويبقى ما في الشريحة 29. وضع p = 2 فتحصل على الطول الإقليدي في الشريحة 30. أما الشريحة 31 فهي المثال نفسه تحت الصيغة العامة، وليست طريقة جديدة.
When the norm changes the answerحين يغيّر المعيار الإجابة
On the slide's matrix L1 and L2 agree, and it is tempting to file the norm as a cosmetic choice. It is not. Compare a row [3, 3] with a row [0, 5] competing for the last surviving slot. L1 scores them 6 against 5 and keeps [3, 3]. L2 scores them sqrt(18) = 4.24 against 5 and keeps [0, 5]. Squaring amplifies the single largest entry, so L2 rewards a row with one dominant weight. Summing treats every unit of magnitude the same, so L1 rewards a row with many moderate weights. Goodfellow et al. describe the same contrast in general terms: the L1 norm grows at the same rate in all locations, while the squared L2 norm increases very slowly near the origin.
على مصفوفة الشريحة يتفق L1 وL2، ومن المغري أن تعتبر اختيار المعيار مسألة شكلية. لكنه ليس كذلك. قارن صفًا [3, 3] بصف [0, 5] يتنافسان على آخر مكان ناجٍ. يعطيهما L1 الدرجتين 6 مقابل 5 فيحتفظ بـ [3, 3]. ويعطيهما L2 sqrt(18) = 4.24 مقابل 5 فيحتفظ بـ [0, 5]. التربيع يضخّم أكبر عنصر منفرد، لذا يكافئ L2 الصف الذي فيه وزن واحد مهيمن. والجمع يعامل كل وحدة من المقدار بالتساوي، لذا يكافئ L1 الصف الذي فيه أوزان متوسطة كثيرة. ويصف Goodfellow et al. التباين نفسه بعبارات عامة: معيار L1 ينمو بالمعدل نفسه في كل المواضع، بينما يزداد مربع معيار L2 ببطء شديد قرب نقطة الأصل.
| Rowالصف | L1 scoreدرجة L1 | L2 scoreدرجة L2 |
|---|---|---|
| [3, 3][3, 3] | 3 + 3 = 63 + 3 = 6 | sqrt(9 + 9) = 4.24sqrt(9 + 9) = 4.24 |
| [0, 5][0, 5] | 0 + 5 = 50 + 5 = 5 | sqrt(0 + 25) = 5sqrt(0 + 25) = 5 |
| Row keptالصف المحتفَظ به | [3, 3][3, 3] | [0, 5][0, 5] |
Try it yourself. The explorer below scores an editable matrix element-wise or row-wise, always computes both row norms so a disagreement is visible without toggling, and ships with the slide's example and the [3, 3] against [0, 5] case as presets. Change a single entry and watch which row the two norms fight over.
جرّب بنفسك. المستكشف أدناه يقيّم مصفوفة قابلة للتعديل على مستوى العنصر أو على مستوى الصف، ويحسب دائمًا معياري الصف كليهما بحيث يظهر أي اختلاف دون الحاجة إلى التبديل، ويأتي بإعدادين جاهزين: مثال الشريحة وحالة [3, 3] مقابل [0, 5]. غيّر عنصرًا واحدًا وراقب على أي صف يتنازع المعياران.
Keeping the top 2 of 4 elements by |w|; the rest become zero. Signs never enter the score. Ties keep the lower index. Achieved sparsity: 50 percent of 4 weights.نحتفظ بأعلى 2 عناصر من أصل 4 حسب |w|؛ ويصبح الباقي أصفارًا. الإشارات لا تدخل في الدرجة أبدًا. عند التعادل يُحتفظ بالفهرس الأصغر. التناثرية المحققة: 50 بالمئة من 4 وزنًا.
Where the row-wise criteria come fromمن أين تأتي معايير الصفوف
The L1 row score is the criterion of Li et al. Their filter importance is the sum of the absolute kernel weights of a filter, which they describe as an expectation of the magnitude of the output feature map. They remove the m filters with the smallest sums, delete the matching input channels from the next layer, and show that pruning the smallest works better than pruning at random or pruning the largest. On CIFAR-10 this cut inference cost by up to 34 percent for VGG-16 and 38 percent for ResNet-110 with retraining recovering the accuracy. Because a filter is a structural set, this filter pruning is Channel pruning from part 04 with an explicit score attached.
درجة الصف بمعيار L1 هي معيار Li et al.. أهمية المرشح عندهم هي مجموع القيم المطلقة لأوزان نواة المرشح، ويصفونها بأنها توقّع لمقدار خريطة السمات الناتجة. يحذفون m من المرشحات ذات أصغر المجاميع، ويحذفون قنوات الدخل المقابلة لها من الطبقة التالية، ويبيّنون أن تقليم الأصغر أفضل من التقليم العشوائي ومن تقليم الأكبر. على CIFAR-10 خفّض هذا تكلفة الاستدلال بنسبة تصل إلى 34 بالمئة لـ VGG-16 و38 بالمئة لـ ResNet-110 مع استعادة الدقة بإعادة التدريب. ولأن المرشح مجموعة هيكلية، فإن تقليم المرشحات هذا (filter pruning) هو تقليم القنوات (channel pruning) من الجزء 04 مع درجة صريحة مرفقة به.
The L2 row score is what Wen et al. put inside training. Their structured sparsity learning adds a group Lasso regularizer to the loss, a sum over groups of sqrt(sum of squared weights in the group), which is exactly the L2 norm of each structural set. The optimizer is then rewarded for driving whole filters, channels, filter shapes or even layers to zero, and they report 5.1x CPU and 3.1x GPU speedups on AlexNet's convolutional layers, and a ResNet on CIFAR-10 shrunk from 20 to 18 layers with accuracy moving from 91.25 to 92.60 percent, still above the original 32-layer ResNet. The difference between the two papers is when the norm is used: Li et al. score a trained network after the fact, Wen et al. shape the network during training so the sets to remove are already near zero.
درجة الصف بمعيار L2 هي ما وضعه Wen et al. داخل التدريب. أسلوبهم لتعلّم التناثر الهيكلي (structured sparsity learning) يضيف إلى الخسارة منظِّم group Lasso، وهو مجموع على المجموعات لـ sqrt(sum of squared weights in the group)، أي بالضبط معيار L2 لكل مجموعة هيكلية. عندها يُكافأ المُحسِّن (optimizer) على دفع مرشحات كاملة، أو قنوات، أو أشكال مرشحات، أو حتى طبقات كاملة إلى الصفر، ويذكرون تسريعًا بمقدار 5.1x على CPU و3.1x على GPU في الطبقات الالتفافية لـ AlexNet، وشبكة ResNet على CIFAR-10 تقلّصت من 20 إلى 18 طبقة مع انتقال الدقة من 91.25 إلى 92.60 بالمئة، وهي ما تزال أعلى من شبكة ResNet الأصلية ذات 32 طبقة. الفرق بين الورقتين هو توقيت استخدام المعيار: يقيّم Li et al. شبكة مدرَّبة بعد انتهاء التدريب، بينما يشكّل Wen et al. الشبكة أثناء التدريب بحيث تكون المجموعات المراد حذفها قريبة من الصفر مسبقًا.
PyTorch exposes the post-hoc version directly. prune.ln_structured(module, name, amount, n, dim) removes the channels with the lowest Ln norm along the specified dimension; n = 1 or 2 picks the norm, and dim = 0 on a weight of shape [c_o, c_i, k_h, k_w] scores whole filters. Finer structural sets such as a row inside a kernel (Vector-level pruning) use the same formula on a smaller S.
تتيح PyTorch النسخة اللاحقة للتدريب (post-hoc) مباشرة. الدالة prune.ln_structured(module, name, amount, n, dim) تحذف القنوات ذات أدنى معيار Ln على امتداد البُعد المحدد؛ القيمة n = 1 أو 2 تختار المعيار، وdim = 0 على وزن شكله [c_o, c_i, k_h, k_w] يقيّم مرشحات كاملة. والمجموعات الهيكلية الأدق مثل صف داخل نواة (vector-level pruning) تستخدم الصيغة نفسها على مجموعة S أصغر.
Recall
Row-wise L1 and L2 scores of [[3, -2], [1, -5]], and the pruned result at 50 percent.ما درجات الصفوف بمعياري L1 وL2 للمصفوفة [[3, -2], [1, -5]]، وما النتيجة المقلَّمة عند 50 بالمئة؟
Recall
Give two rows where L1 and L2 disagree, with the numbers.أعطِ صفّين يختلف عليهما L1 وL2، مع الأرقام.
Quick checkاختبار سريع
Row-wise L1 pruning of [[3, -2], [1, -5]] at 50 percent sparsity removes which row, and why?تقليم L1 على مستوى الصف للمصفوفة [[3, -2], [1, -5]] عند تناثر 50 بالمئة يحذف أي صف، ولماذا؟
Quick checkاختبار سريع
Rows [3, 3] and [0, 5] compete for one surviving slot. Which row does each norm keep?الصفان [3, 3] و[0, 5] يتنافسان على مكان ناجٍ واحد. أي صف يحتفظ به كل معيار؟
A convolution layer has N filters, one per output channel (Convolution weight dimensions). Give each filter a single trainable number, its scaling factor, and multiply that channel's entire output by it. On the slide the factors come out as 1.17, 0.10, 0.29, 0.82, ..., 0.56. Suppose the threshold is 0.3 (the slide only marks the two smallest factors as pruned) and two channels fall below it: filter 1 at 0.10 and filter 2 at 0.29. Delete those filters, delete the input channels that consumed their outputs in the next layer, and what remains is filter 0, filter 3, on to filter N-1: a physically narrower layer.
الطبقة الالتفافية لها N من المرشحات، مرشح لكل قناة خرج (conv weight dimensions). أعطِ كل مرشح رقمًا واحدًا قابلًا للتدريب، هو معامل التحجيم الخاص به، واضرب فيه خرج تلك القناة كله. في الشريحة تظهر المعاملات 1.17, 0.10, 0.29, 0.82, ..., 0.56. افترض أن العتبة 0.3 (الشريحة تكتفي بتعليم أصغر معاملين على أنهما مقلَّمان) وأن قناتين تقعان تحتها: المرشح 1 عند 0.10 والمرشح 2 عند 0.29. احذف هذين المرشحين، واحذف قنوات الدخل التي كانت تستهلك خرجهما في الطبقة التالية، فيبقى المرشح 0 والمرشح 3 وصولًا إلى المرشح N-1: طبقة أضيق فعليًا.
This is Scaling-based pruning, and it differs from the norms in one important way. A norm scores a trained network after the fact, from the weights alone. A scaling factor is a parameter that training itself sets, so the network is asked, during training, how much it wants each channel. Liu et al. call the method network slimming and make the question sharp by adding an L1 penalty on the factors to the loss.
هذا هو التقليم القائم على التحجيم (scaling-based pruning)، وهو يختلف عن المعايير السابقة في أمر مهم واحد. المعيار يقيّم شبكة مدرَّبة بعد انتهاء التدريب، من الأوزان وحدها. أما معامل التحجيم فهو معامل يضبطه التدريب نفسه، فالشبكة تُسأل أثناء التدريب عن مدى حاجتها إلى كل قناة. يسمّي Liu et al. هذه الطريقة network slimming (تنحيف الشبكة) ويجعلون السؤال حادًا بإضافة عقوبة L1 على المعاملات إلى الخسارة.
The L1 penalty is the same L1 from the previous section, now used as a regularizer rather than a score: it charges every unit of |gamma| equally, so factors that the loss does not defend slide all the way to zero. Liu et al. use lambda = 1e-4 for VGGNet and 1e-5 for ResNet and DenseNet on CIFAR. After training they sort every factor in the whole network and set one global threshold at a percentile: pruning 70 percent of channels means the threshold is the 70th percentile of all factors, so layers with many weak channels lose more than layers with strong ones, which is the per-layer Pruning ratio falling out automatically. Then they fine-tune the slimmed network, and can repeat the whole loop.
عقوبة L1 هي نفسها معيار L1 من القسم السابق، لكنها تُستخدم الآن منظِّمًا (regularizer) لا درجةً: فهي تفرض الكلفة نفسها على كل وحدة من |gamma|، فتنزلق المعاملات التي لا تدافع عنها الخسارة حتى تصل إلى الصفر. يستخدم Liu et al. القيمة lambda = 1e-4 لـ VGGNet و1e-5 لـ ResNet وDenseNet على CIFAR. بعد التدريب يرتّبون كل المعاملات في الشبكة كلها ويضعون عتبة عامة واحدة عند نسبة مئوية: تقليم 70 بالمئة من القنوات يعني أن العتبة هي المئين 70 لجميع المعاملات، فالطبقات التي فيها قنوات ضعيفة كثيرة تخسر أكثر من الطبقات ذات القنوات القوية، وهذا هو معدل التقليم (pruning ratio) لكل طبقة يظهر تلقائيًا. ثم يُجرون ضبطًا دقيقًا (fine-tune) للشبكة المنحَّفة، ويمكنهم تكرار الحلقة كلها.
Network slimming on VGGNet, CIFAR-10, 70 percent of channels pruned (Liu et al., 2017)أسلوب network slimming على VGGNet مع CIFAR-10، بتقليم 70 بالمئة من القنوات (Liu et al., 2017)
- Test errorخطأ الاختبار
- 6.34 percent to 6.20 percentمن 6.34 بالمئة إلى 6.20 بالمئة
- Parametersالمعاملات
- 20.04M to 2.30M (88.5 percent fewer)من 20.04M إلى 2.30M (أقل بنسبة 88.5 بالمئة)
- FLOPs
- 7.97e8 to 3.91e8 (51.0 percent fewer)من 7.97e8 إلى 3.91e8 (أقل بنسبة 51.0 بالمئة)
- Multi-passعدة جولات
- up to 20x smaller and 5x less computeأصغر بما يصل إلى 20x وحساب أقل بـ 5x
Why the factor is already there: batch norm gammaلماذا المعامل موجود أصلًا: gamma في batch norm
Where does the scaling factor live? Liu et al. observe that almost every modern convolution is followed by batch normalization, and batch normalization already contains one. Ioffe and Szegedy normalize each channel of a mini-batch to zero mean and unit variance, then let the network undo that with two learned parameters per channel: a scale gamma and a shift beta. Goodfellow et al. explain why they exist: the normalized activation is replaced by gamma H' + beta so the new variable can have any mean and standard deviation the network wants. That gamma is the Batch normalization scaling factor, one per output channel, and it is the scaling factor the slide reuses.
أين يوجد معامل التحجيم؟ يلاحظ Liu et al. أن كل طبقة التفافية حديثة تقريبًا يتبعها batch normalization، وأن batch normalization يحتوي أصلًا على واحد. يطبّع Ioffe وSzegedy كل قناة في الدفعة الصغيرة (mini-batch) إلى متوسط صفري وتباين واحد، ثم يتركان الشبكة تتراجع عن ذلك بمعاملين متعلَّمين لكل قناة: معامل تحجيم gamma ومعامل إزاحة beta. ويشرح Goodfellow et al. سبب وجودهما: يُستبدل التنشيط المطبَّع بـ gamma H' + beta كي يمكن للمتغير الجديد أن يأخذ أي متوسط وانحراف معياري تريده الشبكة. هذه gamma هي batch norm gamma، واحدة لكل قناة خرج، وهي معامل التحجيم الذي تعيد الشريحة استخدامه.
Two things make gamma the right choice, and both are exam material. First, cost: reusing gamma adds no new parameters and no new layers. Liu et al. say it introduces no overhead, and in PyTorch the vector is already sitting in BatchNorm2d.weight, a learnable parameter of size C initialized to one, next to beta in BatchNorm2d.bias initialized to zero, with epsilon = 1e-5 by default.
أمران يجعلان gamma الخيار الصحيح، وكلاهما من مادة الامتحان. الأول هو الكلفة: إعادة استخدام gamma لا تضيف معاملات جديدة ولا طبقات جديدة. يقول Liu et al. إنها لا تُدخل أي عبء إضافي، وفي PyTorch يوجد المتجه أصلًا في BatchNorm2d.weight، وهو معامل قابل للتعلّم حجمه C ومهيّأ بالقيمة واحد، بجانب beta في BatchNorm2d.bias المهيّأ بالصفر، مع epsilon = 1e-5 افتراضيًا.
Second, and more subtle, meaning. Suppose you skipped batch normalization and simply inserted a scaling layer after the convolution. Convolution is linear and scaling is linear, so the network could halve every factor and double the corresponding filter weights without changing a single output. The factor would then say nothing about importance, because the network can move magnitude freely between the factor and the weights, and an L1 penalty on the factor would be defeated the same way. Liu et al. call such a factor meaningless. Batch normalization breaks the symmetry: the normalized activation has a fixed unit scale regardless of what the filter weights do, so gamma alone sets the size of the channel's output and its magnitude is a genuine measure of how much the channel contributes.
الأمر الثاني، وهو أدق، هو المعنى. افترض أنك تخطّيت batch normalization وأدخلت ببساطة طبقة تحجيم بعد الالتفاف. الالتفاف خطي والتحجيم خطي، فتستطيع الشبكة أن تقسم كل معامل على اثنين وتضاعف أوزان المرشح المقابل دون أن يتغير أي خرج. عندها لن يقول المعامل شيئًا عن الأهمية، لأن الشبكة تستطيع نقل المقدار بحرية بين المعامل والأوزان، وستُهزم عقوبة L1 على المعامل بالطريقة نفسها. يصف Liu et al. مثل هذا المعامل بأنه بلا معنى. أما batch normalization فيكسر هذا التماثل: التنشيط المطبَّع له مقياس ثابت يساوي واحدًا مهما فعلت أوزان المرشح، فتحدد gamma وحدها حجم خرج القناة، ويكون مقدارها مقياسًا حقيقيًا لمدى مساهمة القناة.
Recall
Why does network slimming reuse batch norm gamma instead of adding a new scaling layer?لماذا يعيد network slimming استخدام gamma في batch norm بدلًا من إضافة طبقة تحجيم جديدة؟
Recall
Factors 1.17, 0.10, 0.29, 0.82 and 0.56 with a threshold of 0.3: which filters go?المعاملات 1.17 و0.10 و0.29 و0.82 و0.56 مع عتبة 0.3: أي المرشحات تُحذف؟
Quick checkاختبار سريع
Why does network slimming reuse batch normalization's gamma as the channel scaling factor?لماذا يعيد network slimming استخدام gamma في batch normalization بوصفها معامل تحجيم القناة؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- A criterion estimates importance. The less important the removed parameters, the better the pruned network performs.المعيار يقدّر الأهمية. كلما قلّت أهمية المعاملات المحذوفة كان أداء الشبكة المقلَّمة أفضل.
- Magnitude pruning uses absolute value, never the signed weight: -5 is more important than 3.تقليم المقدار يستخدم القيمة المطلقة وليس الوزن بإشارته أبدًا: الوزن -5 أهم من الوزن 3.
- Element-wise: Importance = |W|. [[3, -2], [1, -5]] at 50 percent sparsity becomes [[3, 0], [0, -5]].على مستوى العنصر: Importance = |W|. المصفوفة [[3, -2], [1, -5]] عند تناثر 50 بالمئة تصبح [[3, 0], [0, -5]].
- Row-wise L1 gives 5 and 6, row-wise L2 gives 3.61 and 5.10. Both prune row 0, giving [[0, 0], [1, -5]].على مستوى الصف يعطي L1 القيمتين 5 و6، ويعطي L2 القيمتين 3.61 و5.10. كلاهما يقلّم الصف 0، فتصبح المصفوفة [[0, 0], [1, -5]].
- General form: ||W^(S)||_p = (sum over i in S of |w_i|^p)^(1/p). L1 favors many moderate weights, L2 favors one dominant weight, as [3, 3] against [0, 5] shows.الصيغة العامة: ||W^(S)||_p = (مجموع |w_i|^p على كل i في S)^(1/p). المعيار L1 يفضّل أوزانًا متوسطة كثيرة، والمعيار L2 يفضّل وزنًا واحدًا مهيمنًا، كما تُظهر المقارنة بين [3, 3] و[0, 5].
- Scaling-based pruning trains one factor per output channel and prunes small |gamma|. Network slimming adds an L1 penalty on gamma and prunes below a global percentile.التقليم القائم على التحجيم يدرّب معاملًا واحدًا لكل قناة خرج ويقلّم القنوات ذات |gamma| الصغيرة. أسلوب network slimming يضيف عقوبة L1 على gamma ويقلّم ما يقع تحت نسبة مئوية (percentile) عامة واحدة.
- Batch norm gamma is that factor for free: z_o = gamma (z_i - mu_B)/sqrt(sigma_B^2 + epsilon) + beta, one gamma per channel, no extra parameters.قيمة gamma في batch norm هي هذا المعامل مجانًا: z_o = gamma (z_i - mu_B)/sqrt(sigma_B^2 + epsilon) + beta، قيمة gamma واحدة لكل قناة، ومن دون أي معاملات إضافية.
Sourcesالمصادر
- Learning both Weights and Connections for Efficient Neural NetworksPaperHan, Pool, Tran and Dally, NeurIPS 2015Threshold on absolute value, threshold as quality parameter times layer std, 9x AlexNet and 13x VGG-16 with iterative pruningعتبة على القيمة المطلقة، والعتبة بوصفها معامل جودة مضروبًا في الانحراف المعياري للطبقة، و9x على AlexNet و13x على VGG-16 مع التقليم التكراري(opens in a new tab)(يفتح في تبويب جديد)
- Pruning Filters for Efficient ConvNetsPaperLi, Kadav, Durdanovic, Samet and Graf, ICLR 2017Filter importance as the sum of absolute kernel weights; smallest beats random and largest; 34 percent VGG-16 and 38 percent ResNet-110 FLOP cuts on CIFAR-10أهمية المرشح بوصفها مجموع القيم المطلقة لأوزان النواة؛ حذف الأصغر يتفوق على الحذف العشوائي وحذف الأكبر؛ وخفض FLOPs بنسبة 34 بالمئة على VGG-16 و38 بالمئة على ResNet-110 مع CIFAR-10(opens in a new tab)(يفتح في تبويب جديد)
- Learning Structured Sparsity in Deep Neural NetworksPaperWen, Wu, Wang, Chen and Li, NeurIPS 2016Group Lasso with the L2 norm of each group; filter, channel, shape and depth structures; 5.1x CPU and 3.1x GPU AlexNet conv speedupsأسلوب group Lasso بمعيار L2 لكل مجموعة؛ وبنى على مستوى المرشح والقناة والشكل والعمق؛ وتسريع طبقات الالتفاف في AlexNet بمقدار 5.1x على CPU و3.1x على GPU(opens in a new tab)(يفتح في تبويب جديد)
- Learning Efficient Convolutional Networks through Network SlimmingPaperLiu, Li, Shen, Huang, Yan and Zhang, ICCV 2017L1 penalty on BN gamma, global percentile threshold, why a bare scaling layer is meaningless, VGGNet CIFAR-10 numbersعقوبة L1 على gamma في BN، وعتبة نسبة مئوية (percentile) عامة، ولماذا تكون طبقة التحجيم المجردة بلا معنى، وأرقام VGGNet على CIFAR-10(opens in a new tab)(يفتح في تبويب جديد)
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate ShiftPaperIoffe and Szegedy, ICML 2015Algorithm 1: mini-batch mean and variance, normalization with epsilon, learned gamma and beta per feature mapالخوارزمية 1: متوسط وتباين الدفعة الصغيرة (mini-batch)، والتطبيع مع epsilon، وقيمتا gamma وbeta المتعلَّمتان لكل خريطة سمات(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 2.5: NormsBookGoodfellow, Bengio and Courville, MIT PressLp norm definition (eq. 2.30), L1 versus squared L2 growth, L0 as incorrect terminologyتعريف معيار Lp (المعادلة 2.30)، ونمو L1 مقابل مربع L2، وتسمية L0 بوصفها مصطلحًا غير صحيح(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, section 8.7.1: Batch NormalizationBookGoodfellow, Bengio and Courville, MIT PressWhy gamma and beta are reintroduced after normalizationلماذا تُعاد gamma وbeta بعد التطبيع(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.utils.prune.l1_unstructuredDocsPyTorch documentationRemoves the units with the lowest L1 norm; amount as a fraction or a countتحذف الوحدات ذات أدنى معيار L1؛ ويكون amount نسبة أو عددًا(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.utils.prune.ln_structuredDocsPyTorch documentationRemoves channels with the lowest Ln norm along a chosen dimensionتحذف القنوات ذات أدنى معيار Ln على امتداد بُعد مختار(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.BatchNorm2dDocsPyTorch documentationgamma and beta as learnable vectors of size C, gamma initialized to 1, eps default 1e-5gamma وbeta متجهان قابلان للتعلم بحجم C، وتبدأ gamma بالقيمة 1، والقيمة الافتراضية لـ eps هي 1e-5(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, fall 2024DocsMIT HAN LabLectures 3 and 4, Pruning and Sparsity, the source lineage of these slidesالمحاضرتان 3 و4، التقليم والتناثر (Pruning and Sparsity)، وهما الأصل الذي انحدرت منه هذه الشرائح(opens in a new tab)(يفتح في تبويب جديد)