tf.keras.layers.Conv2D in TensorFlow Python

In this tutorial, we are going to see about Keras 2-Dimensional Convolution neural network layer and the important parameters that are needed to be passed.

A convolution neural network is a network in which each layer is connected to the other through the kernel.

Conv2D in Python

tf.keras.layers.Conv2D(no.of.units, kernel_size, activation, input_shape, padding, strides, dilation_rate).

Let us discuss the role of these parameters in detail.

  • Kernel size – This represents the size of the kernel.
  • Some of the Activation functions are relu, sigmoid, softmax and it takes in string format.
  • Input shape –  This is included in the first layer of the convolution network. It takes rows, columns, and the number of pixels in tuple form.
  • Padding is done to avoid shrinking of the layer during convolving by adding one or more layers around the edges.

                 padding = ‘valid’ represents no padding.

                 padding = ‘same’ represents that the size of the output is the same as the input.

  • Filter, also called the kernel, is the size of the network that is to be convolved with the input matrix.
  • Stride – The default value of stride is 1 (i.e.,) its steps over one position. If stride is 2, it jumps in steps of 2 and so on.
  • Dilation rate refers to the distance between subsequent pixels.

Let us see how to implement it in the model.

After instantiating a sequential model, add the 2D convolution layer.

from keras.layers import Conv2D
Conv2D(15, kernel_size=2, activation='relu', input_shape=(28, 28, 1), padding='same', strides = 2)

Finally, we can fit the model and predict the results.

I hope this post helps!

Leave a Reply

Your email address will not be published.