CNN convolution (convolution) operation code implementation, encapsulated into a function

          Study "Hands-on Deep Learning" and take a note

import numpy as np
def corr2d(X, K):    # 接受输⼊数组X与核数组K,并输出数组Y。
    h, w = K.shape
    Y = np.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))
    for i in range(Y.shape[0]):
        for j in range(Y.shape[1]):
            Y[i, j] = (X[i: i + h, j: j + w] * K).sum()
    return Y

X = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
K = np.array([[0, 1], [2, 3]])
Y = corr2d(X, K)
print(Y)

          Output

[[19. 25.]
[37. 43.]]

         When the default stride is 1,
Insert picture description here
          this is also the theoretical basis for calculating the size of Y in our code

Guess you like

Origin blog.csdn.net/qq_43657442/article/details/109067811