1. Optuna庫的優(yōu)勢
? ? ? ? 對比bayes_opt和hyperoptOptuna不僅可以銜接到PyTorch等深度學習框架上,還可以與sklearn-optimize結合使用,這也是我最喜歡的地方,Optuna因此特性可以被使用于各種各樣的優(yōu)化場景。
?
2. 導入必要的庫及加載數(shù)據(jù)
? ? ? ? 用的是sklearn自帶的房價數(shù)據(jù),只是我把它保存下來了。
import optuna
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold,cross_validate
print(optuna.__version__)
from sklearn.ensemble import RandomForestRegressor as RFR
data = pd.read_csv(r'D:\2暫存文件\Sth with Py\貝葉斯優(yōu)化\data.csv')
X = data.iloc[:,0:8]
y = data.iloc[:,8]
3.?定義目標函數(shù)與參數(shù)空間
? ? ? ? Optuna相對于其他庫,不需要單獨輸入?yún)?shù)或參數(shù)空間,只需要直接在目標函數(shù)中定義參數(shù)空間即可。這里以負均方誤差為損失函數(shù)。
def optuna_objective(trial) :
# 定義參數(shù)空間
n_estimators = trial.suggest_int('n_estimators',10,100,1)
max_depth = trial.suggest_int('max_depth',10,50,1)
max_features = trial.suggest_int('max_features',10,30,1)
min_impurtity_decrease = trial.suggest_float('min_impurity_decrease',0.0, 5.0, step=0.1)
# 定義評估器
reg = RFR(n_estimators=n_estimators,
max_depth=max_depth,
max_features=max_features,
min_impurity_decrease=min_impurtity_decrease,
random_state=1412,
verbose=False,
n_jobs=-1)
# 定義交叉過程,輸出負均方誤差
cv = KFold(n_splits=5,shuffle=True,random_state=1412)
validation_loss = cross_validate(reg,X,y,
scoring='neg_mean_squared_error',
cv=cv,
verbose=True,
n_jobs=-1,
error_score='raise')
return np.mean(validation_loss['test_score'])
4.? 定義優(yōu)化目標函數(shù)
? ? ? ? 在Optuna中我們可以調(diào)用sampler模塊進行選用想要的優(yōu)化算法,比如TPE、GP等等。
def optimizer_optuna(n_trials,algo):
# 定義使用TPE或GP
if algo == 'TPE':
algo = optuna.samplers.TPESampler(n_startup_trials=20,n_ei_candidates=30)
elif algo == 'GP':
from optuna.integration import SkoptSampler
import skopt
algo = SkoptSampler(skopt_kwargs={'base_estimator':'GP',
'n_initial_points':10,
'acq_func':'EI'})
study = optuna.create_study(sampler=algo,direction='maximize')
study.optimize(optuna_objective,n_trials=n_trials,show_progress_bar=True)
print('best_params:',study.best_trial.params,
'best_score:',study.best_trial.values,
'\n')
return study.best_trial.params, study.best_trial.values
5. 執(zhí)行部分
import warnings
warnings.filterwarnings('ignore',message='The objective has been evaluated at this point before trails')
optuna.logging.set_verbosity(optuna.logging.ERROR)
best_params, best_score = optimizer_optuna(200,'TPE')
6. 完整代碼
import optuna
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold,cross_validate
print(optuna.__version__)
from sklearn.ensemble import RandomForestRegressor as RFR
data = pd.read_csv(r'D:\2暫存文件\Sth with Py\貝葉斯優(yōu)化\data.csv')
X = data.iloc[:,0:8]
y = data.iloc[:,8]
def optuna_objective(trial) :
# 定義參數(shù)空間
n_estimators = trial.suggest_int('n_estimators',10,100,1)
max_depth = trial.suggest_int('max_depth',10,50,1)
max_features = trial.suggest_int('max_features',10,30,1)
min_impurtity_decrease = trial.suggest_float('min_impurity_decrease',0.0, 5.0, step=0.1)
# 定義評估器
reg = RFR(n_estimators=n_estimators,
max_depth=max_depth,
max_features=max_features,
min_impurity_decrease=min_impurtity_decrease,
random_state=1412,
verbose=False,
n_jobs=-1)
# 定義交叉過程,輸出負均方誤差
cv = KFold(n_splits=5,shuffle=True,random_state=1412)
validation_loss = cross_validate(reg,X,y,
scoring='neg_mean_squared_error',
cv=cv,
verbose=True,
n_jobs=-1,
error_score='raise')
return np.mean(validation_loss['test_score'])
def optimizer_optuna(n_trials,algo):
# 定義使用TPE或GP
if algo == 'TPE':
algo = optuna.samplers.TPESampler(n_startup_trials=20,n_ei_candidates=30)
elif algo == 'GP':
from optuna.integration import SkoptSampler
import skopt
algo = SkoptSampler(skopt_kwargs={'base_estimator':'GP',
'n_initial_points':10,
'acq_func':'EI'})
study = optuna.create_study(sampler=algo,direction='maximize')
study.optimize(optuna_objective,n_trials=n_trials,show_progress_bar=True)
print('best_params:',study.best_trial.params,
'best_score:',study.best_trial.values,
'\n')
return study.best_trial.params, study.best_trial.values
import warnings
warnings.filterwarnings('ignore',message='The objective has been evaluated at this point before trails')
optuna.logging.set_verbosity(optuna.logging.ERROR)
best_params, best_score = optimizer_optuna(200,'TPE')
文章來源:http://www.zghlxwxcb.cn/news/detail-652495.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-652495.html
到了這里,關于數(shù)據(jù)分析 | 調(diào)用Optuna庫實現(xiàn)基于TPE的貝葉斯優(yōu)化 | 以隨機森林回歸為例的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!