hosa.models.cnn.cnn_models.CNNClassification
- class hosa.models.cnn.cnn_models.CNNClassification(n_outputs, n_kernels, n_neurons_dense_layer=50, optimizer='adam', metrics=None, 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:
hosa.models.cnn.cnn_models.BaseCNNConvolutional Neural Network (CNN) classifier.
The 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, with the softmax activation function.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 to predict.
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.
metrics (list) – List of metrics to be evaluated by the model during training and testing. Each item of the list can be a string (name of a TensorFlow’s built-in function), function, or a tf.keras.metrics.Metric instance. If
None,metricswill default to['accuracy'].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.
Examples
1from tensorflow import keras 2 3from hosa.models.cnn import CNNClassification 4 5# 1 - Load and split the data 6fashion_mnist = keras.datasets.fashion_mnist 7(x_train, y_train), (X_test, y_test) = fashion_mnist.load_data() 8# 2 - Normalize the images, and reshape the images for cnn input 9x_train = x_train / 255.0 10X_test = X_test / 255.0 11x_train = x_train.reshape((-1, 28 * 28)) 12X_test = X_test.reshape((-1, 28 * 28)) 13# 3 - Create and fit the model 14clf = CNNClassification(10, 10, [4, 3]) 15clf.prepare(x_train, y_train) 16clf.compile() 17clf.fit(x_train, y_train) 18# 4 - Calculate predictions 19clf.predict(X_test) 20# 5 - Compute the score 21score = clf.score(X_test, y_test)
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[, validation_size, shuffle, atol, ...])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[, imbalance_correction])Computes the performance metrics on the given input data and target values.