Lecture 04-1 · Pruning and Sparsity I

المحاضرة 04-1 · التقليم والتناثر (Pruning and Sparsity I)

Scaling factors (BN gamma) and second-order OBD

معاملات التحجيم (BN gamma) وOBD من الرتبة الثانية

Slides 32–37 الشرائح 32–37 P2 SHOULD

Two smarter criteria than raw |w|. Scaling-based pruning reads one number per channel, and batch norm already provides it. Optimal Brain Damage asks how much the loss would rise, and three assumptions shrink a Taylor series to one term.

معياران أذكى من |w| الخام. Scaling-based pruning يقرأ رقماً واحداً لكل قناة، وbatch norm يوفره مجاناً. وOptimal Brain Damage يسأل: كم سترتفع loss؟ وثلاثة افتراضات تختصر متسلسلة Taylor إلى حد واحد.

What gets asked ما الذي يُسأل عنه

Expect: which filters a set of scaling factors prunes; why BN gamma is the natural factor; the OBD derivation as "assumption X removes term Y"; and a small calculation of ½ hii wi2 that ranks weights differently from magnitude.

توقّع: أي المرشحات تُحذف بحسب مجموعة scaling factors، ولماذا BN gamma هو المعامل الطبيعي، واشتقاق OBD بصيغة "الافتراض X يحذف الحد Y"، وحساباً صغيراً لـ ½ hii wi2 يرتّب الأوزان بشكل مختلف عن magnitude.

Scaling-based pruning, slide 32

التقليم المبني على معاملات التحجيم (scaling-based pruning)، الشريحة 32

This is a criterion for filter (output channel) pruning. Each output channel of a convolution gets one trainable scaling factor that multiplies the whole output feature map of that channel. After training, channels whose factor has a small magnitude contribute little and are pruned, together with the filter that produces them.

هذا معيار لـ filter pruning (أي تقليم قنوات الإخراج). كل قناة إخراج في طبقة الالتفاف تأخذ معامل تحجيم قابلاً للتدريب (scaling factor) يُضرب في feature map الخاصة بتلك القناة بالكامل. وبعد التدريب، القنوات التي معاملها صغير القيمة المطلقة تساهم قليلاً، فتُحذف مع المرشح الذي ينتجها.

FILTER x GAMMA -> CHANNEL filter scaling factor γ output channel decision Filter 0 1.17 Channel 0 keep Filter 1 0.10 Channel 1 prune Filter 2 0.29 Channel 2 prune Filter 3 0.82 Channel 3 keep Filter N-1 0.56 Channel N-1 keep
Slide 32's example. The two smallest factors, 0.10 and 0.29, take their filters and channels with them; what remains is a narrower dense layer.
مثال الشريحة 32. أصغر معاملين، 0.10 و0.29، يأخذان معهما مرشحيهما وقناتيهما، والباقي طبقة كثيفة أضيق.

Network Slimming: make the factors sparse on purpose

Network Slimming: اجعل المعاملات متناثرة عن قصد

Network Slimming (Liu et al., ICCV 2017) adds an L1 penalty on the scaling factors during training so that unimportant ones are pushed toward zero:

Network Slimming (Liu et al., ICCV 2017) يضيف عقوبة L1 على معاملات التحجيم أثناء التدريب، فتُدفع غير المهمة منها نحو الصفر:

L = Σ(x,y) ℓ( f(x, W), y ) + λ Σγ∈Γ |γ|

λ is a small coefficient (the paper uses about 10-4 to 10-5). Then all γ in the whole network are ranked together and everything below a global percentile is pruned (for example the lowest 70 percent). A single global threshold automatically gives each layer its own pruning ratio. Fine-tune afterwards.

λ معامل صغير (الورقة تستخدم نحو 10-4 إلى 10-5). بعدها تُرتَّب كل قيم γ في الشبكة كلها معاً، ويُحذف كل ما يقع تحت percentile عام (مثلاً أدنى 70%). وعتبة عامة واحدة تعطي كل طبقة نسبة تقليم خاصة بها تلقائياً. ثم يأتي fine-tuning.

Reuse batch norm's gamma, slide 33

أعد استخدام gamma الخاصة بـ batch norm، الشريحة 33

A conv layer followed by batch norm already has exactly one trainable multiplier per output channel:

طبقة الالتفاف التي يتبعها batch norm لديها أصلاً مُضاعِف واحد قابل للتدريب لكل قناة إخراج:

