Majid Al-RaimiNeural network fundamentalsأساسيات الشبكات العصبية

COE 592Lecture 02المحاضرة 02

Neural network fundamentalsأساسيات الشبكات العصبية

The building blocks every embedded model is made of: neurons, weights and activations, fully connected and convolution layers with their tensor shapes and output-size formulas, grouped and depthwise convolution, pooling, the normalization family, activation functions and the attention inside a transformer.اللبنات التي يتكون منها كل نموذج مضمّن: العصبونات والأوزان والتنشيطات (activations)، والطبقات المتصلة كليًا (fully connected) وطبقات الالتفاف (convolution) مع أشكال موتراتها (tensor shapes) وصيغ حجم المخرج، والالتفاف المجمّع (grouped) والعمقي (depthwise)، والتجميع (pooling)، وعائلة التطبيع (normalization)، ودوال التنشيط، والانتباه (attention) داخل المحوّل (transformer).

Partsالأجزاء
6
Conceptsالمفاهيم
28
Slidesالشرائح
45
Readingمدة القراءة
168 min168 دقيقة
Understoodما فهمته
0/28 conceptsمفاهيم
Read the full guideاقرأ الشرح الكاملEvery part on one long page: 6 parts, 28 concepts, about 168 min.كل الأجزاء في صفحة واحدة طويلة: 6 أجزاء، و28 مفهومًا، في نحو 168 دقيقة.

AOverviewنظرة عامة

Before you can shrink a network you have to be able to read one. This lecture is the anatomy lesson for the whole course: it takes every block a modern model is built from, the neuron, the fully connected layer, the convolution with its padding, stride and groups, pooling, the normalization family, the activation function and the transformer block, and teaches you to write each one as a tensor equation whose parameters, activations and output shapes you can count from a diagram.

قبل أن تتمكن من تصغير شبكة عصبية يجب أن تتمكن من قراءتها. هذه المحاضرة هي درس التشريح للمقرر كله: تأخذ كل وحدة بناء يتكوّن منها النموذج الحديث، وهي الخلية العصبية (neuron)، والطبقة كاملة الاتصال (fully connected layer)، والالتفاف (convolution) مع الحشو (padding) والخطوة (stride) والمجموعات (groups)، والتجميع (pooling)، وعائلة التطبيع (normalization)، ودالة التفعيل (activation function)، وكتلة المحوّل (transformer block)، وتعلّمك أن تكتب كل واحدة منها كمعادلة موترات (tensor equation) يمكنك أن تعدّ معاملاتها (parameters) وتفعيلاتها (activations) وأشكال مخرجاتها من الرسم مباشرة.

As a PhD student you need this in three places. Exams hand you a layer list and ask for shapes, parameter counts, output sizes after padding and stride, a batch norm or an attention computed by hand, and the reason a microcontroller prefers ReLU6 to swish. Your research project puts a model on a board with a few hundred kilobytes of SRAM, and every pruning, quantization and architecture decision you defend there is a decision about the counts this lecture defines. And any paper or codebase you read from now on, from MobileNet to a transformer, assumes you can look at W (c_o, c_i, k_h, k_w) or softmax(Q Kᵀ / √d_k) V and see both the arithmetic and the memory it costs.

بصفتك طالب دكتوراه تحتاج هذا في ثلاثة مواضع. الامتحانات تعطيك قائمة طبقات وتطلب الأشكال، وعدد المعاملات، وأحجام المخرجات بعد الحشو والخطوة، وحساب batch norm أو attention يدويًا، وسبب تفضيل المتحكم الدقيق (microcontroller) لـ ReLU6 على swish. ومشروعك البحثي يضع نموذجًا على لوحة فيها بضع مئات من الكيلوبايتات من SRAM، وكل قرار تقليم (pruning) وتكميم (quantization) وتصميم معمارية تدافع عنه هناك هو قرار يتعلق بالأعداد التي تعرّفها هذه المحاضرة. وأي ورقة بحثية أو قاعدة شيفرة تقرؤها من الآن فصاعدًا، من MobileNet إلى المحوّل، تفترض أنك تستطيع النظر إلى W (c_o, c_i, k_h, k_w) أو softmax(Q Kᵀ / √d_k) V فترى الحساب والذاكرة التي يكلّفها معًا.

From one neuron to the transformer blockمن خلية عصبية واحدة إلى كتلة المحوّل

