Python和HyperOpt:如何进行多进程网格搜索?

2024-04-28 16:06:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试调整一些参数,搜索空间非常大。到目前为止,我有5个维度,可能会增加到10个左右。问题是,我认为如果我能想出如何多处理它,我可以得到一个显著的加速,但我找不到任何好的方法来做到这一点。我正在使用hyperopt,我不知道如何使它使用多个核心。以下是我的代码,没有所有无关的东西:

from numpy    import random
from pandas   import DataFrame
from hyperopt import fmin, tpe, hp, Trials





def calc_result(x):

    huge_df = DataFrame(random.randn(100000, 5), columns=['A', 'B', 'C', 'D', 'E'])

    total = 0

    # Assume that I MUST iterate
    for idx_and_row in huge_df.iterrows():
        idx = idx_and_row[0]
        row = idx_and_row[1]


        # Assume there is no way to optimize here
        curr_sum = row['A'] * x['adjustment_1'] + \
                   row['B'] * x['adjustment_2'] + \
                   row['C'] * x['adjustment_3'] + \
                   row['D'] * x['adjustment_4'] + \
                   row['E'] * x['adjustment_5']


        total += curr_sum

    # In real life I want the total as high as possible, but for the minimizer, it has to negative a negative value
    total_as_neg = total * -1

    print(total_as_neg)

    return total_as_neg


space = {'adjustment_1': hp.quniform('adjustment_1', 0, 1, 0.001),
         'adjustment_2': hp.quniform('adjustment_2', 0, 1, 0.001),
         'adjustment_3': hp.quniform('adjustment_3', 0, 1, 0.001),
         'adjustment_4': hp.quniform('adjustment_4', 0, 1, 0.001),
         'adjustment_5': hp.quniform('adjustment_5', 0, 1, 0.001)}

trials = Trials()

best = fmin(fn        = calc_result,
            space     = space,
            algo      = tpe.suggest,
            max_evals = 20000,
            trials    = trials)

到目前为止,我有4个核心,但我基本上可以得到我需要的。如何使hyperopt使用多个内核,或者是否有一个库可以进行多处理?在


Tags: andfromimport核心asspacerowhp
2条回答

如果您有Mac或Linux(或Windows Linux子系统),您可以添加大约10行代码来与ray并行执行此操作。如果您通过latest wheels here安装ray,那么您可以在运行脚本时进行最小的修改,如下所示,使用HyperOpt执行并行/分布式网格搜索。在高级别上,它使用fmin运行tpe.建议并以并行的方式在内部创建一个试验对象。在

from numpy    import random
from pandas   import DataFrame
from hyperopt import fmin, tpe, hp, Trials


def calc_result(x, reporter):  # add a reporter param here

    huge_df = DataFrame(random.randn(100000, 5), columns=['A', 'B', 'C', 'D', 'E'])

    total = 0

    # Assume that I MUST iterate
    for idx_and_row in huge_df.iterrows():
        idx = idx_and_row[0]
        row = idx_and_row[1]


        # Assume there is no way to optimize here
        curr_sum = row['A'] * x['adjustment_1'] + \
                   row['B'] * x['adjustment_2'] + \
                   row['C'] * x['adjustment_3'] + \
                   row['D'] * x['adjustment_4'] + \
                   row['E'] * x['adjustment_5']


        total += curr_sum

    # In real life I want the total as high as possible, but for the minimizer, it has to negative a negative value
    # total_as_neg = total * -1

    # print(total_as_neg)

    # Ray will negate this by itself to feed into HyperOpt
    reporter(timesteps_total=1, episode_reward_mean=total)

    return total_as_neg


space = {'adjustment_1': hp.quniform('adjustment_1', 0, 1, 0.001),
         'adjustment_2': hp.quniform('adjustment_2', 0, 1, 0.001),
         'adjustment_3': hp.quniform('adjustment_3', 0, 1, 0.001),
         'adjustment_4': hp.quniform('adjustment_4', 0, 1, 0.001),
         'adjustment_5': hp.quniform('adjustment_5', 0, 1, 0.001)}

import ray
import ray.tune as tune
from ray.tune.hpo_scheduler import HyperOptScheduler

ray.init()
tune.register_trainable("calc_result", calc_result)
tune.run_experiments({"experiment": {
    "run": "calc_result",
    "repeat": 20000,
    "config": {"space": space}}}, scheduler=HyperOptScheduler())

只是一些关于你问题的旁注。我最近也在做超参数搜索,如果你有自己的原因,请不要理我。在

问题是你应该更喜欢随机搜索而不是网格搜索。

这是他们提出这个建议的paper。在

这里有一些解释:基本上随机搜索更好地分布在子功能上,网格搜索更好地分布在整个特征空间上,这就是为什么感觉这是一种方法。在

http://cs231n.github.io/neural-networks-3/ this is where the image is from

图像来自here

相关问题 更多 >