pool2d¶
- paddle.fluid.layers. pool2d ( input, pool_size=- 1, pool_type='max', pool_stride=1, pool_padding=0, global_pooling=False, use_cudnn=True, ceil_mode=False, name=None, exclusive=True, data_format='NCHW' ) [源代码] ¶
该OP使用上述输入参数的池化配置,为二维空间池化操作,根据 input
,池化核大小 pool_size
,池化类型 pool_type
,步长 pool_stride
,填充 pool_padding
等参数得到输出。
输入 input
和输出(out)采用NCHW或NHWC格式,N为批大小,C是通道数,H是特征高度,W是特征宽度。
参数 pool_size
和 pool_stride
含有两个整型元素,分别表示高度和宽度维度上的参数。
输入 input
和输出(out)的形状可能不同。
例如:
- 输入:
-
input
的形状:\(\left ( N,C,H_{in},W_{in} \right )\) - 输出:
-
out
的形状:\(\left ( N,C,H_{out},W_{out} \right )\)
如果 ceil_mode
= false:
如果 ceil_mode
= true:
如果 exclusive
= false:
如果 exclusive
= true:
如果 pool_padding
= "SAME":
如果 pool_padding
= "VALID":
- 参数:
-
input (Variable) - 形状为 \([N, C, H, W]\) 或 \([N, H, W, C]\) 的4-D Tensor,N是批尺寸,C是通道数,H是特征高度,W是特征宽度,数据类型为float32或float64。
pool_size (int|list|tuple) - 池化核的大小。如果它是一个元组或列表,那么它包含两个整数值:(pool_size_Height, pool_size_Width)。若为一个整数,则表示H和W维度上均为该值,比如若pool_size=2, 则池化核大小为[2,2]。
pool_type (str) - 池化类型,可以为"max"或"avg","max"对应max-pooling,"avg"对应average-pooling。默认值:"max"。
pool_stride (int|list|tuple) - 池化层的步长。如果它是一个元组或列表,它将包含两个整数:(pool_stride_Height, pool_stride_Width)。若为一个整数,则表示H和W维度上均为该值,比如若pool_stride=3, 则池化层步长为[3,3]。默认值:1。
pool_padding (int|list|tuple|str) - 池化填充。如果它是一个字符串,可以是"VALID"或者"SAME",表示填充算法,计算细节可参考上述
pool_padding
= "SAME"或pool_padding
= "VALID" 时的计算公式。如果它是一个元组或列表,它可以有3种格式:(1)包含2个整数值:[pad_height, pad_width];(2)包含4个整数值:[pad_height_top, pad_height_bottom, pad_width_left, pad_width_right];(3)包含4个二元组:当data_format
为"NCHW"时为 [[0,0], [0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]],当data_format
为"NHWC"时为[[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]。若为一个整数,则表示H和W维度上均为该值。默认值:0。global_pooling (bool)- 是否用全局池化。如果global_pooling = True, 已设置的
pool_size
和pool_padding
会被忽略,pool_size
将被设置为 \([H_{in}, W_{in}]\) ,pool_padding
将被设置为0。默认值:False。use_cudnn (bool)- 是否使用cudnn内核。只有已安装cudnn库时才有效。默认值:True。
ceil_mode (bool)- 是否用ceil函数计算输出高度和宽度。计算细节可参考上述
ceil_mode
= true或ceil_mode
= false 时的计算公式。默认值:False。name (str,可选) – 具体用法请参见 Name ,一般无需设置。默认值:None。
exclusive (bool) - 是否在平均池化模式忽略填充值。计算细节可参考上述
exclusive
= true或exclusive
= false 时的计算公式。默认值:True。data_format (str) - 输入和输出的数据格式,可以是"NCHW"和"NHWC"。N是批尺寸,C是通道数,H是特征高度,W是特征宽度。默认值:"NCHW"。
返回: 4-D Tensor,数据类型与 input
一致。
返回类型:Variable。
- 抛出异常:
-
ValueError
- 如果pool_type
既不是"max"也不是"avg"。ValueError
- 如果global_pooling
为False并且pool_size
为-1。TypeError
- 如果use_cudnn
不是bool值。ValueError
- 如果data_format
既不是"NCHW"也不是"NHWC"。ValueError
- 如果pool_padding
是字符串,既不是"SAME"也不是"VALID"。ValueError
- 如果pool_padding
是"VALID",但是ceil_mode
是True。ValueError
- 如果pool_padding
含有4个二元组,与批尺寸对应维度的值不为0或者与通道对应维度的值不为0。ShapeError
- 如果input
既不是4-D Tensor 也不是5-D Tensor。ShapeError
- 如果input
的维度减去pool_stride
的尺寸大小不是2。ShapeError
- 如果pool_size
和pool_stride
的尺寸大小不相等。ShapeError
- 如果计算出的输出形状的元素值不大于0。
代码示例
import paddle.fluid as fluid
data_NCHW = fluid.data(name='data', shape=[None, 3, 8, 8], dtype='float32')
data_NHWC = fluid.data(name='data', shape=[None, 8, 8, 3], dtype='float32')
# example 1:
# ceil_mode = False
out_1 = fluid.layers.pool2d(
input=data_NCHW, # shape: [2, 3, 8, 8]
pool_size=[3,3],
pool_type='avg',
pool_stride=[3,3],
pool_padding=[2,1], # it is same as pool_padding = [2,2,1,1]
global_pooling=False,
ceil_mode=False,
exclusive=True,
data_format="NCHW")
# shape of out_1: [2, 3, 4, 3]
# example 2:
# ceil_mode = True (different from example 1)
out_2 = fluid.layers.pool2d(
input=data_NCHW,
pool_size=[3,3],
pool_type='avg',
pool_stride=[3,3],
pool_padding=[[0,0], [0,0], [2,2], [1,1]], # it is same as pool_padding = [2,2,1,1]
global_pooling=False,
ceil_mode=True,
exclusive=True,
data_format="NCHW")
# shape of out_2: [2, 3, 4, 4] which is different from out_1
# example 3:
# pool_padding = "SAME" (different from example 1)
out_3 = fluid.layers.pool2d(
input=data_NCHW,
pool_size=[3,3],
pool_type='avg',
pool_stride=[3,3],
pool_padding="SAME",
global_pooling=False,
ceil_mode=False,
exclusive=True,
data_format="NCHW")
# shape of out_3: [2, 3, 3, 3] which is different from out_1
# example 4:
# pool_padding = "VALID" (different from example 1)
out_4 = fluid.layers.pool2d(
input=data_NCHW,
pool_size=[3,3],
pool_type='avg',
pool_stride=[3,3],
pool_padding="VALID",
global_pooling=False,
ceil_mode=False,
exclusive=True,
data_format="NCHW")
# shape of out_4: [2, 3, 2, 2] which is different from out_1
# example 5:
# global_pooling = True (different from example 1)
# It will be set pool_size = [8,8] and pool_padding = [0,0] actually.
out_5 = fluid.layers.pool2d(
input=data_NCHW,
pool_size=[3,3],
pool_type='avg',
pool_stride=[3,3],
pool_padding=[2,1],
global_pooling=True,
ceil_mode=False,
exclusive=True,
data_format="NCHW")
# shape of out_5: [2, 3, 1, 1] which is different from out_1
# example 6:
# data_format = "NHWC" (different from example 1)
out_6 = fluid.layers.pool2d(
input=data_NHWC, # shape: [2, 8, 8, 3]
pool_size=[3,3],
pool_type='avg',
pool_stride=[3,3],
pool_padding=[2,1],
global_pooling=False,
ceil_mode=False,
exclusive=True,
data_format="NHWC")
# shape of out_6: [2, 4, 3, 3] which is different from out_1