hosa.models.cnn.cnn_models.BaseCNN
- class hosa.models.cnn.cnn_models.BaseCNN(n_outputs, n_kernels, n_neurons_dense_layer=50, optimizer='adam', cnn_dim=1, kernel_size=2, pool_size=2, strides_convolution=1, strides_pooling=2, padding='valid', dropout_percentage=0.1, activation_function_gol='relu', activation_function_dense='relu', batch_size=1000, epochs=50, patience=5, **kwargs)[source]
Bases:
objectBase class for Convolutional Neural Network (CNN) models for classification and regression.
Each CNN model comprises an input layer, a set of groups of layers (GofLayers [Men20])—where each group is composed of one convolution layer, followed by one pooling layer and a dropout layer—, a dense layer, and an output layer. The output layer is a dense layer with
n_outputsunits, and with the softmax activation function (for classification) or the linear activation function (for regression).Warning
This class should not be used directly. Use derived classes instead, i.e.,
CNNClassificationorCNNRegression.Note
The parameters used in this library were adapted from the exact parameters of the TensorFlow library. Descriptions were thus modified accordingly to our approach. However, refer to the TensorFlow documentation for more details about each of those parameters.
- Parameters
n_outputs (int) – Number of class labels in classification, or the number of numerical values to predict in regression.
n_kernels (list) – i-th element represents the number of output filters of the convolution layer in the i-th GofLayer.
n_neurons_dense_layer (int) – Number of neuron units in the dense layer (i.e., the dense layer after the set of groups of layers).
optimizer (str) – Name of the optimizer. See tensorflow.keras.optimizers.
cnn_dim (int) – Number of dimensions applicable to all the convolution layers of the GofLayers.
kernel_size (int or tuple) – Integer or tuple/list of integers, specifying the length (for
cnn_dim= 1), the height and width (forcnn_dim= 2), or the depth, height and width (forcnn_dim= 3) of the convolution window. It can also be a single integer to specify the same value for all spatial dimensions. This applies to all the convolution layers of the GofLayers.pool_size (int or tuple) – Size of the max-pooling window applicable to all the max-pooling layers of the GofLayers. For
cnn_dim= 1, use a tuple with 1 integer; Forcnn_dim= 2, a tuple with 2 integers and forcnn_dim= 3, a tuple with 3 integers. It can also be a single integer to specify the same value for all spatial dimensions.strides_convolution (int or tuple) – Integer or tuple/list of integers, specifying the strides of the convolution. For
cnn_dim= 1, use a tuple with 1 integer; Forcnn_dim= 2, a tuple with 2 integers and forcnn_dim= 3, a tuple with 3 integers. It can also be a single integer to specify the same value for all spatial dimensions. This applies to all the convolution layers of the GofLayers.strides_pooling (int or tuple) – Integer or tuple/list of integers, specifying the strides of the pooling. For
cnn_dim= 1, use a tuple with 1 integer; Forcnn_dim= 2, a tuple with 2 integers and forcnn_dim= 3, a tuple with 3 integers. It can also be a single integer to specify the same value for all spatial dimensions. This applies to all the pooling layers of the GofLayers.padding (str) – Available options are
validorsame.validmeans no padding.sameresults in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input. This applies to all the pooling layers of the GofLayers.dropout_percentage (float) – Fraction of the input units to drop. This applies to all the dropout layers of the GofLayers.
activation_function_gol (str or None) – Activation function to use in the convolution layers of the GofLayers. If not specified, no activation is applied (i.e., uses the linear activation function). See tensorflow.keras.activations. This applies to all the convolution layers of the GofLayers.
activation_function_dense (str or None) –
Activation function to use on the dense layer (i.e., the dense layer after the set of groups of layers). If not specified, no activation is applied (i.e., uses the linear activation function). See tensorflow.keras.activations.
batch_size (int or None) – Number of samples per batch of computation. If
None,batch_sizewill default to 32.epochs (int) – Maximum number of epochs to train the model.
patience (int) – Number of epochs with no improvement after which training will be stopped.
**kwargs – Ignored. Extra arguments that are used for compatibility’s sake.
Methods
add_gol(n_kernel, cnn_dim)Adds a GofLayer to the estimator.
aux_fit(x, y, callback, validation_size[, ...])Auxiliar function for classification and regression models compatibility.
compile()Compiles the model for training.
fit(x, y, **kwargs)Fits the model to data matrix x and target(s) y.
predict(x, **kwargs)Predicts the target values using the input data in the trained model.
prepare(x, y)Prepares the model by adding the layers to the estimator: input layer, GofLayers, flatten and dense layers.
score(x, y, **kwargs)Computes the performance metric(s) (e.g., accuracy for classification) on the given input data and target values.