zo = γ · zi − μB√(σB2 + ε) + β

The normalized value has zero mean and unit variance per channel, and γ alone sets how strongly that channel speaks. So γ is the scaling factor for free: no extra layer, no extra parameters, no extra inference cost.

القيمة بعد التطبيع لها متوسط صفري وتباين واحد في كل قناة، وγ وحدها تحدد قوة صوت تلك القناة. لذلك γ هي معامل التحجيم مجاناً: بلا طبقة إضافية، ولا معاملات إضافية، ولا كلفة استدلال إضافية.

Why not just bolt a scaling layer after a conv without BN? Because conv is linear: the network could shrink γ by 10 and grow the filter weights by 10 with no change in output, so a small γ would mean nothing. BN's normalization removes the weight scale, which is what makes |γ| a meaningful importance.

ولماذا لا نضيف طبقة تحجيم بعد الالتفاف بدون BN؟ لأن الالتفاف خطي: يمكن للشبكة أن تقسم γ على 10 وتضرب أوزان المرشح في 10 دون أي تغيير في المخرج، فلا تعني γ الصغيرة شيئاً. أما تطبيع BN فيلغي مقياس الأوزان، وهذا ما يجعل |γ| مقياس أهمية ذا معنى.

Second-order pruning: the Taylor expansion, slide 34

التقليم من الرتبة الثانية: توسيع Taylor، الشريحة 34

Pruning is a perturbation δW of the weights. Instead of guessing importance from size, estimate directly how much the loss changes. Expand the loss in a Taylor series:

التقليم اضطراب δW في الأوزان. فبدلاً من تخمين الأهمية من الحجم، قدّر مباشرة كم تتغير loss. وسّع loss في متسلسلة Taylor:

δL = Σi gi δwi + ½ Σi hii δwi2 + ½ Σi≠j hij δwi δwj + O(‖δW‖3) gi = ∂L/∂wi   hij = ∂2L / ∂wi∂wj

Four pieces: a gradient term, a diagonal Hessian term, a cross (off-diagonal) Hessian term, and higher-order terms. Optimal Brain Damage (LeCun, Denker and Solla, NeurIPS 1989) keeps only the second piece.

أربعة أجزاء: حد gradient، وحد قطر Hessian، وحد متقاطع (خارج القطر)، وحدود من رتبة أعلى. وOptimal Brain Damage (LeCun, Denker and Solla, NeurIPS 1989) يحتفظ بالجزء الثاني فقط.

The three OBD assumptions, slides 35 and 36

افتراضات OBD الثلاثة، الشريحتان 35 و36

AssumptionالافتراضTerm it killsالحد الذي يحذفهWhyالسبب
L is nearly quadraticL تربيعية تقريباًO(‖δW‖3)a quadratic has no third derivativesالدالة التربيعية ليس لها مشتقات ثالثة
Training has convergedالتدريب تقارب (converged)Σ gi δwi (first order)Σ gi δwi (الرتبة الأولى)at a minimum the gradient gi ≈ 0عند القاع يكون gradient gi ≈ 0
Deleting each parameter is independentحذف كل معامل مستقل عن غيرهΣi≠j hij δwi δwj (cross terms)Σi≠j hij δwi δwj (الحدود المتقاطعة)only the diagonal of the Hessian is usedيُستخدم قطر Hessian فقط

يربط الجدول كل افتراض بالحد الذي يحذفه: افتراض أن L تربيعية تقريباً يحذف O(‖δW‖3)، وافتراض أن التدريب تقارب (converged) يحذف حد الرتبة الأولى لأن gi ≈ 0 عند القاع، وافتراض استقلال حذف كل معامل يحذف الحدود المتقاطعة فلا يبقى إلا قطر Hessian. والذي يجب أن تحفظه: الترتيب "تربيعي، تقارب، استقلال" يقابل "رتبة ثالثة، رتبة أولى، حدود متقاطعة".

Deleting weight i means setting it to zero, so its perturbation has size |δwi| = |wi|. What is left:

حذف الوزن i يعني جعله صفراً، فحجم الاضطراب |δwi| = |wi|. والذي يتبقى:

importancewi = |δLi| = ½ hii wi2

