carlber

1.多输入通道

  当输入数据含多个通道时,我们需要构造一个输入通道数与输入数据的通道数相同的卷积核。以1维卷积为例,卷积窗口大小为1*1,输入有三个通道,所以卷积的通道数也应该为3个通道。如下图所示,输入的数据有三个通道,卷积也有三个通道,每个通道都是一个1维的卷积核且卷积核的大小为11, 但是这样当输入通道有多个时,我们对各个通道的结果进行了累加,所以不论输入通道数是多少,输出通道数总是1.

        

代码实现:

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.]])

 

2.多输出通道

           

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.]]])

  

  

分类:

技术点:

相关文章:

猜你喜欢