gpt4 book ai didi

python - 了解 hyperopt TPE 算法

转载 作者:行者123 更新时间:2023-11-28 18:57:44 25 4
gpt4 key购买 nike

我正在为我的主项目说明 hyperopt 的 TPE 算法,但似乎无法使算法收敛。据我对原文的了解 paper和 youtube lecture TPE算法的工作步骤如下:

(在下文中,x=超参数和 y=loss)

  1. 首先创建 [x,y] 的搜索历史记录,比如说 10 分。
  2. 根据损失对超参数进行排序,并使用一些分位数 γ 将它们分成两组(γ = 0.5 意味着这些组的大小相同)
  3. 对较差的超参数组 (g(x)) 和较好的超参数组 (l(x)) 进行核密度估计
  4. 良好的估计在 g(x) 中具有低概率而在 l(x) 中具有高概率,因此我们建议在 argmin(g(x)/l(x)) 处评估函数
  5. 在建议的点评估 (x,y) 对并重复步骤 2-5。

我已经在 python 中针对目标函数 f(x) = x^2 实现了这个,但是该算法无法收敛到最小值。

import numpy as np
import scipy as sp
from matplotlib import pyplot as plt
from scipy.stats import gaussian_kde


def objective_func(x):
return x**2

def measure(x):
noise = np.random.randn(len(x))*0
return x**2+noise

def split_meassures(x_obs,y_obs,gamma=1/2):
#split x and y observations into two sets and return a seperation threshold (y_star)
size = int(len(x_obs)//(1/gamma))
l = {'x':x_obs[:size],'y':y_obs[:size]}
g = {'x':x_obs[size:],'y':y_obs[size:]}
y_star = (l['y'][-1]+g['y'][0])/2
return l,g,y_star

#sample objective function values for ilustration
x_obj = np.linspace(-5,5,10000)
y_obj = objective_func(x_obj)

#start by sampling a parameter search history
x_obs = np.linspace(-5,5,10)
y_obs = measure(x_obs)

nr_iterations = 100
for i in range(nr_iterations):

#sort observations according to loss
sort_idx = y_obs.argsort()
x_obs,y_obs = x_obs[sort_idx],y_obs[sort_idx]

#split sorted observations in two groups (l and g)
l,g,y_star = split_meassures(x_obs,y_obs)

#aproximate distributions for both groups using kernel density estimation
kde_l = gaussian_kde(l['x']).evaluate(x_obj)
kde_g = gaussian_kde(g['x']).evaluate(x_obj)

#define our evaluation measure for sampling a new point
eval_measure = kde_g/kde_l

if i%10==0:
plt.figure()
plt.subplot(2,2,1)
plt.plot(x_obj,y_obj,label='Objective')
plt.plot(x_obs,y_obs,'*',label='Observations')
plt.plot([-5,5],[y_star,y_star],'k')
plt.subplot(2,2,2)
plt.plot(x_obj,kde_l)
plt.subplot(2,2,3)
plt.plot(x_obj,kde_g)
plt.subplot(2,2,4)
plt.semilogy(x_obj,eval_measure)
plt.draw()

#find point to evaluate and add the new observation
best_search = x_obj[np.argmin(eval_measure)]
x_obs = np.append(x_obs,[best_search])
y_obs = np.append(y_obs,[measure(np.asarray([best_search]))])

plt.show()

我怀疑发生这种情况是因为我们一直在最确定的地方进行采样,从而使 l(x) 在该点附近越来越窄,这根本不会改变我们的采样位置。那么我的理解有哪些不足呢?

最佳答案

所以,我也在学习TPE。但这是这段代码中的两个问题:

  1. 这段代码只会评估几个独特的点。因为最佳位置是根据内核密度函数推荐的最佳位置计算出来的,但代码无法探索搜索空间。例如,获取功能是做什么的。

  2. 因为此代码只是将新观察值附加到 x 和 y 的列表中。它添加了很多重复项。重复导致一组有偏差的观察结果,并导致非常奇怪的 split ,您可以在后面的图中轻松看到这一点。 eval_measure 开始时类似于目标函数,但后来有所不同。

如果您删除 x_obsy_obs 中的重复项,您可以删除问题编号。 2. 然而,第一个问题只能通过添加某种探索搜索空间的方式来解决。

关于python - 了解 hyperopt TPE 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com