Prune the weights with the smallest induced loss change. hii is non-negative at a minimum (the Hessian is positive semidefinite there), so the importance is never negative. The catch, in red on slide 36: the Hessian is difficult to compute. For n weights it has n2 entries; for 1 million weights that is 1012. OBD needs only the diagonal, but even that requires an extra backward-style pass.

احذف الأوزان التي تُحدث أصغر تغيّر في loss. وhii غير سالبة عند القاع (لأن Hessian هناك positive semidefinite)، فالأهمية لا تكون سالبة أبداً. والمشكلة، بالأحمر في الشريحة 36: Hessian صعبة الحساب. لـ n وزن فيها n2 عنصر، ولمليون وزن يعني ذلك 1012. يحتاج OBD القطر فقط، لكن حتى هذا يتطلب تمريرة إضافية شبيهة بـ backward pass.

Magnitude pruning is a special case

Magnitude pruning حالة خاصة

If every hii were the same constant c, importance would be ½ c wi2, which ranks weights exactly like |wi|. So magnitude pruning = OBD with all hii equal. OBD improves on it by weighting each weight by how curved the loss is in that direction.

لو كانت كل hii تساوي الثابت نفسه c، لكانت الأهمية ½ c wi2، وهذا يرتّب الأوزان تماماً مثل |wi|. إذن magnitude pruning = OBD مع تساوي كل hii. وOBD يتفوق عليه بوزن كل وزن بمقدار انحناء loss في اتجاهه.

Worked مثال محلول
four weights with known diagonal Hessian entries, prune two

  i    w_i    h_ii    |w_i|    0.5 h_ii w_i^2
  1    0.5    10      0.5      0.5 x 10  x 0.25 = 1.25
  2   -2.0     0.1    2.0      0.5 x 0.1 x 4    = 0.20
  3    1.0     1      1.0      0.5 x 1   x 1    = 0.50
  4    0.3    20      0.3      0.5 x 20  x 0.09 = 0.90

magnitude ranking (smallest |w| first):  w4 (0.3), w1 (0.5)
  -> prunes w4 and w1

OBD ranking (smallest 0.5 h w^2 first):  w2 (0.20), w3 (0.50)
  -> prunes w2 and w3

no overlap. w2 = -2.0 is the BIGGEST weight, but the loss is
almost flat in its direction (h = 0.1), so removing it costs
little. w4 = 0.3 is tiny, but sits in a steep direction (h = 20).
Trap فخ

Match each assumption to its term. "Converged" kills the gradient term, not the cross terms. "Independent deletions" kills the cross terms. "Nearly quadratic" kills the cubic and higher terms. Swapping two of these is the classic lost mark.

طابِق كل افتراض مع حدّه. "التقارب" يحذف حد gradient لا الحدود المتقاطعة. و"استقلال الحذف" يحذف الحدود المتقاطعة. و"تربيعي تقريباً" يحذف الحدود التكعيبية وما فوقها. تبديل اثنين منها هو الخطأ الكلاسيكي الذي يضيّع الدرجة.

Slide 36 erratum. The slide writes hii = ∂2L / ∂wi∂wj. A diagonal entry has both derivatives in the same weight: hii = ∂2L / ∂wi2.

خطأ في الشريحة 36. تكتب الشريحة hii = ∂2L / ∂wi∂wj. لكن عنصر القطر مشتقتاه في الوزن نفسه: hii = ∂2L / ∂wi2.

Slide 34 sign convention. The slide defines δL = L(W) − L(W − δW) yet writes every term with a plus sign. A strict expansion of that difference gives Σ gi δwi − ½ Σ h δw2 − ...; the all-plus form is the expansion of L(W + δW) − L(W), the loss increase, which is LeCun's original convention. It does not change the answer: the gradient term is dropped and δwi2 = wi2 regardless of sign, and the criterion uses |δLi| = ½ hii wi2.

اصطلاح الإشارة في الشريحة 34. تعرّف الشريحة δL = L(W) − L(W − δW) ثم تكتب كل الحدود بإشارة موجبة. والتوسيع الدقيق لهذا الفرق يعطي Σ gi δwi − ½ Σ h δw2 − ...؛ أما الصيغة الموجبة كلها فهي توسيع L(W + δW) − L(W)، أي زيادة loss، وهو اصطلاح LeCun الأصلي. ولا يتغير الجواب: حد gradient محذوف، وδwi2 = wi2 مهما كانت الإشارة، والمعيار يستخدم |δLi| = ½ hii wi2.