The path has six stops. You start with one neuron as a weighted sum and stack neurons into fully connected layers and multilayer perceptrons, learning to count parameters against flash and activations against SRAM. You then trade full connectivity for a small shared filter and get the convolution, with its four tensors and its shrinking feature map. Padding, stride and groups follow: the three knobs that set output size, receptive field and cost without touching the idea. Pooling shrinks every map with zero parameters, and the feature hierarchy explains what the stacked filters end up learning. Normalization recentres activations so training works, then folds away at inference. The lecture closes with the activation functions an int8 device can afford and the transformer block, whose attention matrix grows with the square of the sequence length.

المسار فيه ست محطات. تبدأ بخلية عصبية واحدة كمجموع موزون، ثم تكدّس الخلايا في طبقات كاملة الاتصال وفي شبكات متعددة الطبقات (MLP)، وتتعلم أن تقارن عدد المعاملات بذاكرة flash وعدد التفعيلات بذاكرة SRAM. بعدها تستبدل الاتصال الكامل بمرشّح (filter) صغير مشترك فتحصل على الالتفاف، بموتراته الأربعة وخريطة السمات (feature map) التي تتقلص. ثم تأتي الحشو والخطوة والمجموعات: المقابض الثلاثة التي تحدد حجم المخرج والحقل الاستقبالي (receptive field) والتكلفة دون المساس بالفكرة نفسها. التجميع يصغّر كل خريطة دون أي معاملات، وهرمية السمات تشرح ما تتعلمه المرشحات المكدّسة في النهاية. التطبيع يعيد تمركز التفعيلات حتى ينجح التدريب، ثم يُدمج ويختفي عند الاستدلال (inference). وتُختتم المحاضرة بدوال التفعيل التي يقدر عليها جهاز يعمل بـ int8، وبكتلة المحوّل التي تنمو مصفوفة الانتباه فيها مع مربع طول التسلسل.

Formulas you will own by the endالصيغ التي ستتقنها في النهاية

Each row is derived, not quoted, in one of the six parts, and each is checked against a number from the slides (the 47 parameters of the 5-4-3-2 network, the 1216 of the ConvNetJS first layer, the 576 depthwise weights against 36,864 standard). They are the anchors the exam and the reference sheet return to.

كل صف يُشتق اشتقاقًا ولا يُنقل نقلًا في أحد الأجزاء الستة، وكل صف يُتحقق منه مقابل رقم من الشرائح (المعاملات الـ 47 لشبكة 5-4-3-2، والـ 1216 لأول طبقة في ConvNetJS، والأوزان الـ 576 للالتفاف العمقي (depthwise) مقابل 36,864 للالتفاف القياسي). هذه هي المراسي التي يعود إليها الامتحان وورقة المرجع.

BlockالكتلةParametersالمعاملاتOutputالمخرج
Fully connectedالطبقة كاملة الاتصالc_o × c_i + c_o(n, c_o)
Conv2DConv2Dc_o × c_i × k_h × k_w + c_oh_o = floor((h_i + 2p - k_h) / s) + 1
Grouped convالالتفاف المجمّعc_o × (c_i / g) × k_h × k_w + c_osame rule as Conv2Dنفس قاعدة Conv2D
Depthwise convالالتفاف العمقيc × k_h × k_w + csame rule, c_o = c_i = cنفس القاعدة، مع c_o = c_i = c
Poolingالتجميع0h_o = (h_i - F) / S + 1, channels kepth_o = (h_i - F) / S + 1، وعدد القنوات لا يتغير
Batch normتطبيع الدفعة2C (gamma and beta)2C (gamma وbeta)unchanged, folds into the layer beforeلا يتغير، ويُدمج في الطبقة التي قبله
Attention headرأس الانتباه3 × d_model × d_k (W_Q, W_K, W_V)3 × d_model × d_k (W_Q, W_K, W_V)scores N × N, output N × d_vالدرجات N × N، والمخرج N × d_v
What each block stores and what shape it producesما تخزّنه كل كتلة والشكل الذي تُنتجه

