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

TensorFlow · Class

layers.Conv2D

A Keras layer that applies learned two-dimensional convolution filters to inputs. It can add bias and an activation while configurable padding, strides, dilation, layout, and groups determine the resulting feature maps.

How it works

Conv2D learns a bank of spatial kernels and applies them across a 2D input. It can add a bias and then run an activation function, producing one output channel per filter while padding, stride, layout, dilation, and grouping control how the spatial operation is performed.

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 filters, or output channels.
kernel_sizeint or tuple/list of 2 integersConvolution-window height and width.
stridesint or tuple/list of 2 integersWindow step along each spatial dimension.
paddingstringPadding mode: valid or same.
data_formatstring or NoneInput/output dimension ordering.
dilation_rateint or tuple/list of 2 integersSpacing between kernel elements.
groupsintNumber of channel groups.
activationactivation function or NoneOptional activation function.
use_biasboolWhether to add a bias vector.
kernel_initializerinitializerInitializer for the convolution kernel.
bias_initializerinitializerInitializer for the bias.
kernel_regularizerregularizer or NoneRegularizer applied to the kernel.

Returns

A tensor containing the layer's convolution outputs; its spatial layout follows data_format and its channel dimension is filters.

Key points

  • Conv2D is the Keras layer for learned two-dimensional convolution.
  • filters determines how many output feature channels the layer creates.
  • kernel_size may be one integer or two integers describing the window height and width.
  • valid padding performs the convolution without adding border values.
  • same padding adds border padding to balance the spatial treatment around the input.
  • use_bias controls whether a trainable bias vector is added to the convolution results.

Common mistakes

  • Do not combine a stride greater than one with a dilation rate greater than one.

Example: Basic Conv2D layer

import tensorflow as tf

layer = tf.keras.layers.Conv2D(
    filters=32,
    kernel_size=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

Image feature extraction

Extracting local visual patterns from images.

Vision model backbones and heads

Building convolutional classifiers or regressors.

Grouped convolutions

Reducing computation or controlling channel connectivity.

Interview questions

QWhat does a Conv2D layer compute?

Conv2D applies learned two-dimensional kernels over the input and returns feature maps. The number of output channels equals filters; optional bias is added before the optional activation.

Follow-up: How does same padding affect output size? With stride one, it keeps each spatial dimension equal to the input; with larger strides, the output is downsampled.

QHow does data_format affect Conv2D?

The input layout is controlled by data_format. channels_last uses batch, height, width, channels, while channels_first uses batch, channels, height, width.

Follow-up: What must change when switching layouts? The input tensor arrangement must match data_format, and downstream layers must interpret the resulting output consistently.

QHow do kernel_size, strides, dilation_rate, and groups shape the operation?

filters sets the output-space dimension, and kernel_size sets the two-dimensional convolution-window size. strides controls the window step, while dilation_rate spaces kernel elements apart.

Follow-up: When would groups be useful? They split channel connections into independent groups, reducing connectivity; the group count must divide both input channels and filters.

  • tf.keras.layers.Convolution2D
  • tf.keras.layers.DepthwiseConv2D
  • tf.nn.conv2d

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