M3-097M3: Deep Learning FoundationsConvolution & Vision FoundationsEasy
⚡Flashcard Drill➔Mastery:

Calculating Convolutional Layer Parameter Count and Output Spatial Dimensions

📐 Mathematical Definition
params=k2CinCout+Cout;Hout=⌊H+2P−kS⌋+1\text{params}=k^2 C_{in}C_{out}+C_{out};\qquad H_{out}=\left\lfloor\frac{H+2P-k}{S}\right\rfloor+1
⚡ One-Sentence Core Answer
Output spatial dimension is ⌊W−K+2PS⌋+1\lfloor \frac{W - K + 2P}{S} \rfloor + 1; total parameters equal K2⋅Cin⋅Cout+CoutK^2 \cdot C_{\text{in}} \cdot C_{\text{out}} + C_{\text{out}} (with bias).

📌 Key Takeaways

  • •
    Output spatial formula: O=⌊I−K+2PS⌋+1O = \lfloor \frac{I - K + 2P}{S} \rfloor + 1, where II is input size, KK kernel, PP padding, SS stride
  • •
    Parameter count formula: Params=Kh⋅Kw⋅Cin⋅Cout+Cout\text{Params} = K_h \cdot K_w \cdot C_{\text{in}} \cdot C_{\text{out}} + C_{\text{out}} (with bias)
  • •
    Input size independence: convolutional parameter counts are strictly independent of input image spatial height HH and width WW

📐 Mathematical Derivations

Exact Mathematical Formulas:
① Output Spatial Dimensions:
Given input height HinH_{\text{in}}, width WinW_{\text{in}}, kernel size Kh×KwK_h \times K_w, padding Ph,PwP_h, P_w, dilation Dh,DwD_h, D_w, and stride Sh,SwS_h, S_w:
Effective kernel size with dilation: K~=D(K−1)+1\tilde{K} = D(K - 1) + 1.
Hout=⌊Hin−K~h+2PhSh⌋+1H_{\text{out}} = \left\lfloor \frac{H_{\text{in}} - \tilde{K}_h + 2 P_h}{S_h} \right\rfloor + 1, Wout=⌊Win−K~w+2PwSw⌋+1\quad W_{\text{out}} = \left\lfloor \frac{W_{\text{in}} - \tilde{K}_w + 2 P_w}{S_w} \right\rfloor + 1.
- Same Padding Condition (S=1,Hout=HinS=1, H_{\text{out}} = H_{\text{in}}): Requires 2P=K−1  ⟹  P=K−122P = K - 1 \implies P = \frac{K - 1}{2} (for odd kernel KK).
② Parameter Count:
Each output channel requires a 3D kernel of shape (Kh,Kw,Cin)(K_h, K_w, C_{\text{in}}) plus 1 scalar bias.
Params=Cout×(Kh×Kw×Cin+1)\text{Params} = C_{\text{out}} \times (K_h \times K_w \times C_{\text{in}} + 1).
③ Computational FLOPs (Multiply-Adds):
FLOPs=Hout×Wout×Cout×(Kh×Kw×Cin)\text{FLOPs} = H_{\text{out}} \times W_{\text{out}} \times C_{\text{out}} \times (K_h \times K_w \times C_{\text{in}}). Notice that FLOPs scale directly with output spatial resolution HoutWoutH_{\text{out}} W_{\text{out}}, whereas parameters do not.

🏭 Production Trade-offs

Calculated Example: For an input of shape (3,224,224)(3, 224, 224) processed by a Conv2D layer with Cout=64,K=7,S=2,P=3C_{\text{out}} = 64, K=7, S=2, P=3:
- Output size: ⌊224−7+2(3)2⌋+1=⌊2232⌋+1=111+1=112\lfloor \frac{224 - 7 + 2(3)}{2} \rfloor + 1 = \lfloor \frac{223}{2} \rfloor + 1 = 111 + 1 = 112. Output shape is (64,112,112)(64, 112, 112).
- Parameters: 7×7×3×64+64=9,408+64=9,4727 \times 7 \times 3 \times 64 + 64 = 9,408 + 64 = 9,472.
⚠️ Common Interview Pitfalls
  • ✕
    Forgetting to floor the division ⌊… ⌋\lfloor \dots \rfloor, leading to off-by-one errors in asymmetric stride calculations
  • ✕
    Confusing parameter count (independent of H,WH, W) with computational FLOPs (proportional to H×WH \times W)
