pyml.neural_network.layer.transformation.convolutional.Convolutional#
- class Convolutional(in_channels, out_channels, kernel_shape, conv_operation='cross-correlation', padding=0, stride=1, weight_regularizer_l1=0, weight_regularizer_l2=0, bias_regularizer_l1=0, bias_regularizer_l2=0)[source]#
Bases:
_Transformation
,_TrainableTransformation
Convolutional layer for a neural network.
This class implements a convolutional layer with options for various parameters like kernel shape, padding, and stride. It is used for performing convolution or cross-correlation operations.
- Parameters:
in_channels (int) – The number of input channels or depth.
out_channels (int) – The number of output channels.
kernel_shape (int or tuple[int,int]) – The shape of the convolutional kernel. If it’s an integer, it’s treated as a square kernel. If it’s a tuple, it should specify the height and width of the kernel.
conv_operation (str, optional) – The type of convolution operation, either ‘convolution’ or ‘cross-correlation’. By default ‘cross-correlation’. Not implemented yet.
padding (int or tuple[int,int] or str, optional) – Padding to apply to the input. It can be an integer, a tuple of two integers for height and width padding, or one of the strings ‘valid’, ‘full’, or ‘same’. By default 0.
stride (int or tuple[int,int], optional) – The stride to use during convolution. It can be an integer or a tuple of two integers specifying the vertical and horizontal strides. By default 1.
weight_regularizer_l1 (float, optional) – L1 regularization strength for kernel weights, by default 0.
weight_regularizer_l2 (float, optional) – L2 regularization strength for kernel weights, by default 0.
bias_regularizer_l1 (float, optional) – L1 regularization strength for biases, by default 0.
bias_regularizer_l2 (float, optional) – L2 regularization strength for biases, by default 0.
- Variables:
Methods
__init__
Apply the convolution operation to a slice of the input using a given kernel and bias.
Apply padding to a given input matrix.
Performs striding on a matrix.
Perform the backward pass through the convolutional layer.
Perform the forward pass through the convolutional layer.
Return parameters
Set adjacent layers which are needed for the model to iterate through the layers.
Sets the parameters for this layer
- apply_kernel(X_slice, kernel, bias)[source]#
Apply the convolution operation to a slice of the input using a given kernel and bias.
- Return type:
- Parameters:
X_slice (numpy.ndarray) – A slice of the input matrix that matches the shape of the convolutional kernel.
kernel (numpy.ndarray) – The convolutional kernel.
bias (numpy.ndarray) – The bias associated with the kernel.
- Returns:
The result of applying the convolution operation to the input slice using the given kernel and bias.
- Return type:
- apply_padding(X)[source]#
Apply padding to a given input matrix.
- Return type:
ndarray
- Parameters:
X (numpy.ndarray) – Input matrix of the shape (Number of Batches, Channels/Depth, Height, Width).
- Returns:
Input matrix with applied padding.
- Return type:
numpy.ndarray
- static apply_striding(X, stride=1)[source]#
Performs striding on a matrix.
- Return type:
ndarray
- Parameters:
X (numpy.ndarray) – Input matrix of the dimension (Number of Batches, Channels/Depth, Height, Width)
stride (int or tuple[int, int], optional) – Specifies the iteration step size of rows and columns to keep. If set to 1, all columns and rows will be kept. Stride can either be a single int or a tuple, where the first parameter sets the stride for rows (height), and the second parameter sets the stride for columns (width). By default 1.
- Returns:
Input matrix with applied stride.
- Return type:
numpy.ndarray
- backward(dvalues)[source]#
Perform the backward pass through the convolutional layer.
- Return type:
- Parameters:
dvalues (numpy.ndarray) – The gradients of the loss with respect to the layer’s output.
- forward(inputs)[source]#
Perform the forward pass through the convolutional layer.
- Return type:
- Parameters:
inputs (numpy.ndarray) – Input data of the shape (Number of Batches, Channels/Depth, Height, Width).
- set_adjacent_layers(previous_layer, next_layer)#
Set adjacent layers which are needed for the model to iterate through the layers.
- Parameters:
previous_layer (_Layer) – Layer that is previous to this layer.
next_layer (_Layer) – Layer that is subsequent to this layer.