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
的元組,不包括樣本軸),例如,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)
引數
dilation_rate
值 != 1 不相容。"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)
的輸入。如果未指定,則會使用在您的 TF-Keras 設定檔 ~/.keras/keras.json
(如果存在)中找到的 image_data_format
值,否則使用 'channels_last'。請注意,目前 TensorFlow 在 CPU 上不支援 channels_first
格式。預設為 'channels_last'。dilation_rate
值 != 1 與指定任何步幅值 != 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
)。輸入形狀
如果 data_format='channels_first'
,則為形狀為 batch_shape + (channels, rows, cols)
的 4+D 張量;如果 data_format='channels_last'
,則為形狀為 batch_shape + (rows, cols, channels)
的 4+D 張量。
輸出形狀
如果 data_format='channels_first'
,則為形狀為 batch_shape + (filters, new_rows, new_cols)
的 4+D 張量;如果 data_format='channels_last'
,則為形狀為 batch_shape + (new_rows, new_cols, filters)
的 4+D 張量。由於填充,rows
和 cols
值可能已變更。
傳回
表示 activation(conv2d(inputs, kernel) + bias)
的秩 4+ 張量。
引發
padding
為 "causal"
。strides > 1
且 dilation_rate > 1
時。