Memory hook خطّاف للذاكرة

"Quadratic, Converged, Independent: cubic, gradient, cross." Three assumptions, three crossed-out terms, the red X marks on slide 35. What survives is "half h w squared".

"Quadratic, Converged, Independent: cubic, gradient, cross." أي: تربيعي يحذف التكعيبي، والتقارب يحذف gradient، والاستقلال يحذف المتقاطع. ثلاثة افتراضات وثلاثة حدود مشطوبة بعلامات X الحمراء في الشريحة 35. والباقي هو "نصف h في w تربيع".

Check yourself

اختبر نفسك

Which OBD assumption removes the first-order term Σ gi δwi?

أي افتراض في OBD يحذف حد الرتبة الأولى Σ gi δwi؟

At a converged minimum the gradient is approximately zero, so every gi δwi vanishes. "Nearly quadratic" drops the cubic remainder; "independent" drops the cross terms; non-negative hii only guarantees the importance is not negative.

عند قاع متقارب يكون gradient صفراً تقريباً، فتختفي كل حدود gi δwi. و"تربيعي تقريباً" يحذف باقي الرتبة الثالثة، و"الاستقلال" يحذف الحدود المتقاطعة، أما كون hii غير سالبة فيضمن فقط ألا تكون الأهمية سالبة.

A weight has w = -3 and hii = 0.4. What is its OBD importance?

وزن قيمته w = -3 وhii = 0.4. ما أهميته حسب OBD؟

½ x 0.4 x (-3)2 = ½ x 0.4 x 9 = 1.8. Option B forgets the ½. Option A uses w instead of w2. Option D uses |w| instead of w2.

½ x 0.4 x (-3)2 = ½ x 0.4 x 9 = 1.8. الخيار B ينسى ½. والخيار A يستخدم w بدل w2. والخيار D يستخدم |w| بدل w2.

Under what condition does OBD rank weights exactly like magnitude pruning?

متى يرتّب OBD الأوزان تماماً مثل magnitude pruning؟

With hii = c for all i, importance is ½ c wi2, a monotonic function of |wi|. Zero gradient and zero cross terms are already assumed by OBD and do not make its ranking match magnitude.

إذا كانت hii = c لكل i، فالأهمية ½ c wi2، وهي دالة متزايدة في |wi|. أما انعدام gradient وانعدام الحدود المتقاطعة فهما مفترضان أصلاً في OBD ولا يجعلان ترتيبه مطابقاً لـ magnitude.

Why is batch norm's γ a good scaling factor for channel pruning, compared with adding a separate scaling layer after a conv with no BN?

لماذا تُعد γ في batch norm معامل تحجيم جيداً لتقليم القنوات، مقارنة بإضافة طبقة تحجيم منفصلة بعد التفاف بدون BN؟

BN already has one γ per channel, so no parameters or inference cost are added. Without normalization a conv could move scale freely between its weights and a bare scaling factor, so a small factor would carry no information. γ is trainable (option B is false), and fine-tuning is still used after slimming (option C is false).

في BN توجد أصلاً γ واحدة لكل قناة، فلا تُضاف معاملات ولا كلفة استدلال. وبدون تطبيع يستطيع الالتفاف نقل المقياس بحرية بين أوزانه ومعامل التحجيم المجرد، فلا يحمل المعامل الصغير أي معلومة. وγ قابلة للتدريب (فالخيار B خاطئ)، وfine-tuning ما زال مستخدماً بعد slimming (فالخيار C خاطئ).

A layer's channel scaling factors are 1.17, 0.10, 0.29, 0.82, 0.56. You prune the 40 percent of channels with the smallest |γ|. Which channels go?

معاملات التحجيم لقنوات طبقة هي 1.17, 0.10, 0.29, 0.82, 0.56. قلّمت 40% من القنوات ذات أصغر |γ|. أي القنوات تُحذف؟

40 percent of 5 channels is 2. The two smallest factors are 0.10 (channel 1) and 0.29 (channel 2), exactly the ones crossed out on slide 32. Their filters are removed too, leaving a narrower dense layer.

40% من 5 قنوات يساوي 2. وأصغر معاملين هما 0.10 (القناة 1) و0.29 (القناة 2)، وهما بالضبط المشطوبان في الشريحة 32. وتُحذف مرشحاتهما أيضاً، فتبقى طبقة كثيفة أضيق.