hosa.optimization.hosa.HOSARNN

class hosa.optimization.hosa.HOSARNN(x, y, model, n_outputs, parameters, tr, apply_rsv=True, validation_size=0.25, n_splits=10)[source]

Bases: hosa.optimization.hosa.BaseHOSA

Heuristic Oriented Search Algorithm (HOSA) for RNNs.

This class implments the HOSA for optimizing RNNs. Following a heuristic search, the algorithm finetunes the most relevant models’ parameters. Thus, HOSA avoids testing every possible combination, and therefore, an exhaustive search.

Parameters
  • x (numpy.ndarray) – Input data.

  • y (numpy.ndarray) – Target values (class labels in classification, real numbers in regression).

  • model (object) – Class of the object to be optimized. Available options are: RNNClassification, RNNRegression, CNNClassification and CNNRegression.

  • n_outputs (int) – Number of class labels in classification, or the number of numerical values to predict in regression.

  • parameters (dict) – Dictionary with parameters names (str) as keys and lists of parameter settings to try as values.

  • tr (float) – Minimum threshold of improvement of the performance metric.

  • apply_rsv (bool) – True if random sub-sampling validation should be used during the optimization procedure.

  • validation_size (float) – Proportion of the dataset to include in the validation split on the random sub-sampling validation. Ignored if ``apply_rsv = False``.

  • n_splits (int) – Number of splits used in the random sub-sampling validation. Ignored if ``apply_rsv = False``.

Examples

 1import numpy as np
 2from sklearn.model_selection import train_test_split
 3
 4from hosa.models.rnn import RNNClassification
 5from hosa.optimization.hosa import HOSARNN
 6
 7# 1 - Load the dataset
 8dataset = np.loadtxt('...', delimiter=',')
 9x = dataset[:, :-1]
10y = dataset[:, -1]
11# 2 - Split the data in train and test dataset
12x_train, X_test, y_train, y_test = train_test_split(x, y, test_size=.1,
13shuffle=False)
14# 3 - Set the parameters to optimize
15param_grid_rnn = {
16        'overlapping_type':          ['central', 'left'],
17        'model_type':                ['lstm', 'gru'],
18        'overlapping_epochs':        [1],
19        'timesteps':                 [1],
20        'activation_function_dense': ['relu'],
21        'n_units':                   [10, 12],
22        'mults':                     [1, 2],
23        'optimizer':                 ['adam'],
24        'batch_size':                [32],
25}
26# 4 - Create a HOSA instance and find the best model
27clf = HOSARNN(x_train, y_train, RNNClassification, 2, param_grid_rnn, 0.01,
28validation_size=.05, apply_rsv=False)
29clf.fit(max_n_subs_layers=4, show_progress=True, verbose=0, shuffle=False,
30imbalance_correction=True)
31score = clf.score(X_test, y_test)

Methods

fit(max_n_subs_layers[, show_progress, ...])

Optimize the model following the HOSA approach with all sets of parameters.

get_model()

Get the best model found.

get_params()

Get parameters for the best model found.

grid_search([n_kernels, ...])

Runs a grid search on the remaining moldel's parameters.

predict(x, **kwargs)

Predicts the target values using the input data in the best model found.

score(x, y, **kwargs)

Computes the performance metrics on the given input data and target values in the best model found.