Success looks likeعلامات النجاح

  • Write a neuron as y = f(Σ w_i x_i + b), name every symbol in both the biological and the tensor vocabulary, and count layers without counting the input.أن تكتب الخلية العصبية على صورة y = f(Σ w_i x_i + b)، وتسمّي كل رمز بمفردات علم الأحياء ومفردات الموترات معًا، وتعدّ الطبقات دون أن تعدّ طبقة المدخل.
  • State the shapes X (n, c_i), W (c_o, c_i), b (c_o,), Y (n, c_o) of a fully connected layer, and say which count the batch size touches and which memory each lands in.أن تذكر الأشكال X (n, c_i) وW (c_o, c_i) وb (c_o,) وY (n, c_o) للطبقة كاملة الاتصال، وتقول أي العددين يتأثر بحجم الدفعة (batch size) وفي أي ذاكرة يقع كل منهما.
  • Write the four Conv2D tensors in PyTorch order, compute the output size from h_i, k, p and s, and evaluate one output value by hand from a multi-channel input.أن تكتب موترات Conv2D الأربعة بترتيب PyTorch، وتحسب حجم المخرج من h_i وk وp وs، وتحسب قيمة مخرج واحدة يدويًا من مدخل متعدد القنوات.
  • Compute the receptive field after L layers with and without stride, and count the weights of standard, grouped and depthwise convolutions to justify a MobileNet-style block for a device.أن تحسب الحقل الاستقبالي بعد L طبقة مع الخطوة وبدونها، وتعدّ أوزان الالتفاف القياسي والمجمّع (grouped) والعمقي (depthwise) لتبرّر اختيار كتلة على طراز MobileNet لجهاز ما.
  • Chain shapes through a CONV-RELU-POOL network, separate the layers that cost parameters from those that cost only activations, and explain the edges-to-objects hierarchy the filters learn.أن تتتبّع الأشكال عبر شبكة CONV-RELU-POOL، وتفصل الطبقات التي تكلّف معاملات عن تلك التي لا تكلّف إلا تفعيلات، وتشرح الهرمية من الحواف إلى الأجسام التي تتعلمها المرشحات.
  • Write the batch norm transform with every tensor shape, derive the fused W' and b' at inference, and name which axes BN, LN, IN and GN average over.أن تكتب تحويل batch norm مع شكل كل موتر، وتشتق W' وb' المدمجين عند الاستدلال، وتسمّي المحاور التي يحسب عليها كل من BN وLN وIN وGN المتوسط.
  • Draw the transformer block, write Attention(Q, K, V) = softmax(Q Kᵀ / √d_k) V with shapes, compute a small attention by hand, and argue why N² scores and exponentials are the embedded bottlenecks.أن ترسم كتلة المحوّل، وتكتب Attention(Q, K, V) = softmax(Q Kᵀ / √d_k) V مع الأشكال، وتحسب انتباهًا صغيرًا يدويًا، وتبيّن لماذا تكون الدرجات الـ N² والدوال الأسية هي الاختناقات على الأجهزة المدمجة.

How to study this lectureكيف تذاكر هذه المحاضرة

  1. Read the parts in order. Each block reuses the counting recipe of the one before it: the FC layer teaches the recipe, convolution adds spatial dimensions, and every later block is a variation.اقرأ الأجزاء بالترتيب. كل كتلة تعيد استخدام طريقة العدّ في الكتلة التي قبلها: الطبقة كاملة الاتصال تعلّمك الطريقة، والالتفاف يضيف الأبعاد المكانية، وكل كتلة بعدها تنويع عليها.
  2. Keep a pencil next to every worked example. Compute the slide's number before you reveal it, then check yourself in the layer builders, the activation plotter and the attention stepper.أبقِ قلمًا بجانب كل مثال محلول. احسب رقم الشريحة قبل أن تكشفه، ثم تحقق من نفسك في أدوات بناء الطبقات وراسم دوال التفعيل ومتتبّع الانتباه خطوة بخطوة.
  3. Answer every recall prompt in your head before opening it. Shapes and formulas that you retrieved stay; the ones you only read do not.أجب عن كل سؤال استرجاع في ذهنك قبل أن تفتحه. الأشكال والصيغ التي استرجعتها بنفسك تبقى، والتي قرأتها فقط لا تبقى.
  4. Take each quiz and read the explanation even when you are right; the wrong options are the exam traps.حلّ كل اختبار قصير واقرأ الشرح حتى لو كانت إجابتك صحيحة، فالخيارات الخاطئة هي فخاخ الامتحان.
  5. Read the slide errata carefully. Several slides carry inherited arithmetic slips, and spotting why they are wrong is the best test that you own the formula.اقرأ أخطاء الشرائح بعناية. عدة شرائح تحمل أخطاء حسابية موروثة، واكتشاف سبب خطئها هو أفضل اختبار لإتقانك الصيغة.
  6. Mark a concept understood only when you could redo its formula and its slide number from a blank page. Unmarked concepts show you where to return.لا تضع علامة "فهمت" على مفهوم إلا إذا كنت تستطيع إعادة صيغته ورقم شريحته من صفحة بيضاء. المفاهيم غير المعلّمة تريك أين يجب أن تعود.
  7. Come back after a few days and redo the counts cold on a network you have not seen, such as the backbone of your project detector. Spaced practice on a new example is what turns a formula into a skill.عُد بعد بضعة أيام وأعد العدّ من الصفر على شبكة لم ترها من قبل، مثل العمود الفقري (backbone) لكاشف مشروعك. التدريب المتباعد على مثال جديد هو ما يحوّل الصيغة إلى مهارة.