🎯 Interviewer Follow-up Inquiries
  • ?
    How does dilated (atrous) convolution expand receptive field without adding a single trainable parameter?
  • ?
    What padding value PP is required to preserve exact spatial resolution for an odd kernel of size KK with stride S=1S=1?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM3-096: Parameter Sharing in Deep Learning: Forms, Mechanisms, and Regularizing Impact📋Back to BankNext →M3-098: Recursive Calculation of Receptive Field in Convolutional Networks
中文速览与面试要点

计算卷积层的参数量与输出尺寸

参数量 = k²·C_in·C_out + C_out(含 bias);输出尺寸 = ⌊(H+2P−k)/S⌋+1。

核心要点

  • 数学机理: 参数量 ——一个卷积层有 C_out 个滤波器,每个滤波器是 k×k×C_in 的权重张量(对每个输入通道有独立的 k×k 核),故参数量 = k²·C_in·C_out,加 C_out 个 bias。 关键性质 :参数量 与输入的空间尺寸(H、W)无关 ——因为同一组权重在所有位置滑动使用(权重共享),这是 CNN 参数效率的来源(对比全连接层:参数量 ∝ H·W·C_in·C_out,随图像尺寸爆炸)。 输出尺寸 ——卷积在每个位置计算'k×k 窗口与权重的内积',窗口以 stride S 滑动,加 P 圈 padding;输出尺寸 H_out=⌊(H+2P−k)/S⌋+1(向下取整,因为不足一个窗口的部分被丢弃)。 'same' 卷积 (输出尺寸=输入)要求 P=(k−1)/2(k 为奇数)且 S=1。 感受野 ——单层的感受野为 k×k;L 层堆叠(每层 k×k、stride 1)的感受野为 1+L·(k−1)(线性增长);含 stride 时按累积 stride 放大。 有效感受野 (实际影响输出的区域)通常小于理论感受野,且近似高斯分布(中心权重大、边缘小)——这是'深层小核优于浅层大核'的原因之一(VGG 用 3×3 堆叠替代大核)。
  • 深度剖析与工程权衡:① 3×3 堆叠 vs 大核 ——两个 3×3 卷积的感受野等于一个 5×5,但参数更少(2·9=18 vs 25)、非线性更多(两次 ReLU)、故表达能力更强;这是 VGG 的核心论证。② dilation(空洞卷积) ——在不增参数的情况下扩大感受野(核元素间插入空洞),用于分割(DeepLab)与语音;代价是'网格效应'(采样稀疏)。③ stride 与下采样 ——stride>1 同时降分辨率与扩感受野;但会丢失信息,故现代架构常用'stride 1 + pooling'或'可分离下采样'替代。④ 计算量(FLOPs) ——FLOPs = k²·C_in·C_out·H_out·W_out(参数量 × 输出空间尺寸);这是'为什么深而窄优于浅而宽'的量化依据。⑤ 'same' padding 的边界效应 ——零填充会在边界引入'虚假的零',故有用 reflect/replicate padding 的变体。⑥ 面试要点 ——被问'算一下这个卷积层',应能同时给出 参数量与 FLOPs (前者与空间尺寸无关、后者有关),并解释'3×3 堆叠为何优于大核';这是 CV 基础题的高分回答。
  • 参数量与输入空间尺寸无关(权重共享)
  • 输出尺寸由 padding P、stride S、核 k 决定
  • 感受野随层数增长,'有效感受野'小于理论值

常见陷阱

  • 混淆参数量与 FLOPs(后者含空间尺寸)
  • 忘记向下取整导致输出尺寸算错