COE 592Lecture 5.3المحاضرة 5.3Part 06الجزء 06
Binary weights: sign, stochastic binarization and the BWN scaleالأوزان الثنائية: sign والتثنيث العشوائي ومقياس BWN
Pushing to 1 bit: if weights are only +1 or -1, a dot product needs only additions and subtractions, storage drops about 32x. Weights can be binarized deterministically with sign or stochastically with a hard sigmoid, and a single per-tensor scale alpha = mean |W| shrinks the error and nearly closes the accuracy gap.الوصول إلى بت واحد: إذا كانت الأوزان +1 أو -1 فقط، فإن الضرب النقطي يحتاج فقط إلى جمع وطرح، وينخفض التخزين نحو 32x. يمكن تثنيث الأوزان حتميًا بدالة sign أو عشوائيًا بدالة hard sigmoid، ومقياس واحد لكل موتر alpha = mean |W| يقلل الخطأ ويكاد يسد فجوة الدقة.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 49-54
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Parts 01 to 05 squeezed weights to 8, 4 and even 2 bits while keeping a multiply in every multiply-accumulate. This part asks the extreme question: what if each weight is a single bit? You will see that the multiply disappears, memory drops about 32×, and a single well-chosen scale factor rescues almost all of the accuracy.
The thread runs in four steps: where 1-bit weights sit in the quantization family, what they do to a dot product, the two ways to turn a real weight into a bit, and the Binary Weight Network scale that makes it work on ImageNet. For the COE 592 midterm expect a calculation (alpha, the sign matrix, a Frobenius error or a hard-sigmoid probability), a short derivation, and a compare question on deterministic versus stochastic binarization. For edge deployment, this is the bridge to part 07, where binarizing the activations too turns the remaining additions into XNOR and popcount.
ضغطت الأجزاء من 01 إلى 05 الأوزان إلى 8 و4 بل وحتى 2 بت، مع الإبقاء على عملية ضرب داخل كل عملية ضرب وتجميع (multiply-accumulate). هذا الجزء يطرح السؤال الأقصى: ماذا لو كان كل وزن بتًا واحدًا فقط؟ سترى أن الضرب يختفي، وأن الذاكرة تنخفض بنحو 32×، وأن معامل مقياس واحدًا مختارًا بعناية يستعيد كل الدقة تقريبًا.
يسير الخيط في أربع خطوات: أين تقع الأوزان ذات البت الواحد في عائلة التكميم، وماذا تفعل بحاصل الضرب النقطي، والطريقتان لتحويل وزن حقيقي إلى بت، ثم مقياس شبكة Binary Weight Network الذي يجعلها تعمل على ImageNet. في اختبار منتصف الفصل لمقرر COE 592 توقّع سؤالًا حسابيًا (قيمة alpha، أو مصفوفة الإشارات، أو خطأ Frobenius، أو احتمال hard sigmoid)، واشتقاقًا قصيرًا، وسؤال مقارنة بين التثنئة الحتمية والعشوائية. وبالنسبة للنشر على الأجهزة الطرفية (edge)، فهذا الجزء هو الجسر إلى الجزء 07، حيث تؤدي تثنئة التنشيطات أيضًا إلى تحويل عمليات الجمع المتبقية إلى XNOR وpopcount.
By the end you canفي نهاية هذا الجزء ستستطيع
- Place binary and ternary quantization in the storage and computation table next to K-means and linear quantization.تضع التكميم الثنائي والثلاثي في جدول التخزين والحساب بجانب تكميم K-means والتكميم الخطي.
- Evaluate a dot product with real and with binarized weights, and explain why multiplications disappear.تحسب حاصل الضرب النقطي بأوزان حقيقية ثم بأوزان مثنّاة، وتشرح لماذا تختفي عمليات الضرب.
- Justify the about 32x memory and about 2x computation savings of real-input, binary-weight layers.تبرّر توفير الذاكرة بنحو 32x وتوفير الحساب بنحو 2x في الطبقات ذات المدخلات الحقيقية والأوزان الثنائية.
- Apply deterministic sign binarization and stochastic hard-sigmoid binarization, including the r = 0 case.تطبّق التثنئة الحتمية بالإشارة sign والتثنئة العشوائية بالـ hard sigmoid، بما في ذلك حالة r = 0.
- Compute alpha = (1/n) ||W||_1, the binarized matrix and both squared Frobenius errors for the slide's 4 x 4 matrix.تحسب alpha = (1/n) ||W||_1 والمصفوفة المثنّاة وخطأي Frobenius المربّعين لمصفوفة الشريحة 4 x 4.
- Derive why alpha = mean |W| is the error-minimizing scale.تشتق لماذا يكون alpha = mean |W| هو المقياس الذي يجعل الخطأ أصغر ما يمكن.
Take the running 4 × 4 weight matrix of this course, the one that starts with 2.09, −0.98, 1.48, 0.09. Lecture 05-1 stored it four ways. As plain FP32 it costs 16 × 32 = 512 bits. K-means replaced each value with a 2-bit index into a four-entry floating-point codebook. Linear quantization stored 2-bit integers and rebuilt values as (q − Z) × S with Z = −1, S = 1.07. The fourth column of the recap slide keeps one bit per weight.
خذ مصفوفة الأوزان 4 × 4 المتكررة في هذا المقرر، تلك التي تبدأ بـ 2.09، −0.98، 1.48، 0.09. خزّنتها المحاضرة 05-1 بأربع طرق. بصيغة FP32 العادية تكلّف 16 × 32 = 512 بت. استبدل تكميم K-means كل قيمة بفهرس من 2 بت يشير إلى دفتر رموز (codebook) من أربع قيم عائمة. وخزّن التكميم الخطي أعدادًا صحيحة من 2 بت وأعاد بناء القيم بالعلاقة (q − Z) × S حيث Z = −1 وS = 1.07. أما العمود الرابع في شريحة المراجعة فيحتفظ ببت واحد لكل وزن.
| Schemeالطريقة | Storageالتخزين | Computationالحساب | Bits per weightالبتات لكل وزن |
|---|---|---|---|
| Full precisionالدقة الكاملة | Floating-point weightsأوزان بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة | 32 bits32 بت |
| K-meansK-means | Integer indices plus a floating-point codebookفهارس صحيحة مع دفتر رموز بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة | 2 bits plus codebook2 بت مع دفتر الرموز |
| Linearخطي | Integer weights (with S and Z)أوزان صحيحة (مع S وZ) | Integer arithmeticحساب بالأعداد الصحيحة | 2 to 8 bitsمن 2 إلى 8 بت |
| Binary or ternaryثنائي أو ثلاثي | Binary or ternary weightsأوزان ثنائية أو ثلاثية | Bit operations (add, subtract, XNOR)عمليات على البتات (جمع، طرح، XNOR) | 1 or 2 bits1 أو 2 بت |
Reading the table as a ladderقراءة الجدول كسُلّم
Read the table along two axes. The storage row shrinks from left to right, and so does the price of the arithmetic. K-means saves memory but still multiplies floats, because the codebook entries are floats. Linear quantization moves the multiply into cheap integer hardware. Binarization goes one step further: if every weight is +1 or −1, there is nothing left to multiply by, and the computation row becomes bit operations. That is why the slide labels the last column Bit Operations rather than integer arithmetic.
اقرأ الجدول على محورين. صف التخزين يصغر من اليسار إلى اليمين، وكذلك تكلفة العمليات الحسابية. تكميم K-means يوفّر الذاكرة لكنه ما زال يضرب أعدادًا عائمة، لأن مدخلات دفتر الرموز أعداد عائمة. والتكميم الخطي ينقل الضرب إلى عتاد الأعداد الصحيحة الرخيص. أما binarization (التثنئة) فتذهب خطوة أبعد: إذا كان كل وزن +1 أو −1 فلا يبقى شيء نضرب به، ويصبح صف الحساب عمليات على البتات. لهذا تسمّي الشريحة العمود الأخير Bit Operations (عمليات البتات) وليس حسابًا بالأعداد الصحيحة.
Look closely at the binary matrix on the recap slide: it shows 0 and 1, not −1 and +1. That is the stored bit pattern. The bit 1 encodes +1 and the bit 0 encodes −1. Row one, 1 0 1 1, is exactly the sign pattern +1 −1 +1 +1 of 2.09, −0.98, 1.48, 0.09, and the same holds for every row of the BWN matrix you will build in the last concept. Keep this encoding in mind, since part 07 uses it to turn dot products into XNOR and popcount.
انظر جيدًا إلى المصفوفة الثنائية في شريحة المراجعة: إنها تُظهر 0 و1، لا −1 و+1. هذا هو نمط البتات المخزّن. البت 1 يمثّل +1 والبت 0 يمثّل −1. الصف الأول 1 0 1 1 هو بالضبط نمط الإشارات +1 −1 +1 +1 للقيم 2.09, −0.98, 1.48, 0.09، والأمر نفسه ينطبق على كل صف في مصفوفة BWN التي ستبنيها في المفهوم الأخير. احفظ هذا الترميز في ذهنك، لأن الجزء 07 يستخدمه لتحويل حاصل الضرب النقطي إلى XNOR وpopcount.
Recall
Without looking, fill in the storage and computation rows for K-means, linear and binary quantization.دون النظر، املأ صفّي التخزين والحساب لتكميم K-means والتكميم الخطي والتكميم الثنائي.
K-means: integer indices and a floating-point codebook, floating-point arithmetic. Linear: integer weights, integer arithmetic. Binary or ternary: binary or ternary weights, bit operations. Only the last two change the arithmetic; K-means only saves memory.
K-means: فهارس صحيحة ودفتر رموز عائم، وحساب بالأعداد العائمة. الخطي: أوزان صحيحة وحساب بالأعداد الصحيحة. الثنائي أو الثلاثي: أوزان ثنائية أو ثلاثية وعمليات على البتات. الأخيران فقط يغيّران طريقة الحساب، أما K-means فيوفّر الذاكرة فقط.
Quick checkاختبار سريع
Which scheme stores integer codes yet still computes with floating-point arithmetic?أي طريقة تخزّن رموزًا صحيحة لكنها ما زالت تحسب بالفاصلة العائمة؟
Every fully connected or convolution layer reduces to dot products. Take one output neuron with weights [8, −3, 5, −1] and inputs [5, 2, 0, 1].
كل طبقة كاملة الاتصال (fully connected) أو طبقة التفاف (convolution) تُختزل إلى حواصل ضرب نقطية. خذ عصبون إخراج واحدًا أوزانه [8, −3, 5, −1] ومدخلاته [5, 2, 0, 1].
Now replace each weight by its sign: [+1, −1, +1, −1]. Multiplying by +1 is just keeping the input, and multiplying by −1 is just negating it. The product terms collapse to the inputs themselves, added or subtracted.
الآن استبدل كل وزن بإشارته: [+1, −1, +1, −1]. الضرب في +1 يعني ببساطة إبقاء المدخل كما هو، والضرب في −1 يعني ببساطة عكس إشارته. فتنهار حدود الضرب إلى المدخلات نفسها، مجموعة أو مطروحة.
Notice that the answer changed from 33 to 2. Signs alone throw away magnitudes, so the raw binary output is not an approximation of the real one in scale. The last concept of this part fixes this with a scale factor: α = (8 + 3 + 5 + 1)/4 = 4.25 would give 4.25 × 2 = 8.5, and the multiply by α happens once per output, not once per weight.
لاحظ أن الناتج تغيّر من 33 إلى 2. الإشارات وحدها تتخلص من المقادير، لذلك فإن الناتج الثنائي الخام ليس تقريبًا للناتج الحقيقي من حيث الحجم. يعالج المفهوم الأخير في هذا الجزء ذلك بمعامل مقياس: القيمة α = (8 + 3 + 5 + 1)/4 = 4.25 تعطي 4.25 × 2 = 8.5، والضرب في α يحدث مرة واحدة لكل مخرج، لا مرة لكل وزن.
Where the 32x and 2x come fromمن أين يأتي الرقمان 32x و2x
| Inputالمدخل | Weightالوزن | Operationsالعمليات | Memoryالذاكرة | Computationالحساب |
|---|---|---|---|---|
| ℝℝ | ℝℝ | + and ×+ و× | 1×1× | 1×1× |
| ℝℝ | 𝔹𝔹 | + and −+ و− | ≈32× lessأقل بنحو 32× | ≈2× lessأقل بنحو 2× |
- Memory, about 32×. An FP32 weight is 32 bits and a binary weight is 1 bit. It is "about" because a few full-precision numbers remain, such as a scale per layer or filter. For a layer of 1,000,000 weights plus one FP32 scale, the ratio is 32,000,000 / 1,000,032 ≈ 31.999.الذاكرة، نحو 32×. الوزن بصيغة FP32 يشغل 32 بت والوزن الثنائي يشغل 1 بت. نقول "نحو" لأن بعض الأعداد بالدقة الكاملة تبقى، مثل مقياس لكل طبقة أو لكل مرشّح (filter). لطبقة فيها 1,000,000 وزن ومقياس واحد بصيغة FP32، تكون النسبة 32,000,000 / 1,000,032 ≈ 31.999.
- Computation, about 2×. A dot product of length n costs n multiplications and about n additions. Binary weights delete the multiplications and keep the additions and subtractions, so the operation count roughly halves. The real saving in energy and chip area is larger, because a multiplier costs far more than an adder; BinaryConnect makes exactly this point.الحساب، نحو 2×. حاصل الضرب النقطي بطول n يكلّف n عملية ضرب ونحو n عملية جمع. الأوزان الثنائية تحذف عمليات الضرب وتُبقي الجمع والطرح، لذلك ينخفض عدد العمليات إلى النصف تقريبًا. أما التوفير الحقيقي في الطاقة ومساحة الشريحة فأكبر من ذلك، لأن الضارب (multiplier) يكلّف أكثر بكثير من الجامع (adder)، وهذه بالضبط النقطة التي تؤكدها ورقة BinaryConnect.
- Why not more than 2×? The inputs are still real numbers, so the additions are still full precision. Only when activations are binarized as well (the third row of the table, filled in part 07) do the additions become XNOR and popcount.لماذا لا يزيد عن 2×؟ المدخلات ما زالت أعدادًا حقيقية، لذلك تبقى عمليات الجمع بالدقة الكاملة. فقط عندما تُثنّى التنشيطات أيضًا (الصف الثالث في الجدول، الذي يُملأ في الجزء 07) تتحول عمليات الجمع إلى XNOR وpopcount.
Worked exampleمثال محلول
Memory of the slide's 4 x 4 matrixذاكرة مصفوفة الشريحة 4 x 4
FP32FP32
16 × 32 = 512 bits.16 × 32 = 512 بت.Binary weights onlyأوزان ثنائية فقط
16 × 1 = 16 bits, exactly 32× less.16 × 1 = 16 بت، أي أقل بمقدار 32× بالضبط.Binary weights plus one FP32 scaleأوزان ثنائية مع مقياس واحد بصيغة FP32
16 + 32 = 48 bits, so 512 / 48 ≈ 10.7×. The scale dominates in a tiny matrix but is negligible in a real layer.16 + 32 = 48 بت، إذن 512 / 48 ≈ 10.7×. المقياس يطغى في مصفوفة صغيرة جدًا، لكنه مهمل في طبقة حقيقية.Resultالنتيجة
The "about 32×" holds for real layer sizes, where one scale is spread over many weights.عبارة "نحو 32×" صحيحة لأحجام الطبقات الحقيقية، حيث يتوزع مقياس واحد على عدد كبير من الأوزان.
Recall
Why is the computation saving only about 2x while the memory saving is about 32x?لماذا يكون توفير الحساب نحو 2x فقط بينما توفير الذاكرة نحو 32x؟
Memory depends only on the weight bit width, which falls from 32 to 1. Computation counts operations: the n multiplications vanish but the n additions of real inputs remain, so the count roughly halves.
الذاكرة تعتمد فقط على عرض بتات الوزن، الذي ينخفض من 32 إلى 1. أما الحساب فيعدّ العمليات: عمليات الضرب الـ n تختفي، لكن عمليات الجمع الـ n على المدخلات الحقيقية تبقى، لذلك ينخفض العدد إلى النصف تقريبًا.
Quick checkاختبار سريع
A layer has real inputs and binary weights. Which pair of savings matches the lecture?طبقة مدخلاتها حقيقية وأوزانها ثنائية. أي زوج من التوفير يطابق المحاضرة؟
Suppose a weight has the real value r = 0.4. One option says: it is positive, so it becomes +1, every time. Another option says: it is positive but not by much, so make it +1 seven times out of ten and −1 three times out of ten. These are the two binarization rules on slide 53.
افترض أن وزنًا قيمته الحقيقية r = 0.4. خيار أول يقول: إنه موجب، إذن يصبح +1 في كل مرة. وخيار ثانٍ يقول: إنه موجب لكن ليس بفارق كبير، إذن اجعله +1 سبع مرات من عشر و−1 ثلاث مرات من عشر. هاتان هما قاعدتا التثنئة في الشريحة 53.
Deterministic binarization: a threshold at zeroالتثنئة الحتمية: عتبة عند الصفر
Deterministic binarization compares the value with a threshold, almost always 0, and outputs the sign.
Deterministic binarization (التثنئة الحتمية) تقارن القيمة بعتبة، وهي 0 في الغالب تقريبًا، وتُخرج الإشارة.
The ≥ matters. Mathematically sign(0) = 0, but a binary weight has no zero level, so this convention sends r = 0 to +1. In the slide matrix the two zeros, at row 3 column 3 and row 4 column 2, both become +1. In hardware the rule is free: for a floating-point or two's complement number the sign bit already is the answer.
العلامة ≥ مهمة. رياضيًا sign(0) = 0، لكن الوزن الثنائي ليس له مستوى صفري، لذلك يرسل هذا الاصطلاح r = 0 إلى +1. في مصفوفة الشريحة يتحول الصفران، في الصف 3 العمود 3 وفي الصف 4 العمود 2، كلاهما إلى +1. وفي العتاد هذه القاعدة مجانية: في العدد العائم أو العدد بصيغة المتمم الثنائي (two's complement) يكون بت الإشارة هو الجواب نفسه.
Stochastic binarization: a coin weighted by the valueالتثنئة العشوائية: عملة مرجّحة بالقيمة
Stochastic binarization draws the bit at random, with a probability set by the value. In BinaryConnect (Courbariaux, Bengio and David, NeurIPS 2015) the probability is the hard sigmoid.
Stochastic binarization (التثنئة العشوائية) تسحب البت عشوائيًا باحتمال تحدده القيمة. في BinaryConnect (Courbariaux و Bengio و David، مؤتمر NeurIPS 2015) يكون هذا الاحتمال هو دالة hard sigmoid.
The hard sigmoid is a straight ramp clipped to [0, 1]: 0 for r ≤ −1, 1 for r ≥ 1, and the line (r + 1)/2 in between. BinaryConnect picked it over the smooth sigmoid because it is far cheaper to compute, in software and in hardware.
دالة hard sigmoid منحدر مستقيم مقصوص إلى المجال [0, 1]: قيمتها 0 عندما r ≤ −1، و1 عندما r ≥ 1، والخط (r + 1)/2 بينهما. اختارتها BinaryConnect بدل دالة sigmoid الناعمة لأنها أرخص بكثير في الحساب، برمجيًا وفي العتاد.
Worked exampleمثال محلول
Stochastic binarization of four weightsالتثنئة العشوائية لأربعة أوزان
r = 0.4r = 0.4
σ = (0.4 + 1)/2 = 0.7. So q = +1 with probability 0.7, −1 with 0.3.σ = (0.4 + 1)/2 = 0.7. إذن q = +1 باحتمال 0.7، و−1 باحتمال 0.3.r = −0.4r = −0.4
σ = 0.6/2 = 0.3. Mostly −1, but +1 three times in ten.σ = 0.6/2 = 0.3. غالبًا −1، لكن +1 ثلاث مرات من عشر.r = 0r = 0
σ = 0.5: a fair coin. Compare with the deterministic rule, which always gives +1.σ = 0.5: عملة عادلة. قارن ذلك بالقاعدة الحتمية التي تعطي دائمًا +1.r = 1.5 and r = −2r = 1.5 و r = −2
Clipped: σ(1.5) = 1 and σ(−2) = 0, so both are certain and agree with sign(r).مقصوصتان: σ(1.5) = 1 وσ(−2) = 0، إذن كلتاهما مؤكدة وتتفق مع sign(r).Resultالنتيجة
Inside [−1, 1] the expected bit is E[q] = p − (1 − p) = 2p − 1 = r. For r = 0.4, E[q] = 0.7 − 0.3 = 0.4.داخل المجال [−1, 1] تكون القيمة المتوقعة للبت E[q] = p − (1 − p) = 2p − 1 = r. عند r = 0.4: E[q] = 0.7 − 0.3 = 0.4.
That last line is the whole reason for the randomness. On average the stochastic bit equals the real weight, so rounding errors on different weights and different mini-batches cancel instead of piling up, in the same spirit as DropConnect noise. BinaryConnect also clips the stored real weights to [−1, 1] after each update, which is exactly the range where E[q] = r holds.
هذا السطر الأخير هو السبب الكامل للعشوائية. في المتوسط يساوي البت العشوائي الوزن الحقيقي، لذلك تُلغي أخطاء التقريب في الأوزان المختلفة والدفعات الصغيرة (mini-batches) المختلفة بعضها بعضًا بدل أن تتراكم، بروح ضجيج DropConnect نفسها. كما تقصّ BinaryConnect الأوزان الحقيقية المخزّنة إلى المجال [−1, 1] بعد كل تحديث، وهو بالضبط المجال الذي تتحقق فيه E[q] = r.
Drag r below and watch p, 1 − p and the expected value move, while the deterministic answer only flips at zero.
اسحب r في الأسفل وراقب كيف تتحرك p و1 − p والقيمة المتوقعة، بينما لا يتغير الجواب الحتمي إلا عند الصفر.
| Deterministicالحتمية | Stochastic (BinaryConnect)العشوائية (BinaryConnect) | |
|---|---|---|
| Ruleالقاعدة | q = sign(r)q = sign(r) | q = +1 with p = σ(r), else −1q = +1 باحتمال p = σ(r)، وإلا −1 |
| Same input twiceالمدخل نفسه مرتين | Same output every timeالمخرج نفسه في كل مرة | Can give different outputsقد يعطي مخرجات مختلفة |
| Value at r = 0القيمة عند r = 0 | +1+1 | +1 or −1, each with p = 0.5+1 أو −1، لكل منهما p = 0.5 |
| Expected valueالقيمة المتوقعة | sign(r)sign(r) | 2p − 1 = r for |r| ≤ 12p − 1 = r عندما |r| ≤ 1 |
| Hardware costتكلفة العتاد | One comparator (read the sign bit)مقارن واحد (قراءة بت الإشارة) | Needs a random number per weightيحتاج رقمًا عشوائيًا لكل وزن |
| Typical useالاستخدام المعتاد | Inference and most trainingالاستدلال ومعظم التدريب | Training as a regularizer (BinaryConnect)التدريب كمنظِّم (BinaryConnect) |
Why the deterministic rule wins in practiceلماذا تفوز القاعدة الحتمية عمليًا
The BinaryNet paper (Courbariaux et al., 2016) states the trade-off plainly: stochastic binarization is more appealing in theory, but harder to implement because the hardware must generate random bits every time it quantizes. A random number generator per weight is area, energy and latency that a tiny edge accelerator would rather not spend. So inference uses sign, and the stochastic form, if used at all, lives in training, where it acts as a regularizer.
ورقة BinaryNet (Courbariaux وزملاؤه، 2016) تذكر المفاضلة بوضوح: التثنئة العشوائية أكثر جاذبية نظريًا، لكنها أصعب في التنفيذ لأن العتاد يجب أن يولّد بتات عشوائية في كل مرة يكمّم فيها. مولّد أرقام عشوائية لكل وزن يعني مساحة وطاقة وزمن تأخير يفضّل مسرّع طرفي صغير ألا يدفعها. لذلك يستخدم الاستدلال (inference) دالة sign، أما الصيغة العشوائية، إن استُخدمت أصلًا، فمكانها التدريب حيث تعمل كمنظِّم (regularizer).
Recall
What is P(q = +1) for r = -0.6, r = 0 and r = 3 under stochastic binarization?ما قيمة P(q = +1) عند r = -0.6 و r = 0 و r = 3 في التثنئة العشوائية؟
σ(−0.6) = 0.4/2 = 0.2, σ(0) = 0.5, and σ(3) = min(2, 1) = 1. Deterministic binarization would give −1, +1 and +1.
σ(−0.6) = 0.4/2 = 0.2، وσ(0) = 0.5، وσ(3) = min(2, 1) = 1. أما التثنئة الحتمية فستعطي −1 و+1 و+1.
Quick checkاختبار سريع
Why does the lecture call stochastic binarization harder to implement than deterministic binarization?لماذا تصف المحاضرة التثنئة العشوائية بأنها أصعب في التنفيذ من التثنئة الحتمية؟
Binarize four small, realistic weights: [0.02, −0.05, 0.03, −0.01]. The signs are [+1, −1, +1, −1], and the squared error is 0.98² + 0.95² + 0.97² + 0.99² ≈ 3.78. The weights themselves only have energy 0.0039, so setting them all to zero would be about a thousand times closer. The signs are right; the size is absurd. A single scale fixes it: α = (0.02 + 0.05 + 0.03 + 0.01)/4 = 0.0275 gives ±0.0275 and an error of only 0.000875.
ثنِّ أربعة أوزان صغيرة واقعية: [0.02, −0.05, 0.03, −0.01]. الإشارات هي [+1, −1, +1, −1]، والخطأ المربّع 0.98² + 0.95² + 0.97² + 0.99² ≈ 3.78. أما طاقة الأوزان نفسها فلا تتجاوز 0.0039، لذلك فإن جعلها كلها أصفارًا سيكون أقرب بنحو ألف مرة. الإشارات صحيحة، لكن الحجم سخيف. مقياس واحد يصلح ذلك: α = (0.02 + 0.05 + 0.03 + 0.01)/4 = 0.0275 يعطي ±0.0275 وخطأً قدره 0.000875 فقط.
The Binary Weight Network ruleقاعدة Binary Weight Network
The Binary Weight Network (BWN) of XNOR-Net (Rastegari et al., ECCV 2016) approximates the real tensor as a scale times a sign tensor. The sign is kept from deterministic binarization, and the scale is the mean absolute value.
شبكة Binary Weight Network في ورقة XNOR-Net (Rastegari وزملاؤه، مؤتمر ECCV 2016) تقرّب الموتر الحقيقي بمقياس مضروب في موتر إشارات. الإشارة مأخوذة من التثنئة الحتمية، والمقياس هو متوسط القيم المطلقة.
The scale is stored as one FP32 number and applied once to the output of the dot product, so it costs one multiply per output, not per weight. The XNOR-Net paper later reuses the same idea for activations.
يُخزَّن المقياس كعدد واحد بصيغة FP32 ويُطبَّق مرة واحدة على ناتج حاصل الضرب النقطي، لذلك يكلّف عملية ضرب واحدة لكل مخرج، لا لكل وزن. وتعيد ورقة XNOR-Net لاحقًا استخدام الفكرة نفسها مع التنشيطات.
Worked exampleمثال محلول
BWN on the slide's 4 x 4 matrixBWN على مصفوفة الشريحة 4 x 4
Write down Wاكتب W
[[2.09, −0.98, 1.48, 0.09], [0.05, −0.14, −1.08, 2.12], [−0.91, 1.92, 0, −1.03], [1.87, 0, 1.53, 1.49]][[2.09, −0.98, 1.48, 0.09], [0.05, −0.14, −1.08, 2.12], [−0.91, 1.92, 0, −1.03], [1.87, 0, 1.53, 1.49]]Binarize with signثنِّ باستخدام sign
W^B = [[1, −1, 1, 1], [1, −1, −1, 1], [−1, 1, 1, −1], [1, 1, 1, 1]]. The zeros at (3, 3) and (4, 2) become +1.W^B = [[1, −1, 1, 1], [1, −1, −1, 1], [−1, 1, 1, −1], [1, 1, 1, 1]]. الصفران في الموضعين (3, 3) و(4, 2) يصبحان +1.L1 normنُظيم L1
Row sums of absolute values: 4.64 + 3.39 + 3.86 + 4.89 = 16.78.مجاميع القيم المطلقة لكل صف: 4.64 + 3.39 + 3.86 + 4.89 = 16.78.Scaleالمقياس
α = 16.78 / 16 = 1.04875 ≈ 1.05.α = 16.78 / 16 = 1.04875 ≈ 1.05.Error without the scaleالخطأ دون المقياس
‖W − W^B‖²_F = 9.28 (exactly 9.2832).‖W − W^B‖²_F = 9.28 (بالضبط 9.2832).Error with the scaleالخطأ مع المقياس
‖W − αW^B‖²_F = 9.245 (exactly 9.2452).‖W − αW^B‖²_F = 9.245 (بالضبط 9.2452).Resultالنتيجة
The scale always helps or ties. Here the gain is small because the mean magnitude 1.05 is already close to 1; for real weights, which are typically far smaller than 1, the gain is huge, as the opening example showed.المقياس دائمًا يحسّن الخطأ أو يساويه. هنا المكسب صغير لأن متوسط المقدار 1.05 قريب أصلًا من 1، أما في الأوزان الحقيقية، التي تكون عادة أصغر بكثير من 1، فالمكسب هائل، كما أظهر المثال الافتتاحي.
| Approximationالتقريب | Scaleالمقياس | Squared errorالخطأ المربّع | Whyالسبب |
|---|---|---|---|
| sign(W)sign(W) | 11 | 9.289.28 | Every cell is ±1, far from 2.09 or 0.05كل خلية ±1، بعيدة عن 2.09 أو 0.05 |
| α · sign(W)α · sign(W) | 1.048751.04875 | 9.2459.245 | Cells are ±1.05, the best a single scale can doالخلايا ±1.05، أفضل ما يستطيعه مقياس واحد |
Why alpha is the mean of |W|لماذا تكون alpha متوسط |W|
This is the derivation from XNOR-Net and a natural exam question. Write the error for a fixed sign vector B ∈ {+1, −1}ⁿ and expand.
هذا هو الاشتقاق من ورقة XNOR-Net، وهو سؤال امتحان طبيعي. اكتب الخطأ لمتجه إشارات ثابت B ∈ {+1, −1}ⁿ ثم افكّه.
- For any positive α, the error is smallest when WᵀB is largest. Each term w_i b_i is largest when b_i has the sign of w_i, so B* = sign(W) and WᵀB* = Σ|w_i| = ‖W‖₁.لأي α موجبة، يكون الخطأ أصغر ما يمكن عندما يكون WᵀB أكبر ما يمكن. كل حد w_i b_i يكون أكبر ما يمكن عندما تحمل b_i إشارة w_i، إذن B* = sign(W) وWᵀB* = Σ|w_i| = ‖W‖₁.
- Differentiate in α: dJ/dα = 2αn − 2‖W‖₁. Setting it to zero gives α* = ‖W‖₁ / n, the mean absolute weight. The second derivative 2n is positive, so this is a minimum.اشتق بالنسبة إلى α: dJ/dα = 2αn − 2‖W‖₁. مساواتها بالصفر تعطي α* = ‖W‖₁ / n، أي متوسط القيمة المطلقة للأوزان. والمشتقة الثانية 2n موجبة، إذن هذه قيمة صغرى.
- Substituting back gives the minimum error J* = WᵀW − ‖W‖₁² / n. On the slide matrix: 26.8432 − 16.78² / 16 = 26.8432 − 17.5980 = 9.2452, matching the worked example.بالتعويض نحصل على أصغر خطأ J* = WᵀW − ‖W‖₁² / n. على مصفوفة الشريحة: 26.8432 − 16.78² / 16 = 26.8432 − 17.5980 = 9.2452، وهو مطابق للمثال المحلول.
What the scale buys on ImageNetماذا يكسب المقياس على ImageNet
XNOR-Net trained AlexNet three ways on ImageNet. Full precision reached 56.6% top-1. BinaryConnect, signs with no scale, reached 35.4%, a drop of 21.2 points. BWN, the same signs plus α, reached 56.8%, 0.2 points above full precision. The paper also tried learning the scale as a separate layer instead of computing it with the formula and got only 46.2%, so the closed-form α is not a detail.
درّبت ورقة XNOR-Net شبكة AlexNet بثلاث طرق على ImageNet. الدقة الكاملة وصلت إلى 56.6% في top-1. وBinaryConnect، أي إشارات بلا مقياس، وصلت إلى 35.4%، بانخفاض 21.2 نقطة. وBWN، أي الإشارات نفسها مع α، وصلت إلى 56.8%، أي أعلى من الدقة الكاملة بـ 0.2 نقطة. وجرّبت الورقة أيضًا تعلّم المقياس كطبقة مستقلة بدل حسابه بالصيغة، فلم تحصل إلا على 46.2%، لذلك فإن α بالصيغة المغلقة ليست تفصيلًا ثانويًا.
Recall
Compute alpha and the sign vector for W = [0.6, -0.2, 0, -1.0].احسب alpha ومتجه الإشارات للمتجه W = [0.6, -0.2, 0, -1.0].
sign(W) = [+1, −1, +1, −1] (the zero becomes +1). α = (0.6 + 0.2 + 0 + 1.0)/4 = 0.45, so αW^B = [0.45, −0.45, 0.45, −0.45].
sign(W) = [+1, −1, +1, −1] (الصفر يصبح +1). α = (0.6 + 0.2 + 0 + 1.0)/4 = 0.45، إذن αW^B = [0.45, −0.45, 0.45, −0.45].
Recall
Why does BWN almost match full precision on AlexNet while BinaryConnect loses 21.2 points?لماذا تكاد BWN تطابق الدقة الكاملة على AlexNet بينما تخسر BinaryConnect نحو 21.2 نقطة؟
Both use the same signs, but BinaryConnect has no scale, so every weight is forced to magnitude 1. BWN multiplies by α = mean |W|, the error-minimizing scale, which restores the right magnitude per filter and closes the gap.
كلتاهما تستخدم الإشارات نفسها، لكن BinaryConnect بلا مقياس، فيُجبَر كل وزن على المقدار 1. أما BWN فتضرب في α = mean |W|، المقياس الذي يجعل الخطأ أصغر ما يمكن، فتستعيد المقدار الصحيح لكل مرشّح وتسدّ الفجوة.
Quick checkاختبار سريع
For W = [2, -1, 0, -1], what are alpha and the binarized weights alpha times sign(W)?للمتجه W = [2, -1, 0, -1]، ما قيمة alpha وما الأوزان المثنّاة alpha مضروبة في sign(W)؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Binary and ternary quantization store 1 or 2 bit weights and compute with bit operations, the far end of the table that starts at FP32 and passes through K-means and linear quantization.التكميم الثنائي والثلاثي يخزّن أوزانًا من 1 أو 2 بت ويحسب بعمليات البتات، وهو الطرف الأقصى من الجدول الذي يبدأ بـ FP32 ويمر بتكميم K-means والتكميم الخطي.
- Real dot product: 8x5 + (-3)x2 + 5x0 + (-1)x1 = 33. With weights [1, -1, 1, -1] it becomes 5 - 2 + 0 - 1 = 2, using only additions and subtractions.حاصل الضرب النقطي الحقيقي: 8x5 + (-3)x2 + 5x0 + (-1)x1 = 33. ومع الأوزان [1, -1, 1, -1] يصبح 5 - 2 + 0 - 1 = 2، باستخدام الجمع والطرح فقط.
- Real input, binary weight: about 32x less weight memory (32 bits to 1) and about 2x less computation (the n multiplications vanish, the n additions stay).مدخلات حقيقية وأوزان ثنائية: ذاكرة أوزان أقل بنحو 32x (من 32 بت إلى 1) وحساب أقل بنحو 2x (تختفي عمليات الضرب الـ n وتبقى عمليات الجمع الـ n).
- Deterministic: q = sign(r), +1 for r >= 0 and -1 for r < 0, so zero maps to +1.الحتمية: q = sign(r)، أي +1 عندما r >= 0 و -1 عندما r < 0، لذلك يتحول الصفر إلى +1.
- Stochastic (BinaryConnect): q = +1 with probability sigma(r) = min(max((r + 1)/2, 0), 1). At r = 0.4, p = 0.7. E[q] = r inside [-1, 1]. Needs random bits, so it is harder in hardware.العشوائية (BinaryConnect): q = +1 باحتمال sigma(r) = min(max((r + 1)/2, 0), 1). عند r = 0.4 يكون p = 0.7. وE[q] = r داخل [-1, 1]. تحتاج بتات عشوائية، لذلك هي أصعب في العتاد.
- BWN: W^B = sign(W), alpha = (1/n)||W||_1. On the slide matrix sum |W| = 16.78, alpha = 1.04875 (about 1.05), error 9.28 for sign only and 9.245 with alpha.BWN: W^B = sign(W) و alpha = (1/n)||W||_1. في مصفوفة الشريحة sum |W| = 16.78 و alpha = 1.04875 (تقريبًا 1.05)، والخطأ 9.28 بالإشارة وحدها و 9.245 مع alpha.
- alpha = mean |W| comes from setting d/d alpha of ||W - alpha sign(W)||^2 = 2 alpha n - 2 ||W||_1 to zero.تأتي alpha = mean |W| من مساواة مشتقة ||W - alpha sign(W)||^2 بالنسبة إلى alpha، وهي 2 alpha n - 2 ||W||_1، بالصفر.
- AlexNet on ImageNet, top-1 versus full precision: BinaryConnect -21.2 points, BWN +0.2 points.شبكة AlexNet على ImageNet، دقة top-1 مقارنة بالدقة الكاملة: BinaryConnect تخسر 21.2 نقطة، و BWN تكسب 0.2 نقطة.
Sourcesالمصادر
- BinaryConnect: Training Deep Neural Networks with binary weights during propagationsPaperCourbariaux, Bengio and David, NeurIPS 2015Deterministic sign with w >= 0 to +1, stochastic binarization with the hard sigmoid clip((x+1)/2, 0, 1), multiply-accumulate replaced by additions and subtractions, weight clipping to [-1, 1]الإشارة الحتمية مع تحويل w >= 0 إلى +1، والتثنئة العشوائية بدالة hard sigmoid أي clip((x+1)/2, 0, 1)، واستبدال الضرب والتجميع بعمليات جمع وطرح، وقصّ الأوزان إلى [-1, 1](opens in a new tab)(يفتح في تبويب جديد)
- Binarized Neural Networks: Training Deep Neural Networks with Weights and Activations Constrained to +1 or -1PaperCourbariaux, Hubara, Soudry, El-Yaniv and Bengio, 2016Stochastic binarization is harder to implement because hardware must generate random bits, so the deterministic sign is usedالتثنئة العشوائية أصعب في التنفيذ لأن العتاد يجب أن يولّد بتات عشوائية، لذلك تُستخدم الإشارة الحتمية(opens in a new tab)(يفتح في تبويب جديد)
- XNOR-Net: ImageNet Classification Using Binary Convolutional Neural NetworksPaperRastegari, Ordonez, Redmon and Farhadi, ECCV 2016BWN derivation B* = sign(W), alpha* = ||W||_1 / n, about 32x memory saving, AlexNet top-1: BWN 56.8, BC 35.4, full precision 56.6, separate-layer alpha 46.2اشتقاق BWN: B* = sign(W) و alpha* = ||W||_1 / n، وتوفير في الذاكرة بنحو 32x، ودقة top-1 لشبكة AlexNet: BWN 56.8 و BC 35.4 والدقة الكاملة 56.6 و alpha كطبقة مستقلة 46.2(opens in a new tab)(يفتح في تبويب جديد)
- A Survey of Quantization Methods for Efficient Neural Network InferencePaperGholami, Kim, Dong, Yao, Mahoney and Keutzer, 2021Section on extreme quantization: binarization and ternarization, BinaryConnect, BWN and XNOR-Net in contextقسم التكميم المتطرف: التثنئة والتثليث، و BinaryConnect و BWN و XNOR-Net في سياقها(opens in a new tab)(يفتح في تبويب جديد)