Keras layers have a number of common methods:
- layer.get_weights() – returns the layer weights as a list of Numpy arrays.
- layer.set_weights(weights) – sets the layer weights from the list of arrays (with the same shapes as the get_weights output).
- layer.get_config() – returns a dictionary containing a layer configuration.
A layer can be restored from its saved configuration using the following code:
layer = Dense(32) config = layer.get_config () reconstructed_layer = Dense.from_config (config)
If a layer has one node (that is, if it is not a common layer), then you can get its input tensor, output tensor, dimension of the input array, and dimension of the output array through the properties:
The Dense layer implements the operation: output = activation (dot (input, kernel) + bias) where activation is the activation function passed as activation argument, kernel is the matrix of the weights layer, and bias is the offset vector created by the layer.
The dense layer is created using the method:
keras.layers.Dense(units, 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)
Consider an example of creating a dense layer:
# first create a sequential model model = Sequential () # add the first dense layer # the model will accept an input array (*, 16) and an output array (*, 32) model.add (Dense (32, input_shape = (16,))) # when adding the following layers there is no need to specify the size of the input arrays model.add (Dense (32))
Overfitting is one of the problems of deep neural networks, consisting in the fact that the model recognizes only examples from the training sample, adapting to training examples, instead of learning to classify examples that did not participate in training (losing the ability to generalize). The most effective solution to retraining is the exception method (Dropout).
keras.layers.Dropout (rate, noise_shape = None, seed = None)
Networks for training are obtained by dropping out neurons with a probability rate, so the probability that a neuron will remain in the network is 1-rate. The “exception” of a neuron means that for any input data or parameters it returns 0.
To convert the result into a specific form, you must use the method:
The target_shape argument is a tuple of integers.
Consider an example:
model.add (Reshape ((3, 4), input_shape = (12,))) # dimension of the output layer array: model.output_shape == (None, 3, 4) model.add (Reshape ((6, 2))) # dimension of the output layer array: model.output_shape == (None, 6, 2) To resize the input array, you can use the method: keras.layers.Permute (dims)
This method is useful, for example, for connecting RNN and connections together.
The convolution layer is the main unit of the convolutional neural network. The convolution layer includes a filter for each channel, the convolution core of which processes the previous layer in fragments (summing up the results of the matrix product for each fragment). The weights of the convolution kernel (a small matrix) are unknown and are established in the learning process. The peculiarity of the convolutional layer is a relatively small number of parameters set during training.
- Conv1D – This layer creates a convolutional core, in a single spatial (or temporal) dimension:
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)
The main arguments are:
• filters: the dimension of the output space (i.e. the number of output filters in convolution).
• kernel_size: an integer or a list of integers that defines the length of the convolution window.
• strides: an integer or a list of integers that defines the length of the convolution step.
• activation: function to activate the layer. If this parameter is not specified, then activation is not applied (i.e., the “linear” activation function $a(x) = x$).
- Conv2D is a 2D convolutional layer (for example, spatial convolution over images). This layer creates a convolution kernel to create an output tensor.
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)
- Conv3D is a 3D convolutional layer (for example, spatial convolution over volumes). This layer creates a convolution kernel that is collapsed with an input layer to create an output tensor.
keras.layers.Conv3D (filters, kernel_size, strides = (1, 1, 1), padding = 'valid', data_format = None, dilation_rate = (1, 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).
When creating this layer, if use_bias = True, then an offset vector is created and added to the output. When using this layer as the first layer in the model, as input_shape, you must specify a tuple of integers that does not include the sample axis, for example, input_shape = (128, 128, 128, 1).
The pooling layer is a non-linear compaction of a feature map, and a group of pixels (usually 2 × 2 in size) is compressed to one pixel, passing through a non-linear transformation. Transformations affect non-intersecting rectangles or squares, each of which is compressed into one pixel, and a pixel with the maximum value is selected. The pooling operation can significantly reduce the spatial volume of the image. Pooling is interpreted as follows. If at the previous convolution operation some signs have already been identified, then for further processing such a detailed image is no longer necessary, and it is compacted to less detailed. In addition, filtering already unnecessary parts helps not to retrain. A layer of pooling is usually inserted after the convolution layer before the layer of the next convolution.
The function of the maximum is most commonly used.
keras.layers.MaxPooling1D (pool_size = 2, strides = None, padding = 'valid')
The main arguments are:
- pool_size: Integer, the size of the maximum join windows.
- strides: parameter with which to scale down. For example, 2 will halve the input.
keras.layers.MaxPooling2D (pool_size = (2, 2), strides = None, padding = 'valid', data_format = None)
The join operation for spatial data.
keras.layers.MaxPooling3D (pool_size = (2, 2, 2), strides = None, padding = 'valid', data_format = None)
The operation of combining three-dimensional data (spatial or spatial-temporal association).