[英]Optimize a function in scipy without explicitly defining the gradient
我目前正在嘗試使用 scipy 優化 function。 我對變量有一些限制,並且從這個鏈接: http://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/optimize.ZFC35FDC70D5FC69D2369883A822C7 在他們的示例中,他們有一個明確定義的輸入結果公式,他們從中找到梯度。 我有一個極其令人作嘔的計算密集型 function 計算電磁場如何從金屬壁反彈,這絕不可以用封閉形式表示(如果你有興趣,我正在使用 MEEP FDTD Python 模擬)。 是否有等效的 function 構建到 scipy 中,可以為您找到 function 的梯度然后進行優化? Or, equivalently, is there a function built into scipy (any basic python library would be fine) which would find the gradient of a function for me, which I could then pass into this optimization program? 任何建議將不勝感激。
由於您無法輕松計算梯度,因此使用無梯度優化算法可能會有所回報。 以下是SciPy中可用的一些概述:
http://scipy-lectures.github.io/advanced/mathematical_optimization/#gradient-less-methods
還有盆地跳躍算法,類似於模擬退火,在該頁面上沒有提到:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.basinhopping.html
看起來scipy.optimize.minimize
的 SLSQP 不一定需要漸變。 查看這個開源項目, arch ,代碼,大約 738-748 行; 他們使用 SLSQP 但不提供漸變。 像這樣:
opt = minimize(
func,
sv,
args=args,
method="SLSQP",
bounds=bounds,
constraints=ineq_constraints,
tol=tol,
callback=_callback,
options=options,
)
此外,在1.8.0
版本上,以下代碼段有效:
import scipy.optimize
res = scipy.optimize.minimize(
lambda x: x[0]**2, [3.],
method='SLSQP', bounds=[(1., None)])
print(res.success) #: True
這也說明了事實。
我沒有研究 scipy 的代碼,但我猜如果你不提供分析的,他們會計算數值梯度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.