- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图了解“dogleg”方法在 Python 的 scipy.optimize.minimize
中是如何工作的功能。我正在调整帮助页面底部的示例。
根据注释,dogleg 方法需要 Jacobian 和 Hessian 参数。为此,我使用 numdifftools
包裹:
import numpy as np
from scipy.optimize import minimize
from numdifftools import Jacobian, Hessian
def fun(x,a):
return (x[0] - 1)**2 + (x[1] - a)**2
x0 = np.array([2,0]) # initial guess
a = 2.5
res = minimize(fun, x0, args=(a), method='dogleg',
jac=Jacobian(fun)([2,0]), hess=Hessian(fun)([2,0]))
print(res)
编辑:
如果我按照下面帖子的建议进行更改,
res = minimize(fun, x0, args=a, method='dogleg',
jac=Jacobian(lambda x: fun(x,a)),
hess=Hessian(lambda x: fun(x,a)))
我得到一个错误 TypeError: <lambda>() takes 1 positional argument but 2 were given
.我做错了什么?
在初始猜测时计算 Jacobian 和 Hessian 是否正确 x0
?
最佳答案
我知道这是一个玩具示例,但我想指出的是,使用像 Jacobian
或 Hessian
这样的工具来计算导数而不是推导函数本身是相当昂贵的。例如你的方法:
x0 = np.array([2, 0])
a = 2.5
%timeit minimize(fun, x0, args=(a,), method='dogleg', jac=fun_der, hess=fun_hess)
100 loops, best of 3: 13.6 ms per loop
但是您可以这样计算导数函数:
def fun_der(x, a):
dx = 2 * (x[0] - 1)
dy = 2 * (x[1] - a)
return np.array([dx, dy]
def fun_hess(x, a):
dx = 2
dy = 2
return np.diag([dx, dy])
%timeit minimize(fun, x0, args=(a,), method='dogleg', jac=fun_der, hess=fun_hess)
1000 loops, best of 3: 279 µs per loop
如您所见,速度几乎快了 50 倍。它真的开始添加复杂的功能。因此,我总是尝试自己明确地推导函数,不管这可能有多困难。一个有趣的例子是基于内核的 Inductive Matrix Completion 实现。 .
argmin --> sum((A - gamma_u(X) Z gamma_v(Y))**2 - lambda * ||Z||**2)
where gamma_u = (1/sqrt(m_x)) * [cos(UX), sin(UX)] and
gamma_v = (1/sqrt(m_y)) * [cos(VY), sin(VY)]
X.shape = n_x, p; Y.shape = n_y, q; U.shape = m_x, p; V.shape = m_y, q; Z.shape = 2m_x, 2m_y
与显式推导和利用这些函数相比,从该方程计算梯度和 hessian 是极其不合理的。因此,正如@bnaul 指出的那样,如果您的函数确实具有封闭形式的导数,那么您确实想要计算和使用它们。
关于python - `scipy.optimize.minimize` 中的 Jacobian 和 Hessian 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137092/
我正在使用 scipy.optimize.minimize 进行优化,并尝试使用以下方法:“Newton-CG”、“dogleg”和“trust-ncg”。据我了解,对于这些方法,需要目标函数的雅可比
我正在尝试对任意多个链接的串行链进行逆运动学。 如下paper ,我找到了如何计算雅可比矩阵的示例。 Entry (i, j) = v[j] * (s[i] - p[j]) 地点: v[j] is t
我想知道下面的代码如何才能更快。目前,它似乎慢得不合理,我怀疑我可能错误地使用了 autograd API。我期望的输出是 timeline 的每个元素在 f 的 jacobian 中求值,我确实得到
当使用 lsqcurvefit 执行非线性最小二乘法拟合时,将 Jacobian 作为输出(即 [x,resnorm,residual,exitflag,output,lambda,jacobian]
我正在尝试计算 (x,y)=(0,0) 处的雅可比行列式,但无法这样做。 import sympy as sp from sympy import * import numpy as np x,y=s
我正在尝试定义一个 custom gradient in TensorFlow对于返回非标量的函数。 问题的简化玩具版本是: import tensorflow as tf @tf.custom_gr
我正在寻找通过 Pytorch 获取函数雅可比行列式的最有效方法,到目前为止已提出以下解决方案: # Setup def func(X): return torch.stack((X.pow(
我打算在 MCMC 例程中使用 Boost odeint 库来估计 ODE 模型中的参数。由于这些 ODE 可能是刚性的,我需要能够将雅可比矩阵传递到具有导数的求解器中。我想创建一个类,它具有作为私有
我正在 Dymola(基于 modelica 的商业软件)中运行系统模型。模型是关于强制对流下多孔物质的传热和传质。我提出这个问题的问题是关于模型模拟过程中产生的错误。 使用模型的当前设置,可以在给定
我正在尝试实现显示的示例 here . 但是当运行代码并使用 Debug模式时,没有返回值。我假设我没有使用正确的旋转轴。 额外的 Unity 细节: Joints 变量是一个游戏对象数组(由 4 个
import math from scipy.optimize import fsolve def sigma(s, Bpu): return s - math.sin(s) - mat
我目前正在使用 SciPy.optimize 中的最小化 (BFGS) 函数来校准我的模型。校准后,参数会不时受到轻微扰动,我想重新使用之前校准的 Jacobian 和 IHessian 作为我的初始
在 scipy.optimize.root 的文档中对于 method = lm,以下是 options 关键字的默认值。 options={'col_deriv': 0, 'diag': None,
有没有办法让 scipy.optimize.minimize 在每次迭代时打印估计梯度的值?我想将它与我正在计算的分析梯度值进行比较。 最佳答案 您可以为 scipy.optimize.minimiz
我正在尝试用 Python 制作 Newton - CG 优化。我的函数是 f(x,y) =(1-x)^2 + 2(y-x^2)^2。初始点:x = 3,y = 2。这是我的代码: from scip
我正在尝试使用 OpenGL、Eigen3 和 "Jacobian pseudoinverse" 实现简单的反向运动学测试方法。 系统使用“Jacobian transpose”算法运行良好,但是,一
我试图了解“dogleg”方法在 Python 的 scipy.optimize.minimize 中是如何工作的功能。我正在调整帮助页面底部的示例。 根据注释,dogleg 方法需要 Jacobia
在 R 中,当您仅提供优化问题的目标函数和约束时,是否可以通过分析找到雅可比/海森/稀疏模式? AMPL 可以做到这一点,据我所知,即使是 MATLAB 也可以做到这一点,但我不知道您是否需要 Kni
我是一名优秀的程序员,十分优秀!