Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

Conv2D

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.

How it works

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.

Syntax

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
)

Parameters

NameTypeDescription
filtersintNumber of output filters, which determines the output channel dimension.
kernel_sizeint or tuple/list of 2 integersHeight and width of the convolution window; an integer or pair of integers.
stridesint or tuple/list of 2 integersSpatial step size for the convolution; an integer or pair of integers.
paddingstringPadding mode: valid or same, case-insensitive.
data_formatstring or NoneInput and output channel layout.
dilation_rateint or tuple/list of 2 integersDilation factor for the convolution window.
groupsintNumber of channel groups for grouped convolution.
activationactivation function or NoneActivation function applied to the output.
use_biasboolWhether to create and add a bias vector.
kernel_initializerinitializerInitializer for the convolution kernel.
bias_initializerinitializerInitializer for the bias vector.
kernel_regularizerregularizer or NoneRegularizer for the convolution kernel.

Returns

A tensor containing the convolution outputs, with the output channel dimension equal to filters.

Key points

  • Conv2D is Keras's layer for learned two-dimensional convolution.
  • filters controls the number of output channels produced by the layer.
  • kernel_size defines the height and width of the sliding convolution window.
  • same padding distributes border padding across the spatial dimensions, whereas valid adds none.
  • use_bias determines whether a trainable bias vector is added after convolution.
  • A non-null activation transforms the convolution result after the optional bias addition.

Common mistakes

  • Do not combine a stride greater than 1 with a dilation rate greater than 1.
  • Same padding preserves spatial dimensions only when the stride is 1.

Example: Basic 2D convolution

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.

When to use it

Learn local visual patterns

Image classification and feature extraction

Build CNN blocks

Downstream convolutional architectures

Reduce channel mixing or computation

Grouped channel processing

Interview questions

QWhat does tf.keras.layers.Conv2D do?

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.

QWhat are filters and kernel_size?

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.

QHow do data_format and groups affect Conv2D?

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.Convolution2D
  • tf.keras.layers.DepthwiseConv2D
  • tf.nn.conv2d
  • tf.keras.layers.Conv1D
  • tf.keras.layers.Conv3D

Source: tf.keras.layers.Conv2D. Checked 2026-10-15.