卷积的理解 python代码实现 pytorch 多输入多输出通道的理解
ef corr2d(X,K): h,w = K.shape Y = torch.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 def corr2d_mutil_in(X,K): h,w = K.shape[1],K.shape[2] value = torch.zeros(X.shape[0] - h + 1,X.shape[1] - w + 1) for x,k in zip(X,K): value = value + corr2d(x,k) return value X = torch.tensor([[[1,2,3],[4,5,6],[7,8,9]], [[1,1,1],[1,1,1],[1,1,1]], [[2,2,2],[2,2,2],[2,2,2]]]) K = torch.tensor([[[1]],[[2]],[[3]]]) print(K.shape) corr2d_mutil_in(X,K) Output: tensor([[ 9., 10., 11.], [12., 13., 14.], [15., 16., 17.]])
X = torch.tensor([[[1,2,3],[4,5,6],[7,8,9]], [[1,1,1],[1,1,1],[1,1,1]], [[2,2,2],[2,2,2],[2,2,2]]]) K = torch.tensor([[[[1]],[[2]],[[3]]], [[[4]],[[1]],[[1]]], [[[5]],[[3]],[[3]]]]) print(K.shape) 输出: torch.Size([3, 3, 1, 1])
def corr2d_multi_in_out(X,K): return torch.stack([corr2d_mutil_in(X,k) for k in K]) corr2d_multi_in_out(X,K) 输出: tensor([[[ 9., 10., 11.], [12., 13., 14.], [15., 16., 17.]], [[ 7., 11., 15.], [19., 23., 27.], [31., 35., 39.]], [[14., 19., 24.], [29., 34., 39.], [44., 49., 54.]]])