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

5 min readOct 28, 2022

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

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

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

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

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

• `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.

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

Zoom: contract the region of interest.

`from bayes_opt import SequentialDomainReductionTransformerbounds_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)`

--

--