GPyOpt迭代查找最大目标函数值;检索建议的下一个位置

2024-04-29 21:29:00 发布

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

我刚开始用GPy和GPyOpt。我的目标是设计一个迭代过程来找到x的位置,其中y是最大的。虚拟x阵列的跨距从0到100,步长为0.5。虚拟y数组是x数组的函数。真函数是y=-x**2+50*x+5,所以y最大值是当x=25.0时。在

我首先随机给x-array分配5个点(对应5个y值),然后运行贝叶斯优化,让它推荐下一个采样位置。我可以使用方便myBopt.plot_收购()生成绘图。示例图如下。 enter image description hereenter image description here

问题:

(1)高斯型峰值和垂直线是什么意思?他们有什么建议?我假设高斯峰的中心是建议的下一个采样位置,对吗?在

(2)如何恢复高斯峰的中心位置?我试图从myBopt中打印出一些东西,但是在任何地方都找不到(如果我想好如何获得这个数字,我可以将它附加到原始列表中,以开始另一个BO并找到下一个位置,直到收敛为止)。在

有没有获取原始数据的方法(3)有没有绘图功能?这一定是保存在什么地方了。在

(4)我还生成了收敛图(在采集图下),我真的不太理解。有人能给我解释一下吗?在

谢谢。在

import GPyOpt
import GPy
from numpy.random import seed
import numpy as np
import matplotlib.pyplot as plt
import random

N = 5
x_array = np.arange(0,100,0.5)
x_random = np.array(sorted(random.sample(x_array, N)))
y_random = (-x_random**2 + 50*x_random + 5) # y = -x**2 + 50*x  + 5

## x_feed and y_feed are the matrices that will be fed into Bayesian Optimization
x_feed = x_random[:, None] # (200, 1)
y_feed = y_random[:, None] # (200, 1)

##creat the objective function
class max_number(object):
    def __init__(self, x_feed, y_feed):
        self.x_feed = x_feed
        self.y_feed = y_feed

    def f(self, x):
        return np.dot(1.0*(x_feed == x).sum(axis = 1), y_feed)[:, None]


func = max_number(x_feed, y_feed)
domain = [{'name' : 'guess_number',
          'type' :  'bandit',
          'domain': x_feed}]

seed(123)
myBopt = GPyOpt.methods.BayesianOptimization(f = func.f,
                                             domain = domain,
                                             acquisition_type = 'EI',
                                             maximize = True,
                                             exact_feval = False,
                                             initial_design_numdata = 5,
                                             verbosity = True)

max_iter = 50
myBopt.run_optimization(max_iter)

myBopt.plot_acquisition()

print 'x random initial points {}'.format(x_random)
print 'y random initial points {}'.format(y_random)
print 'myBopt.X {}'.format(myBopt.X)
print 'myBopt.x_opt {}'.format(myBopt.x_opt)
print 'myBopt.Y {}'.format(myBopt.Y)
print 'myBopt.Y_best {}'.format(myBopt.Y_best)
print 'myBopt.Y_new {}'.format(myBopt.Y_new)
print 'myBopt.suggest_next_locations {}'.format(myBopt.suggest_next_locations())

Tags: importselfnoneformatnumberdomainfeednp
1条回答
网友
1楼 · 发布于 2024-04-29 21:29:00

有不少问题。一个对未来的建议,它更适合的格式,所以有一个帖子每个问题。但现在:

  1. 垂直红线表示采集功能建议运行该功能的最后一个点。钟形曲线是整个采集函数图的一部分(注意红线延伸到整个y = 0线)-它只是绘制采集函数,这样您就可以直观地了解它最有可能建议下一点的位置。

  2. 您需要自己优化采集函数。或者您可以使用BO.suggest_next_locations。结帐教程笔记本,有后一个例子。

  3. 我建议浏览一下plot_acquisition的源代码,很清楚使用了什么数据以及如何访问这些数据。

  4. 我认为这些情节都有非常自我描述的标题。左边的图显示两个连续调用目标函数之间的距离。随着时间的推移,你会期望它会缩小,因为优化找到了最佳值。右边的图显示到目前为止发现的y的最佳值。随着时间的推移,你会期望它会变平。

相关问题 更多 >