亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用多個數據集優化數值模型

使用多個數據集優化數值模型

幕布斯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 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號