我正在尝试使用 FloatKrigingSurrogate() 在 openmdao 1.7.1 中训练一个代理。我收到一条错误消息:
/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py:145: RuntimeWarning: divide by zero encountered in log
reduced_likelihood = -(np.log(np.sum(sigma2)) + logdet / self.n_samples)
/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/slsqp.py:62: RuntimeWarning: invalid value encountered in subtract
jac[i] = (func(*((x0+dx,)+args)) - f0)/epsilon
Traceback (most recent call last):
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/drivers/predeterminedruns_driver.py", line 441, in _try_case
root.solve_nonlinear(metadata=metadata)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/group.py", line 733, in solve_nonlinear
self.nl_solver.solve(params, unknowns, resids, self, metadata)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/solvers/solver_base.py", line 23, in wrapper
fn(driver, params, unknowns, resids, system, metadata)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/solvers/run_once.py", line 51, in solve
system.children_solve_nonlinear(local_meta)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/group.py", line 751, in children_solve_nonlinear
sub._sys_solve_nonlinear(sub.params, sub.unknowns, sub.resids)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/component.py", line 617, in _sys_solve_nonlinear
self.solve_nonlinear(params, unknowns, resids)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/components/meta_model.py", line 211, in solve_nonlinear
self._train()
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/components/meta_model.py", line 371, in _train
surrogate.train(self._training_input, self._training_output[name])
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py", line 96, in train
bounds=bounds)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 455, in minimize
constraints, callback=callback, **options)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/slsqp.py", line 363, in _minimize_slsqp
fx = func(x)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 289, in function_wrapper
return function(*(wrapper_args + args))
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py", line 89, in _calcll
loglike = self._calculate_reduced_likelihood_params(np.exp(thetas))[0]
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py", line 133, in _calculate_reduced_likelihood_params
[U,S,Vh] = linalg.svd(R)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/linalg/decomp_svd.py", line 88, in svd
a1 = _asarray_validated(a, check_finite=check_finite)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/_lib/_util.py", line 187, in _asarray_validated
a = toarray(a)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1033, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs
Traceback (most recent call last):
File "C3DMMdriver.py", line 321, in <module>
sample.run()
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/problem.py", line 1167, in run
self.driver.run(self)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/drivers/predeterminedruns_driver.py", line 409, in run
self._run_serial()
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/drivers/predeterminedruns_driver.py", line 473, in _run_serial
exec('raise exc[0], exc[1], exc[2]')
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/drivers/predeterminedruns_driver.py", line 441, in _try_case
root.solve_nonlinear(metadata=metadata)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/group.py", line 733, in solve_nonlinear
self.nl_solver.solve(params, unknowns, resids, self, metadata)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/solvers/solver_base.py", line 23, in wrapper
fn(driver, params, unknowns, resids, system, metadata)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/solvers/run_once.py", line 51, in solve
system.children_solve_nonlinear(local_meta)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/group.py", line 751, in children_solve_nonlinear
sub._sys_solve_nonlinear(sub.params, sub.unknowns, sub.resids)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/core/component.py", line 617, in _sys_solve_nonlinear
self.solve_nonlinear(params, unknowns, resids)
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/components/meta_model.py", line 211, in solve_nonlinear
self._train()
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/components/meta_model.py", line 371, in _train
surrogate.train(self._training_input, self._training_output[name])
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py", line 96, in train
bounds=bounds)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 455, in minimize
constraints, callback=callback, **options)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/slsqp.py", line 363, in _minimize_slsqp
fx = func(x)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 289, in function_wrapper
return function(*(wrapper_args + args))
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py", line 89, in _calcll
loglike = self._calculate_reduced_likelihood_params(np.exp(thetas))[0]
File "/applications/Frameworks/Python_Frameworks/OpenMDAO/openmdao-1.7.1/lib/python2.7/site-packages/openmdao/surrogate_models/kriging.py", line 133, in _calculate_reduced_likelihood_params
[U,S,Vh] = linalg.svd(R)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/linalg/decomp_svd.py", line 88, in svd
a1 = _asarray_validated(a, check_finite=check_finite)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/scipy/_lib/_util.py", line 187, in _asarray_validated
a = toarray(a)
File "/applications/Python_distributions/python2.7_anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1033, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs
我已经检查了我正在为其制作模型的应用程序的数据文件。我已将训练数据复制到一个文件中并进行了检查。一切都是一个数字。它并不总是一团糟,但我打算将这部分作为一个更大的问题的一部分,其中采样部分是嵌套的,所以如果它真的发生了,整个问题就会陷入困境。我还能如何寻找搞砸的东西?我的代码摘录:
class C3DMM(MetaModel):
def __init__(self):
super(C3DMM, self).__init__()
self.add_param('AOA', val= 0.)
self.add_output('cl', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('cd', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('cm', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('wfy', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('wfx', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('wmz', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('hfy', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('hfx', val=0., surrogate=FloatKrigingSurrogate())
self.add_output('dlcoefs', val=np.zeros(96), surrogate=FloatKrigingSurrogate())
class C3Dsurr(Group):
def __init__(self,):
super(C3Dsurr, self).__init__()
# Create meta_model for C3D with response
self.add('c3dmm', C3DMM())
# Create variable for problem interaction
self.add('mmalp', IndepVarComp('AOA',0.))
self.connect('mmalp.AOA','c3dmm.AOA')
...
train=Problem(root=Group())
train.root.add('train', IndepVarComp('AOA',0.))
train.root.add('c3d',GoC3D())
train.root.connect('train.AOA','c3d.AOA')
train.driver = FullFactorialDriver(num_levels=samples)
train.driver.add_desvar("train.AOA", lower= -4.,\
upper= 32.)
recorder = InMemoryRecorder()
recorder.options['record_params'] = True
recorder.options['record_metadata'] = True
train.driver.add_recorder(recorder)
train.setup()
train.run()
train.cleanup()
...
sample=Problem(root=C3Dsurr())
...
sample.setup()
# train the surrogate
alp_train=[]
cl_train=[]
cm_train=[]
cd_train=[]
wfy_train=[]
wfx_train=[]
wmz_train=[]
hfy_train=[]
hfx_train=[]
knot_train=[]
dlc_train=[]
if debug:
dbfile=open('C3Dsample.dat','w')
dbfile.write('variables=AOA,cl,cm,cd,wfy,wfx,wmz,hfy,hfx,knots,coefs\n')
dbfile.write('variables=AOA,cl,cm,cd,wfy,wfx,wmz,hfy,hfx,coefs\n')
for line in recorder.iters:
alp_train.append(line['params']['c3d.AOA'])
cl_train.append(line['unknowns']['c3d.cl'])
cm_train.append(line['unknowns']['c3d.cm'])
cd_train.append(line['unknowns']['c3d.cd'])
wfy_train.append(line['unknowns']['c3d.wfy'])
wfx_train.append(line['unknowns']['c3d.wfx'])
wmz_train.append(line['unknowns']['c3d.wmz'])
hfy_train.append(line['unknowns']['c3d.hfy'])
hfx_train.append(line['unknowns']['c3d.hfx'])
#knot_train.append(line['unknowns']['c3d.knots'].flatten())
dlc_train.append(line['unknowns']['c3d.dlcoefs'].flatten())
if debug:
dbfile.write('%f '% line['params']['c3d.AOA'])
dbfile.write('%f '% line['unknowns']['c3d.cl'])
dbfile.write('%f '% line['unknowns']['c3d.cm'])
dbfile.write('%f '% line['unknowns']['c3d.cd'])
dbfile.write('%f '% line['unknowns']['c3d.wfy'])
dbfile.write('%f '% line['unknowns']['c3d.wfx'])
dbfile.write('%f '% line['unknowns']['c3d.wmz'])
dbfile.write('%f '% line['unknowns']['c3d.hfy'])
dbfile.write('%f '% line['unknowns']['c3d.hfx'])
#for each in line['unknowns']['c3d.knots'].flatten():
#dbfile.write('%f '%each)
for each in line['unknowns']['c3d.dlcoefs'].flatten():
dbfile.write('%f '%each)
dbfile.write('\n')
...
sample.setup()
# train the surrogate
sample['c3dmm.train:AOA']=alp_train
sample['c3dmm.train:cl']=cl_train
sample['c3dmm.train:cm']= cm_train
sample['c3dmm.train:cd']= cd_train
sample['c3dmm.train:wfy']= wfy_train
sample['c3dmm.train:wfx']= wfx_train
sample['c3dmm.train:wmz']= wmz_train
sample['c3dmm.train:hfy']= hfy_train
sample['c3dmm.train:hfx']= hfx_train
#sample['c3dmm.train:knots']= knot_train
sample['c3dmm.train:dlcoefs']= dlc_train
sample.run()
9 月 1 日根据 Kenneth 的评论进行了修订:显然,克里金替代模型需要一个挑战。它不喜欢愚蠢的简单函数,即 f(x)=constant。我稍微修改了文档中的元模型示例问题:
from __future__ import print_function
import sys
import numpy as np
from openmdao.api import Group, Component, MetaModel, KrigingSurrogate, FloatKrigingSurrogate
class TrigMM(Group):
''' FloatKriging gives responses as floats '''
def __init__(self):
super(TrigMM, self).__init__()
# Create meta_model for f_x as the response
sin_mm = self.add("sin_mm", MetaModel())
sin_mm.add_param('x', val=0.)
sin_mm.add_output('f_x:float', val=0., surrogate=FloatKrigingSurrogate())
# train a vector or a set of scalars
#sin_mm.add_output('g', val=np.zeros(SURVECSIZ), surrogate=KrigingSurrogate())
for each in range(SURVECSIZ):
sin_mm.add_output('g%d'%each, val=0., surrogate=FloatKrigingSurrogate())
from openmdao.api import Problem
prob = Problem()
prob.root = TrigMM()
prob.setup()
#traning data is just set manually. No connected input needed, since
# we're assuming the data is pre-existing
prob['sin_mm.train:x'] = np.linspace(0,10,20)
prob['sin_mm.train:f_x:float'] = np.sin(prob['sin_mm.train:x'])
for each in range(SURVECSIZ):
prob['sin_mm.train:g%d'%each]=np.sin(prob['sin_mm.train:x'])*(float(each))
try:
samplex=float(sys.argv[1])
except IndexError:
samplex=2.1
prob['sin_mm.x'] = samplex #prediction happens at this value
prob.run()
print('float predicted:', '%3.4f'%prob['sin_mm.f_x:float']) #predicted value
print('float actual :', '%3.4f'%np.sin(samplex))
for each in range(SURVECSIZ):
print('g%d predicted:'%each, '%3.4f,'%prob['sin_mm.g%d'%each]) #predicted value
print('g actual :', '%3.4f'%(np.sin(samplex)*(float(each))))
我最初想知道是否有最大数量的代理,所以我建立了那个变量 SURVECSIZ。即使 SURVECSIZ=1 它也死了。但是当我将 gi 的函数更改为乘以 (float(each)+1.) 时,它就会运行。将 +1 放在括号之外以使值成为常数 = 1 而不是 0 也会导致它死亡。我猜克里金模型过程会根据数据的变化范围进行标准化。克里金只是不想在一个简单的问题上浪费时间。不幸的是,在某些情况下,我的变异可能为零,而克里金模型需要允许它。