Types of Keras Layers

Keras provide different standard layers to achieve functionalities like convolution, applying non linearity to generate an effective neural network.

1. Convolutional Layer

This layer is mainly used in case of Image processing or Video processing tasks for spatial convolution over images or sequences. The functionality of the convolution layer is to apply the specified filters for input image to generate feature maps. Different types of convolution layers are Conv1D, Conv2D, Conv3D, DepthwiseConv2D Layer, SeparableConv2D Layer etc., among which Conv2D is the one which is used frequently.

Syntax: layers.Conv2D(filters=32, kernel_size=(3, 3), strides=(1,1), activation=’relu’, padding=’same’)

Parameters:

  • Filters: Specifies number of filters for the convolution layer to generate the feature map.
  • Kernel_size: Dimensions of the Convolution kernel is included through this parameter.
  • Strides : The stride determines the step size of the sliding window during convolution.
  • Activation : To specify the activation function to introduce non linearity to the layer.
  • Other optional parameters include padding, kernel_initializer etc.,
  • A convolution layer with 32 filters and 3×3 kernel with 1 stride movement in both directions and relu as activation function.

2. Pooling Layer

Pooling layer is used to reduce the dimensions of the feature map from the previous layer before passing it to next layer in-order to make the computation faster and prevent overfitting. Two main types of pooling layer are max pooling layer and average pooling layer.

  • Max pooling layer takes the maximum of the input region. If we consider a 2×2 matrix it is replaced by single value which is maximum among the four values.
  • Similarly Average pooling layer takes the average of all the input values. If we consider a 2×2 matrix it is replaced by a single value which is average of all the four values.

Syntrax layers.MaxPooling2D(pool_size=(2,2), strides=(1,1), padding=’same’)

Parameters:

  • pool_size: It represents the dimensions of the window or grid over which pooling is performed in the input matrix.
  • strides, padding are two other parameters which can be used with pooling layer.
  • A Maximum pooling layer of window size 2×2 and padding as same which makes output shape same as input.

3. Dense Layer

The most frequently used keras layer which connects every neuron of the preceding layer to every neuron of current layer. It is also known as Fully connected layer.

Syntax: layers.Dense(units = 4, use_bias = False)

Parameter:

  • Units : This is the main parameter of dense layer. It represents the number of neurons in the layer.
  • Use_bias : A Boolean value which specifies whether the bias vector need to be included or not in the layer. The default value for this parameter is True.
  • Some other common parameters like kernel_initializer, activation, kernel_regularizer can be specified with Dense layer
  • A Dense layer with 4 neurons and without any bias added to the sum of weights of neurons.

4. Flatten Layer

Flattening layer is used to flatten the input. Regardless of the dimensions of the tensor, flatten layer converts it to a single dimension tensor. For example, if we consider applying flatten layer to 2×2 input the output will be a single dimension tensor with 4 values.

Syntax: layers.Flatten(data_format = None),

  • The only parameter the flatten layer takes is data_format which is used for switching between data formats.

5. Dropout Layer

Overfitting is one of the critical issues related to neural networks to ensure there is no overfitting of model to the data this dropout layer is introduced. This layer randomly sets a part or a fraction of input data to zero during training.

Syntax: layers.Dropout(rate = 0.5,noise_shape=(16,32),seed=32)

Parameters:

  • Rate : specifies the fraction of total input units to be dropped. The value of rate lies between 0 and 1.
  • noise_shape : A 1D integer tensor that depicts the binary dropout mask’s shape and is multiplied by the input.
  • Seed : A random python integer for reproducibility.

6. Embedding layer

Embedding layer is used mainly in case of text classifications such as sentiment analysis , document categorization etc., It helps in conversion of words of the text into integer vectors of fixed length.

Syntax: layers.Embedding(input_dim = 50, output_dim = 5, input_length=10, mask_zero = True)

Parameters:

  • input_dim : Size of the text i.e., number of unique words passed to neural network.
  • output_dim : Represents the length of vector generated for each unique word.
  • input_length : Represents the maximum length of the input sequences.
  • mask_zero : A Boolean value to specify the layer to ignore input values equal to mask_value. It is helpful when dealing with sequences of variable length. Default value for mask_value is 0.

7. Activation layer

Activation function can also be introduced as a separate layer instead of including it as parameter in other keras layers.

Syntax: activation_layer = layers.Activation(‘relu’)

Keras Layers API

Keras is a powerful API built on top of deep learning libraries like TensorFlow and PyTorch. The Layers API is a key component of Keras, allowing you to stack predefined layers or create custom layers for your model. In this article, we will discuss the Keras layers API.

Similar Reads

What is Keras layers?

The key functionality of layers is analyzing the structure of the data thatis being fed into the neural network. Keras provides different types of standard layers that provide the functionality to perform operations like convolution, pooling, flattening, etc., on input data to generate the expected output. The key parameters that are specified for these layers include dimensions of the input data, which is mandatory and some other optional parameters, including:...

Types of Keras Layers:

Keras provide different standard layers to achieve functionalities like convolution, applying non linearity to generate an effective neural network....

Keras Model API

There are mainly two API of keras model....

Contact Us