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)
参数
dilation_rate 值不兼容。"valid" 或 "same" 之一(不区分大小写)。"valid" 表示无填充。"same" 表示在输入的左/右或上/下均匀填充零。当 padding="same" 且 strides=1 时,输出与输入具有相同的大小。channels_last(默认)或 channels_first 之一。输入中维度的顺序。channels_last 对应于形状为 (batch_size, height, width, channels) 的输入,而 channels_first 对应于形状为 (batch_size, channels, height, width) 的输入。如果未指定,它将使用 ~/.keras/keras.json 中的 TF-Keras 配置文件中的 image_data_format 值(如果存在),否则使用 'channels_last'。请注意,channels_first 格式目前不受 TensorFlow 在 CPU 上的支持。默认为 'channels_last'。dilation_rate 值与指定任何 != 1 的步长值不兼容。filters / groups 个滤波器进行卷积。输出是将所有 groups 的结果沿通道轴连接起来。输入通道数和 filters 都必须能被 groups 整除。keras.activations)。kernel 权重矩阵的初始化器(请参阅 keras.initializers)。默认为 'glorot_uniform'。keras.initializers)。默认为 'zeros'。kernel 权重矩阵的正则化函数(请参阅 keras.regularizers)。keras.regularizers)。keras.regularizers)。keras.constraints)。keras.constraints)。输入形状
形状为 batch_shape + (channels, rows, cols) 的 4+D 张量(如果 data_format='channels_first')或形状为 batch_shape + (rows, cols, channels) 的 4+D 张量(如果 data_format='channels_last')。
输出形状
形状为 batch_shape + (filters, new_rows, new_cols) 的 4+D 张量(如果 data_format='channels_first')或形状为 batch_shape + (new_rows, new_cols, filters) 的 4+D 张量(如果 data_format='channels_last')。由于填充,rows 和 cols 的值可能已更改。
返回
一个表示 activation(conv2d(inputs, kernel) + bias) 的秩为 4+ 的张量。
抛出
padding 为 "causal"。strides > 1 且 dilation_rate > 1 时。