本文整理汇总了Python中ray.tune.grid_search方法的典型用法代码示例。如果您正苦于以下问题:Python tune.grid_search方法的具体用法?Python tune.grid_search怎么用?Python tune.grid_search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ray.tune
的用法示例。
在下文中一共展示了tune.grid_search方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _generic_finetune_defense
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def _generic_finetune_defense(train, dual_defense=False, envs=None, exp_suffix=""):
"""Finetuning victim against adversary.
This is the most generic helper method, used as a base for `_hyper_finetune_defense`
and `_finetune_defense`.
"""
_sparse_reward(train)
train["num_env"] = 16 # TODO(adam): cleaner way of allowing finetuning LSTMs
train["normalize_observations"] = False
ray_config = {
FINETUNE_PATHS_TYPES: tune.grid_search(
_finetune_configs(envs=envs, dual_defense=dual_defense)
),
}
dual_name = "dual" if dual_defense else "single"
exp_name = f"finetune_defense_{dual_name}_{exp_suffix}"
return ray_config, exp_name
示例2: generate_test_data
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def generate_test_data():
"""Used by tests/generate_test_data.sh to generate tests/data/gather_tb/.
"tests/data/gather_tb/" should contain 4 Tensorboard run directories ("sb_tb/" and
"tb/" for each of two trials in the search space below).
"""
sacred_ex_name = "expert_demos"
run_name = "TEST"
n_seeds = 1
search_space = {
"config_updates": {
"init_rl_kwargs": {
"learning_rate": tune.grid_search([3e-4 * x for x in (1 / 3, 1 / 2)]),
},
}
}
base_named_configs = ["cartpole", "fast"]
base_config_updates = {
"init_tensorboard": True,
"rollout_save_final": False,
}
示例3: example_gail_easy
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def example_gail_easy():
sacred_ex_name = "train_adversarial"
run_name = "example-gail-easy"
n_seeds = 1
search_space = {
"named_configs": tune.grid_search([[env] for env in EASY_ENVS]),
"config_updates": {
"init_trainer_kwargs": {
"init_rl_kwargs": {
"learning_rate": tune.grid_search(np.logspace(3e-6, 1e-1, num=3)),
"nminibatches": tune.grid_search([16, 32, 64]),
},
},
},
}
base_config_updates = {
"init_tensorboard": True,
"init_trainer_kwargs": {"use_gail": True},
}
示例4: test_ls_with_cfg
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def test_ls_with_cfg(start_ray, tmpdir):
experiment_name = "test_ls_with_cfg"
experiment_path = os.path.join(str(tmpdir), experiment_name)
tune.run(
"__fake",
name=experiment_name,
stop={"training_iteration": 1},
config={"test_variable": tune.grid_search(list(range(5)))},
local_dir=str(tmpdir))
columns = [CONFIG_PREFIX + "test_variable", "trial_id"]
limit = 4
with Capturing() as output:
commands.list_trials(experiment_path, info_keys=columns, limit=limit)
lines = output.captured
assert all(col in lines[1] for col in columns)
assert lines[1].count("|") == len(columns) + 1
assert len(lines) == 3 + limit + 1
示例5: tune_example
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def tune_example(num_workers=1, use_gpu=False):
TorchTrainable = TorchTrainer.as_trainable(
model_creator=model_creator,
data_creator=data_creator,
optimizer_creator=optimizer_creator,
loss_creator=nn.MSELoss, # Note that we specify a Loss class.
num_workers=num_workers,
use_gpu=use_gpu,
config={BATCH_SIZE: 128}
)
analysis = tune.run(
TorchTrainable,
num_samples=3,
config={"lr": tune.grid_search([1e-4, 1e-3])},
stop={"training_iteration": 2},
verbose=1)
return analysis.get_best_config(metric="validation_loss", mode="min")
# __end_torch_tune_example__
示例6: get_tune_config
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def get_tune_config(tune_options, options_files):
with open(tune_options) as f:
options = yaml.load(f, Loader=yaml.FullLoader)
if "epochs" in options and options["epochs"] == 1:
raise ValueError("Using only 1 epoch, must be a mistake.")
config = {}
for k, v in options.items():
if not k.startswith("var:"):
config[k] = v
else:
config[k.replace("var:", "")] = tune.grid_search(v)
if options_files is not None:
print("Options files: {}".format(options_files))
config["options"] = [os.path.realpath(op) for op in options_files]
return config
示例7: gen_trainer_config
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def gen_trainer_config(env_config):
# if args.algo == 'DQN':
# config = dqn.DEFAULT_CONFIG.copy()
# elif args.algo == 'PPO':
# config = ppo.DEFAULT_CONFIG.copy()
# elif args.algo == 'APEX':
# config = dqn.apex.APEX_DEFAULT_CONFIG.copy()
# elif args.algo == 'APPO':
# config = ppo.appo.DEFAULT_CONFIG.copy()
# elif args.algo == 'IMPALA':
# config = impala.DEFAULT_CONFIG.copy()
# elif args.algo == 'A3C':
# config = a3c.DEFAULT_CONFIG.copy()
# elif args.algo == 'A2C':
# config = a3c.a2c.A2C_DEFAULT_CONFIG.copy()
# else:
# assert 0 == 1, 'Unexpected args.algo.'
config = {"ignore_worker_failures": True,
"env": CityflowGymEnv, "env_config": env_config,
"num_gpus": 0, "num_workers": 24,
"num_cpus_per_worker": 1, # "num_gpus_per_worker": 0.03125,
"num_cpus_for_driver": 1}
# config['lr'] = grid_search([1e-2, 1e-3, 1e-4])
return config
示例8: run_param_specs
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def run_param_specs(param_specs):
'''Run the given param_specs in parallel trials using ray. Used for benchmarking.'''
ray.init()
ray_trials = tune.run(
ray_trainable,
name='param_specs',
config={
'spec': tune.grid_search(param_specs),
'trial_index': 0,
},
resources_per_trial=infer_trial_resources(param_specs[0]),
num_samples=1,
reuse_actors=False,
server_port=util.get_port(),
)
ray.shutdown()
示例9: build_config_space
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def build_config_space(spec):
'''
Build ray config space from flattened spec.search
Specify a config space in spec using `"{key}__{space_type}": {v}`.
Where `{space_type}` is `grid_search` of `ray.tune`, or any function name of `np.random`:
- `grid_search`: str/int/float. v = list of choices
- `choice`: str/int/float. v = list of choices
- `randint`: int. v = [low, high)
- `uniform`: float. v = [low, high)
- `normal`: float. v = [mean, stdev)
For example:
- `"explore_anneal_epi__randint": [10, 60],` will sample integers uniformly from 10 to 60 for `explore_anneal_epi`,
- `"lr__uniform": [0.001, 0.1]`, and it will sample `lr` using `np.random.uniform(0.001, 0.1)`
If any key uses `grid_search`, it will be combined exhaustively in combination with other random sampling.
'''
space_types = ('grid_search', 'choice', 'randint', 'uniform', 'normal')
config_space = {}
for k, v in util.flatten_dict(spec['search']).items():
key, space_type = k.split('__')
assert space_type in space_types, f'Please specify your search variable as {key}__<space_type> in one of {space_types}'
if space_type == 'grid_search':
config_space[key] = tune.grid_search(v)
elif space_type == 'choice':
config_space[key] = tune.sample_from(lambda spec, v=v: random.choice(v))
else:
np_fn = getattr(np.random, space_type)
config_space[key] = tune.sample_from(lambda spec, v=v: np_fn(*v))
return config_space
示例10: _best_guess_spec
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def _best_guess_spec(envs=None):
spec = {
"config": {
"env_name:embed_path": tune.grid_search(_env_victim(envs)),
"embed_index": tune.sample_from(
lambda spec: VICTIM_INDEX[spec.config["env_name:embed_path"][0]]
),
"seed": tune.grid_search(list(range(3))),
},
}
return spec
示例11: _finetune_spec
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def _finetune_spec(envs=None):
spec = {
"config": {
"env_name:embed_path": tune.grid_search(_env_victim(envs)),
"seed": tune.grid_search(list(range(3))),
"load_policy": {
"path": tune.sample_from(lambda spec: spec.config["env_name:embed_path"][1]),
},
"embed_index": tune.sample_from(
lambda spec: VICTIM_INDEX[spec.config["env_name:embed_path"][0]]
),
},
}
return spec
示例12: _finetune_defense
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def _finetune_defense(train, **kwargs):
"""Multi-seed, long (20e6) timestep finetuning against adversary."""
ray_config, exp_name = _generic_finetune_defense(train, **kwargs)
ray_config["seed"] = tune.grid_search(list(range(5)))
spec = {
"config": ray_config,
}
return spec, exp_name
# ### RETRAINING ADVERSARY AGAINST ADVERSARIALLY-FINETUNED VICTIM ### #
示例13: _generic_train_adv_against_finetuned
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def _generic_train_adv_against_finetuned(train, finetune_run, from_scratch=True):
"""Retrain adversary against defensively finetuned victim.
This is the most generic helper method, that is used by `_hyper_train_adv_against_finetuned`
and `_train_adv_against_finetuned`."""
_sparse_reward(train)
train["embed_type"] = "ppo2" # all victims are new-style policies because we finetuned them
ray_config = {
TRAIN_AGAINST_FINETUNED_PATHS: tune.grid_search(
_train_against_finetuned_configs(finetune_run=finetune_run, from_scratch=from_scratch)
),
}
from_scratch_name = "from_scratch" if from_scratch else "finetune"
exp_name = f"adv_{from_scratch_name}_against_{finetune_run}"
return ray_config, exp_name
示例14: debug_config
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def debug_config(score):
"""Try zero-agent and random-agent against pre-trained zoo policies."""
score = dict(score)
score["episodes"] = 1
score["agent_a_type"] = "zoo"
score["agent_b_type"] = "zoo"
spec = {"config": {"agent_a_path": tune.grid_search(["1", "2"])}}
exp_suffix = "debug"
_ = locals() # quieten flake8 unused variable warning
del _
示例15: debug_config
# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import grid_search [as 别名]
def debug_config():
spec = {
"config": {"seed": tune.grid_search([0, 1])},
}
exp_name = "debug"
_ = locals() # quieten flake8 unused variable warning
del _