from mxnet import autograd,ndfrom mxnet import gluon,initfrom mxnet.gluon import nn,loss as glossfrom mxnet.gluon import data as gdatadef pool2d(X, pool_size, mode='max'): p_h, p_w = pool_size Y = nd.zeros((X.shape[0]-p_h+1,X.shape[1] - p_w+1)) for i in range(Y.shape[0]): for j in range(Y.shape[1]): if mode=='max': Y[i,j] = X[i:i+p_h,j:j+p_w].max() elif mode=='avg': Y[i,j] = X[i:i+p_h,j:j+p_w].mean() return Yx = nd.array([[0,1,2],[3,4,5],[6,7,8]])print(pool2d(x,(2,2)))print(pool2d(x,(2,2),'avg'))# 填充和步幅X = nd.arange(16).reshape((1,1,4,4))print(X)# MaxPool2D 默认步幅 和 池化窗口相同pool2d = nn.MaxPool2D(3)print(pool2d(X))# 指定步幅和填充pool2d = nn.MaxPool2D(3,padding=1,strides=2)print(pool2d(X))# 指定非正方形池化窗口,指定高宽上的填充和步幅pool2d = nn.MaxPool2D((2,3),padding=(1,2),strides=(2,3))print(pool2d(X))# 多通道X = nd.concat(X,X+1,dim=1)print(X)pool2d = nn.MaxPool2D(3,padding=1,strides=2)print(pool2d(X))