openmdao - check_totals 在 OpenMDAO 中写入一个大向量
问题描述
我想检查输出相对于大量输入的总导数,但我不想检查数组每个成员的导数,因为数组太大,而且复杂跨数组每个成员的步骤(或有限差异)将花费太长时间。有没有办法只检查数组的单个成员?
或者,有没有办法在整个数组中为 check_totals 执行方向导数?此功能似乎仅适用于 check_partials?
解决方案
从 OpenMDAO 版本 3.1.1 开始,我们没有对总数进行定向检查,但这是一个好主意,当我们找到最佳方法时,我们可能会实施它。
作为目前的解决方法,我认为对模型进行定向导数的最简单方法是通过创建一个在某个随机方向上采取“步骤”的组件来临时修改模型,然后将其插入到组件前面宽输入。我在这里整理了一个简单的例子:
import numpy as np
import openmdao.api as om
n = 50
class DirectionalComp(om.ExplicitComponent):
def setup(self):
self.add_input('x', 1.0)
self.add_output('y', np.ones(n))
self.A = -1.0 + 2.0 * np.random.random(n)
self.declare_partials('y', 'x', rows=np.arange(n), cols=np.repeat(0, n), val=self.A)
def compute(self, inputs, outputs, discrete_inputs=None, discrete_outputs=None):
x = inputs['x']
outputs['y'] = x * self.A
prob = om.Problem()
model = prob.model
# Add something like this
model.add_subsystem('p', om.IndepVarComp('x', 1.0))
model.add_subsystem('direction', DirectionalComp())
model.connect('p.x', 'direction.x')
model.connect('direction.y', 'comp.x')
model.add_design_var('p.x')
# Old Model
model.add_subsystem('comp', om.ExecComp('y = 2.0*x', x=np.ones((n, )), y=np.ones((n, ))))
model.add_constraint('comp.y', lower=0.0)
prob.setup()
prob.run_model()
totals = prob.check_totals()
推荐阅读
- php - 在谷歌上 https 重定向后页面返回 500 错误,但在浏览器中有效
- java - 使用 Spring SOAP 解码 202 接受的响应
- r - 我可以在因子变量上拆分 describe() 吗?
- reactjs - React Modal 与 axios 无限循环
- visual-studio-code - 如何让相对路径在 VSCode 中可导航
- python - 面向对象的python中的嵌套类
- powershell - 如何通过 Powershell 将 XML-Data 读入数据表并通过给定的 XmlSchema 强制使用正确的类型?
- python - Python3 'bytes' 对象中的 HMAC-SHA1 没有属性 'encode'
- tensorflow - 无法将 pb 模型加载到 keras 模型中
- c++ - 在参数化构造函数中使用 this 指针