多通道卷积计算
特殊的卷积层1*1卷积核
代码:
""" 多输入多输出的互相关运算 """ import torch from torch import nn from d2l import torch as d2l """ 实现多输入通道互相关运算 """ def corr2d_multi_in(x, k): return sum(d2l.corr2d(x, k) for x, k in zip(x, k)) """ 验证互相关运算的输出 """ X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]) K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]]) print(corr2d_multi_in(X, K)) """ 计算多个通道的输出的互相关函数 """ def corr2d_multi_in_out(X, K): return torch.stack([corr2d_multi_in(X, k) for k in K], 0) K = torch.stack((K, K + 1, K + 2), 0) print(K.shape) print(corr2d_multi_in_out(X, K)) """ 1*1卷积 """ def corr2d_multi_in_out_1_1(X, K): c_i, h, w = X.shape c_o = K.shape[0] X = X.reshape((c_i, h * w)) K = K.reshape((c_o, c_i)) Y = torch.matmul(K, X) return Y.reshape((c_o, h, w)) X = torch.normal(0, 1, (3, 3, 3)) K = torch.normal(0, 1, (2, 3, 1, 1)) Y1 = corr2d_multi_in_out_1_1(X, K) Y2 = corr2d_multi_in_out(X, K) assert float(torch.abs(Y1 - Y2).sum()) < 1e-6标签:multi,1.0,卷积,torch,corr2d,神经网络,李沐,out From: https://blog.csdn.net/2401_87085787/article/details/142107878