//目录

池化层pooling

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 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 Y

x = 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))

 

posted @ 2018-11-29 16:55  小草的大树梦  阅读(298)  评论(0编辑  收藏  举报