多输入通道和多输出通道

多输入通道

多输入通道和多输出通道_第1张图片

多输出通道:

设卷积核输入通道,输出通道是 ci , co,高宽为 kh , kw

为每个输出通道分别建立 ci * kh * kw 的核数组。在输出通道维上连结,卷积核的形状即为co * ci *kh*kw

from mxnet import autograd,nd
from mxnet import gluon,init
from mxnet.gluon import nn,loss as gloss
from mxnet.gluon import data as gdata

# 二维卷积层
def corr2d(X,K):
    h, w = K.shape
    Y = nd.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_multi_in(X,K):
    return nd.add_n(*[corr2d(x,k) for x,k in zip(X,K)])

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

print(corr2d_multi_in(X,K))

# 多通道输出
# 为每个输出通道分别创建 ci * kh * kw 的核数组
# 将他们在输出通道维上连结,卷积核的形状即为 co * ci * kh * kw 的核数组

def corr2d_multi_in_out(X,K):
    return nd.stack(*[corr2d_multi_in(X,k) for k in K])

# 3 通道核 3 * 2 * 2 * 2
K = nd.stack(K,K+1,K+2)
print(K)
print(corr2d_multi_in_out(X,K))

 

转载于:https://www.cnblogs.com/TreeDream/p/10038674.html

你可能感兴趣的:(多输入通道和多输出通道)