def _dimension_space_from_dict(dict_hyperparams): hyperparams = list() if not isinstance(dict_hyperparams, dict): raise TypeError('Hyperparams must be a dictionary.') for name, hyperparam in dict_hyperparams.items(): hp_type = hyperparam['type'] if hp_type == 'int': hp_range = hyperparam.get('range') or hyperparam.get('values') hp_min = min(hp_range) if hp_range else None hp_max = max(hp_range) if hp_range else None hp_instance = space.Integer(hp_min, hp_max, name=name) elif hp_type == 'float': hp_range = hyperparam.get('range') or hyperparam.get('values') hp_min = min(hp_range) hp_max = max(hp_range) hp_instance = space.Real(hp_min, hp_max, name=name) elif hp_type == 'bool': hp_instance = space.Categorical([True, False], name=name) elif hp_type == 'str': hp_choices = hyperparam.get('range') or hyperparam.get('values') hp_instance = space.Categorical(hp_choices, name=name) hyperparams.append(hp_instance) return hyperparams
def init_search_space_dict(test=False) -> dict: search_space_dict = dict( dim_batch_sampling_method=space.Categorical(categories=['random_geometric', 'random_uniform', 'systematic_uniform'], name='batch_sampling_method'), dim_window_size=space.Integer(low=10, high=1000, name='window_size'), dim_stride=space.Integer(low=1, high=10, name='stride'), dim_batch_size=space.Integer(low=10, high=1000, name='batch_size'), dim_num_training_steps=space.Integer(low=10000, high=5000000, name='num_training_steps'), dim_learning_rate=space.Real(low=1e-6, high=1e-2, prior='log-uniform', name='learning_rate'), dim_geometric_decay=space.Real(low=1e-6, high=1, prior='log-uniform', name='geometric_decay'), dim_conv_layers_seperable=space.Categorical(categories=[True, False], name='conv_layers_separable'), dim_len_conv1_filters=space.Integer(low=2, high=10, name='len_conv1_filters'), dim_num_conv1_features=space.Integer(low=1, high=64, name='num_conv1_features'), dim_num_conv2_features=space.Integer(low=8, high=128, name='num_conv2_features'), dim_num_fc1_neurons=space.Integer(low=8, high=32, name='num_fc1_neurons'), ### Third conv layer doesn't work yet dim_model_ending=space.Categorical(categories=['one_fc_layer', 'two_fc_layers', 'third_conv_layer'], name='model_ending'), dim_dropout_keep_prob=space.Real(low=.1, high=.9, name='dropout_keep_prob'), ) if test: search_space_dict.update({'dim_batch_size': space.Integer(low=10, high=30, name='batch_size'), 'dim_num_training_steps': space.Integer(low=2, high=4, name='num_training_steps'), 'dim_window_size': space.Integer(low=10, high=50, name='window_size'), 'dim_stride': space.Integer(low=1, high=2, name='stride')}) return search_space_dict
def __init__(self, model_callable, param_space, x_train, y_train, kfold_n_splits=5, score_sign=-1, score_measure=None, x_test=None, y_test=None): """ @param model_callable: @param param_space: @param x_train: @param y_train: @param n_calls: @param kfold_n_splits: this is used when no x_test, y_test given, cross validate score, but if x_test, y_test are given, not used @param score_sign: -1 if we want to max the value return by score_measure, 1 if we want to min it @param score_measure: default None for f1_score with avg is macro, callable for score calculation, take y_true as first arg, y_pred as second arg @param x_test: test data set data @param y_test: test data set label """ self.model = model_callable self.x_train = x_train self.y_train = y_train self.x_test = x_test self.y_test = y_test self.param_space = [] self.param_names = [] for param_config in param_space: self.param_names.append(param_config[-1]) if isinstance(param_config[0], list): self.param_space.append( space.Categorical(param_config[0], name=param_config[-1])) elif isinstance(param_config[0], float): self.param_space.append( space.Real(low=param_config[0], high=param_config[1], prior='uniform', name=param_config[-1])) elif isinstance(param_config[0], int): self.param_space.append( space.Integer(low=param_config[0], high=param_config[1], name=param_config[-1])) else: raise self.kfold_n_splits = kfold_n_splits if score_measure is not None: self.score_sign = score_sign self.score_measure = score_measure else: self.score_measure = partial(f1_score, average='macro') self.score_sign = -1
def skopt_bayesian_optimization(X, y): param_space = [ space.Integer(3, 15, name='max_depth'), space.Integer(100, 600, name='n_estimators'), space.Categorical(['gini', 'entropy'], name='criterion'), space.Real(0.01, 1, prior='uniform', name='max_features') ] param_names = ['max_depth', 'n_estimators', 'criterion', 'max_features'] optimization_function = partial(optimize, param_names=param_names, x=X, y=y) result = gp_minimize(optimization_function, dimensions=param_space, n_calls=15, n_random_starts=10, verbose=10) print(dict(zip(param_names, result.x)))
xtest = x[test_idx] ytest = y[test_idx] model.fit(xtrain, ytrain) preds = model.predict(xtest) fold_acc = metrics.accuracy_score(ytest, preds) accuracies.append(fold_acc) return -1 * np.mean(accuracies) param_space = [ space.Integer(3, 15, name="max_depth"), space.Integer(100, 600, name="n_estimators"), space.Categorical(["gini", "entropy"], name="criterion"), space.Real(0.01, 1, prior="uniform", name="max_features"), ] param_names = ["max_depth", "n_estimators", "criterion", "max_features"] optimization_function = partial(optimize, param_names=param_names, x=X, y=y) result = gp_minimize( optimization_function, dimensions=param_space, n_calls=15, n_random_starts=10, verbose=10, )
vpn_perspective = sample_perspective_clms[0] stim_IDs = data['stim_IDs'] #stimulus IDs of winning model new_ID = data['new_IDs'] #trials where new ID is introduced numb_prev_presentations = data['number_of_prev_presentations_raw '] #number_of_prev_presentations stim_IDs_perspective = data[vpn_perspective] #view dependent VPN_output = data[vpn_answer] #VPN answers alpha_raw = np.around(np.linspace(0, 0.9, num=100),decimals = 2) sigma_raw = np.around(np.linspace(0, 0.9, num=100),decimals = 2) beta_raw = np.around(np.linspace(0.1, 19.9, num=200),decimals = 2) lamda_raw = np.around(np.linspace(0, 1.9, num=200),decimals = 2) alpha_cat = space.Categorical(categories=alpha_raw,name='alpha_cat',transform = 'identity') # {0,1} rate at which familiarity was aquired sigma_cat = space.Categorical(categories=sigma_raw,name='sigma_cat',transform = 'identity') # {0,1} context dependent learning rate beta_cat = space.Categorical(categories=beta_raw,name='beta_cat',transform = 'identity') # {0,20} general disposition of VPS towards stochasticity of actions lamda_cat = space.Categorical(categories=lamda_raw,name='lamda_cat',transform = 'identity') # {0,1} maximum familiarity example = alpha_cat.rvs(1)[0] beta_cat.rvs(1)[0] lamda_cat.rvs(1)[0] params_m_1 = [.38,.74,5.27,.64] params_m_2 = [.02,.16,18.41,1.44] params_m_3 = [.29,2.89,.62] params_m_4 = [.38,1.19,.74] params_m_5 = [.83,.13,.8,1.82,1.02] params_m_6 = [.55,.02,.33,.5,1.07,1.49]
# read the training data df = pd.read_csv('dataset/train.csv') # here we have training features x = df.drop('price_range' , axis = 1).values # and the targets y = df.price_range.values # define a parameter sapce param_space = [ # max_depth is an integer between 3 and 10 space.Integer(3,15, name='max_depth'), # n_estimator is an integer between 50 and 1500 space.Integer(100, 1500 , name="n_estimators"), # criterion is a category. here we define list of categories space.Categorical(['gini' , 'entropy'] , name='criterion'), # you can also have real numbered sapce and define a # distribution you want to pick it from space.Real(0.01 , 1 , prior='uniform' , name='max_features') ] # make a list of params names # this has to be same order as the search sapce # inside the main function param_names = [ 'max_depth', 'n_estimators', 'criterion', 'max_features' ]
from deephyper.problem import NaProblem from nas_big_data.covertype.dense_skipco import create_search_space from nas_big_data.covertype.load_data import load_data dim = space.Real(1, 1000, prior="log-uniform") Problem = NaProblem(seed=2019) Problem.load_data(load_data) Problem.search_space(create_search_space, num_layers=10) Problem.hyperparameters( batch_size=space.Categorical([32, 64, 128, 256, 512, 1024]), learning_rate=space.Real(1e-3, 1e-1, prior="log-uniform"), optimizer="adam", num_epochs=20, # maximal bound verbose=0, callbacks=dict( CSVExtendedLogger=dict(), ModelCheckpoint=dict( monitor="val_acc", mode="max", save_best_only=True, verbose=0, filepath="model.h5", save_weights_only=True, ), ),
from skopt import space from skopt.utils import use_named_args from skopt import gp_minimize """ We will tune the following hyperparameters of the SVM model: - C, the regularization parameter. - kernel, the type of kernel used in the model. - degree, used for the polynomial kernel. - gamma, used in most other kernels. """ search_space = list() search_space.append(space.Real(1e-6, 100.0, prior="log-uniform", name="C")) search_space.append( space.Categorical(["linear", "poly", "rbf", "sigmoid"], name="kernel")) search_space.append(space.Integer(1, 5, name="degree")) search_space.append(space.Real(1e-6, 100.0, prior="log-uniform", name="gamma")) # define the function used to evaluate a given configuration @use_named_args(search_space) def evaluate_model(**params): # configure the model with specific hyperparameters model = SVC() model.set_params(**params) # define test harness cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1) # calculate 10-fold cross validation result = cross_val_score(model, X, y, cv=cv, n_jobs=-1, scoring='accuracy') # calculate the mean of the scores