Learn local visual patterns
Image classification and feature extraction
TensorFlow · Class
tf.keras.layers.Conv2D is a 2D convolution layer that learns spatial filters and produces a configurable number of output channels. Padding, strides, dilation, groups, bias, initialization, regularization, constraints, and activation control its behavior.
Conv2D slides learned windows across two spatial dimensions, producing one output channel per filter. It can add a bias and then apply an activation; padding and stride control the output dimensions.
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
)| Name | Type | Description |
|---|---|---|
filters | int | Number of output filters, which determines the output channel dimension. |
kernel_size | int or tuple/list of 2 integers | Height and width of the convolution window; an integer or pair of integers. |
strides | int or tuple/list of 2 integers | Spatial step size for the convolution; an integer or pair of integers. |
padding | string | Padding mode: valid or same, case-insensitive. |
data_format | string or None | Input and output channel layout. |
dilation_rate | int or tuple/list of 2 integers | Dilation factor for the convolution window. |
groups | int | Number of channel groups for grouped convolution. |
activation | activation function or None | Activation function applied to the output. |
use_bias | bool | Whether to create and add a bias vector. |
kernel_initializer | initializer | Initializer for the convolution kernel. |
bias_initializer | initializer | Initializer for the bias vector. |
kernel_regularizer | regularizer or None | Regularizer for the convolution kernel. |
A tensor containing the convolution outputs, with the output channel dimension equal to filters.
import tensorflow as tf
layer = tf.keras.layers.Conv2D(
filters=32,
kernel_size=(3, 3),
padding="same",
activation="relu",
)
x = tf.random.normal((1, 28, 28, 3))
y = layer(x)
print(y.shape) # (1, 28, 28, 32)Creates a 3×3 convolution with 32 output channels, preserves spatial dimensions with same padding, and applies ReLU.
Image classification and feature extraction
Downstream convolutional architectures
Grouped channel processing
Conv2D applies learned convolution kernels across the input's two spatial dimensions. The number of filters determines the output channel count; an optional bias and activation are applied afterward.
Follow-up: How does padding affect shape? With valid padding, no border padding is added; with same padding and stride 1, the spatial output size matches the input.
filters is the number of output channels, while kernel_size specifies the two-dimensional convolution-window size. Each filter learns a spatial pattern and contributes one output channel.
Follow-up: What shape can kernel_size take? It can be one integer or a tuple/list containing two integers.
The layer accepts inputs with a channels-last or channels-first layout, selected by data_format. The default data format is determined by the Keras configuration when data_format is omitted.
Follow-up: What does groups change? It partitions input and output channels into groups, requiring compatible channel divisibility and performing grouped convolutions.
tf.keras.layers.Convolution2Dtf.keras.layers.DepthwiseConv2Dtf.nn.conv2dtf.keras.layers.Conv1Dtf.keras.layers.Conv3D