当前位置: 首页>>代码示例>>Python>>正文


Python tune.sample_from方法代码示例

本文整理汇总了Python中ray.tune.sample_from方法的典型用法代码示例。如果您正苦于以下问题:Python tune.sample_from方法的具体用法?Python tune.sample_from怎么用?Python tune.sample_from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ray.tune的用法示例。


在下文中一共展示了tune.sample_from方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ray_trainable

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def ray_trainable(config, reporter):
    '''
    Create an instance of a trainable function for ray: https://ray.readthedocs.io/en/latest/tune-usage.html#training-api
    Lab needs a spec and a trial_index to be carried through config, pass them with config in ray.run() like so:
    config = {
        'spec': spec,
        'trial_index': tune.sample_from(lambda spec: gen_trial_index()),
        ... # normal ray config with sample, grid search etc.
    }
    '''
    from convlab.experiment.control import Trial
    # restore data carried from ray.run() config
    spec = config.pop('spec')
    trial_index = config.pop('trial_index')
    spec['meta']['trial'] = trial_index
    spec = inject_config(spec, config)
    # run SLM Lab trial
    metrics = Trial(spec).run()
    metrics.update(config) # carry config for analysis too
    # ray report to carry data in ray trial.last_result
    reporter(trial_data={trial_index: metrics}) 
开发者ID:ConvLab,项目名称:ConvLab,代码行数:23,代码来源:search.py

示例2: testLongFilename

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def testLongFilename(self):
        def train(config, reporter):
            assert os.path.join(ray.utils.get_user_temp_dir(), "logdir",
                                "foo") in os.getcwd(), os.getcwd()
            reporter(timesteps_total=1)

        register_trainable("f1", train)
        run_experiments({
            "foo": {
                "run": "f1",
                "local_dir": os.path.join(ray.utils.get_user_temp_dir(),
                                          "logdir"),
                "config": {
                    "a" * 50: tune.sample_from(lambda spec: 5.0 / 7),
                    "b" * 50: tune.sample_from(lambda spec: "long" * 40),
                },
            }
        }) 
开发者ID:ray-project,项目名称:ray,代码行数:20,代码来源:test_api.py

示例3: testGridSearchAndEval

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def testGridSearchAndEval(self):
        trials = self.generate_trials({
            "run": "PPO",
            "config": {
                "qux": tune.sample_from(lambda spec: 2 + 2),
                "bar": grid_search([True, False]),
                "foo": grid_search([1, 2, 3]),
                "baz": "asd",
            },
        }, "grid_eval")
        trials = list(trials)
        self.assertEqual(len(trials), 6)
        self.assertEqual(trials[0].config, {
            "bar": True,
            "foo": 1,
            "qux": 4,
            "baz": "asd",
        })
        self.assertEqual(trials[0].evaluated_params, {
            "bar": True,
            "foo": 1,
            "qux": 4,
        })
        self.assertEqual(trials[0].experiment_tag, "0_bar=True,foo=1,qux=4") 
开发者ID:ray-project,项目名称:ray,代码行数:26,代码来源:test_var.py

示例4: ops_experiment

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def ops_experiment(size, ntrials, nsteps, result_dir, nthreads, smoke_test):
    config={
        'size': size,
        'lr': sample_from(lambda spec: math.exp(random.uniform(math.log(1e-4), math.log(5e-1)))),
        'seed': sample_from(lambda spec: random.randint(0, 1 << 16)),
        'n_steps_per_epoch': nsteps,
     }
    experiment = RayExperiment(
        name=f'Ops_factorization_{size}',
        run=TrainableOps,
        local_dir=result_dir,
        num_samples=ntrials,
        checkpoint_at_end=True,
        resources_per_trial={'cpu': nthreads, 'gpu': 0},
        stop={
            'training_iteration': 1 if smoke_test else 99999,
            'negative_loss': -1e-8
        },
        config=config,
    )
    return experiment 
开发者ID:HazyResearch,项目名称:learning-circuits,代码行数:23,代码来源:learning_ops.py

