【发布时间】: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_value 和 run_grid_search_experiment 以使用具有 8 个不同变量的成本函数 cost_function_1。例如,python 函数 get_best_value 中的语句 idx_min = np.argmin(f(candidates)) 将不起作用,因为每个候选对象都是一个维度为 1 x 8 的 numpy 数组(因为要考虑 8 个变量)。
谁能帮我修改函数get_best_value 和run_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