检查偏导数和有限差分错误
Check partial derivatives and finite differences error
关于我之前的问题,我看到你解决了一次与运行相关的问题。我想尝试,但我仍然对以下代码中显示的导数检查和有限差分有问题:
""" Unconstrained optimization of the scaled paraboloid component."""
from __future__ import print_function
import sys
import numpy as np
from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer
class Paraboloid(Component):
def __init__(self):
super(Paraboloid, self).__init__()
self.add_param('X', val=np.array([0.0, 0.0]))
self.add_output('f_xy', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
X = params['X']
x = X[0]
y = X[1]
unknowns['f_xy'] = (1000.*x-3.)**2 + (1000.*x)*(0.01*y) + (0.01*y+4.)**2 - 3.
def linearize(self, params, unknowns, resids):
""" Jacobian for our paraboloid."""
X = params['X']
J = {}
x = X[0]
y = X[1]
J['f_xy', 'X'] = np.array([[ 2000000.0*x - 6000.0 + 10.0*y,
0.0002*y + 0.08 + 10.0*x]])
return J
if __name__ == "__main__":
top = Problem()
root = top.root = Group()
#root.fd_options['force_fd'] = True # Error if uncommented
root.add('p1', IndepVarComp('X', np.array([3.0, -4.0])))
root.add('p', Paraboloid())
root.connect('p1.X', 'p.X')
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.add_desvar('p1.X',
lower=np.array([-1000.0, -1000.0]),
upper=np.array([1000.0, 1000.0]),
scaler=np.array([1000., 0.001]))
top.driver.add_objective('p.f_xy')
top.setup()
top.check_partial_derivatives()
top.run()
top.check_partial_derivatives()
print('\n')
print('Minimum of %f found at (%s)' % (top['p.f_xy'], top['p.X']))
第一次检查工作正常,但第二次 check_partial_derivatives
给出了 FD 的奇怪结果:
[...]
Partial Derivatives Check
----------------
Component: 'p'
----------------
p: 'f_xy' wrt 'X'
Forward Magnitude : 1.771706e-04
Reverse Magnitude : 1.771706e-04
Fd Magnitude : 9.998228e-01
Absolute Error (Jfor - Jfd) : 1.000000e+00
Absolute Error (Jrev - Jfd) : 1.000000e+00
Absolute Error (Jfor - Jrev): 0.000000e+00
Relative Error (Jfor - Jfd) : 1.000177e+00
Relative Error (Jrev - Jfd) : 1.000177e+00
Relative Error (Jfor - Jrev): 0.000000e+00
Raw Forward Derivative (Jfor)
[[ -1.77170624e-04 -8.89040341e-10]]
Raw Reverse Derivative (Jrev)
[[ -1.77170624e-04 -8.89040341e-10]]
Raw FD Derivative (Jfd)
[[ 0.99982282 0. ]]
Minimum of -27.333333 found at ([ 6.66666658e-03 -7.33333333e+02])
并且(可能不相关)当我尝试设置 root.fd_options['force_fd'] = True
(只是为了看看)时,我在第一次检查时出错:
Partial Derivatives Check
----------------
Component: 'p'
----------------
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 59, in
File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\problem.py", line 1827, in check_partial_derivatives
u_size = np.size(dunknowns[u_name])
File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\vec_wrapper.py", line 398, in __getitem__
return self._dat[name].get()
File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\vec_wrapper.py", line 223, in _get_scalar
return self.val[0]
IndexError: index 0 is out of bounds for axis 0 with size 0
我使用 OpenMDAO HEAD (d1e12d4)。
这只是有限差分的步长问题。 2nd FD 发生在不同的点(最佳点),并且在那个点它必须更敏感。
我试过中心差
top.root.p.fd_options['form'] = 'central'
并且得到了更好的结果。
----------------
Component: 'p'
----------------
p: 'f_xy' wrt 'X'
Forward Magnitude : 1.771706e-04
Reverse Magnitude : 1.771706e-04
Fd Magnitude : 1.771738e-04
设置 'fd' 时出现的异常是一个真正的错误,与 des_var 上的缩放器相关是一个数组。感谢您的报告;我们会得到一个故事来修复它。
关于我之前的问题
""" Unconstrained optimization of the scaled paraboloid component."""
from __future__ import print_function
import sys
import numpy as np
from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer
class Paraboloid(Component):
def __init__(self):
super(Paraboloid, self).__init__()
self.add_param('X', val=np.array([0.0, 0.0]))
self.add_output('f_xy', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
X = params['X']
x = X[0]
y = X[1]
unknowns['f_xy'] = (1000.*x-3.)**2 + (1000.*x)*(0.01*y) + (0.01*y+4.)**2 - 3.
def linearize(self, params, unknowns, resids):
""" Jacobian for our paraboloid."""
X = params['X']
J = {}
x = X[0]
y = X[1]
J['f_xy', 'X'] = np.array([[ 2000000.0*x - 6000.0 + 10.0*y,
0.0002*y + 0.08 + 10.0*x]])
return J
if __name__ == "__main__":
top = Problem()
root = top.root = Group()
#root.fd_options['force_fd'] = True # Error if uncommented
root.add('p1', IndepVarComp('X', np.array([3.0, -4.0])))
root.add('p', Paraboloid())
root.connect('p1.X', 'p.X')
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.add_desvar('p1.X',
lower=np.array([-1000.0, -1000.0]),
upper=np.array([1000.0, 1000.0]),
scaler=np.array([1000., 0.001]))
top.driver.add_objective('p.f_xy')
top.setup()
top.check_partial_derivatives()
top.run()
top.check_partial_derivatives()
print('\n')
print('Minimum of %f found at (%s)' % (top['p.f_xy'], top['p.X']))
第一次检查工作正常,但第二次 check_partial_derivatives
给出了 FD 的奇怪结果:
[...] Partial Derivatives Check ---------------- Component: 'p' ---------------- p: 'f_xy' wrt 'X' Forward Magnitude : 1.771706e-04 Reverse Magnitude : 1.771706e-04 Fd Magnitude : 9.998228e-01 Absolute Error (Jfor - Jfd) : 1.000000e+00 Absolute Error (Jrev - Jfd) : 1.000000e+00 Absolute Error (Jfor - Jrev): 0.000000e+00 Relative Error (Jfor - Jfd) : 1.000177e+00 Relative Error (Jrev - Jfd) : 1.000177e+00 Relative Error (Jfor - Jrev): 0.000000e+00 Raw Forward Derivative (Jfor) [[ -1.77170624e-04 -8.89040341e-10]] Raw Reverse Derivative (Jrev) [[ -1.77170624e-04 -8.89040341e-10]] Raw FD Derivative (Jfd) [[ 0.99982282 0. ]] Minimum of -27.333333 found at ([ 6.66666658e-03 -7.33333333e+02])
并且(可能不相关)当我尝试设置 root.fd_options['force_fd'] = True
(只是为了看看)时,我在第一次检查时出错:
Partial Derivatives Check ---------------- Component: 'p' ---------------- Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 59, in File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\problem.py", line 1827, in check_partial_derivatives u_size = np.size(dunknowns[u_name]) File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\vec_wrapper.py", line 398, in __getitem__ return self._dat[name].get() File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\vec_wrapper.py", line 223, in _get_scalar return self.val[0] IndexError: index 0 is out of bounds for axis 0 with size 0
我使用 OpenMDAO HEAD (d1e12d4)。
这只是有限差分的步长问题。 2nd FD 发生在不同的点(最佳点),并且在那个点它必须更敏感。
我试过中心差
top.root.p.fd_options['form'] = 'central'
并且得到了更好的结果。
----------------
Component: 'p'
----------------
p: 'f_xy' wrt 'X'
Forward Magnitude : 1.771706e-04
Reverse Magnitude : 1.771706e-04
Fd Magnitude : 1.771738e-04
设置 'fd' 时出现的异常是一个真正的错误,与 des_var 上的缩放器相关是一个数组。感谢您的报告;我们会得到一个故事来修复它。