示例5: fft_experiment_block2x2

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def fft_experiment_block2x2(size, ntrials, nsteps, result_dir, nthreads, smoke_test):
    config={
        'size': size,
        'lr': sample_from(lambda spec: math.exp(random.uniform(math.log(1e-4), math.log(5e-1)))),
        'seed': sample_from(lambda spec: random.randint(0, 1 << 16)),
        'n_steps_per_epoch': nsteps,
     }
    experiment = RayExperiment(
        name=f'Fft_factorization_block_{size}',
        run=TrainableFftBlock2x2,
        local_dir=result_dir,
        num_samples=ntrials,
        checkpoint_at_end=True,
        resources_per_trial={'cpu': nthreads, 'gpu': 0},
        stop={
            'training_iteration': 1 if smoke_test else 99999,
            'negative_loss': -1e-8
        },
        config=config,
    )
    return experiment 
开发者ID:HazyResearch,项目名称:learning-circuits,代码行数:23,代码来源:learning_fft_old.py

示例6: fft_experiment_blockperm

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def fft_experiment_blockperm(size, ntrials, nsteps, result_dir, nthreads, smoke_test):
    config={
        'size': size,
        'lr': sample_from(lambda spec: math.exp(random.uniform(math.log(1e-4), math.log(5e-1)))),
        'seed': sample_from(lambda spec: random.randint(0, 1 << 16)),
        'n_steps_per_epoch': nsteps,
     }
    experiment = RayExperiment(
        name=f'Fft_factorization_block_perm_{size}',
        run=TrainableFftBlockPerm,
        local_dir=result_dir,
        num_samples=ntrials,
        checkpoint_at_end=True,
        resources_per_trial={'cpu': nthreads, 'gpu': 0},
        stop={
            'training_iteration': 1 if smoke_test else 99999,
            'negative_loss': -1e-8
        },
        config=config,
    )
    return experiment 
开发者ID:HazyResearch,项目名称:learning-circuits,代码行数:23,代码来源:learning_fft_old.py

示例7: fft_experiment_blockperm_transpose

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def fft_experiment_blockperm_transpose(size, ntrials, nsteps, result_dir, nthreads, smoke_test):
    config={
        'size': size,
        'lr': sample_from(lambda spec: math.exp(random.uniform(math.log(1e-4), math.log(5e-1)))),
        'seed': sample_from(lambda spec: random.randint(0, 1 << 16)),
        'n_steps_per_epoch': nsteps,
     }
    experiment = RayExperiment(
        name=f'Fft_factorization_block_perm_transpose_{size}',
        run=TrainableFftBlockPermTranspose,
        local_dir=result_dir,
        num_samples=ntrials,
        checkpoint_at_end=True,
        resources_per_trial={'cpu': nthreads, 'gpu': 0},
        stop={
            'training_iteration': 1 if smoke_test else 99999,
            'negative_loss': -1e-8
        },
        config=config,
    )
    return experiment 
开发者ID:HazyResearch,项目名称:learning-circuits,代码行数:23,代码来源:learning_fft_old.py

示例8: fft_experiment_block

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def fft_experiment_block(trainable, size, ntrials, nsteps, nepochsvalid, result_dir, nthreads, smoke_test):
    config={
        'target_matrix': named_target_matrix('dft', size),
        'lr': sample_from(lambda spec: math.exp(random.uniform(math.log(1e-4), math.log(5e-1)))),
        'seed': sample_from(lambda spec: random.randint(0, 1 << 16)),
        'n_steps_per_epoch': nsteps,
        'n_epochs_per_validation': nepochsvalid,
        'complex': True,
     }
    experiment = RayExperiment(
        name=f'Fft_factorization_{trainable.__name__}_{size}',
        run=trainable,
        local_dir=result_dir,
        num_samples=ntrials,
        checkpoint_at_end=True,
        resources_per_trial={'cpu': nthreads, 'gpu': 0},
        stop={
            'training_iteration': 1 if smoke_test else 99999,
            'negative_loss': -1e-8
        },
        config=config,
    )
    return experiment 
开发者ID:HazyResearch,项目名称:learning-circuits,代码行数:25,代码来源:learning_fft.py

