VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/python-tensorflow-tf-keras-layers-conv2d-function/

⇱ Python Tensorflow - tf.keras.layers.Conv2D() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Tensorflow - tf.keras.layers.Conv2D() Function

Last Updated : 23 Jul, 2025

The tf.keras.layers.Conv2D() function in TensorFlow is a key building block of Convolutional Neural Networks (CNNs). It applies convolutional operations to input images, extracting spatial features that improve the model’s ability to recognize patterns.

The Conv2D layer applies a 2D convolution over an input image, performing the following operation:

where:

  • convolution(input, kernel): A sliding window operation (filter) applied over the input image.
  • kernel: A set of learnable weights (filters) that detect specific features.
  • bias: A bias vector added to the convolution output.
  • activation: An activation function applied element-wise.

Syntax of tf.keras.layers.Conv2D()

tf.keras.layers.Conv2D(
filters,
kernel_size,
strides=(1, 1),
padding='valid',
activation=None,
use_bias=True,
kernel_initializer="glorot_uniform",
bias_initializer="zeros"
)

Parameters Explained:

  • filters (Required): Number of filters (kernels) applied in the convolution. Determines the number of output channels.
  • kernel_size (Required): Size of the filter (e.g., (3,3) for a 3×3 kernel).
  • strides (Default: (1,1)): Step size for moving the filter across the input.
  • padding:
    • 'valid' (default): No padding, output size is reduced.
    • 'same': Zero-padding is added to keep the output size the same as the input.
  • activation (Optional): Activation function (e.g., 'relu', 'sigmoid').
  • use_bias (Default: True): Whether to include a bias term.
  • kernel_initializer (Default: glorot_uniform): Defines how the filters are initialized.
  • bias_initializer (Default: zeros): Defines how the bias is initialized.

Using Conv2D in a CNN Model

Below is the implementation of the CNN model.

Output:

👁 Capture


Comment