pyMAISE.Tuner
- class pyMAISE.Tuner(xtrain, ytrain, model_settings)[source]
Bases:
objectHyperparameter tuning object.
Supported Models
Supported models include
Linear: linear regressor,Lasso: lasso regressor,Logistic: logistic regressor,SVM: support vector regressorand classifier,
DT: decision tree regressorand classifier,
RF: random forest regressorand classifier,
KN: k-nearest neighbors regressorand classifier,
EN: elastic net regressor,RD: ridge regressor,ET: extra trees regressorand classifier,
AB: AdaBoost regressorand classifier,
GP: Gaussian process regressorand classifier,
GB: gradient boosting regressorand classifier,
Stacking: stacking regressorand classifier,
MultiOutput: multioutput regressorand classifier.
from [PVG+11] and sequential neural networks from [C+15].
Supported Neural Network Layers and Optimizers
pyMAISE supports the following neural network layers using [C+15]:
Dense: dense,Dropout: dropout,LSTM: LSTM,GRU: GRU,Conv1D: 1D convolution,Conv2D: 2D convolution,Conv3D: 3D convolution,MaxPooling1D: max pooling for 1D temporal data,MaxPooling2D: max pooling for 2D temporal data,MaxPooling3D: max pooling for 3D temporal data,Flatten: flatten,Reshape: reshape,
and the following optimizers:
SGD: gradient descent,RMSprop: RMSprop,Adam: Adam,AdamW: AdamW,Adadelta: Adadelta,Adagrad: Adagrad,Adamax: Adamax,Adafactor: Adafactor,Nadam: Nadam,Ftrl: FTRL.
Note
For additional layer or optimizer support, submit a detailed issue at the pyMAISE GitHub repository outlining the layer or optimizer required.
- Parameters:
xtrain (xarray.DataArray) – Input training data.
ytrain (xarray.DataArray) – Output training data.
model_settings (dict of int, float, str, or pyMAISE.HyperParameters) –
This dictionary specifies the name of the models of interest, which are assigned as a list to the
modelskey. The model names are provided in the Supported Models section; all names that do not match those keys are assumed to be neural network models. For specific hyperparameters, please refer to the links provided for the models.For classical models, sklearn models [PVG+11], this dictionary specifies the hyperparameters different from default but remain constant throughout the hyperparameter tuning process. This is done by assigning a sub-dictionary under the key of the model’s name.
For neural network models [C+15], this dictionary specifies both hyperparameters that remain constant throughout tuning and the tuning space using
pyMAISE.Int,pyMAISE.Float,pyMAISE.Choice,pyMAISE.Boolean, andpyMAISE.Fixed. This is done in the same way as classical models, where hyperparameters and their values are specified in sub-dictionaries under their model’s key. In addition, number of layers, optimizers, wrappers, and sublayers can be specified.
Warning
When hyperparameter tuning a neural network with multiple of the same layer in one model, ensure the names of the layers are different, but the keywords are still present. For example, a dense sequential neural network with multiple dense layers can use names like
Dense_input,Dense_hidden, andDense_output.Examples
Given 2D input and output training data (
xtrain,ytrain) an example using linear and random forest models.import pyMAISE as mai model_settings = { "models": ["Linear", "RF"], "RF": { "n_estimators": 150, }, } tuner = mai.Tuner(xtrain, ytrain, model_settings)
From the above, we see we specify a linear model with default hyperparameters and a random forest model with all default hyperparameters except for 150 estimators.
Given 3D input and 2D output time series data (
xtrain,ytrain) frompyMAISE.preprocessing.SplitSequence, we can define a CNN-LSTM.import pyMAISE as mai from tensorflow.keras.layers import TimeDistributed from tensorflow.keras.callbacks import ReduceLROnPlateau cnn_lstm_structure = { "Reshape_input": { "target_shape": (2, 2, xtrain.shape[-1]) }, "Conv1D": { "filters": mai.Int(min_value=50, max_value=150), "kernel_size": 1, "activation": "relu", "wrapper": ( TimeDistributed, { "input_shape": (None, 2, xtrain.shape[-1]) }, ), }, "MaxPooling1D": { "pool_size": 2, "wrapper": TimeDistributed, }, "Flatten": { "wrapper": TimeDistributed, }, "LSTM": { "num_layers": mai.Int(min_value=0, max_value=3), "units": mai.Int(min_value=20, max_value=100), "activation": "tanh", "recurrent_activation": "sigmoid", "recurrent_dropout": mai.Choice([0.0, 0.2, 0.4, 0.6]), "return_sequences": True, }, "LSTM_output": { "units": mai.Int(min_value=20, max_value=100), "activation": "tanh", "recurrent_activation": "sigmoid", }, "Dense": { "units": ytrain.shape[-1], "activation": "linear", }, } fitting = { "batch_size": 512, "epochs": 5, "validation_split":0.15, "callbacks": [ ReduceLROnPlateau( monitor='val_mean_absolute_error', factor=0.8, patience=2, min_lr=0, verbose=1, ), EarlyStopping( monitor="val_mean_absolute_error", patience=3, ) ] } adam = { "learning_rate": mai.Float(min_value=0.00001, max_value=0.001), "clipnorm": mai.Float(min_value=0.8, max_value=1.2), "clipvalue": mai.Float(min_value=0.3, max_value=0.7), } compiling = { "loss": "mean_absolute_error", "metrics": ["mean_absolute_error"], } model_settings = { "models": ["CNN-LSTM"], "CNN-LSTM": { "structural_params": cnn_lstm_structure, "optimizer": "Adam", "Adam": adam, "compile_params": compiling, "fitting_params": fitting, }, } tuner = mai.Tuner(xtrain, ytrain, model_settings=model_settings)
We see that we defined a neural network with 7 layers with the following tuning space:
1D convolutional layer filters,
hidden LSTM number of layers,
hidden LSTM units,
hidden LSTM recurrent dropout,
output LSTM units,
Adam learning rate,
Adam clipnorm,
Adam clipvalue.
Additionally, the
Conv1D,MaxPooling1D, andFlattenlayers use thekeras.layers.TimeDistributedwrapper to accommodate the temporal dimension.Methods
bayesian_search(param_spaces[, models, ...])Bayesian search over hyperparameter space for classical models.
convergence_plot([ax, model_types])Create a convergence plot for search using
pyMAISE.Tuner.cv_performance_data.grid_search(param_spaces[, models, scoring, ...])Grid search over hyperparameter space for classical models.
manual_search([models, model_settings])Fit a single hyperparameter configuration.
nn_bayesian_search([models, objective, ...])Bayesian search for neural networks.
nn_grid_search([models, objective, ...])Grid search for neural networks.
nn_hyperband_search([models, objective, ...])Hyperband search for neural networks.
nn_random_search([models, objective, ...])Random search for neural networks.
random_search(param_spaces[, models, ...])Random search over hyperparameter space for classical models.
Attributes
Cross-validation performance, mean and standard deviation of the test score, for each model.
Dictionary of supported models.
- bayesian_search(param_spaces, models=None, scoring=None, n_iter=50, optimizer_kwargs=None, fit_params=None, n_jobs=None, n_points=1, refit=True, cv=None, pre_dispatch='2*n_jobs')[source]
Bayesian search over hyperparameter space for classical models. This function uses skopt.BayesSearchCV [sko16].
- Parameters:
param_spaces (dict of dict of skopt.space.Dimension instance) – The parameters which will be tuned through a Bayesian search over every configuration of hyperparameter in each model dictionary. Each parameter is defined using
skopt.space.Dimensioninstances (Real, Integer, or Categorical).models (list of str or None, default=None) – A list of model names defined in the initialization of
pyMAISE.Tuner. IfNonethen all classical models are subject to Bayesian search.
Note
For information on
scoring,n_iter,optimizer_kwargs,fit_params,n_jobs,n_points,refit,cv, andpre_dispatchrefer to skopt’s documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- convergence_plot(ax=None, model_types=None)[source]
Create a convergence plot for search using
pyMAISE.Tuner.cv_performance_data.- Parameters:
ax (matplotlib.pyplot.axis or None, default=None) – Axis object. If
Nonethen one is created.model_types (list of str or None, default=None) – List of model names to add to the convergence plot. If
Nonethen all are added.
- Returns:
ax – Axis object.
- Return type:
matplotlib.pyplot.axis or None, default=None
- property cv_performance_data
Cross-validation performance, mean and standard deviation of the test score, for each model.
- Type:
list of float
- grid_search(param_spaces, models=None, scoring=None, n_jobs=None, refit=True, cv=None, pre_dispatch='2*n_jobs')[source]
Grid search over hyperparameter space for classical models. This function uses sklearn.model_selection.GridSearchCV [PVG+11].
- Parameters:
param_spaces (dict of dict of list) – The parameters which will be tuned through an exhaustive search over every configuration of hyperparameter in each model dictionary. Each parameter is defined as a dictionary key and assigned a list.
models (list of str or None, default=None) – A list of model names that were defined in the initialization of
pyMAISE.Tuner. IfNonethen all classical models are subject to grid search.
Note
For information on
scoring,n_jobs,refit,cv, andpre_dispatchrefer to sklearn’s documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- manual_search(models=None, model_settings=None)[source]
Fit a single hyperparameter configuration.
- Parameters:
models (list of str or None, default=None) – The names of the models to be fit using manual search. If
Nonethen all the models specified in the initialization of thepyMAISE.Tunerare fit.model_settings (dict of int, float, or str) – The model settings for the models which are sub-dictionaries under the model key. If
Nonethen the hyperparameter configurations specified in the initialization of thepyMAISE.Tunerare used.
- Returns:
data – The hyperparameters and models for each model type.
- Return type:
dict of tuple(pd.DataFrame, model object)
- nn_bayesian_search(models=None, objective=None, max_trials=10, num_initial_points=None, alpha=0.0001, beta=2.6, hyperparameters=None, tune_new_entries=True, allow_new_entries=True, max_retries_per_trial=0, max_consecutive_failed_trials=1, overwrite=True, directory='./', project_name='best_hp', cv=5, shuffle=False)[source]
Bayesian search for neural networks. This function uses keras_tuner.oracles.BayesianOptimizationOracle with
pyMAISE.CVTunerfor cross-validation. Iterate over sampled hyperparameter space using Bayesian optimization and return the top models for each model type.- Parameters:
models (list of string or None, default=None) – The names of the neural network models for Bayesian search. If
None, then all neural networks are fit with Bayesian search.objective (str or keras_tuner.Objective, default=None) – The objective of the search. If the objective is a
strof a sklearn.metrics, then that is used as the objective. Otherwise the builtin objectives within KerasTuner are used.cv (int or cross-validation generator, default=5) – If an
intthen either sklearn.model_selection.StratifiedKFold or sklearn.model_selection.KFold are used depending on thepyMAISE.Settings.problem_typeand output data type. If the problem is a classification problem and the output data is either binary or multiclass, then sklearn.model_selection.StratifiedKFold is used.shuffle (bool, default=False) – Whether to shuffle the data before cross-validation split.
Note
For information on
max_trials,num_initial_points,alpha,beta,hyperparameters,tune_new_entries,max_retries_per_trial,max_consecutive_failed_trials,overwrite,directory, andproject_namerefer to KerasTuner documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided, thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- nn_grid_search(models=None, objective=None, max_trials=None, hyperparameters=None, allow_new_entries=True, tune_new_entries=True, max_retries_per_trial=0, max_consecutive_failed_trials=1, overwrite=True, directory='./', project_name='best_hp', cv=5, shuffle=False)[source]
Grid search for neural networks. This function uses keras_tuner.oracles.GridSearchOracle with
pyMAISE.CVTunerfor cross validation. Iterate over the defined search space and return the top models for each model type.- Parameters:
models (list of string or None, default=None) – The names of the neural network models for grid search. If
None, then all neural networks are fit with grid search.objective (str or keras_tuner.Objective, default=None) – The objective of the search. If the objective is a
strof a sklearn.metrics, then that is used as the objective. Otherwise, the built-in objectives within KerasTuner are used.cv (int or cross-validation generator, default=5) –
If an
int, then either sklearn.model_selection.StratifiedKFold or sklearn.model_selection.KFold are used depending on thepyMAISE.Settings.problem_typeand output data type. If the problem is a classification problem and the output data is either binary or multiclass, then sklearn.model_selection.StratifiedKFold is used.shuffle (bool, default=False) – Whether to shuffle the data before cross-validation split.
Note
For information on
max_trials,hyperparameters,allow_new_entries,tune_new_entries,max_consecutive_failed_trials,overwrite,directory, andproject_namerefer to the KerasTuner documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided, thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- nn_hyperband_search(models=None, objective=None, max_epochs=100, factor=3, hyperband_iterations=1, hyperparameters=None, tune_new_entries=True, allow_new_entries=True, max_retries_per_trial=0, max_consecutive_failed_trials=3, overwrite=True, directory='./', project_name='best_hp', cv=5, shuffle=False)[source]
Hyperband search for neural networks. This function uses keras_tuner.oracles.HyperbandOracle with
pyMAISE.CVTunerfor cross validation.- Parameters:
models (list of string or None, default=None) – The names of the neural network models for grid search. If
None, then all neural networks are fit with grid search.objective (str or keras_tuner.Objective, default=None) – The objective of the search. If the objective is a
strof a sklearn.metrics then that is used as the objective. Otherwise the builtin objectives within KerasTuner are used.cv (int or cross-validation generator, default=5) –
If an
int, then either sklearn.model_selection.StratifiedKFold or sklearn.model_selection.KFold are used depending on thepyMAISE.Settings.problem_typeand output data type. If the problem is a classification problem and the output data is either binary or multiclass then sklearn.model_selection.StratifiedKFold is used.shuffle (bool, default=False) – Whether to shuffle the data before cross-validation split.
Note
For information on
max_epochs,factor,hyperband_iterations,hyperparameters,tune_new_entries,allow_new_entries,max_retries_per_trial,max_consecutive_failed_trials,overwrite,directory, andproject_namerefer to KerasTuner documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided, thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- nn_random_search(models=None, objective=None, max_trials=10, hyperparameters=None, allow_new_entries=True, tune_new_entries=True, max_retries_per_trial=0, max_consecutive_failed_trials=1, overwrite=True, directory='./', project_name='best_hp', cv=5, shuffle=False)[source]
Random search for neural networks. This function uses keras_tuner.oracles.RandomSearchOracle with
pyMIASE.CVTunerfor cross validation. Sample the defined search space based on a random distribution for each model type.- Parameters:
models (list of string or None, default=None) – The names of the neural network models for random search. If
None, then all neural networks are fit with random search.objective (str or keras_tuner.Objective, default=None) – The objective of the search. If the objective is a
strof a sklearn.metrics, then that is used as the objective. Otherwise, the built-in objectives within KerasTuner are used.cv (int or cross-validation generator, default=5) –
If an
int, then either sklearn.model_selection.StratifiedKFold or sklearn.model_selection.KFold are used depending on thepyMAISE.Settings.problem_typeand output data type. If the problem is a classification problem and the output data is either binary or multiclass, then sklearn.model_selection.StratifiedKFold is used.shuffle (bool, default=False) – Whether to shuffle the data before cross-validation split.
Note
For information on
max_trials,hyperparameters,allow_new_entries,tune_new_entries,max_retries_per_trial,max_consecutive_failed_trials,overwrite,directory, andproject_namerefer to KerasTuner documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided, thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- random_search(param_spaces, models=None, scoring=None, n_iter=10, n_jobs=None, refit=True, cv=None, pre_dispatch='2*n_jobs')[source]
Random search over hyperparameter space for classical models. This function uses sklearn.model_selection.RandomizedSearchCV [PVG+11].
- Parameters:
param_spaces (dict of dict of list or distributions) – The parameters which will be tuned through a random search over every configuration of hyperparameter in each model dictionary. Each parameter is defined as a dictionary key and assigned a list or distribution with an
rvsmethod.models (list of str or None, default=None) – A list of model names defined in the initialization of
pyMAISE.Tuner. IfNonethen all classical models are subject to grid search.
Note
For information on
scoring,n_iter,n_jobs,refit,cv, andpre_dispatchrefer to sklearn’s documentation.- Returns:
data – The hyperparameters and models for the top
pyMAISE.Settings.num_configs_savedfor each model. If fewer configurations are provided thanpyMAISE.Settings.num_configs_savedthen all are taken.- Return type:
dict of tuple(pd.DataFrame, model object)
- supported_classical_models = {'AB': <class 'pyMAISE.methods._adaboost.AdaBoost'>, 'DT': <class 'pyMAISE.methods._dtree.DecisionTree'>, 'EN': <class 'pyMAISE.methods._elastic.ElasticNet'>, 'ET': <class 'pyMAISE.methods._extra_trees.ExtraTrees'>, 'GB': <class 'pyMAISE.methods._gradient_boosting.GradientBoosting'>, 'GP': <class 'pyMAISE.methods._gaussian_process.GaussianProcess'>, 'KN': <class 'pyMAISE.methods._kneighbors.KNeighbors'>, 'Lasso': <class 'pyMAISE.methods._lasso.LassoRegression'>, 'Linear': <class 'pyMAISE.methods._linear.LinearRegression'>, 'Logistic': <class 'pyMAISE.methods._logistic_regression.LogisticRegression'>, 'MultiOutput': <class 'pyMAISE.methods._multi_output.MultiOutput'>, 'RD': <class 'pyMAISE.methods._ridge.RidgeRegression'>, 'RF': <class 'pyMAISE.methods._rforest.RandomForest'>, 'SVM': <class 'pyMAISE.methods._svm.SVM'>, 'Stacking': <class 'pyMAISE.methods._stacking.Stacking'>}
Dictionary of supported models.
- Type:
dict of pyMAISE.methods