为了账号安全,请及时绑定邮箱和手机立即绑定

使用多个数据集优化数值模型

使用多个数据集优化数值模型

幕布斯6054654 2024-01-16 15:49:58
所以我有一个问题,此时我有点迷失。因此,任何意见都将不胜感激,因为我现在真的很挣扎!我有一个模型,想使用我得到的一些实验数据来检查/优化。一般来说,我的模型需要两个输入(我们称之为:时间和温度)并有 8 个变量 (x0-x7)。该模型生成两个输出(out1 和 out2)。我的每组实验数据都为我提供了 4 组可用于优化的信息:2 个输入(时间和温度)和 2 个实验结果(结果 1 和结果 2)。最终我想最小化 result1 和 out1 以及 result2 和 out2 之间的差异。因此,基本上用几组数据最小化两个残差,这些数据受 8 个共同参数 (x0-x7) 的影响。我对参数 x0-x7 有一些限制,这可以有所帮助,但除此之外没有真正的限制。到目前为止,我已经尝试使用scipy.minimize我的实验结果数据集进行迭代,如下所示(非常示意性):import numpy as npfrom scipy.optimize import minimize    Experiment=[['Set 1','Set 2',             'Set 3','Set 4'],                   [Out 1-1,Out 1-2,                    Out 1-3,Out 1-4],                   [Out 2-1,Out 2-2,                    Out 2-3,Out 2-4],            ]global curr_casecurr_case=0 #just for debugging in the first place    def objective_fcn(x):            SetFitParameters(x) #x0-x7            #---------probably totally dumb: iteration-----------    global curr_case    #number of experimental set        curr_case=curr_case+1    if curr_case==len(Experiment):        curr_case=0    #----------------------------------------------------            getTemp(curr_case) # function that gets time and temperature from experimental data as two arrays - time and temperature            RefVariables(x) #sets some global variabales needed for ModelCal using x0-x7            ModelCal(time,Temperature)  #gives Out1 and Out2            f1 = abs(Out1[Upper_index-1]-Experiment[1][curr_case]) #compares Out1 with result1 (from experimental data)    f2 = abs(Out2[Upper_index-1]-Experiment[2][curr_case]) #compares Out2 with result2 (from experimental data)            # some weighting factors for the future - maybe?    A=1    B=1           return A*f1+B*f2       bounds_x1=(1450,1700) #upper and lower bonds of x0bounds_x2=(0.1,1)bounds_x3=(1450,1700)bounds_x4=(0.1,7)bounds_x5=(1450,1700)bounds_x6=(0.1,7)bounds_x7=(1450,1700)bounds_x8=(0.1,7)    
查看完整描述

1 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

共享对象函数的基本思想很好。我并没有真正深入探讨 OP 尝试的细节,因为这可能会产生误导。该过程是定义可用于最小二乘拟合的适当残差函数。Python 有多种可能性可以做到这一点。我将展示scipy.optimize.leastsq与此密切相关的scipy.optimize.least_squares。


import numpy as np

from scipy.optimize import least_squares ## allows bounds and has given loss functions but provides only Jacobian

from scipy.optimize import leastsq ## provides scaled covariance matrix



"""

some arbitrary test function taking two inputs and providing

two correlated outputs with shared parameters - only three for testing.

"""

def test_function( time, temp, x0, x1, x2 ):

    s = np.sqrt( time/x0 ) * np.log( ( temp - x1 ) / x2 )

    t = np.exp( - time/x0 ) * np.sqrt( (time/x0)**2 + ( ( temp - x1 ) / x2 )**2 )

    return s, t


### make some data with noise

indata = list()

for _ in range( 60 ):

    a = 50 * np.random.random()

    b = 10 + 25 * np.random.random()

    indata.append( [a,b] )


outdata = list()

for a,b in indata:

    s,t = test_function( a, b, 3.78, 5.33, 12.88 )

    noise1 = np.random.normal( scale=0.01 )

    noise2 = np.random.normal( scale=0.01 )

    outdata.append( [s + noise1, t + noise2 ] )


indata = np.array( indata)

outdata = np.array( outdata)


#########################################################################

### define the residuals function for fitting This is the important part!

#########################################################################


def residuals( params, xdata, ydata, weightA=1, weightB=1 ):

    x0, x1, x2 = params

    diff = list()

    for ab, st in zip( indata, outdata ):

        a, b = ab

        s, t = st

        sf, tf = test_function( a, b, x0,x1, x2 )

        diff.append( weightA * ( s - sf ) )

        diff.append( weightB * ( t - tf ) )

    return diff


### Fit

solx, cov, info, msg, ier = leastsq( 

    residuals, [ 3.8, 5.0, 12.5],

    args=( indata, outdata ), full_output=True

)

print solx

print cov

sol = least_squares( residuals, [ 3.8, 5.0, 12.5 ], args=( indata, outdata ))

print sol.x

根据OP的需要修改它应该很容易。


查看完整回答
反对 回复 2024-01-16
  • 1 回答
  • 0 关注
  • 35 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信