Skip to content
BIASED
Back to topic

Optional

Convolution output size and parameters

H′ formula and NumPy cross-correlation on the topic example; ResNet stem parameter count. Self-check in the notebook.

Version
v1
Minutes
~20
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

DL · Размер выхода свёртки / Convolution output sizing

Activity version: 1 · Seed: n/a · Данные: синтетические тензоры; без внешних датасетов.

Сверяет формулу стороны после свёртки с NumPy cross-correlation (тема Свёртка):

H′=⌊H+2p−ks⌋+1H' = \left\lfloor \frac{H + 2p - k}{s} \right\rfloor + 1

и число параметров Cout(Cink2+1)C_{\mathrm{out}}(C_{\mathrm{in}} k^2 + 1).

Ожидаемые числа
Case (H,W,k,p,s)(H,W,k,p,s) H′×W′H'\times W' params
same 3×33\times3 (224,224,3,1,1)(224,224,3,1,1) 224×224224\times224 17921792 (Cin=3,Cout=64C_{in}{=}3,C_{out}{=}64, bias)
ResNet stem (224,224,7,3,2)(224,224,7,3,2) 112×112112\times112 94729472 / 94089408 без bias
topic Y0,0Y_{0,0} 4×44\times4, KK cols −1,0,1-1,0,1 2×22\times2 Y0,0=5Y_{0,0}=5, row0 [5,7]
Самопроверка в ноутбуке

Активность не входит в процент освоения.

import numpy as np
 
def out_side(H, k, p, s):
    return (H + 2 * p - k) // s + 1
 
assert out_side(224, 3, 1, 1) == 224
assert out_side(224, 7, 3, 2) == 112
assert out_side(4, 3, 0, 1) == 2
 
def n_params(C_in, C_out, k, bias=True):
    return C_out * (C_in * k * k + (1 if bias else 0))
 
assert n_params(3, 64, 3, True) == 1792
assert n_params(3, 64, 7, True) == 9472
assert n_params(3, 64, 7, False) == 9408
print("sizes OK", out_side(224, 7, 3, 2), n_params(3, 64, 3, True))
 
X = np.array([[1, 0, 2, 3], [0, 1, 3, 2], [1, 0, 2, 3], [0, 1, 2, 3]], dtype=float)
K = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=float)
 
def corr2d(X, K):
    H, W = X.shape
    k = K.shape[0]
    out = np.zeros((H - k + 1, W - k + 1))
    for i in range(out.shape[0]):
        for j in range(out.shape[1]):
            out[i, j] = np.sum(X[i:i + k, j:j + k] * K)
    return out
 
Y = corr2d(X, K)
print("Y", Y)
assert Y[0, 0] == 5
assert list(Y[0]) == [5.0, 7.0]
assert list(Y[1]) == [6.0, 6.0]
print("OK: convolution sizing + worked example")
 
Самопроверка в ноутбуке
  • same / ResNet stem размеры совпали
  • params 1792 и 9472 (с bias) / 9408 без bias
  • Y0,0=5Y_{0,0}=5, первая строка [5, 7]