Conv2D 层

[源代码]

Conv2D

tf_keras.layers.Conv2D(
    filters,
    kernel_size,
    strides=(1, 1),
    padding="valid",
    data_format=None,
    dilation_rate=(1, 1),
    groups=1,
    activation=None,
    use_bias=True,
    kernel_initializer="glorot_uniform",
    bias_initializer="zeros",
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs
)

2D 卷积层(例如,图像上的空间卷积)。

此层创建一个卷积核,该卷积核与层输入进行卷积,以生成输出张量。如果 use_bias 为 True,则会创建一个偏置向量并添加到输出中。最后,如果 activation 不为 None,则也会将其应用于输出。

当使用此层作为模型的第一层时,请提供关键字参数 input_shape(整数或 None 的元组,不包括样本轴),例如,对于 data_format="channels_last" 中的 128x128 RGB 图片,请使用 input_shape=(128, 128, 3)。当某个维度的大小可变时,您可以使用 None

示例

>>> # The inputs are 28x28 RGB images with `channels_last` and the batch
>>> # size is 4.
>>> input_shape = (4, 28, 28, 3)
>>> x = tf.random.normal(input_shape)
>>> y = tf.keras.layers.Conv2D(
... 2, 3, activation='relu', input_shape=input_shape[1:])(x)
>>> print(y.shape)
(4, 26, 26, 2)
>>> # With `dilation_rate` as 2.
>>> input_shape = (4, 28, 28, 3)
>>> x = tf.random.normal(input_shape)
>>> y = tf.keras.layers.Conv2D(
...     2, 3,
...     activation='relu',
...     dilation_rate=2,
...     input_shape=input_shape[1:])(x)
>>> print(y.shape)
(4, 24, 24, 2)
>>> # With `padding` as "same".
>>> input_shape = (4, 28, 28, 3)
>>> x = tf.random.normal(input_shape)
>>> y = tf.keras.layers.Conv2D(
... 2, 3, activation='relu', padding="same", input_shape=input_shape[1:])(x)
>>> print(y.shape)
(4, 28, 28, 2)
>>> # With extended batch shape [4, 7]:
>>> input_shape = (4, 7, 28, 28, 3)
>>> x = tf.random.normal(input_shape)
>>> y = tf.keras.layers.Conv2D(
... 2, 3, activation='relu', input_shape=input_shape[2:])(x)
>>> print(y.shape)
(4, 7, 26, 26, 2)

参数

  • filters:整数,输出空间的维度(即卷积中的输出滤波器数量)。
  • kernel_size:一个整数或包含 2 个整数的元组/列表,指定 2D 卷积窗口的高度和宽度。可以是一个整数,为所有空间维度指定相同的值。
  • strides:一个整数或 2 个整数的元组/列表,指定卷积在高度和宽度方向上的步幅。可以是一个单独的整数,为所有空间维度指定相同的值。指定任何不等于 1 的步幅值与指定任何不等于 1 的 dilation_rate 值不兼容。
  • padding"valid""same"(不区分大小写)之一。"valid" 表示无填充。"same" 会在输入层的左/右或上/下进行零填充。当 padding="same"strides=1 时,输出的大小与输入相同。
  • data_format:一个字符串,channels_last(默认)或 channels_first 之一。输入中维度的顺序。channels_last 对应于形状为 (batch_size, height, width, channels) 的输入,而 channels_first 对应于形状为 (batch_size, channels, height, width) 的输入。如果未指定,则使用您 TF-Keras 配置文件中的 image_data_format 值,该文件位于 ~/.keras/keras.json(如果存在),否则默认为 'channels_last'。请注意,channels_first 格式目前不受 TensorFlow on CPU 的支持。默认为 'channels_last'。
  • dilation_rate:一个整数或 2 个整数的元组/列表,指定要用于空洞卷积的空洞率。可以是一个单独的整数,为所有空间维度指定相同的值。目前,指定任何不等于 1 的 dilation_rate 值与指定任何不等于 1 的步幅值不兼容。
  • groups:一个正整数,指定输入在通道轴上分割的组数。每组独立地用 filters / groups 个滤波器进行卷积。输出是所有 groups 结果沿通道轴的连接。输入通道和 filters 都必须能被 groups 整除。
  • activation:要使用的激活函数。如果您不指定任何内容,则不应用激活(请参阅 keras.activations)。
  • use_bias:布尔值,表示该层是否使用偏置向量。
  • kernel_initializerkernel 权重矩阵的初始化器(请参阅 keras.initializers)。默认为 'glorot_uniform'。
  • bias_initializer:偏置向量的初始化器(请参阅 keras.initializers)。默认为 'zeros'。
  • kernel_regularizer:应用于 kernel 权重矩阵的正则化函数(请参阅 keras.regularizers)。
  • bias_regularizer:应用于偏置向量的正则化函数(请参阅 keras.regularizers)。
  • activity_regularizer:应用于层输出(其“激活”)的正则化函数(请参阅 keras.regularizers)。
  • kernel_constraint:应用于核矩阵的约束函数(请参阅 keras.constraints)。
  • bias_constraint:应用于偏置向量的约束函数(请参阅 keras.constraints)。

输入形状

4+D 张量,形状为:如果 data_format='channels_first',则为 batch_shape + (channels, rows, cols);如果 data_format='channels_last',则为 batch_shape + (rows, cols, channels)

输出形状

4+D 张量,形状为:如果 data_format='channels_first',则为 batch_shape + (filters, new_rows, new_cols);如果 data_format='channels_last',则为 batch_shape + (new_rows, new_cols, filters)rowscols 的值可能因填充而改变。

返回

一个秩为 4+ 的张量,表示 activation(conv2d(inputs, kernel) + bias)

引发

  • ValueError:如果 padding"causal"
  • ValueError:当 strides > 1dilation_rate > 1 都存在时。