tf.keras.layers.MaxPooling2D

2D max pooling layer

keras.layers.MaxPooling2D(pool_size=(2, 2), 
                          strides=None, 
                          padding='valid', 
                          data_format=None)

Detailed parameters

  • pool_size : pooling window size
  • strides : the pooling stride, the default value is equal to pool_size
  • padding : ' VALID' or ' SAME', ' VALID' means no padding, ' SAME' means padding with 0
  • data_format : Indicates the dimension order of the input tensor, the default is [batch, height, width, channel]

example

from tensorflow.keras.layers import MaxPool2D
import tensorflow as tf
import numpy as np

# 定义一个最大池化层,用0填充
pool1 = MaxPool2D(pool_size=(2, 2),
                  strides=None,
                  padding='SAME',
                  data_format=None)

# 定义一个最大池化层,不填充
pool2 = MaxPool2D(pool_size=(2, 2),
                  strides=None,
                  padding='VALID',
                  data_format=None)

# 生成一个维度为[64, 101, 101, 3]的矩阵
x = np.random.random((64, 101, 101, 3))

# 转成tensor类型,第一个维度64表示batch
x = tf.convert_to_tensor(x)
print(x.shape) # [64, 101, 101, 3]

# 进行最大池化
y1 = pool1(x)
print(y1.shape) # [64, 51, 51, 3]

# 进行最大池化
y2 = pool2(x)
print(y2.shape) # [64, 50, 50, 3]

Guess you like

Origin blog.csdn.net/weixin_46566663/article/details/127621307