COE 592Lecture 02المحاضرة 02Part 02الجزء 02
Convolution 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)، ومثال محلول بالكامل بقناتين.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 8-15
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Every efficiency technique in this course is an operation on the tensors of a convolution layer. Lecture 3 counts parameters and MACs from these shapes, pruning in lecture 4 removes entries of W, quantization shrinks the bits of W and Y, and the depthwise and grouped convolutions later in this lecture change the shape of W itself. Every model-size or MAC estimate for an embedded target starts from that shape and from the formula for the output height h_o.
Part 01 gave you the fully connected layer, whose weight matrix (c_o, c_i) connects every input to every output. This part crosses out that matrix and rebuilds it as a convolution: a small filter that sees only a local patch and is reused at every position. You will learn to read the four Conv2D tensors, to predict how much the feature map shrinks, to compute one output value by hand from a two-channel input, and to count parameters. These four skills are the most reliable exam material in the lecture, and they are the same arithmetic you will do when you size a model for a microcontroller or read a MobileNet table.
كل تقنية لرفع الكفاءة في هذا المقرر هي عملية على موتّرات (tensors) طبقة الالتفاف. المحاضرة 3 تعدّ المعاملات وعمليات الـ MACs انطلاقًا من هذه الأشكال، والـ pruning في المحاضرة 4 يحذف عناصر من W، والـ quantization يقلّص عدد بتات W وY، والالتفافات الـ depthwise والـ grouped التي تأتي لاحقًا في هذه المحاضرة تغيّر شكل W نفسه. وكل تقدير لحجم النموذج أو لعدد الـ MACs على جهاز مدمج يبدأ من هذا الشكل ومن صيغة ارتفاع المخرج h_o.
قدّم لك الجزء 01 الطبقة كاملة الاتصال (fully connected)، التي تربط مصفوفة أوزانها (c_o, c_i) كل مدخل بكل مخرج. هذا الجزء يشطب تلك المصفوفة ويعيد بناءها على شكل التفاف: مرشّح صغير لا يرى إلا رقعة محلية ويُعاد استخدامه في كل موضع. ستتعلم قراءة موتّرات Conv2D الأربعة، وتوقّع مقدار انكماش الـ feature map، وحساب قيمة مخرج واحدة يدويًا من مدخل ذي قناتين، وعدّ المعاملات. هذه المهارات الأربع هي أكثر مادة امتحانية موثوقة في المحاضرة، وهي الحساب نفسه الذي ستجريه عندما تقدّر حجم نموذج لمتحكم دقيق (microcontroller) أو تقرأ جدول MobileNet.
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain, with the receptive field and weight sharing, why one filter is reused at every position and why the parameter count does not depend on image size.تشرح، باستخدام المجال الاستقبالي (receptive field) ومشاركة الأوزان (weight sharing)، لماذا يُعاد استخدام المرشّح الواحد في كل موضع، ولماذا لا يعتمد عدد المعاملات على حجم الصورة.
- Write the shapes of X, Y, W and b for any Conv2D from its c_i, c_o, k_h, k_w and input size, in PyTorch order.تكتب أشكال X وY وW وb لأي Conv2D انطلاقًا من c_i وc_o وk_h وk_w وحجم المدخل، بترتيب PyTorch.
- Compute the output size without padding at stride 1, and with the general stride formula that slide 15 uses ahead of slide 18.تحسب حجم المخرج بدون padding عند stride يساوي 1، ثم بالصيغة العامة للـ stride التي تستخدمها الشريحة 15 قبل أن تعرّفها الشريحة 18.
- Compute one output value by hand from a multi-channel input, summing per-channel contributions and adding the bias.تحسب قيمة مخرج واحدة يدويًا من مدخل متعدد القنوات، بجمع مساهمة كل قناة على حدة ثم إضافة الـ bias.
- Count a Conv2D's parameters as c_o c_i k_h k_w + c_o and check it against the 1216 of the ConvNetJS demo.تعدّ معاملات Conv2D على أنها c_o c_i k_h k_w + c_o وتتحقق منها مقابل الرقم 1216 في عرض ConvNetJS.
Take a CIFAR-10 image: 32 x 32 pixels in three colour channels, so 3072 numbers. A single neuron of the Fully connected layer from Part 01 has one weight for each of those 3072 inputs, and CS231n uses exactly this count to show why the fully connected design does not scale to images. A neuron in a Convolution layer does something far more modest. It looks at a 5 x 5 patch of the image across all three channels, so 5 x 5 x 3 = 75 numbers, and ignores everything else. That patch is the neuron's Receptive field, and the slide states the whole idea in one sentence: the output neuron is connected to input neurons in the receptive field.
خذ صورة من CIFAR-10: 32 x 32 بكسلًا في ثلاث قنوات لونية (channels)، أي 3072 رقمًا. عصبون واحد في الـ fully connected layer من الجزء 01 له وزن واحد لكل مدخل من هذه المدخلات الـ 3072، ويستخدم CS231n هذا العدد تحديدًا ليبيّن لماذا لا يتوسع التصميم كامل الاتصال ليناسب الصور. أما العصبون في الـ convolution layer فيفعل شيئًا أكثر تواضعًا بكثير: ينظر إلى رقعة بحجم 5 x 5 من الصورة عبر القنوات الثلاث كلها، أي 5 x 5 x 3 = 75 رقمًا، ويتجاهل كل ما عداها. هذه الرقعة هي الـ receptive field (المجال الاستقبالي) للعصبون، والشريحة تلخّص الفكرة كلها في جملة واحدة: عصبون المخرج متصل بعصبونات المدخل الموجودة في المجال الاستقبالي.
That sentence hides two separate decisions, and Goodfellow, Bengio and Courville name them in section 9.2 of Deep Learning. The first is sparse interactions. With m inputs and n outputs, a fully connected layer stores m x n weights. If each output is allowed to touch only k inputs, the layer stores k x n. On the CIFAR image that is 75 per output instead of 3072.
تخفي هذه الجملة قرارين منفصلين، ويسمّيهما Goodfellow وBengio وCourville في القسم 9.2 من كتاب Deep Learning. الأول هو التفاعلات المتفرقة (sparse interactions). مع m مدخلًا وn مخرجًا، تخزّن الطبقة كاملة الاتصال m x n وزنًا. فإذا سُمح لكل مخرج بأن يلمس k مدخلًا فقط، تخزّن الطبقة k x n. وفي صورة CIFAR يعني ذلك 75 وزنًا لكل مخرج بدلًا من 3072.
The second decision is Weight sharing. Sparse connectivity alone would still give every output position its own private set of 75 weights. Convolution goes further and uses the same 75 numbers at every position. Goodfellow and colleagues put it plainly: rather than learning a separate set of parameters for every location, we learn only one set, so the storage of one filter drops to k parameters. They also give the synonym you will meet in older papers, tied weights. Slide 8 labels its figure Weight Sharing, and slides 12 and 13 show it in motion: the same green, yellow and cyan filters are applied wherever the red window sits on the blue input.
القرار الثاني هو الـ weight sharing (مشاركة الأوزان). الاتصال المتفرق وحده كان سيعطي كل موضع مخرج مجموعته الخاصة من 75 وزنًا. أما الالتفاف فيذهب أبعد ويستخدم الأرقام الـ 75 نفسها في كل موضع. ويقولها Goodfellow وزملاؤه بوضوح: بدلًا من تعلّم مجموعة منفصلة من المعاملات لكل موقع، نتعلم مجموعة واحدة فقط، فينخفض تخزين مرشّح (filter) واحد إلى k معاملًا (parameters). ويذكرون أيضًا المرادف الذي ستصادفه في الأوراق الأقدم: الأوزان المربوطة (tied weights). الشريحة 8 تعنون شكلها بـ Weight Sharing، والشريحتان 12 و13 تُظهرانه أثناء الحركة: المرشّحات الخضراء والصفراء والسماوية نفسها تُطبَّق أينما كانت النافذة الحمراء على المدخل الأزرق.
What the saving looks like in a real first layerكيف يبدو هذا التوفير في طبقة أولى حقيقية
CS231n works through AlexNet's first layer to show the size of the effect, and the numbers are worth holding in memory for the exam. The input is 227 x 227 x 3 (the AlexNet paper says 224 x 224; CS231n uses 227 so that (227 - 11)/4 + 1 = 55 comes out exact), the filters are 11 x 11 x 3, and the output is 55 x 55 x 96.
يمرّ CS231n على الطبقة الأولى من AlexNet ليبيّن حجم الأثر، والأرقام تستحق أن تحفظها للامتحان. المدخل 227 x 227 x 3 (ورقة AlexNet تقول 224 x 224؛ ويستخدم CS231n القيمة 227 لكي يخرج (227 - 11)/4 + 1 = 55 عددًا صحيحًا)، والمرشّحات 11 x 11 x 3، والمخرج 55 x 55 x 96.
Worked exampleمثال محلول
AlexNet conv1 with and without weight sharingالطبقة conv1 في AlexNet مع مشاركة الأوزان وبدونها
Count the output neuronsعُدّ عصبونات المخرج
55 x 55 x 96 = 290,400 outputs, one per position per output Channel.55 x 55 x 96 = 290,400 مخرجًا، واحد لكل موضع ولكل قناة مخرج.Weights per neuron with a local receptive field onlyالأوزان لكل عصبون مع مجال استقبالي محلي فقط
No sharingبدون مشاركة
290,400 x 364 = 105,705,600 parameters for one layer.290,400 x 364 = 105,705,600 معامل لطبقة واحدة.With sharingمع المشاركة
One filter per output channel: 96 x 363 = 34,848 weights plus 96 biases, so 34,944.مرشّح واحد لكل قناة مخرج: 96 x 363 = 34,848 وزنًا إضافة إلى 96 قيمة bias، أي 34,944.Reductionمقدار التخفيض
About 3025x fewer parameters, and 3025 = 55 x 55 is exactly the number of positions the filter is reused at.معاملات أقل بنحو 3025x مرة، و3025 = 55 x 55 هو بالضبط عدد المواضع التي يُعاد فيها استخدام المرشّح.
Crossing out the fully connected shapesشطب أشكال الطبقة كاملة الاتصال
The slide introduces the shapes by literally striking through the fully connected ones. In Part 01 the input was X (n, c_i): n samples in the batch (the Batch size), each a vector of c_i features. A 1D convolution adds a spatial axis of width w_i, so X becomes (n, c_i, w_i) and Y becomes (n, c_o, w_o). The weight matrix (c_o, c_i) gains a kernel width k_w and becomes (c_o, c_i, k_w). The bias does not change at all: still one number per output channel, (c_o,). The table already shows the 2D column, which adds a height axis in the same way; the next concept explains each of its axes.
تقدّم الشريحة الأشكال بشطب أشكال الطبقة كاملة الاتصال حرفيًا. في الجزء 01 كان المدخل X (n, c_i): n عينة في الدفعة (الـ batch size)، كل منها متجه من c_i خاصية. الالتفاف أحادي البعد (1D) يضيف محورًا مكانيًا عرضه w_i، فيصبح X بالشكل (n, c_i, w_i) ويصبح Y بالشكل (n, c_o, w_o). ومصفوفة الأوزان (c_o, c_i) تكتسب عرض نواة k_w فتصبح (c_o, c_i, k_w). أما الـ bias فلا يتغير إطلاقًا: لا يزال رقمًا واحدًا لكل قناة مخرج، (c_o,). والجدول يعرض مسبقًا عمود الحالة ثنائية البعد (2D)، الذي يضيف محور الارتفاع بالطريقة نفسها؛ والمفهوم التالي يشرح كل محور فيه.
| Tensorالموتّر | Fully connectedكاملة الاتصال | 1D convolutionالتفاف أحادي البعد | 2D convolutionالتفاف ثنائي البعد |
|---|---|---|---|
| Input Xالمدخل X | (n, c_i)(n, c_i) | (n, c_i, w_i)(n, c_i, w_i) | (n, c_i, h_i, w_i)(n, c_i, h_i, w_i) |
| Output Yالمخرج Y | (n, c_o)(n, c_o) | (n, c_o, w_o)(n, c_o, w_o) | (n, c_o, h_o, w_o)(n, c_o, h_o, w_o) |
| Weights Wالأوزان W | (c_o, c_i)(c_o, c_i) | (c_o, c_i, k_w)(c_o, c_i, k_w) | (c_o, c_i, k_h, k_w)(c_o, c_i, k_h, k_w) |
| Bias bالـ bias b | (c_o,)(c_o,) | (c_o,)(c_o,) | (c_o,)(c_o,) |
Recall
A 1D convolution has 8 input channels, 32 output channels and kernel width 5. Write the shapes of W and b, and say what happens to them if the input width doubles.التفاف أحادي البعد له 8 قنوات مدخل و32 قناة مخرج وعرض نواة 5. اكتب شكلي W وb، وقل ماذا يحدث لهما إذا تضاعف عرض المدخل.
Quick checkاختبار سريع
Why does a convolution layer on a 32x32x3 image need far fewer parameters than a fully connected layer?لماذا تحتاج طبقة الالتفاف على صورة 32x32x3 معاملات أقل بكثير من الطبقة كاملة الاتصال؟
Start with a layer you could type into PyTorch today: nn.Conv2d(3, 16, kernel_size=5), applied to a batch of 8 RGB images of 32 x 32. Four tensors take part, and every axis of each one has a name.
ابدأ بطبقة يمكنك كتابتها في PyTorch اليوم: nn.Conv2d(3, 16, kernel_size=5)، مطبّقة على دفعة من 8 صور RGB بحجم 32 x 32. تشارك أربعة موتّرات، ولكل محور في كل منها اسم.
nn.Conv2d(3, 16, kernel_size=5) on 8 images of 32x32, no paddingnn.Conv2d(3, 16, kernel_size=5) على 8 صور بحجم 32x32، بدون padding
- Input Xالمدخل X
- (8, 3, 32, 32): batch, input channels, height, width(8, 3, 32, 32): الدفعة (batch)، قنوات المدخل، الارتفاع، العرض
- Weights Wالأوزان W
- (16, 3, 5, 5): output channels, input channels, kernel height, kernel width(16, 3, 5, 5): قنوات المخرج، قنوات المدخل، ارتفاع النواة، عرض النواة
- Bias bالـ bias b
- (16,): one scalar per output channel(16,): قيمة عددية واحدة لكل قناة مخرج
- Output Yالمخرج Y
- (8, 16, 28, 28): batch, output channels, then 32 - 5 + 1 = 28 each way(8, 16, 28, 28): الدفعة، قنوات المخرج، ثم 32 - 5 + 1 = 28 في كل اتجاه
The general rule is the 2D column that the previous concept left open. The input X is (n, c_i, h_i, w_i), the output Y is (n, c_o, h_o, w_o), the weights W are (c_o, c_i, k_h, k_w) and the bias b is (c_o,). The PyTorch documentation for torch.nn.Conv2d confirms this exact ordering: the weight is stored as (out_channels, in_channels / groups, kernel_size[0], kernel_size[1]) and the bias as (out_channels). The groups divisor is 1 by default; it becomes important in Part 03 when grouped and depthwise convolutions arrive.
القاعدة العامة هي عمود الحالة ثنائية البعد الذي تركه المفهوم السابق مفتوحًا. المدخل X شكله (n, c_i, h_i, w_i)، والمخرج Y شكله (n, c_o, h_o, w_o)، والأوزان W شكلها (c_o, c_i, k_h, k_w)، والـ bias b شكله (c_o,). وتؤكد وثائق PyTorch الخاصة بـ torch.nn.Conv2d هذا الترتيب بالضبط: الوزن مخزّن بالشكل (out_channels, in_channels / groups, kernel_size[0], kernel_size[1]) والـ bias بالشكل (out_channels). القاسم groups قيمته الافتراضية 1؛ ويصبح مهمًا في الجزء 03 عندما تظهر الالتفافات الـ grouped والـ depthwise.
A filter is a block, not a squareالمرشّح كتلة، لا مربع
Slides 9 to 11 make one argument in three frames. The first frame draws a single filter as a green block with edges labelled k_h, k_w and c_i, sitting next to the red window on the input. The block is as deep as the input has channels. When it is applied at one position, it multiplies all c_i x k_h x k_w input values under the window by its own c_i x k_h x k_w weights, sums everything into a single number, and drops that number into one output channel. CS231n states the rule as a sentence to memorise: the extent of the connectivity along the depth axis is always equal to the depth of the input volume.
تبني الشرائح من 9 إلى 11 حجة واحدة في ثلاث لقطات. اللقطة الأولى ترسم مرشّحًا واحدًا على شكل كتلة خضراء أضلاعها موسومة بـ k_h وk_w وc_i، بجوار النافذة الحمراء على المدخل. عمق الكتلة يساوي عدد قنوات المدخل. وعندما تُطبَّق في موضع واحد، تضرب كل قيم المدخل الـ c_i x k_h x k_w الواقعة تحت النافذة في أوزانها الـ c_i x k_h x k_w، وتجمع كل شيء في رقم واحد، وتضع ذلك الرقم في قناة مخرج واحدة. ويصوغ CS231n القاعدة في جملة تستحق الحفظ: امتداد الاتصال على محور العمق يساوي دائمًا عمق حجم المدخل.
The second frame adds a yellow filter. It has the same depth c_i, sees the same window, and drops its number into a second output channel. The third frame shows the full stack of c_o filters with a bracket labelled c_o, and the output is now c_o channels deep. That bracket is literally the first axis of W. Choosing c_o is a design decision made by whoever builds the network; it is not derived from c_i.
اللقطة الثانية تضيف مرشّحًا أصفر. له العمق نفسه c_i، ويرى النافذة نفسها، ويضع رقمه في قناة مخرج ثانية. اللقطة الثالثة تعرض المكدّس الكامل من c_o مرشّحًا مع قوس موسوم بـ c_o، ويصبح عمق المخرج الآن c_o قناة. هذا القوس هو حرفيًا المحور الأول في W. اختيار c_o قرار تصميمي يتخذه من يبني الشبكة؛ وهو لا يُشتق من c_i.
Bias and batch, the two easy axesالـ bias والدفعة، المحوران السهلان
The Bias is added once per output channel, after the whole c_i-deep dot product, which is why its shape is (c_o,) exactly as in the fully connected layer of Part 01. The Batch size n appears in X and Y but never in W or b: the same weights process every sample in the batch. If you are ever unsure of a shape, check that n is absent from the parameters and that c_o leads W.
يُضاف الـ bias مرة واحدة لكل قناة مخرج، بعد حاصل الضرب النقطي الكامل الذي عمقه c_i، ولهذا شكله (c_o,) تمامًا كما في الطبقة كاملة الاتصال في الجزء 01. أما الـ batch size n فيظهر في X وY لكنه لا يظهر أبدًا في W أو b: الأوزان نفسها تعالج كل عينة في الدفعة. وإذا شككت يومًا في شكل ما، فتحقق من أن n غائب عن المعاملات وأن c_o يتصدّر W.
Recall
Write the shapes of X, W, b and Y for nn.Conv2d(3, 16, kernel_size=5) applied to a batch of 8 images of 32x32 with no padding.اكتب أشكال X وW وb وY للطبقة nn.Conv2d(3, 16, kernel_size=5) مطبّقة على دفعة من 8 صور بحجم 32x32 بدون padding.
Recall
A Conv2D has 64 input channels and 128 filters of 3x3. How many output channels does it produce, and what is the shape of W?طبقة Conv2D لها 64 قناة مدخل و128 مرشّحًا بحجم 3x3. كم قناة مخرج تنتج، وما شكل W؟
Quick checkاختبار سريع
A Conv2D layer has c_i = 3, c_o = 16 and 5x5 kernels. Which is the shape of its weight tensor W?طبقة Conv2D فيها c_i = 3 وc_o = 16 ونوى بحجم 5x5. ما شكل موتّر أوزانها W؟
Slide 14 gives the smallest example that shows the shrink. The input is 4 x 4, the Kernel (filter) is 3 x 3, there is no padding and the window moves one cell at a time. Along the top row the window can start at column 0 or column 1. Starting at column 2 would need columns 2, 3 and 4, and there is no column 4. Two starting positions per axis means the output is 2 x 2.
تعطي الشريحة 14 أصغر مثال يُظهر الانكماش. المدخل 4 x 4، والـ kernel بحجم 3 x 3، ولا يوجد padding، والنافذة تتحرك خلية واحدة في كل مرة. على الصف العلوي يمكن أن تبدأ النافذة عند العمود 0 أو العمود 1. البدء عند العمود 2 يحتاج الأعمدة 2 و3 و4، ولا يوجد عمود 4. موضعا بدء لكل محور يعنيان أن المخرج 2 x 2.
Worked exampleمثال محلول
Counting the valid positions on slide 14عدّ المواضع الصالحة في الشريحة 14
Last valid start indexآخر فهرس بدء صالح
The window of height k_h = 3 starting at row r covers rows r to r + 2. It fits while r + 2 <= 3, so the last start is r = h_i - k_h = 1.النافذة التي ارتفاعها k_h = 3 وتبدأ عند الصف r تغطي الصفوف من r إلى r + 2. وهي تتسع ما دام r + 2 <= 3، لذا فآخر بداية هي r = h_i - k_h = 1.Count the startsعُدّ مواضع البدء
Rows 0 and 1, which is h_i - k_h + 1 = 4 - 3 + 1 = 2 positions.الصفان 0 و1، أي h_i - k_h + 1 = 4 - 3 + 1 = 2 موضعين.Resultالنتيجة
h_o = w_o = 2. The feature map is 2 x 2, smaller than the input by k - 1 = 2 along each axis (one cell lost at each border).h_o = w_o = 2. الـ feature map حجمها 2 x 2، أصغر من المدخل بمقدار k - 1 = 2 على كل محور (خلية مفقودة عند كل حافة).
Slides 12 and 13 are two frames of the same motion, taken from the MIT 6.5940 lecture that this section of the deck reproduces. The red window moves one step at a time along a spatial dimension, and at every stop the same green, yellow and cyan filters are applied, which is why slide 12 carries the label Weight Sharing. Nothing about the filters changes as the window moves; this is where Weight sharing becomes visible rather than stated. Each stop adds one short stack of c_o values to the output, one value per filter, so the output has exactly one spatial cell per valid window position. The count of valid positions is the whole story of the output size.
الشريحتان 12 و13 لقطتان من الحركة نفسها، مأخوذتان من محاضرة MIT 6.5940 التي يعيد هذا القسم من العرض إنتاجها. تتحرك النافذة الحمراء خطوة واحدة في كل مرة على امتداد بُعد مكاني، وفي كل توقف تُطبَّق المرشّحات الخضراء والصفراء والسماوية نفسها، ولهذا تحمل الشريحة 12 عنوان Weight Sharing. لا شيء في المرشّحات يتغير مع حركة النافذة؛ وهنا يصبح الـ weight sharing مرئيًا بدلًا من أن يكون مجرد عبارة. كل توقف يضيف إلى المخرج مكدّسًا قصيرًا من c_o قيمة، قيمة واحدة لكل مرشّح، لذا فللمخرج خلية مكانية واحدة بالضبط لكل موضع صالح للنافذة. وعدد المواضع الصالحة هو القصة الكاملة لحجم المخرج.
The general formula, one part earlyالصيغة العامة، قبل موعدها بجزء واحد
Slide 15 asks you to use a Stride of 2 before slide 18 has defined stride, so you need the general rule now. With padding p on every border and stride s, Dumoulin and Visin's relationship 6 gives the output size, and the PyTorch documentation states the same formula with a dilation term that is 1 throughout this course.
تطلب منك الشريحة 15 استخدام stride قيمته 2 قبل أن تعرّف الشريحة 18 الـ stride، لذا تحتاج القاعدة العامة الآن. مع padding قدره p على كل حافة وstride قدره s، تعطي العلاقة 6 عند Dumoulin وVisin حجم المخرج، وتذكر وثائق PyTorch الصيغة نفسها مع حد dilation قيمته 1 طوال هذا المقرر.
| h_ih_i | kk | ss | pp | Arithmeticالحساب | h_oh_o |
|---|---|---|---|---|---|
| 44 | 33 | 11 | 00 | (4 - 3)/1 + 1(4 - 3)/1 + 1 | 22 |
| 44 | 22 | 22 | 00 | (4 - 2)/2 + 1(4 - 2)/2 + 1 | 22 |
| 3232 | 55 | 11 | 00 | (32 - 5)/1 + 1(32 - 5)/1 + 1 | 2828 |
| 3232 | 55 | 11 | 22 | (32 + 4 - 5)/1 + 1(32 + 4 - 5)/1 + 1 | 3232 |
| 224224 | 77 | 22 | 33 | floor((224 + 6 - 7)/2) + 1floor((224 + 6 - 7)/2) + 1 | 112112 |
Two rows deserve a second look. The 32, 5, 1, 2 row is the ConvNetJS demo on slide 25, which uses pad 2 precisely so that its 5 x 5 convolution keeps the 32 x 32 size, giving the 32 x 32 x 16 output printed there. The 224, 7, 2, 3 row is the first layer of a ResNet, which halves the spatial size in one step. Both are the same formula.
صفّان يستحقان نظرة ثانية. صف 32, 5, 1, 2 هو عرض ConvNetJS في الشريحة 25، الذي يستخدم pad قدره 2 تحديدًا لكي يحافظ التفافه 5 x 5 على الحجم 32 x 32، فيعطي المخرج 32 x 32 x 16 المطبوع هناك. وصف 224, 7, 2, 3 هو الطبقة الأولى في ResNet، التي تنصّف الحجم المكاني في خطوة واحدة. وكلاهما الصيغة نفسها.
Recall
A 32x32 input meets a 5x5 kernel at stride 1. What is the output size with no padding, and with padding 2?مدخل بحجم 32x32 يلتقي بـ kernel بحجم 5x5 عند stride يساوي 1. ما حجم المخرج بدون padding، ومع padding قدره 2؟
Quick checkاختبار سريع
An input of 4x4 is convolved with a 3x3 kernel, stride 1, no padding. What is the output spatial size?مدخل بحجم 4x4 يُطبَّق عليه التفاف بـ kernel بحجم 3x3، وstride يساوي 1، بدون padding. ما الحجم المكاني للمخرج؟
Slide 15 is the exam question in miniature: two input channels of 4 x 4, one output channel, a 2 x 2 filter, Stride 2, no padding and no Bias. Input channel 1 has every row equal to 1 2 3 4; input channel 2 has every row equal to 5 6 7 8. The filter's first slab is [[2, 1], [1, 2]] and its second slab is [[1, 2], [2, 1]]. In the shapes of the previous concepts, X is (1, 2, 4, 4) and W is (1, 2, 2, 2).
الشريحة 15 هي سؤال الامتحان في صورة مصغّرة: قناتا مدخل بحجم 4 x 4، وقناة مخرج واحدة، ومرشّح (filter) بحجم 2 x 2، وstride قدره 2، بلا padding وبلا bias. كل صف في قناة المدخل الأولى يساوي 1 2 3 4؛ وكل صف في قناة المدخل الثانية يساوي 5 6 7 8. الشريحة الأولى من المرشّح هي [[2, 1], [1, 2]] والثانية [[1, 2], [2, 1]]. وبأشكال المفاهيم السابقة، X شكله (1, 2, 4, 4) وW شكله (1, 2, 2, 2).
The formula says what the filter-is-a-block idea implied: the sum over c folds all input channels into one number. There is no separate output per input channel. With that in mind the four values of the slide are four applications of the same recipe. Strictly, this is cross-correlation: mathematical convolution flips the kernel first, and this formula does not. Deep learning libraries, PyTorch and this deck all call the unflipped version convolution (Goodfellow et al., section 9.1), and this course follows that convention.
تقول الصيغة ما كانت فكرة أن المرشّح كتلة تتضمنه: الجمع على c يطوي كل قنوات المدخل في رقم واحد. لا يوجد مخرج منفصل لكل قناة مدخل. ومع وضع ذلك في الذهن، فالقيم الأربع في الشريحة هي أربعة تطبيقات للوصفة نفسها. وبدقة، هذا ارتباط متقاطع (cross-correlation): الالتفاف الرياضي يقلب النواة أولًا، وهذه الصيغة لا تفعل ذلك. مكتبات التعلم العميق وPyTorch وهذا العرض كلها تسمّي النسخة غير المقلوبة التفافًا (Goodfellow et al.، القسم 9.1)، وهذا المقرر يتبع هذا العرف.
Worked exampleمثال محلول
Slide 15, worked from the shapes downالشريحة 15، محلولة انطلاقًا من الأشكال
Output sizeحجم المخرج
Stride 2 with no padding: (4 - 2)/2 + 1 = 2, so the output is 2 x 2 and the window visits four positions, starting at rows and columns 0 and 2.stride قدره 2 بلا padding: (4 - 2)/2 + 1 = 2، لذا فالمخرج 2 x 2 والنافذة تزور أربعة مواضع، تبدأ عند الصفين والعمودين 0 و2.Top-left value, window at rows 0 to 1, columns 0 to 1القيمة العلوية اليسرى، النافذة على الصفين 0 إلى 1 والعمودين 0 إلى 1
Per-channel contributions at position (0, 0)مساهمة كل قناة عند الموضع (0, 0)
- Channel 1القناة 1
- 2·1 + 1·2 + 1·1 + 2·2 = 92·1 + 1·2 + 1·1 + 2·2 = 9
- Channel 2القناة 2
- 1·5 + 2·6 + 2·5 + 1·6 = 331·5 + 2·6 + 2·5 + 1·6 = 33
- Biasالـ bias
- 00
- Sumالمجموع
- 9 + 33 = 429 + 33 = 42
Top-right value, window at rows 0 to 1, columns 2 to 3القيمة العلوية اليمنى، النافذة على الصفين 0 إلى 1 والعمودين 2 إلى 3
Per-channel contributions at position (0, 1)مساهمة كل قناة عند الموضع (0, 1)
- Channel 1القناة 1
- 2·3 + 1·4 + 1·3 + 2·4 = 212·3 + 1·4 + 1·3 + 2·4 = 21
- Channel 2القناة 2
- 1·7 + 2·8 + 2·7 + 1·8 = 451·7 + 2·8 + 2·7 + 1·8 = 45
- Biasالـ bias
- 00
- Sumالمجموع
- 21 + 45 = 6621 + 45 = 66
Bottom rowالصف السفلي
Every row of both inputs is identical, so the windows at rows 2 to 3 see the same numbers and give 42 and 66 again.كل صفوف المدخلين متطابقة، لذا فالنوافذ على الصفين 2 إلى 3 ترى الأرقام نفسها وتعطي 42 و66 مرة أخرى.Output feature mapالـ feature map الناتجة
[[42, 66], [42, 66]], exactly as printed on the slide.[[42, 66], [42, 66]]، تمامًا كما هي مطبوعة على الشريحة.
Now do it with your hands. The stepper below holds the slide 15 numbers, highlights the current window on both input channels, lists the products channel by channel and fills the output one cell at a time. Switch the stride to 1 to see the 3 x 3 output, then change a few cells and predict the result before you press Next.
الآن جرّبها بيدك. الأداة التفاعلية أدناه تحمل أرقام الشريحة 15، وتبرز النافذة الحالية على قناتي المدخل كلتيهما، وتسرد حواصل الضرب قناة بقناة، وتملأ المخرج خلية خلية. غيّر الـ stride إلى 1 لترى المخرج 3 x 3، ثم غيّر بعض الخلايا وتوقّع النتيجة قبل أن تضغط Next.
- This position
- press Next to place the windowاضغط التالي لوضع النافذة
Every input and filter cell is editable, negative weights included; a blank cell counts as 0. Rows and columns are numbered from 0, as in the formula above. Each channel of the filter multiplies the matching input channel inside the highlighted window, the two channel sums are added into one value, and the bias is added last.Channel 1 and channel 2 keep their colours throughout.كل خلية في الإدخال وفي المرشح قابلة للتعديل، بما في ذلك الأوزان السالبة، والخلية الفارغة تُحسب 0. تُرقَّم الصفوف والأعمدة بدءًا من 0، كما في الصيغة أعلاه. تضرب كل قناة من قنوات المرشح قناة الإدخال المقابلة لها داخل النافذة المظللة، ثم يُجمع مجموعا القناتين في قيمة واحدة، ويُضاف الانحياز في النهاية. تحتفظ القناة 1 والقناة 2 بلونيهما طوال الوقت.
| Stride 2 (the slide)stride يساوي 2 (الشريحة) | Stride 1stride يساوي 1 | |
|---|---|---|
| Output sizeحجم المخرج | (4 - 2)/2 + 1 = 2, so 2x2(4 - 2)/2 + 1 = 2، إذن 2x2 | (4 - 2)/1 + 1 = 3, so 3x3(4 - 2)/1 + 1 = 3، إذن 3x3 |
| Channel 1 partials, one rowالمجاميع الجزئية للقناة 1، صف واحد | 9, 219, 21 | 9, 15, 219, 15, 21 |
| Channel 2 partials, one rowالمجاميع الجزئية للقناة 2، صف واحد | 33, 4533, 45 | 33, 39, 4533, 39, 45 |
| One output rowصف مخرج واحد | 42, 6642, 66 | 42, 54, 6642, 54, 66 |
| Windows visitedعدد النوافذ التي تمت زيارتها | 44 | 99 |
The stride-1 column shows what the middle window adds. At columns 1 to 2, channel 1 gives 2·2 + 1·3 + 1·2 + 2·3 = 15 and channel 2 gives 1·6 + 2·7 + 2·6 + 1·7 = 39, so 54 sits between 42 and 66. Stride 2 skips that window entirely, which is why the output is smaller and the compute drops from 9 windows to 4. On a large input the compute falls to about a quarter of the stride-1 cost, since both axes are halved.
يُظهر عمود stride يساوي 1 ما تضيفه النافذة الوسطى. عند العمودين 1 إلى 2 تعطي القناة الأولى 2·2 + 1·3 + 1·2 + 2·3 = 15 وتعطي القناة الثانية 1·6 + 2·7 + 2·6 + 1·7 = 39، لذا تقع 54 بين 42 و66. الـ stride الذي يساوي 2 يتخطى تلك النافذة كليًا، ولهذا يكون المخرج أصغر وينخفض الحساب من 9 نوافذ إلى 4. وعلى مدخل كبير ينخفض الحساب إلى نحو ربع تكلفة stride يساوي 1، لأن المحورين كليهما يُنصَّفان.
Recall
In the slide 15 example, what are the per-channel contributions to the top-right output value?في مثال الشريحة 15، ما مساهمة كل قناة في قيمة المخرج العلوية اليمنى؟
Recall
Same data with stride 1: what is the output size, and what is the middle column?البيانات نفسها مع stride يساوي 1: ما حجم المخرج، وما العمود الأوسط؟
Quick checkاختبار سريع
In the slide 15 example (two input channels, one filter with a 2x2 slab per input channel, stride 2, no bias), what is the top-left output value?في مثال الشريحة 15 (قناتا مدخل، ومرشّح واحد بشريحة 2x2 لكل قناة مدخل، وstride يساوي 2، بدون bias)، ما قيمة المخرج العلوية اليسرى؟
Counting parametersعدّ المعاملات
The shapes of W and b give the parameter count directly, and that count is what decides whether a model fits in a microcontroller's flash. The weights number c_o x c_i x k_h x k_w, the size of W, and there is one bias per output channel.
شكلا W وb يعطيان عدد المعاملات مباشرة، وهذا العدد هو ما يقرر هل يتسع النموذج في ذاكرة الـ flash لمتحكم دقيق. عدد الأوزان (weights) هو c_o x c_i x k_h x k_w، أي حجم W، ويوجد bias واحد لكل قناة مخرج.
Slide 25 later in this lecture shows the ConvNetJS CIFAR-10 demo, whose first convolution has 16 filters of 5 x 5 over the 3 colour channels. The demo prints 16 x 5 x 5 x 3 + 16 = 1216 parameters, and the formula reproduces it: 1200 weights and 16 biases. Use that number as a retrieval hook; if your formula does not give 1216 for that layer, you have forgotten the biases or swapped a factor.
تعرض الشريحة 25 لاحقًا في هذه المحاضرة عرض ConvNetJS على CIFAR-10، الذي فيه أول التفاف من 16 مرشّحًا بحجم 5 x 5 على القنوات اللونية الثلاث 3. يطبع العرض 16 x 5 x 5 x 3 + 16 = 1216 معاملًا، والصيغة تعيد إنتاج هذا الرقم: 1200 وزن و16 قيمة bias. استخدم هذا الرقم كخطاف للتذكّر؛ إذا لم تعطك صيغتك 1216 لهذه الطبقة، فقد نسيت الـ biases أو بدّلت أحد العوامل.
| Layerالطبقة | Arithmeticالحساب | Parametersالمعاملات | Depends on h_i, w_i?هل يعتمد على h_i وw_i؟ |
|---|---|---|---|
| Conv2D, 16 filters of 5x5, 3 to 16 channelsConv2D، 16 مرشّحًا بحجم 5x5، من 3 قنوات إلى 16 | 16 x 3 x 5 x 5 + 1616 x 3 x 5 x 5 + 16 | 12161216 | Noلا |
| Fully connected, 32x32x3 = 3072 inputs to 16 outputsطبقة كاملة الاتصال، 32x32x3 = 3072 مدخلًا إلى 16 مخرجًا | 3072 x 16 + 163072 x 16 + 16 | 49,16849,168 | Yesنعم |
| AlexNet conv1, 96 filters of 11x11, 3 to 96 channelsالطبقة conv1 في AlexNet، 96 مرشّحًا بحجم 11x11، من 3 قنوات إلى 96 | 96 x 3 x 11 x 11 + 9696 x 3 x 11 x 11 + 96 | 34,94434,944 | Noلا |
| Changeالتغيير | Arithmeticالحساب | Parametersالمعاملات |
|---|---|---|
| Input 64x64x3, convمدخل 64x64x3، التفاف | 16 x 3 x 5 x 5 + 1616 x 3 x 5 x 5 + 16 | 12161216 |
| Input 64x64x3, fully connectedمدخل 64x64x3، طبقة كاملة الاتصال | 12288 x 16 + 1612288 x 16 + 16 | 196,624196,624 |
| Kernels 3x3نوى بحجم 3x3 | 16 x 3 x 3 x 3 + 1616 x 3 x 3 x 3 + 16 | 448448 |
| Filters 3232 مرشّحًا | 32 x 3 x 5 x 5 + 3232 x 3 x 5 x 5 + 32 | 24322432 |
Only the filter geometry and the channel counts move the number.
وحدهما هندسة المرشّح وعدد القنوات يحرّكان الرقم.
Recall
How many parameters does nn.Conv2d(3, 16, kernel_size=5) have, and what changes if the images become 64x64?كم معاملًا في nn.Conv2d(3, 16, kernel_size=5)، وماذا يتغير إذا أصبحت الصور 64x64؟
Quick checkاختبار سريع
A Conv2D with 16 filters of 5x5x3 plus biases (the ConvNetJS first layer) has how many parameters?طبقة Conv2D فيها 16 مرشّحًا بحجم 5x5x3 مع الـ biases (الطبقة الأولى في ConvNetJS)، كم معاملًا فيها؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- A conv output neuron is connected only to the input neurons in its receptive field, and the same filter is reused at every position (weight sharing).عصبون المخرج في طبقة الالتفاف متصل فقط بعصبونات المدخل الواقعة في مجاله الاستقبالي (receptive field)، والمرشّح نفسه يُعاد استخدامه في كل موضع (weight sharing).
- Shapes: X (n, c_i, h_i, w_i), Y (n, c_o, h_o, w_o), W (c_o, c_i, k_h, k_w), b (c_o,). The batch size n never appears in W or b.الأشكال: X (n, c_i, h_i, w_i)، وY (n, c_o, h_o, w_o)، وW (c_o, c_i, k_h, k_w)، وb (c_o,). حجم الدفعة n لا يظهر أبدًا في W أو b.
- A filter is not 2D: it is c_i deep and produces exactly one output channel, so c_o filters give c_o channels regardless of c_i.المرشّح ليس ثنائي الأبعاد: عمقه c_i وينتج قناة مخرج واحدة بالضبط، لذا فإن c_o مرشّحًا تعطي c_o قناة مهما كانت قيمة c_i.
- Without padding at stride 1, h_o = h_i - k_h + 1: a 4x4 input with a 3x3 kernel gives 2x2; in general h_o = floor((h_i + 2p - k_h)/s) + 1.بدون padding وعند stride يساوي 1 يكون h_o = h_i - k_h + 1: مدخل 4x4 مع kernel بحجم 3x3 يعطي 2x2؛ وبشكل عام h_o = floor((h_i + 2p - k_h)/s) + 1.
- Slide 15: output size (4 - 2)/2 + 1 = 2; top-left 9 + 33 = 42, top-right 21 + 45 = 66; output [[42, 66], [42, 66]]; bias is ignored on the slide.الشريحة 15: حجم المخرج (4 - 2)/2 + 1 = 2؛ القيمة العلوية اليسرى 9 + 33 = 42، والعلوية اليمنى 21 + 45 = 66؛ المخرج [[42, 66], [42, 66]]؛ والشريحة تتجاهل الـ bias.
- Parameters = c_o c_i k_h k_w + c_o; 16 filters of 5x5x3 plus biases give 1216; the count is independent of h_i and w_i, unlike an FC layer's 3072 x 16 + 16 = 49,168.عدد المعاملات = c_o c_i k_h k_w + c_o؛ 16 مرشّحًا بحجم 5x5x3 مع الـ biases تعطي 1216؛ والعدد لا يعتمد على h_i وw_i، بخلاف طبقة FC التي تحتاج 3072 x 16 + 16 = 49,168.
Sourcesالمصادر
- Deep Learning, chapter 9: Convolutional Networks, section 9.2 MotivationBookMIT Press (Goodfellow, Bengio and Courville)Sparse interactions (m x n against k x n), parameter sharing and tied weights, receptive field of one output unitالتفاعلات المتفرقة (m x n مقابل k x n)، ومشاركة المعاملات والأوزان المربوطة (tied weights)، والمجال الاستقبالي لوحدة مخرج واحدة(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 02: Basics of Deep LearningDocsMIT HAN Lab (Song Han), Fall 2023Original slides that slides 8 to 14 reproduce: receptive field, tensor shapes, weight sharing, h_o = h_i - k_h + 1الشرائح الأصلية التي تعيد الشرائح من 8 إلى 14 إنتاجها: المجال الاستقبالي، وأشكال الموتّرات، ومشاركة الأوزان، وh_o = h_i - k_h + 1(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.Conv2dDocsPyTorch documentationWeight (out_channels, in_channels / groups, kH, kW), bias (out_channels), output size formula with padding, dilation and strideالوزن (out_channels, in_channels / groups, kH, kW)، والـ bias (out_channels)، وصيغة حجم المخرج مع padding وdilation وstride(opens in a new tab)(يفتح في تبويب جديد)
- CS231n Convolutional Neural Networks for Visual Recognition: Convolutional NetworksDocsStanford University3072 weights for one FC neuron on CIFAR-10, depth of connectivity equals input depth, AlexNet conv1 parameter counts with and without sharing3072 وزنًا لعصبون FC واحد على CIFAR-10، وعمق الاتصال يساوي عمق المدخل، وعدد معاملات conv1 في AlexNet مع المشاركة وبدونها(opens in a new tab)(يفتح في تبويب جديد)
- A guide to convolution arithmetic for deep learningPaperarXiv (Dumoulin and Visin, 2016)Relationship 1: o = i - k + 1; relationship 6: o = floor((i + 2p - k)/s) + 1العلاقة 1: o = i - k + 1؛ والعلاقة 6: o = floor((i + 2p - k)/s) + 1(opens in a new tab)(يفتح في تبويب جديد)
- ConvNetJS CIFAR-10 demoDocsAndrej Karpathy, StanfordFirst conv layer: 16 filters of 5x5, stride 1, pad 2 on a 32x32x3 input, 1216 parameters as shown on slide 25أول طبقة التفاف: 16 مرشّحًا بحجم 5x5، وstride يساوي 1، وpad يساوي 2 على مدخل 32x32x3، و1216 معاملًا كما يظهر في الشريحة 25(opens in a new tab)(يفتح في تبويب جديد)
- Gradient-based learning applied to document recognitionPaperProceedings of the IEEE (LeCun, Bottou, Bengio and Haffner, 1998)Local receptive fields and shared weights as the architectural ideas of LeNet-5; section II.A credits earlier work by Fukushima (1980) and LeCun et al. (1989)المجالات الاستقبالية المحلية والأوزان المشتركة بوصفها الفكرتين المعماريتين في LeNet-5؛ ويَنسب القسم II.A الفضل إلى أعمال سابقة لـ Fukushima (1980) وLeCun et al. (1989)(opens in a new tab)(يفتح في تبويب جديد)