gpt4 book ai didi

Scipy 选择 nan 作为输入同时最小化

转载 作者:行者123 更新时间:2023-12-05 06:38:30 25 4
gpt4 key购买 nike

我有这个目标函数(在 python 中):

actions= [...] # some array
Na= len(actions)
# maximize p0 * qr(s,a0,b0) + ... + pn * qr(s,an,bn)
def objective(x):
p = x[:Na] # p is a probability distribution
b = x[Na:2 * Na] # b is an array of positive unbounded scalars
q = np.array([qr(s, actions[a], b[a]) for a in range(0, Na)]) # s is an array
rez = - np.dot(p, q) # np stands for numpy library
return rez

qrqc 是回归树,它们是将数组映射到标量的函数。

我有这些约束:

# p0 * qc(s,a0,b0) + ... + pn * qc(s,an,bn) < beta
def constraint(x):
p = x[:Na]
b = x[Na:2 * Na]
q = np.array([qc(s, actions[a], b[a]) for a in range(0, Na)])
rez = beta - np.dot(p, q) # beta is a scalar
return rez

# elements of p should sum to 1
def constraint_proba_sum_1(x):
p = x[:Na]
rez = 0
for i in range(0, Na):
rez += p[i]
rez = 1 - rez
return rez

我如何最小化:

constraints = ({'type': 'ineq', 'fun': constraint},
{'type': 'eq', 'fun': constraint_proba_sum_1})

res = opt.minimize(fun=objective, x0=np.array([0.5, 0.5, 10, 10]), constraints=constraints,
bounds=[(0, 1), (0, 1), (0, None), (0, None)])

问题是 opt.minimize 在其最小化过程“slsqp”中有时使用 nan 数组作为输入。因此 qr 树会引发错误。为什么它会在什么情况下评估此类数组?

我确实意识到这个问题与这篇文章相同 Scipy optimizations methods select nan for input parameter但它没有解决,它看起来像函数依赖。

编辑:看来如果我删除约束 constraint_proba_sum_1(x),我将不再有 NaN 值作为输入。

EDIT 2:我尝试了另一个 API,pyOPT 和 SLSQP 优化,我遇到了同样的问题。

最佳答案

我观察到 scipy 的 differential_evolution 优化器有类似的行为,我可以将其追溯到 polish 参数,它在全局优化后运行局部最小化。由于这仅在 DE 优化器的最大迭代之后出现(显然我的模型参数无法从我手头的数据中识别),DE opt 对象的局部最小化器的初始化导致与其他帖子描述的类似效果.

我的解决方法是在目标函数中捕获 NaN 值的出现并引发异常,因为只有当 DE 优化器找不到优化时才会发生这种情况。

关于Scipy 选择 nan 作为输入同时最小化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45966102/

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