Keras 2 API 文档 / 层 API / 重塑层 / ZeroPadding2D 层

ZeroPadding2D 层

[源]

ZeroPadding2D

tf_keras.layers.ZeroPadding2D(padding=(1, 1), data_format=None, **kwargs)

用于二维输入(例如图片)的零填充层。

此层可以在图像张量的顶部、底部、左侧和右侧添加零行和零列。

示例

>>> input_shape = (1, 1, 2, 2)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[[0 1]
   [2 3]]]]
>>> y = tf.keras.layers.ZeroPadding2D(padding=1)(x)
>>> print(y)
tf.Tensor(
  [[[[0 0]
     [0 0]
     [0 0]
     [0 0]]
    [[0 0]
     [0 1]
     [2 3]
     [0 0]]
    [[0 0]
     [0 0]
     [0 0]
     [0 0]]]], shape=(1, 3, 4, 2), dtype=int64)

参数

  • padding: 整型,或 2 个整型的元组,或由 2 个 2 个整型的元组组成的元组。
    • 如果为整型:对高度和宽度应用相同的对称填充。
    • 如果为 2 个整型的元组:解释为高度和宽度的两个不同对称填充值:(symmetric_height_pad, symmetric_width_pad)
    • 如果为由 2 个 2 个整型的元组组成的元组:解释为 ((顶部填充, 底部填充), (左侧填充, 右侧填充))
  • data_format: 字符串,可选值为 channels_last(默认)或 channels_first。输入维度的顺序。channels_last 对应形状为 (batch_size, height, width, channels) 的输入,而 channels_first 对应形状为 (batch_size, channels, height, width) 的输入。未指定时,使用 TF-Keras 配置文件 ~/.keras/keras.json 中找到的 image_data_format 值(如果存在),否则为 'channels_last'。默认为 'channels_last'。

输入形状

4D 张量,形状如下: - 如果 data_format"channels_last"(batch_size, rows, cols, channels) - 如果 data_format"channels_first"(batch_size, channels, rows, cols)

输出形状

4D 张量,形状如下: - 如果 data_format"channels_last"(batch_size, padded_rows, padded_cols, channels) - 如果 data_format"channels_first"(batch_size, channels, padded_rows, padded_cols)