padding='Same' 在 Keras 中表示当输入大小和内核大小不完全匹配时,根据需要添加填充以弥补重叠。
padding='Same' 的示例:
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2) ,padding='Same'))
# Model Summary
model.summary()
代码的输出-
Model: "sequential_20"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_28 (Conv2D) (None, 3, 3, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
图示:
下图显示了当 padding='Same' 时输入 (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) 的填充情况。
----------------------------------------------- -------------------------------------------------- -----------------
Keras 中的padding='Valid' 表示不添加填充。
padding='Valid' 的示例: Conv2D 使用的输入与我们上面用于 padding = 'Same' 的输入相同。即(input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2))
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2) ,padding='Valid'))
# Model Summary
model.summary()
代码的输出-
Model: "sequential_21"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_29 (Conv2D) (None, 2, 2, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
图示:
下图显示,当 padding='Valid' 时,输入 (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) 没有添加填充。
----------------------------------------------- -------------------------------------------------- -----------------
现在让我们尝试使用与 padding='Valid' 相同的代码作为输入 (input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2))。 此处padding='Valid' 的行为应与padding='Same' 相同。
代码 -
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2) ,padding='Valid'))
# Model Summary
model.summary()
代码的输出-
Model: "sequential_22"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_30 (Conv2D) (None, 3, 3, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________