Lecture 02 · NN Fundamentals
المحاضرة 02 · أساسيات الشبكات العصبية
Activation functions for embedded nets
دوال التنشيط (activation functions) للشبكات المدمجة
P2 SHOULDSix formulas on one slide. For this course the question is not just "which bends" but "which is cheap and quantizes well on a microcontroller". Two answers: ReLU6 for its bounded range, hard swish for having no exponential.
ست معادلات في شريحة واحدة. وفي هذا المقرر السؤال ليس فقط "أيها غير خطي" بل "أيها رخيص ويتحمّل quantization جيداً على متحكم دقيق". والجواب اثنان: ReLU6 لأن مداه محدود، وhard swish لأنه لا يحتوي على دالة أُسية.
Why there has to be a non-linearity
لماذا لا بد من دالة غير خطية
Slide 40 draws the neuron: yj = f(Σi wixi + b). The f is the activation. Remove it and two layers become one, because a linear map of a linear map is linear:
ترسم الشريحة 40 الخلية العصبية: yj = f(Σi wixi + b). والدالة f هي دالة التنشيط (activation). احذفها وتصبح الطبقتان طبقة واحدة، لأن الدالة الخطية لدالة خطية هي دالة خطية:
W2(W1x + b1) + b2 = (W2W1)x + (W2b1 + b2)
scalar layers, no activation layer 1 h = 2x + 1 layer 2 y = 3h + 4 = 3(2x + 1) + 4 = 6x + 7 one layer with w = 6, b = 7 does exactly the same job. a hundred stacked linear layers are still one line.
طبقتان عدديتان بلا تنشيط: h = 2x + 1 ثم y = 3h + 4 = 6x + 7. طبقة واحدة بـ w = 6 وb = 7 تؤدي العمل نفسه تماماً. ومئة طبقة خطية مكدّسة تبقى خطاً مستقيماً واحداً.
The six on slide 40
الدوال الست في الشريحة 40
| Nameالاسم | Formulaالمعادلة | Rangeالمدى | Derivativeالمشتقة | Cost on an MCUالتكلفة على MCU |
|---|---|---|---|---|
| Sigmoid | 1 / (1 + e−x) | (0, 1) | s(1 − s), max 0.25s(1 − s)، أقصاها 0.25 | exponentialدالة أسية |
| ReLU | max(0, x) | [0, ∞) | 0 or 10 أو 1 | one compareمقارنة واحدة |
| ReLU6 | min(max(0, x), 6) | [0, 6] | 1 on (0, 6), else 01 على (0, 6)، وإلا 0 | two comparesمقارنتان |
| Leaky ReLU | max(αx, x) | (−∞, ∞) | α or 1α أو 1 | compare, multiplyمقارنة وضرب |
| Swish | x / (1 + e−x) | [−0.278, ∞) | s + x s(1 − s) | exponentialدالة أسية |
| Hard swishHard swish | x · ReLU6(x + 3) / 6 | [−0.375, ∞) | 0, (2x + 3)/6, or 10، أو (2x + 3)/6، أو 1 | clamp, multiplyقصّ (clamp) وضرب |
يجمع الجدول الدوال الست. Sigmoid مداه (0, 1) ومشتقته s(1 − s) لا تتجاوز 0.25 ويحتاج دالة أُسية. ReLU هو max(0, x) ومداه غير محدود من الأعلى. ReLU6 هو min(max(0, x), 6) ومداه [0, 6]. Leaky ReLU هو max(αx, x). Swish هو x / (1 + e−x) ويحتاج دالة أُسية. وhard swish هو x · ReLU6(x + 3) / 6 ويكفيه قص وضرب. المكتوب بلون مميز هو ما يهم الأنظمة المدمجة.
The slide's piecewise hard swish says the same thing: 0 for x ≤ −3, x for x ≥ 3, and x(x + 3)/6 in between. The slide also lists tanh, GELU, ELU and Mish as "other activation functions".
الصيغة المجزّأة لـ hard swish في الشريحة تقول الشيء نفسه: 0 عندما x ≤ −3، وx عندما x ≥ 3، وx(x + 3)/6 بينهما. وتذكر الشريحة أيضاً tanh وGELU وELU وMish كدوال تنشيط أخرى.
في اللوحة الأولى يتسطّح sigmoid من الجهتين، ومشتقته (المنقّطة) قمتها 0.25 عند الصفر. في اللوحة الثانية ReLU6 هو ReLU مع سقف عند 6. وفي الثالثة يرسم hard swish منحنى swish بقطع مستقيمة ومنحنى تربيعي، والفرق بينهما أقل من 0.15 في كل مكان.
Why each one matters on embedded hardware
لماذا تهمّ كل دالة على العتاد المدمج
Sigmoid: saturation kills gradients
Sigmoid: التشبّع يقتل التدرّجات
At x = 0 the slope is s(1 − s) = 0.5 × 0.5 = 0.25, the maximum. At x = −4 it is already 0.018 × 0.982 = 0.018. Backpropagation multiplies one such factor per layer, so ten sigmoid layers shrink the gradient by at least 0.2510 ≈ 10−6. It also needs ex, which a small MCU computes with a slow library call or a lookup table.
عند x = 0 يكون الميل s(1 − s) = 0.5 × 0.5 = 0.25، وهو أعلى قيمة. وعند x = −4 يصبح 0.018 × 0.982 = 0.018. والـ backpropagation يضرب عاملاً كهذا لكل طبقة، فعشر طبقات sigmoid تُصغّر الـ gradient بمقدار 0.2510 ≈ 10−6 على الأقل. ويحتاج أيضاً إلى ex، الذي يحسبه المتحكم الصغير باستدعاء مكتبة بطيء أو بجدول بحث.
ReLU: the default
ReLU: الخيار الافتراضي
max(0, x) is one comparison, its slope is 1 for every positive input so gradients do not shrink, and it outputs exact zeros, which later lectures exploit as activation sparsity. Its weakness for int8: the range is unbounded, so the quantization range has to be measured from data.
max(0, x) مقارنة واحدة فقط، وميله 1 لكل مدخل موجب فلا يتقلّص الـ gradient، ويُخرج أصفاراً حقيقية تستغلها المحاضرات اللاحقة كـ activation sparsity. نقطة ضعفه مع int8: مداه غير محدود، فلا بد من قياس مدى الـ quantization من البيانات.
ReLU6: a fixed int8 grid
ReLU6: شبكة int8 ثابتة
Capping at 6 means the output range is always [0, 6], known before you see any data. Mapping it onto 256 int8 levels gives a step of 6/255 ≈ 0.0235 everywhere. With plain ReLU one outlier activation of 50 would stretch the step to 50/255 ≈ 0.196, more than 8 times coarser for every normal value. This robustness to low precision is why MobileNetV2 uses ReLU6.
السقف عند 6 يعني أن مدى المخرج دائماً [0, 6]، ومعروف قبل رؤية أي بيانات. وتوزيعه على 256 مستوى في int8 يعطي خطوة 6/255 ≈ 0.0235 في كل مكان. أما مع ReLU العادي فقيمة شاذة واحدة مثل 50 تمدّ الخطوة إلى 50/255 ≈ 0.196، أي أخشن بأكثر من 8 مرات لكل القيم العادية. وهذه المتانة مع الدقة المنخفضة هي سبب استخدام MobileNetV2 لـ ReLU6.
Hard swish: swish without the exponential
Hard swish: swish بلا دالة أسية
Swish x·s(x) beats ReLU on deep models but needs ex. Hard swish replaces the sigmoid with the piecewise-linear ReLU6(x + 3)/6, so it costs one add, one clamp, one multiply and a shift or constant multiply. It stays within 0.15 of swish everywhere (the worst gap, 0.142, is at x = ±3). MobileNetV3 uses it.
Swish أي x·s(x) يتفوّق على ReLU في النماذج العميقة لكنه يحتاج ex. أما hard swish فيستبدل الـ sigmoid بالدالة الخطية المجزّأة ReLU6(x + 3)/6، فتكلفته جمع وقص وضرب وضرب في ثابت. ويبقى ضمن 0.15 من swish في كل مكان (أكبر فرق 0.142 عند x = ±3). ويستخدمه MobileNetV3.
Leaky ReLU: never fully dead
Leaky ReLU: لا تموت تماماً أبداً
max(αx, x) keeps a small slope α (0.01 by default in PyTorch) for negative inputs, so a neuron that only ever sees negative inputs still receives a gradient.
max(αx, x) يحتفظ بميل صغير α (القيمة الافتراضية 0.01 في PyTorch) للمدخلات السالبة، فالخلية التي لا ترى إلا مدخلات سالبة تبقى تستقبل gradient.
Evaluate them by hand
احسبها باليد
x -4 -1 0 2 7 sigmoid 0.018 0.269 0.5 0.881 0.999 sigmoid' 0.018 0.197 0.25 0.105 0.001 ReLU 0 0 0 2 7 ReLU6 0 0 0 2 6 <- capped leaky 0.01 -0.04 -0.01 0 2 7 swish -0.072 -0.269 0 1.762 6.994 hard swish 0 -0.333 0 1.667 7 |gap| 0.072 0.064 0 0.095 0.006 how the hard swish column is built: x = -4 x + 3 = -1 ReLU6 = 0 -4 * 0 / 6 = 0 x = -1 x + 3 = 2 ReLU6 = 2 -1 * 2 / 6 = -0.333 x = 2 x + 3 = 5 ReLU6 = 5 2 * 5 / 6 = 1.667 x = 7 x + 3 = 10 ReLU6 = 6 7 * 6 / 6 = 7
قيّم كل دالة عند x = −4, −1, 0, 2, 7. لاحظ أن ReLU6 يقص 7 إلى 6، وأن مشتقة sigmoid قمتها 0.25 عند الصفر. ولحساب hard swish: أضف 3، ثم طبّق ReLU6، ثم اضرب في x واقسم على 6. مثلاً عند x = 2: 2 + 3 = 5، ثم 2 × 5 / 6 = 1.667. والفرق عن swish في كل النقاط أقل من 0.15.
"Six is the ceiling, three is the shift." ReLU6 clips at 6. Hard swish shifts by 3 before the ReLU6 and divides by 6 after, so its gate goes from 0 at x = −3 to 1 at x = +3.
"ستة هي السقف، وثلاثة هي الإزاحة." ReLU6 يقص عند 6. وhard swish يزيح بـ 3 قبل ReLU6 ويقسم على 6 بعده، فتنتقل بوابته من 0 عند x = −3 إلى 1 عند x = +3.
The sigmoid's maximum slope is 0.25, not 0.5 or 1. 0.5 is the sigmoid's value at 0. Its derivative there is 0.5 × (1 − 0.5) = 0.25.
أعلى ميل لـ sigmoid هو 0.25، لا 0.5 ولا 1. فـ 0.5 هي قيمة الدالة عند الصفر، أما مشتقتها هناك فهي 0.5 × (1 − 0.5) = 0.25.
ReLU6's bound helps quantization, not accuracy. The reason to cap at 6 is that a known, fixed range gives a fixed int8 scale. It is not chosen because it trains to higher float accuracy than ReLU.
حدّ ReLU6 يساعد الـ quantization لا الدقة. سبب القص عند 6 هو أن المدى المعروف الثابت يعطي scale ثابتاً في int8. لم يُختر لأنه يعطي دقة أعلى من ReLU بالأعداد العشرية.
Hard swish is not ReLU6. It is x times ReLU6(x + 3)/6. It goes slightly negative (minimum −0.375 at x = −1.5), and it is unbounded above.
Hard swish ليس ReLU6. بل هو x مضروباً في ReLU6(x + 3)/6. ويصبح سالباً قليلاً (أدنى قيمة −0.375 عند x = −1.5)، وهو غير محدود من الأعلى.
Everything on one card
كل شيء في بطاقة واحدة
Activation cheat card
بطاقة مراجعة دوال التنشيط
- Why: without f, W2(W1x + b1) + b2 is one linear layer.
السبب: بدون f يصبح W2(W1x + b1) + b2 طبقة خطية واحدة.
- Sigmoid: 1/(1 + e−x), range (0, 1), slope at most 0.25, saturates.
Sigmoid: 1/(1 + e−x)، مداه (0, 1)، وميله لا يتجاوز 0.25، ويتشبّع.
- ReLU: max(0, x). Default, cheap, unbounded.
ReLU: max(0, x). الافتراضي، رخيص، وغير محدود.
- ReLU6: min(max(0, x), 6). Range [0, 6], int8 step 6/255. MobileNetV2.
ReLU6: min(max(0, x), 6). مداه [0, 6]، وخطوة int8 تساوي 6/255. MobileNetV2.
- Hard swish: x · ReLU6(x + 3)/6. Within 0.15 of swish, no exponential. MobileNetV3.
Hard swish: x · ReLU6(x + 3)/6. ضمن 0.15 من swish، وبلا دالة أُسية. MobileNetV3.
- Leaky ReLU: max(αx, x), small negative slope keeps gradients alive.
Leaky ReLU: max(αx, x)، والميل السالب الصغير يُبقي الـ gradient حياً.
Check yourself
اختبر نفسك
What is the largest value the derivative of the sigmoid can take?
ما أكبر قيمة يمكن أن تأخذها مشتقة sigmoid؟
s'(x) = s(x)(1 − s(x)). At x = 0, s = 0.5, so the slope is 0.25, and it only falls from there. Option B confuses the function's value with its slope.
s'(x) = s(x)(1 − s(x)). عند x = 0 تكون s = 0.5، فالميل 0.25، ثم يتناقص بعد ذلك. والخيار B يخلط بين قيمة الدالة وميلها.
Compute hard swish at x = 1.
احسب hard swish عند x = 1.
x + 3 = 4, ReLU6(4) = 4, then 1 × 4 / 6 = 0.667. Option C is soft swish, 1 × s(1) = 0.731, so the gap here is 0.064. Option D forgets the divide by 6.
x + 3 = 4، وReLU6(4) = 4، ثم 1 × 4 / 6 = 0.667. الخيار C هو swish الناعم، 1 × s(1) = 0.731، فالفرق هنا 0.064. والخيار D نسي القسمة على 6.
Why did MobileNetV2 choose ReLU6 over ReLU?
لماذا اختار MobileNetV2 دالة ReLU6 بدلاً من ReLU؟
A fixed range gives a fixed quantization step (6/255 in uint8) that no outlier can stretch. The gradient is still 1 on (0, 6) and 0 elsewhere (A), it still outputs zeros (B), and it has two kinks, so it is not smooth (C).
المدى الثابت يعطي خطوة quantization ثابتة (6/255 في uint8) لا تستطيع أي قيمة شاذة أن تمدّها. الـ gradient ما زال 1 على (0, 6) و0 في غيره (A)، وما زال يُخرج أصفاراً (B)، وله زاويتان فهو ليس ناعماً (C).
A network has three linear layers and no activation functions. What can it represent?
شبكة فيها ثلاث طبقات خطية بلا أي دوال تنشيط. ماذا يمكنها أن تمثّل؟
The composition collapses: W3W2W1 is one matrix and the biases fold into one vector. Universal approximation (A) needs a non-linearity. Composing linear maps never raises the degree (C).
التركيب ينهار: W3W2W1 مصفوفة واحدة، والانحيازات تندمج في متجه واحد. التقريب الشامل (A) يحتاج دالة غير خطية. وتركيب دوال خطية لا يرفع الدرجة أبداً (C).
Why is hard swish preferred over swish on a microcontroller?
لماذا يُفضَّل hard swish على swish في المتحكم الدقيق؟
Swish needs e−x. Hard swish uses ReLU6(x + 3)/6 as its gate, which is just a clamp. It is unbounded above (A), differs by up to 0.142 (B), and reaches −0.375 at x = −1.5 (D).
يحتاج swish إلى e−x. أما hard swish فبوابته ReLU6(x + 3)/6، وهي مجرد قص. وهو غير محدود من الأعلى (A)، ويختلف بما يصل إلى 0.142 (B)، ويصل إلى −0.375 عند x = −1.5 (D).