如何使用Bayesian Optimization自動化調整機器學習模型的超參數?

Yanwei Liu
5 min readOct 28, 2022

--

bayes_opt為一個可以用來進行Bayesian Optimization的套件。給定search space,該套件能透過貝氏統計方法進行優化,找出較佳的參數組合。

而我們可以將該方法應用於機器學習模型的超參數挑選。

一般常用來進行機器學習模型超參數挑選的方法有

  1. 人工挑選
  2. Grid Search
  3. Random Search
  4. Bayesian Optimization
  5. Gradient-based Optimization
  6. Evolutionary Optimization

本文介紹的bayes_opt是4. Bayesian Optimization

範例程式碼:

tau1, tau2, tau3, tau4為我們機器學習模型當中,想要進行挑選的超參數。

pbounds定義要尋找的超參數有哪些,其中(0.1, 0.2)代表(最小值, 最大值),也就是搜尋的範圍,以此類推。

search函式當中的params用來建立dict的資料型態,保存搜尋到的tau1, tau2, tau3, tau4;Testing為一個用來得到測試分數score的函式,須根據自己的需求設計。

第24行初始化bayes_optimizer,使用pbounds作為search space,並執行search函式,在優化的過程中,我們希望得到的score越高越好。score可以是Accuracy、F1 Score。也就是說,這個scroe是我們進行超參數優化的指標,score越高代表該組超參數組合表現得越好。

第25行將score, tau1, tau2, tau3, tau4數值寫入到第5行定義的json檔案當中

第26行開始進行Bayesian Optimization,根據官方文件的說明:

  • n_iter: How many steps of bayesian optimization you want to perform. The more steps the more likely to find a good maximum you are.
  • init_points: How many steps of random exploration you want to perform. Random exploration can help by diversifying the exploration space.

n_iter和init_points可依照自己的需求進行調整,n_iter預設值為25、init_points預設值為5

pbounds的區間過大該怎麼辦?

bayes_opt提供了一個叫作Sequential Domain Reduction的方法,官方介紹如下:

converge on an optimal point quickly rather than try to find the optimal point, contracting the domain around the current optimal value as the search progresses can speed up the search progress considerably.

不直接尋找最優的參數組合,而是在當下最佳Score的參數組合下,縮小搜尋的區間範圍,藉此加速整個搜尋過程。

主要由Pan和Zoom組成:

Pan: recentering the region of interest around the most optimal point found.

Zoom: contract the region of interest.

Pan和Zoom示意圖,取自:https://github.com/fmfn/BayesianOptimization/blob/master/examples/sdr.png

使用上,只要將SequentialDomainReductionTransformer作為BayesianOptimization的argument即可,如下所示:

from bayes_opt import SequentialDomainReductionTransformer

bounds_transformer = SequentialDomainReductionTransformer(minimum_window=0.5)
bayes_optimizer = BayesianOptimization(search, random_state=42, pbounds=pbounds, bounds_transformer=bounds_transformer)
bayes_optimizer.maximize(init_points=15, n_iter=25, acq="ei", xi=0)

參考資料

--

--