Keras - Convolution Layers


Advertisements

Keras contains a lot of layers for creating Convolution based ANN, popularly called as Convolution Neural Network (CNN). All convolution layer will have certain properties (as listed below), which differentiate it from other layers (say Dense layer).

Filters − It refers the number of filters to be applied in the convolution. It affects the dimension of the output shape.

kernel size − It refers the length of the convolution window.

Strides − It refers the stride length of the convolution.

Padding − It refers the how padding needs to be done on the output of the convolution. It has three values which are as follows −

  • valid means no padding

  • causal means causal convolution.

  • same means the output should have same length as input and so, padding should be applied accordingly

Dilation Rate − dilation rate to be applied for dilated convolution. −

Another important aspect of the convolution layer is the data format. The data format may be to two type,

channel_last: channel_last − specifies that the channel data is placed as last entry. Here, channel refers the actual data and it will be placed in the last dimension of the input space. −

For example, let us consider an input shape, (30, 10, 128). Here, the value in first dimension, 30 refers the batch size, the value in second dimension, 10 refers the timesteps in temporal convolution and the value in third dimension 128 refers the actual values of the input. This is the default setting in Keras.

channel_first: channel_first is just opposite to channet_last. Here, the input values are placed in the second dimension, next to batch size.

Let us see check the all the layer used for CNN provided by Keras layers in this chapter.

Conv1D

Conv1D layer is used in temporal based CNN. The input shape of the ConvID will be in below format −

(batch_size, timesteps, features)

where,

  • batch_size refers the size of the batch.

  • timesteps refers the number of time steps provided in the input.

  • features refer the number of features available in the input.

The output shape of the Conv1D is as follows −

(batch_size, new_steps, filters)

where, filters refer the number of filters specified as one of the arguments.

The signature of the ConvID function and its arguments with default value is as follows −

keras.layers.Conv1D( 
   filters, 
   kernel_size, 
   strides = 1, 
   padding = 'valid', 
   data_format = 'channels_last', 
   dilation_rate = 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
)

Conv2D

It is a convolution 2D layer. It creates a convolutional kernel with the layer input creates a tensor of outputs. input_shape refers the tuple of integers with RGB value in data_format = “channels_last”.

The signature of the Conv2D function and its arguments with default value is as follows −

keras.layers.Conv2D 
   (filters, kernel_size, 
   strides = (1, 1), 
   padding = 'valid', 
   data_format = None, 
   dilation_rate = (1, 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
)

Here,

  • strides refer an integer specifying the strides of the convolution along the height and width.

Advertisements