gpt4 book ai didi

python - 属性错误: 'float' object has no attribute 'gradient' scipy python

转载 作者:行者123 更新时间:2023-12-01 03:33:04 24 4
gpt4 key购买 nike

我正在使用 scipy 优化线性函数,

   def func(weights):
var = ['x1', 'x2', 'x3', 'x4']
if weights is None:
weights = np.ones(len(var)) / len(var)
return len(set([var[i] for i in range(len(weights)) if weights[i]>0]))/len(var)

res = minimize(lambda x: func(x), x0=[0.25,0.25,0.25,0.25],method='SLSQP',
jac=ad.gh(lambda x: func(x))[0], bounds=((0.,1.),)*4,
options = {'disp':True, 'ftol': 1e-20, 'maxiter': 1000},
constraints= {'type': 'eq', 'fun': lambda x: sum(x) - 1.0})

我收到以下错误。

Traceback (most recent call last):
File "D:/applicatio/Sub Applicatio/main.py", line 338, in <module>
constraints= {'type': 'eq', 'fun': lambda x: sum(x) - 1.0})
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\_minimize.py", line 455, in minimize
constraints, callback=callback, **options)
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\slsqp.py", line 383, in _minimize_slsqp
g = append(fprime(x),0.0)
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\optimize.py", line 289, in function_wrapper
return function(*(wrapper_args + args))
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\ad\__init__.py", line 1090, in grad
return numpy.array(ans.gradient(list(xa)))
AttributeError: 'float' object has no attribute 'gradient'

如何优化这种简单的线性函数?有什么建议 ?谢谢。

最佳答案

你到底是什么func在做什么?

def func(weights):
....
return len(set())/len(var)

您会得到 set对象,然后是长度,项数。那代表什么?这不是线性的;它需要整数跳转。

In [318]: x0=[0.25,0.25,0.25,0.25]
In [319]: def func(weights):
...: var = ['x1', 'x2', 'x3', 'x4']
...: if weights is None:
...: weights = np.ones(len(var)) / len(var)
...: return len(set([var[i] for i in range(len(weights)) if weights
...: [i]>0]))/len(var)
...:
In [320]: func(x0)
Out[320]: 1.0
In [321]: x0=np.array(x0)
In [322]: func(x0)
Out[322]: 1.0
In [323]: func(x0+.1)
Out[323]: 1.0
In [324]: func(x0-.1)
Out[324]: 1.0
In [325]: func(x0-1)
Out[325]: 0.0

事实上,它所做的只是计算 x0 有多少个。值为 >0除以 4 - 因此生成 0、.25、.5、.75 或 1。

minimize将从 x0 开始,并找出如何func(x0)x0 中的微小变化而变化。

还有你的jac ,是基于此 func 的东西还有,jac=ad.gh(lambda x: func(x))[0]

==============

我认为您不需要使用 lambda

 `lambda x: func(x)`

只要给 func作为论点。它需要正确数量的参数(例如初始的 x0 )。

====================

运行代码,但没有 jac参数(我不知道 ad.gh 是什么):

In [543]: def func(weights):
...: var = ['x1', 'x2', 'x3', 'x4']
...: if weights is None:
...: weights = np.ones(len(var)) / len(var)
...: return len(set([var[i] for i in range(len(weights)) if weights
...: [i]>0]))/len(var)
...:
In [544]: optimize.minimize(lambda x: func(x), x0=[0.25,0.25,0.25,0.25],method='
...: SLSQP',bounds=((0.,1.),)*4,options = {'disp':True, 'ftol': 1e-20, 'max
...: iter': 1000},constraints= {'type': 'eq', 'fun': lambda x: sum(x) - 1.0
...: })
Optimization terminated successfully. (Exit mode 0)
Current function value: 1.0
Iterations: 1
Function evaluations: 6
Gradient evaluations: 1
Out[544]:
fun: 1.0
jac: array([ 0., 0., 0., 0., 0.])
message: 'Optimization terminated successfully.'
nfev: 6
nit: 1
njev: 1
status: 0
success: True
x: array([ 0.25, 0.25, 0.25, 0.25])

看起来它正在尝试围绕 x0 进行一些小更改,并发现没有任何变化(小的变化不会使任何元素变为 0)。换句话说,您的func已经处于局部最小值,平坦区域。

关于python - 属性错误: 'float' object has no attribute 'gradient' scipy python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666349/

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