2D 卷积层(例如图像上的空间卷积)。
用法
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
)参数
-
filters整数,输出空间的维度(即卷积中输出滤波器的数量)。 -
kernel_size一个整数或 2 个整数的元组/列表,指定 2D 卷积窗口的高度和宽度。可以是单个整数,为所有空间维度指定相同的值。 -
strides一个整数或 2 个整数的元组/列表,指定卷积沿高度和宽度的步幅。可以是单个整数,为所有空间维度指定相同的值。指定任何步幅值 != 1 与指定任何dilation_rate值 != 1 不兼容。 -
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)的输入。它默认为您的 Keras 配置文件中的image_data_format值~/.keras/keras.json。如果您从未设置它,那么它将是channels_last。 -
dilation_rate一个整数或 2 个整数的元组/列表,指定用于扩张卷积的扩张率。可以是单个整数,为所有空间维度指定相同的值。目前,指定任何dilation_rate值 != 1 与指定任何步幅值 != 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应用于层输出的正则化函数(其"activation")(参见keras.regularizers)。 -
kernel_constraint应用于核矩阵的约束函数(参见keras.constraints)。 -
bias_constraint应用于偏置向量的约束函数(参见keras.constraints)。
返回
-
表示
activation(conv2d(inputs, kernel) + bias)的 4+ 阶张量。
抛出
-
ValueError如果padding是"causal"。 -
ValueError当strides > 1和dilation_rate > 1时。
该层创建一个卷积核,该卷积核与层输入进行卷积以产生输出张量。如果 use_bias 为 True,则会创建一个偏置向量并将其添加到输出中。最后,如果 activation 不是 None ,它也会应用于输出。
当将此层用作模型中的第一层时,请提供关键字参数 input_shape(整数元组或 None ,不包括样本轴),例如input_shape=(128, 128, 3) 用于 data_format="channels_last" 中的 128x128 RGB 图片。当维度具有可变大小时,您可以使用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)
输入形状:
4+D 张量与形状:batch_shape + (channels, rows, cols) if data_format='channels_first' 或 4+D 张量与形状:batch_shape + (rows, cols, channels) if data_format='channels_last'。
输出形状:
4+D 张量与形状:batch_shape + (filters, new_rows, new_cols) if data_format='channels_first' 或 4+D 张量与形状:batch_shape +
(new_rows, new_cols, filters) if data_format='channels_last'。 rows 和 cols 值可能因填充而发生更改。
相关用法
- Python tf.keras.layers.Conv2DTranspose用法及代码示例
- Python tf.keras.layers.Conv1D用法及代码示例
- Python tf.keras.layers.Conv1DTranspose用法及代码示例
- Python tf.keras.layers.Conv3D用法及代码示例
- Python tf.keras.layers.Conv3DTranspose用法及代码示例
- Python tf.keras.layers.Concatenate用法及代码示例
- Python tf.keras.layers.CategoryEncoding用法及代码示例
- Python tf.keras.layers.Cropping1D用法及代码示例
- Python tf.keras.layers.Cropping2D用法及代码示例
- Python tf.keras.layers.Cropping3D用法及代码示例
- Python tf.keras.layers.InputLayer用法及代码示例
- Python tf.keras.layers.serialize用法及代码示例
- Python tf.keras.layers.Dropout用法及代码示例
- Python tf.keras.layers.maximum用法及代码示例
- Python tf.keras.layers.LayerNormalization用法及代码示例
- Python tf.keras.layers.RepeatVector用法及代码示例
- Python tf.keras.layers.Multiply用法及代码示例
- Python tf.keras.layers.Activation用法及代码示例
- Python tf.keras.layers.experimental.preprocessing.PreprocessingLayer.adapt用法及代码示例
- Python tf.keras.layers.subtract用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.keras.layers.Conv2D。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
