本文整理匯總了Python中hyperopt.hp.choice方法的典型用法代碼示例。如果您正苦於以下問題:Python hp.choice方法的具體用法?Python hp.choice怎麽用?Python hp.choice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類hyperopt.hp
的用法示例。
在下文中一共展示了hp.choice方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_compilefn_train_test_split
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def test_compilefn_train_test_split(tmpdir):
db_name = "test"
exp_name = "test2"
fn = CompileFN(db_name, exp_name,
data_fn=data.data,
model_fn=model.build_model,
optim_metric="acc",
optim_metric_mode="max",
# eval
valid_split=.5,
stratified=False,
random_state=True,
save_dir="/tmp/")
hyper_params = {
"data": {},
"shared": {"max_features": 100, "maxlen": 20},
"model": {"filters": hp.choice("m_filters", (2, 5)),
"hidden_dims": 3,
},
"fit": {"epochs": 1}
}
fn_test(fn, hyper_params, tmp_dir=str(tmpdir))
trials = Trials()
best = fmin(fn, hyper_params, trials=trials, algo=tpe.suggest, max_evals=2)
assert isinstance(best, dict)
示例2: __init__
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def __init__(self, estimator=None, max_evals=50, cv=5, handle_cv_failure=False,
scoring='accuracy', best_score=0.0, max_opt_time=None, max_eval_time=None,
pgo:Optional[PGO]=None, show_progressbar=True, args_to_scorer=None,
verbose=False):
self.max_evals = max_evals
if estimator is None:
self.estimator = LogisticRegression()
else:
self.estimator = estimator
self.search_space = hp.choice('meta_model', [hyperopt_search_space(self.estimator, pgo=pgo)])
self.scoring = scoring
self.best_score = best_score
self.handle_cv_failure = handle_cv_failure
self.cv = cv
self._trials = Trials()
self.max_opt_time = max_opt_time
self.max_eval_time = max_eval_time
self.show_progressbar = show_progressbar
if args_to_scorer is not None:
self.args_to_scorer = args_to_scorer
else:
self.args_to_scorer = {}
self.verbose = verbose
示例3: test_convert_conditional_space
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def test_convert_conditional_space(self):
a_or_b = configuration_space.CategoricalHyperparameter("a_or_b", ["a", "b"])
cond_a = configuration_space.UniformFloatHyperparameter(
'cond_a', 0, 1, conditions=[['a_or_b == a']])
cond_b = configuration_space.UniformFloatHyperparameter(
'cond_b', 0, 3, q=0.1, conditions=[['a_or_b == b']])
conditional_space = {"a_or_b": a_or_b, "cond_a": cond_a, "cond_b": cond_b}
cs = self.pyll_writer.write(conditional_space)
expected = StringIO.StringIO()
expected.write('from hyperopt import hp\nimport hyperopt.pyll as pyll')
expected.write('\n\n')
expected.write('param_0 = hp.uniform("cond_a", 0.0, 1.0)\n')
expected.write('param_1 = hp.quniform("cond_b", -0.0499, 3.05, 0.1)\n')
expected.write('param_2 = hp.choice("a_or_b", [\n')
expected.write(' {"a_or_b": "a", "cond_a": param_0, },\n')
expected.write(' {"a_or_b": "b", "cond_b": param_1, },\n')
expected.write(' ])\n\n')
expected.write('space = {"a_or_b": param_2}\n')
self.assertEqual(expected.getvalue(), cs)
示例4: test_operator_in
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def test_operator_in(self):
a_or_b = configuration_space.CategoricalHyperparameter("a_or_b", ["a", "b"])
cond_a = configuration_space.UniformFloatHyperparameter(
'cond_a', 0, 1, conditions=[['a_or_b == a']])
cond_b = configuration_space.UniformFloatHyperparameter(
'cond_b', 0, 3, q=0.1, conditions=[['a_or_b == b']])
e = configuration_space.UniformFloatHyperparameter("e", 0, 5,
conditions=[['a_or_b in {a,b}']])
conditional_space_operator_in = {"a_or_b": a_or_b, "cond_a": cond_a,
"cond_b": cond_b, "e": e}
cs = self.pyll_writer.write(conditional_space_operator_in)
expected = StringIO.StringIO()
expected.write('from hyperopt import hp\nimport hyperopt.pyll as pyll')
expected.write('\n\n')
expected.write('param_0 = hp.uniform("cond_a", 0.0, 1.0)\n')
expected.write('param_1 = hp.quniform("cond_b", -0.0499, 3.05, 0.1)\n')
expected.write('param_2 = hp.uniform("e", 0.0, 5.0)\n')
expected.write('param_3 = hp.choice("a_or_b", [\n')
expected.write(' {"a_or_b": "a", "cond_a": param_0, "e": param_2, '
'},\n')
expected.write(' {"a_or_b": "b", "cond_b": param_1, "e": param_2, '
'},\n')
expected.write(' ])\n\n')
expected.write('space = {"a_or_b": param_3}\n')
self.assertEqual(expected.getvalue(), cs)
示例5: set_default_hyperparameters
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def set_default_hyperparameters(self):
"""
Set default hyperparameter space. If none is provided, default is used.
"""
self.hyperparameter_space = {
'scale_X': hp.choice('scale_X', ['std', 'mm01', 'mm11', None]),
'scale_y': hp.choice('scale_y', ['std', 'mm01', 'mm11', None]),
}
if self.input_obj.keywords['pes_format'] == 'interatomics':
self.set_hyperparameter('morse_transform', hp.choice('morse_transform',[{'morse': True,'morse_alpha': hp.quniform('morse_alpha', 1, 2, 0.1)},{'morse': False}]))
else:
self.set_hyperparameter('morse_transform', hp.choice('morse_transform',[{'morse': False}]))
if self.pip:
val = hp.choice('pip',[{'pip': True,'degree_reduction': hp.choice('degree_reduction', [True,False])}])
self.set_hyperparameter('pip', val)
else:
self.set_hyperparameter('pip', hp.choice('pip', [{'pip': False}]))
if self.input_obj.keywords['gp_ard'] == 'opt': # auto relevancy determination (independant length scales for each feature)
self.set_hyperparameter('ARD', hp.choice('ARD', [True,False]))
#TODO add optional space inclusions, something like: if option: self.hyperparameter_space['newoption'] = hp.choice(..)
示例6: test_compilefn_cross_val
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def test_compilefn_cross_val(tmpdir):
db_name = "test"
exp_name = "test2"
fn = CompileFN(db_name, exp_name,
cv_n_folds=3,
stratified=False,
random_state=True,
data_fn=data.data,
model_fn=model.build_model,
optim_metric="loss",
optim_metric_mode="min",
save_dir="/tmp/")
hyper_params = {
"data": {},
"shared": {"max_features": 100, "maxlen": 20},
"model": {"filters": hp.choice("m_filters", (2, 5)),
"hidden_dims": 3,
},
"fit": {"epochs": 1}
}
fn_test(fn, hyper_params, tmp_dir=str(tmpdir))
trials = Trials()
best = fmin(fn, hyper_params, trials=trials, algo=tpe.suggest, max_evals=2)
assert isinstance(best, dict)
示例7: train_and_predict_with_time_control_basic
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def train_and_predict_with_time_control_basic(X_train: pd.DataFrame, X_test:pd.DataFrame, y:pd.Series, config:Config, random_state, test=False):
if test:
X_train, X_test, y, y_test = data_split_by_time(X_train, y, test_size=0.2)
params = {
"objective": "binary",
"metric": "auc",
"verbosity": -1,
"seed": 1,
"num_threads": 4
}
result = None
##-------------Generate random index--------------
#index = random_state.choice(X_train.shape[0], 30000, replace=False)
#index.sort()
### If assign the n_p_ratio, the HPO process will become very slow, which
### indicates low convergence speed.
#index = sample(X_train, y, sample_size=30000, n_p_ratio=20, is_sort=True)
index = sample(X_train, y, sample_size=30000, is_sort=True, random_state=random_state, same_ratio=False)
X_sample = X_train.iloc[index]
y_sample = y.iloc[index]
hyperparams = hyperopt_lightgbm_basic(X_sample, y_sample, params, config)
X_train, X_val, y, y_val = data_split_by_time(X_train, y, test_size=0.1)
train_data = lgb.Dataset(X_train, y)
val_data = lgb.Dataset(X_val, y_val)
model = lgb.train({**params, **hyperparams}, train_data, 10000,
val_data, early_stopping_rounds=30, verbose_eval=100)
result = model.predict(X_test)
return result
示例8: hyperopt_lightgbm_basic
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def hyperopt_lightgbm_basic(X, y, params, config, max_evals=50):
X_train, X_test, y_train, y_test = data_split_by_time(X, y, test_size=0.2)
X_train, X_val, y_train, y_val = data_split_by_time(X, y, test_size=0.3)
train_data = lgb.Dataset(X_train, label=y_train)
val_data = lgb.Dataset(X_val, label=y_val)
space = {
"learning_rate": hp.loguniform("learning_rate", np.log(0.01), np.log(0.5)),
#"forgetting_factor": hp.loguniform("forgetting_factor", 0.01, 0.1)
#"max_depth": hp.choice("max_depth", [-1, 2, 3, 4, 5, 6]),
"max_depth": hp.choice("max_depth", [1, 2, 3, 4, 5, 6]),
"num_leaves": hp.choice("num_leaves", np.linspace(10, 200, 50, dtype=int)),
"feature_fraction": hp.quniform("feature_fraction", 0.5, 1.0, 0.1),
"bagging_fraction": hp.quniform("bagging_fraction", 0.5, 1.0, 0.1),
"bagging_freq": hp.choice("bagging_freq", np.linspace(0, 50, 10, dtype=int)),
"reg_alpha": hp.uniform("reg_alpha", 0, 2),
"reg_lambda": hp.uniform("reg_lambda", 0, 2),
"min_child_weight": hp.uniform('min_child_weight', 0.5, 10),
}
def objective(hyperparams):
model = lgb.train({**params, **hyperparams}, train_data, 100,
val_data, early_stopping_rounds=30, verbose_eval=0)
pred = model.predict(X_test)
score = roc_auc_score(y_test, pred)
return {'loss': -score, 'status': STATUS_OK}
trials = Trials()
best = hyperopt.fmin(fn=objective, space=space, trials=trials,
algo=tpe.suggest, max_evals=max_evals, verbose=1,
rstate=np.random.RandomState(1))
hyperparams = space_eval(space, best)
log(f"auc = {-trials.best_trial['result']['loss']:0.4f} {hyperparams}")
return hyperparams
示例9: hyperopt_lightgbm
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def hyperopt_lightgbm(X_train: pd.DataFrame, y_train: pd.Series, params: Dict, config: Config, max_evals=10):
X_train, X_test, y_train, y_test = data_split_by_time(X_train, y_train, test_size=0.2)
X_train, X_val, y_train, y_val = data_split_by_time(X_train, y_train, test_size=0.3)
train_data = lgb.Dataset(X_train, label=y_train)
valid_data = lgb.Dataset(X_val, label=y_val)
space = {
"learning_rate": hp.loguniform("learning_rate", np.log(0.01), np.log(0.5)),
#"max_depth": hp.choice("max_depth", [-1, 2, 3, 4, 5, 6]),
"max_depth": hp.choice("max_depth", [1, 2, 3, 4, 5, 6]),
"num_leaves": hp.choice("num_leaves", np.linspace(10, 200, 50, dtype=int)),
"feature_fraction": hp.quniform("feature_fraction", 0.5, 1.0, 0.1),
"bagging_fraction": hp.quniform("bagging_fraction", 0.5, 1.0, 0.1),
"bagging_freq": hp.choice("bagging_freq", np.linspace(0, 50, 10, dtype=int)),
"reg_alpha": hp.uniform("reg_alpha", 0, 2),
"reg_lambda": hp.uniform("reg_lambda", 0, 2),
"min_child_weight": hp.uniform('min_child_weight', 0.5, 10),
}
def objective(hyperparams):
if config.time_left() < 50:
return {'status': STATUS_FAIL}
else:
model = lgb.train({**params, **hyperparams}, train_data, 100,
valid_data, early_stopping_rounds=10, verbose_eval=0)
pred = model.predict(X_test)
score = roc_auc_score(y_test, pred)
#score = model.best_score["valid_0"][params["metric"]]
# in classification, less is better
return {'loss': -score, 'status': STATUS_OK}
trials = Trials()
best = hyperopt.fmin(fn=objective, space=space, trials=trials,
algo=tpe.suggest, max_evals=max_evals, verbose=1,
rstate=np.random.RandomState(1))
hyperparams = space_eval(space, best)
log(f"auc = {-trials.best_trial['result']['loss']:0.4f} {hyperparams}")
return hyperparams
示例10: __init__
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def __init__(self):
self.search_space = {
'learning_rate': hp.loguniform('learning_rate', np.log(0.00001), np.log(0.1)),
'L1_flag': hp.choice('L1_flag', [True, False]),
'hidden_size': scope.int(hp.qloguniform('hidden_size', np.log(8), np.log(256),1)),
'batch_size': scope.int(hp.qloguniform('batch_size', np.log(8), np.log(4096),1)),
'margin': hp.uniform('margin', 0.0, 10.0),
'optimizer': hp.choice('optimizer', ["adam", "sgd", 'rms']),
'epochs': hp.choice('epochs', [500]) # always choose 10 training epochs.
}
示例11: hyperopt_lightgbm
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def hyperopt_lightgbm(X: pd.DataFrame, y: pd.Series, params: Dict, config: Config):
X_train, X_val, y_train, y_val = data_split(X, y, test_size=0.5)
train_data = lgb.Dataset(X_train, label=y_train)
valid_data = lgb.Dataset(X_val, label=y_val)
space = {
"max_depth": hp.choice("max_depth", np.arange(2, 10, 1, dtype=int)),
# smaller than 2^(max_depth)
"num_leaves": hp.choice("num_leaves", np.arange(4, 200, 4, dtype=int)),
"feature_fraction": hp.quniform("feature_fraction", 0.2, 0.8, 0.1),
# "bagging_fraction": hp.quniform("bagging_fraction", 0.2, 0.8, 0.1),
# "bagging_freq": hp.choice("bagging_freq", np.linspace(0, 10, 2, dtype=int)),
# "scale_pos_weight":hp.uniform('scale_pos_weight',1.0, 10.0),
# "colsample_by_tree":hp.uniform("colsample_bytree",0.5,1.0),
"min_child_weight": hp.quniform('min_child_weight', 2, 50, 2),
"reg_alpha": hp.uniform("reg_alpha", 2.0, 8.0),
"reg_lambda": hp.uniform("reg_lambda", 2.0, 8.0),
"learning_rate": hp.quniform("learning_rate", 0.05, 0.4, 0.01),
# "learning_rate": hp.loguniform("learning_rate", np.log(0.04), np.log(0.5)),
#
"min_data_in_leaf": hp.choice('min_data_in_leaf', np.arange(200, 2000, 100, dtype=int)),
#"is_unbalance": hp.choice("is_unbalance", [True])
}
def objective(hyperparams):
model = lgb.train({**params, **hyperparams}, train_data, 300,
valid_data, early_stopping_rounds=45, verbose_eval=0)
score = model.best_score["valid_0"][params["metric"]]
# in classification, less is better
return {'loss': -score, 'status': STATUS_OK}
trials = Trials()
best = hyperopt.fmin(fn=objective, space=space, trials=trials,
algo=tpe.suggest, max_evals=150, verbose=1,
rstate=np.random.RandomState(1))
hyperparams = space_eval(space, best)
log(f"auc = {-trials.best_trial['result']['loss']:0.4f} {hyperparams}")
return hyperparams
示例12: predict
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def predict(self, x):
data = xgb.DMatrix(x)
pred = self.model.predict(data)
return pred
# -----------------------------------
# 探索するパラメータの空間の指定
# -----------------------------------
# hp.choiceでは、複數の選択肢から選ぶ
# hp.uniformでは、下限・上限を指定した一様分布から抽出する。引數は下限・上限
# hp.quniformでは、下限・上限を指定した一様分布のうち一定の間隔ごとの點から抽出する。引數は下限・上限・間隔
# hp.loguniformでは、下限・上限を指定した対數が一様分布に従う分布から抽出する。引數は下限・上限の対數をとった値
示例13: test_read_switch
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def test_read_switch(self):
# 0 switch
# 1 hyperopt_param
# 2 Literal{dist1}
# 3 randint
# 4 Literal{2}
# 5 Literal{uniform}
# 6 Literal{normal}
dist = hp.choice('dist1', ['uniform', 'normal'])
ret = self.pyll_reader.read_switch(dist)
expected = configuration_space.CategoricalHyperparameter(
'dist1', ['uniform', 'normal'])
self.assertEqual(expected, ret)
bigger_choice = hp.choice('choice', [
{'choice': "zero", 'a': 0, 'b': hp.uniform('b', 0, 10)},
{'choice': "other", 'a': 1, 'b': hp.uniform('b', 0, 10)}])
ret = self.pyll_reader.read_switch(bigger_choice)
expected = configuration_space.CategoricalHyperparameter(
'choice', ['zero', 'other'])
self.assertEqual(expected, ret)
self.assertEqual(2, len(self.pyll_reader.constants))
# Only the hyperparameter b is put into pyll_reader.hyperparameters
self.assertEqual(1, len(self.pyll_reader.hyperparameters))
# TODO: duplicate these tests for Integer/care about integers + test if
# the warning of non-uniform parameters is actually printed
示例14: test_convert_complex_space
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def test_convert_complex_space(self):
cs = self.pyll_writer.write(config_space)
expected = StringIO.StringIO()
expected.write('from hyperopt import hp\nimport hyperopt.pyll as pyll')
expected.write('\n\n')
expected.write('param_0 = hp.uniform("LOG2_C", -5.0, 15.0)\n')
expected.write('param_1 = hp.uniform("LOG2_gamma", -14.9999800563, '
'3.0)\n')
expected.write('param_2 = hp.choice("kernel", [\n')
expected.write(' {"kernel": "linear", },\n')
expected.write(' {"kernel": "rbf", "LOG2_gamma": param_1, },\n')
expected.write(' ])\n')
expected.write('param_3 = hp.uniform("lr", 0.0001, 1.0)\n')
expected.write('param_4 = pyll.scope.int(hp.quniform('
'"neurons", 15.50001, 1024.5, 16.0))\n')
expected.write('param_5 = hp.choice("classifier", [\n')
expected.write(' {"classifier": "nn", "lr": param_3, "neurons": '
'param_4, },\n')
expected.write(' {"classifier": "svm", "LOG2_C": param_0, '
'"kernel": param_2, },\n')
expected.write(' ])\n')
expected.write('param_6 = hp.choice("preprocessing", [\n')
expected.write(' {"preprocessing": "None", },\n')
expected.write(' {"preprocessing": "pca", },\n')
expected.write(' ])\n\n')
expected.write('space = {"classifier": param_5, '
'"preprocessing": param_6}\n')
self.assertEqual(expected.getvalue(), cs)
self.pyll_writer.reset_hyperparameter_countr()
expected.seek(0)
cs = self.pyll_writer.write(config_space_2)
self.assertEqual(expected.getvalue().replace("gamma", "gamma_2"), cs)
示例15: set_default_hyperparameters
# 需要導入模塊: from hyperopt import hp [as 別名]
# 或者: from hyperopt.hp import choice [as 別名]
def set_default_hyperparameters(self, nn_search_space=1):
"""
Set default hyperparameter space. If none is provided, default is used.
Parameters
----------
nn_search_space : int
Which tier of default hyperparameter search spaces to use. Neural networks have too many hyperparameter configurations to search across,
so this option reduces the number of variable hyperparameters to search over. Generally, larger integer => more hyperparameters, and more iterations of hp_maxit are recommended.
"""
if nn_search_space == 1:
self.hyperparameter_space = {
'scale_X': hp.choice('scale_X',
[
{'scale_X': 'mm11',
'activation': hp.choice('activ2', ['tanh'])},
{'scale_X': 'std',
'activation': hp.choice('activ3', ['tanh'])},
]),
'scale_y': hp.choice('scale_y', ['std', 'mm01', 'mm11']),}
# TODO make more expansive search spaces, benchmark them, expose them as input options
#elif nn_search_space == 2:
#elif nn_search_space == 3:
else:
raise Exception("Invalid search space specification")
# Standard geometry transformations, always use these.
if self.input_obj.keywords['pes_format'] == 'interatomics':
self.set_hyperparameter('morse_transform', hp.choice('morse_transform',[{'morse': True,'morse_alpha': hp.quniform('morse_alpha', 1, 2, 0.1)},{'morse': False}]))
else:
self.set_hyperparameter('morse_transform', hp.choice('morse_transform',[{'morse': False}]))
if self.pip:
val = hp.choice('pip',[{'pip': True,'degree_reduction': hp.choice('degree_reduction', [True,False])}])
self.set_hyperparameter('pip', val)
else:
self.set_hyperparameter('pip', hp.choice('pip', [{'pip': False}]))