2014-01-26 30 views
2

在優化期間,對輸入參數進行規範化以使其處於相同的數量級上通常很有幫助,因此收斂性可能會好得多。例如,如果我們想最小化f(x),而合理的近似值爲x0 = [1e3,1e-4],那麼將x0 [0]和x0 [1]歸一化到大約相同的數量級可能會有所幫助(通常是O(1))。在python中進行優化的規範化

我的問題是,我一直在使用scipy.optimize,特別是L-BFGS-B算法。我想知道,我是否需要通過編寫函數來手動規範化,或者算法已經爲我做了這個?

謝謝!

+0

您可能必須在http://scicomp.stackexchange.com/ – Ali

回答

3

我寫了一個快速小程序來測試你的問題。總結:如果參數在彼此的幾個數量級內,則算法處理它(它成功收斂並且不需要做更多的功能評估)。

另一方面,當您開始超越10000的因子時,算法開始崩潰並出錯。

下面是程序:

import scipy.optimize 


def test_lbfgsb(): 
    def surface(x): 
     return (x[0] - 3.0) ** 2 + (factor * x[1] - 4.0) ** 2 

    factor = None 
    for exponent in xrange(0, 9): 
     magnitude = 10 ** exponent 
     factors = [x * magnitude for x in [1, 3]] 
     for factor in factors: 
      optimization_result = scipy.optimize.minimize(surface, [0, 0], method='l-bfgs-b') 
      desc = 'at factor %d' % (factor) 
      if not optimization_result.success: 
       print '%s FAILURE (%s)' % (desc, optimization_result.message) 
      else: 
       print '%s, found min at %s, after %d evaluations' % (
        desc, optimization_result.x, optimization_result.nfev) 


test_lbfgsb() 

下面是它的輸出:

at factor 1, found min at [ 3.00000048 4.00000013], after 12 evaluations 
at factor 3, found min at [ 2.99999958 1.33333351], after 36 evaluations 
at factor 10, found min at [ 3.00000059 0.39999999], after 28 evaluations 
at factor 30, found min at [ 2.99999994 0.13333333], after 36 evaluations 
at factor 100, found min at [ 3.00000013 0.03999999], after 40 evaluations 
at factor 300, found min at [ 3.   0.01333333], after 52 evaluations 
at factor 1000, found min at [ 3.   0.00399999], after 64 evaluations 
at factor 3000, found min at [ 3.00000006e+00 1.33332833e-03], after 72 evaluations 
at factor 10000, found min at [ 3.00002680e+00 3.99998309e-04], after 92 evaluations 
at factor 30000, found min at [ 3.00000002e+00 1.33328333e-04], after 104 evaluations 
at factor 100000 FAILURE (ABNORMAL_TERMINATION_IN_LNSRCH) 
at factor 300000, found min at [ 3.00013621e+00 1.33292531e-05], after 196 evaluations 
at factor 1000000, found min at [ 3.00000348e-12 3.99500004e-06], after 60 evaluations 
at factor 3000000 FAILURE (ABNORMAL_TERMINATION_IN_LNSRCH) 
at factor 10000000 FAILURE (ABNORMAL_TERMINATION_IN_LNSRCH) 
at factor 30000000 FAILURE (ABNORMAL_TERMINATION_IN_LNSRCH) 
at factor 100000000 FAILURE (ABNORMAL_TERMINATION_IN_LNSRCH) 
at factor 300000000, found min at [ 3.33333330e-17 8.33333350e-09], after 72 evaluations 
+0

我不明白一個更好的運氣這個例子的理論最優向量參數x應該是什麼。我猜它應該是[3,4],但正如你在實驗中看到的那樣,在第三因素中,第二個參數x [1]遠離真正的最優值收斂,不是嗎? – user819893

+0

在這個例子中,「表面」是兩個變量的函數,它看起來像一個碗(由兩個拋物線形成)。另一種編寫它的方法是:z =(x-3)^ 2 +(f * y-4)^ 2。然後,最小的z將是[x = 3,y = 4/f]。 (這裏的'f'在示例程序中被稱爲'factor')。 – mattsh