【问题标题】:performing grid search for 8 hyperparameters on Python在 Python 上对 8 个超参数执行网格搜索
【发布时间】:2021-03-27 15:22:57
【问题描述】:

我正在尝试对具有 8 个不同变量的成本函数执行网格搜索:

cost_function_1 = lambda x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8: (675 + (x_1-15)**2 + x_1**2 * np.cos(x_1*np.pi)) + x_2^2 + x_3^2 + x_4^2 + x_5^2 + x_6^2 + x_7^2 + x_8^2

变量x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8 是我模型的超参数。它们的值被定义为区间 [-10,30] 内的网格:

X_MIN_1 = -10
X_MAX_1 = 30
x_1 = x_2 = x_3 = x_4 = x_5 = x_6 = x_7 = x_8 = np.linspace(X_MIN_1, X_MAX_1, 10)

如果我的成本函数 cost_function_1 只有 1 个变量,我会像下面这样进行网格搜索:

def get_best_value(f, candidates):
    """
    Return the candidate that yielded the lowest cost in the function f.
    :param f: cost function
    :param candidates: x candidates
    :return: the best candidate
    """
    idx_min = np.argmin(f(candidates))
    return candidates[idx_min]

def run_grid_search_experiment(f, n_trials, X_MIN, X_MAX):
    """
    Run the experiment for grid search.
    :param f: cost function
    :param n_trials: number of trials
    :param show_plot: show plot if true
    :return: the best candidate
    """
    gs_candidates = np.linspace(X_MIN, X_MAX, n_trials)
    selected_value = get_best_value(f, gs_candidates)

    return selected_value

gs_selected_value = run_grid_search_experiment(cost_function_1, 
                                               10, -10, 30)

但是,我不确定如何修改函数 get_best_valuerun_grid_search_experiment 以使用具有 8 个不同变量的成本函数 cost_function_1。例如,python 函数 get_best_value 中的语句 idx_min = np.argmin(f(candidates)) 将不起作用,因为每个候选对象都是一个维度为 1 x 8 的 numpy 数组(因为要考虑 8 个变量)。

谁能帮我修改函数get_best_valuerun_grid_search_experiment 以执行8 个变量的网格搜索?

【问题讨论】:

  • 在 sklearn 中有一个基本但不错的网格搜索实现:scikit-learn.org/stable/modules/generated/…
  • @Swier 您好,感谢您的评论。在这个model_selection 函数中,我看到estimator 参数可用于指定“评分函数”。这个“得分函数”会是我的成本函数cost_function_1吗?对不起,如果我理解缓慢。
  • 你的意思是x_2**2,而不是你的函数中的x_2^2

标签: python numpy machine-learning grid-search hyperparameters


【解决方案1】:

假设您的功能是:

cost_function_1 = lambda x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8: (675 + (x_1-15)**2 + x_1**2 * np.cos(x_1*np.pi)) + x_2**2 + x_3**2 + x_4**2 + x_5**2 + x_6**2 + x_7**2 + x_8**2
X_MIN_1 = -10.
X_MAX_1 = 30.
n_trials = 10
x = np.linspace(X_MIN_1, X_MAX_1, n_trials)
#create a grid for 8-dimensional search, you can have per dimension specific linespace too.
X = np.meshgrid(x,x,x,x,x,x,x,x)
#find function values on grid
out = cost_function_1(*X)
#find index of minimum
idx = np.unravel_index(out.argmin(), out.shape)
#find corresponding value of grid
[x[idx[i]] for i in np.arange(8)]

以上值的输出:

[-1.1111111111111107, 21.111111111111114, -1.1111111111111107, -1.1111111111111107, -1.1111111111111107, -1.1111111111111107, -1.1111111111111107, -1.1111111111111107]

【讨论】:

  • 您好,感谢您的回答。从上面的代码中,找到由 8 个变量的这些网格产生的最小 cost_function_1 值的最有效方法是什么?所以基本上,如果我想在值[x[idx[i]] for i in np.arange(8)] 处找到cost_function_1 的值怎么办?谢谢,
  • @vverdic 只需在该点调用您的函数cost_function_1(*point)。但是,如果您只对最小值感兴趣而不是它的要点,请使用out.min()。请查看stackoverflow.com/help/someone-answers 了解如何接受答案并欢迎来到 SO。
猜你喜欢
  • 2016-09-20
  • 2023-03-09
  • 2017-11-25
  • 2021-06-14
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 2016-07-12
  • 2016-05-12
相关资源
最近更新 更多