示例9: ray_trainable

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def ray_trainable(config, reporter):
    '''
    Create an instance of a trainable function for ray: https://ray.readthedocs.io/en/latest/tune-usage.html#training-api
    Lab needs a spec and a trial_index to be carried through config, pass them with config in ray.run() like so:
    config = {
        'spec': spec,
        'trial_index': tune.sample_from(lambda spec: gen_trial_index()),
        ... # normal ray config with sample, grid search etc.
    }
    '''
    import os
    os.environ.pop('CUDA_VISIBLE_DEVICES', None)  # remove CUDA id restriction from ray
    from slm_lab.experiment.control import Trial
    # restore data carried from ray.run() config
    spec = config.pop('spec')
    spec = inject_config(spec, config)
    # tick trial_index with proper offset
    trial_index = config.pop('trial_index')
    spec['meta']['trial'] = trial_index - 1
    spec_util.tick(spec, 'trial')
    # run SLM Lab trial
    metrics = Trial(spec).run()
    metrics.update(config)  # carry config for analysis too
    # ray report to carry data in ray trial.last_result
    reporter(trial_data={trial_index: metrics}) 
开发者ID:kengz,项目名称:SLM-Lab,代码行数:27,代码来源:search.py

示例10: build_config_space

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [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 
开发者ID:ConvLab,项目名称:ConvLab,代码行数:32,代码来源:search.py

示例11: run_ray_search

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def run_ray_search(spec):
    '''
    Method to run ray search from experiment. Uses RandomSearch now.
    TODO support for other ray search algorithms: https://ray.readthedocs.io/en/latest/tune-searchalg.html
    '''
    logger.info(f'Running ray search for spec {spec["name"]}')
    # generate trial index to pass into Lab Trial
    global trial_index  # make gen_trial_index passable into ray.run
    trial_index = -1

    def gen_trial_index():
        global trial_index
        trial_index += 1
        return trial_index

    ray.init()

    ray_trials = tune.run(
        ray_trainable,
        name=spec['name'],
        config={
            "spec": spec,
            "trial_index": tune.sample_from(lambda spec: gen_trial_index()),
            **build_config_space(spec)
        },
        resources_per_trial=infer_trial_resources(spec),
        num_samples=spec['meta']['max_trial'],
        queue_trials=True,
    )
    trial_data_dict = {}  # data for Lab Experiment to analyze
    for ray_trial in ray_trials:
        ray_trial_data = ray_trial.last_result['trial_data']
        trial_data_dict.update(ray_trial_data)

    ray.shutdown()
    return trial_data_dict 
开发者ID:ConvLab,项目名称:ConvLab,代码行数:38,代码来源:search.py

示例12: _best_guess_spec

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [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 
开发者ID:HumanCompatibleAI,项目名称:adversarial-policies,代码行数:13,代码来源:train.py

示例13: _finetune_spec

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [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 
开发者ID:HumanCompatibleAI,项目名称:adversarial-policies,代码行数:16,代码来源:train.py

示例14: testConditionResolution

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def testConditionResolution(self):
        trials = self.generate_trials({
            "run": "PPO",
            "config": {
                "x": 1,
                "y": tune.sample_from(lambda spec: spec.config.x + 1),
                "z": tune.sample_from(lambda spec: spec.config.y + 1),
            },
        }, "condition_resolution")
        trials = list(trials)
        self.assertEqual(len(trials), 1)
        self.assertEqual(trials[0].config, {"x": 1, "y": 2, "z": 3})
        self.assertEqual(trials[0].evaluated_params, {"y": 2, "z": 3})
        self.assertEqual(trials[0].experiment_tag, "0_y=2,z=3") 
开发者ID:ray-project,项目名称:ray,代码行数:16,代码来源:test_var.py

示例15: testDependentLambda

# 需要导入模块: from ray import tune [as 别名]
# 或者: from ray.tune import sample_from [as 别名]
def testDependentLambda(self):
        trials = self.generate_trials({
            "run": "PPO",
            "config": {
                "x": grid_search([1, 2]),
                "y": tune.sample_from(lambda spec: spec.config.x * 100),
            },
        }, "dependent_lambda")
        trials = list(trials)
        self.assertEqual(len(trials), 2)
        self.assertEqual(trials[0].config, {"x": 1, "y": 100})
        self.assertEqual(trials[1].config, {"x": 2, "y": 200}) 
开发者ID:ray-project,项目名称:ray,代码行数:14,代码来源:test_var.py


注:本文中的ray.tune.sample_from方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。