Sourcesالمصادر

BThe 6 partsالأجزاء الـ6

  1. 01Neurons, weights and the fully connected layerالعصبونات والأوزان والطبقة المتصلة كليًاThe vocabulary of neural networks and the simplest layer, where every output neuron sees every input neuron.مفردات الشبكات العصبية وأبسط طبقة، حيث يرى كل عصبون في المخرج كل عصبون في المدخل.5 concepts5 مفاهيمSlides 1-7الشرائح 1-730 min30 دقيقة
    1. 1.1Why an embedded-ML course starts by counting neuronsلماذا يبدأ مقرر تعلّم الآلة على الأنظمة المدمجة بعدّ العصبونات
    2. 1.2One neuron: weighted inputs, a bias, and a squashing functionعصبون واحد: مدخلات موزونة، وbias، ودالة ضغط (squashing function)
    3. 1.3Layers, width, depth, and the two synonym familiesالطبقات، والعرض (width)، والعمق (depth)، وعائلتا المترادفات
    4. 1.4The fully connected layer as one matrix productالطبقة كاملة الاتصال (fully connected) على أنها عملية ضرب مصفوفات واحدة
    5. 1.5Stacking FC layers into a multilayer perceptronتكديس طبقات FC لبناء بيرسبترون متعدد الطبقات (MLP)
  2. 02Convolution layers and their tensor shapesطبقات الالتفاف (Convolution) وأشكال موتراتهاLocal receptive fields and weight sharing, from 1D to 2D convolution, the no-padding output size and a fully worked two-channel example.حقول الاستقبال المحلية ومشاركة الأوزان، من الالتفاف أحادي البعد إلى ثنائي البعد، وحجم المخرج بدون حشو (padding)، ومثال محلول بالكامل بقناتين.4 concepts4 مفاهيمSlides 8-15الشرائح 8-1524 min24 دقيقة
    1. 2.1Local connections and one shared filter: the two ideas behind convolutionاتصالات محلية ومرشّح واحد مشترك: الفكرتان وراء الالتفاف
    2. 2.2Reading the four Conv2D tensors: X, Y, W and bقراءة موتّرات Conv2D الأربعة: X وY وW وb
    3. 2.3Sliding the window, and why the feature map shrinksتحريك النافذة، ولماذا تنكمش الـ feature map
    4. 2.4Computing one output value by hand, then counting the parametersحساب قيمة مخرج واحدة يدويًا، ثم عدّ المعاملات
  3. 03Padding, stride, receptive field and grouped convolutionالحشو والخطوة وحقل الاستقبال والالتفاف المجمّعHow padding and stride control the output size, how the receptive field grows with depth, and how grouped and depthwise convolution cut parameters.كيف يتحكم الحشو (padding) والخطوة (stride) في حجم المخرج، وكيف ينمو حقل الاستقبال (receptive field) مع العمق، وكيف يقلل الالتفاف المجمّع (grouped) والعمقي (depthwise) عدد المعاملات.4 concepts4 مفاهيمSlides 16-20الشرائح 16-2024 min24 دقيقة
    1. 3.1Padding: choosing the output size without touching the weightsالحشو: اختيار حجم المخرج دون المساس بالأوزان
    2. 3.2Receptive field: why deep nets stride to see the whole imageمجال الاستقبال: لماذا تستخدم الشبكات العميقة الخطوة لترى الصورة كاملة
    3. 3.3Grouped convolution: g narrower convolutions side by sideالالتفاف المجمّع: g التفافات أضيق جنبًا إلى جنب
    4. 3.4Depthwise convolution and the question the slide asksالالتفاف العمقي والسؤال الذي تطرحه الشريحة
  4. 04Pooling and what CNN filters learnالتجميع (Pooling) وما تتعلمه مرشحات CNNParameter-free downsampling with max and average pooling, the feature hierarchy a CNN learns, and two interactive demos.تقليل الأبعاد بدون معاملات عبر التجميع الأقصى (max) والمتوسط (average)، والتسلسل الهرمي للسمات الذي تتعلمه شبكة CNN، وعرضان تفاعليان.4 concepts4 مفاهيمSlides 21-26الشرائح 21-2624 min24 دقيقة
    1. 4.1Pooling shrinks every map, and only the mapالـ pooling يصغّر كل خريطة، والخريطة فقط
    2. 4.2Two hyperparameters, zero parameters: the pooling arithmeticمعاملان فائقان وصفر معاملات: حساب الـ pooling
    3. 4.3From edges to objects: what the filters learnمن الحواف إلى الأجسام: ماذا تتعلم الـ filters
    4. 4.4Two browser demos as counting practiceعرضان في المتصفح كتمرين على العدّ
  5. 05Batch, layer, instance and group normalizationتطبيع الدفعة والطبقة والمثيل والمجموعةWhy nicely scaled activations make optimization easier, how batch normalization computes, scales and shifts, how it changes at inference, and the axes each normalization variant averages over.لماذا تجعل التنشيطات ذات المقياس المناسب التحسين أسهل، وكيف يحسب تطبيع الدفعة (batch normalization) ثم يقيس ويزيح، وكيف يتغير أثناء الاستدلال (inference)، والمحاور التي يحسب كل نوع من التطبيع المتوسط عليها.5 concepts5 مفاهيمSlides 27-38الشرائح 27-3830 min30 دقيقة
    1. 5.1Why a layer wants zero-centered, evenly scaled inputsلماذا تريد الطبقة مدخلات متمركزة حول الصفر وبمقياس متساوٍ
    2. 5.2The batch norm transform: normalize per feature, then let gamma and beta choose the rangeتحويل الـ batch norm: وحّد المقياس لكل خاصية، ثم دع gamma و beta تختاران المدى
    3. 5.3At inference the statistics are frozen, so BN folds into the layer before itفي الاستدلال تُجمَّد الإحصاءات، فيندمج الـ BN في الطبقة التي قبله
    4. 5.4Batch, layer, instance and group norm differ only in which cells share one meanالـ batch norm والـ layer norm والـ instance norm والـ group norm تختلف فقط في الخلايا التي تشترك في متوسط واحد
    5. 5.5Batch norm by hand: four samples, three features, every numberالـ batch norm يدويًا: أربع عينات، ثلاث خاصيات، كل رقم
  6. 06Activation functions and the transformerدوال التنشيط والمحوّل (Transformer)The non-linearities used in embedded networks, then the transformer block: scaled dot-product attention worked by hand and the position-wise feed-forward network.اللاخطيات المستخدمة في الشبكات المضمّنة، ثم كتلة المحوّل: انتباه الضرب النقطي المقيّس (scaled dot-product attention) محلولًا يدويًا، وشبكة التغذية الأمامية الموضعية (position-wise feed-forward).6 concepts6 مفاهيمSlides 39-45الشرائح 39-4536 min36 دقيقة
    1. 6.1Why the neuron needs a bend, and which bends an MCU can affordلماذا يحتاج العصبون إلى انحناء غير خطي، وأيّ انحناءات يستطيع الـ MCU تحمّل كلفتها
    2. 6.2The transformer block: attention, Add and Norm, feed-forward, positional encodingكتلة الـ transformer: الانتباه (attention)، وAdd and Norm، والشبكة الأمامية (feed-forward)، والترميز الموضعي (positional encoding)
    3. 6.3Attention as retrieval: query, key, value and the N x N costالانتباه بوصفه استرجاعًا: الاستعلام (query) والمفتاح (key) والقيمة (value) وكلفة N x N
    4. 6.4Scaled dot-product attention computed by hand on four tokensحساب scaled dot-product attention يدويًا على أربعة tokens
    5. 6.5From attention weights to a position-wise feed-forward networkمن أوزان الانتباه إلى شبكة أمامية تعمل على كل موضع على حدة (position-wise feed-forward network)
    6. 6.6Where this lecture's ideas come fromمن أين جاءت أفكار هذه المحاضرة

CGlossary and referenceالمصطلحات والمرجع