tf.keras.layers.MaxPooling2D

2D最大池化层

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

参数详解

  • pool_size: 池化窗口大小
  • strides: 池化步长,默认值等于 pool_size
  • padding: 'VALID' 或 'SAME','VALID'表示无填充,'SAME'表示用0填充
  • data_format: 表示输入张量的维度顺序,默认为 [batch, height, width, channel]

示例

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]

你可能感兴趣的:(tensorflow学习笔记,深度学习,人工智能,keras,tensorflow)