当前位置:网站首页>【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
2022-06-23 06:24:00 【szZack】
xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
环境
- Ubuntu18.04
- python3.6.9
- TensorFlow 2.4.2
- cuda 11.0
- xgboost 1.5.2
依赖库
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV #网格搜索 from sklearn.metrics import make_scorer from sklearn.metrics import r2_score from sklearn.ensemble import GradientBoostingRegressor from sklearn.multioutput import MultiOutputRegressor import xgboost as xgb import joblib调参核心代码
def tune_parameter(train_data_path, test_data_path, n_input, n_output, version): # 模型调参 x,y = load_data(version, 'train', train_data_path, n_input, n_output) train_x,test_x,train_y,test_y = train_test_split(x,y,test_size=0.2,random_state=2022) gsc = GridSearchCV( estimator=xgb.XGBRegressor(seed=42, tree_method='gpu_hist', gpu_id=3), param_grid={ "learning_rate": [0.05, 0.10, 0.15], "n_estimators":[400, 500, 600, 700], "max_depth": [ 3, 5, 7], "min_child_weight": [ 1, 3, 5, 7], "gamma":[ 0.0, 0.1, 0.2], "colsample_bytree":[0.7, 0.8, 0.9], "subsample":[0.7, 0.8, 0.9], }, cv=3, scoring='neg_mean_squared_error', verbose=0, n_jobs=4) grid_result = MultiOutputRegressor(gsc).fit(train_x, train_y) #best_params = grid_result.estimators_[0].best_params_ print('-'*20) print('best_params:') for i in range(len(grid_result.estimators_)): print(i, grid_result.estimators_[i].best_params_) model = grid_result pre_y = model.predict(test_x) print('-'*20) #计算决策系数r方 r2 = performance_metric(test_y, pre_y) print('test_r2 = ', r2) def performance_metric(y_true, y_predict): score = r2_score(y_true,y_predict) MSE=np.mean(( y_predict- y_true)**2) print('RMSE: ',MSE**0.5) MAE=np.mean(np.abs( y_predict- y_true)) print('MAE: ',MAE) return score保存调参后的模型,增加下面代码即可
joblib.dump(model, './ml_data/xgb_%d_%d_%s.model' %(n_input, n_output, version))调参:修改 param_grid 为自己的参数即可
param_grid = { "learning_rate": [0.05, 0.10, 0.15], "n_estimators":[400, 500, 600, 700], "max_depth": [ 3, 5, 7], "min_child_weight": [ 1, 3, 5, 7], "gamma":[ 0.0, 0.1, 0.2], "colsample_bytree":[0.7, 0.8, 0.9], "subsample":[0.7, 0.8, 0.9], }调参线程数量 n_jobs=4 ,可根据自己的机器设定,设置 n_jobs=-1 容易报错
GPU参数设置:
estimator=xgb.XGBRegressor(seed=42, tree_method='gpu_hist', gpu_id=3)tree_method=‘gpu_hist’ 表示使用GPU训练模型,
gpu_id=3 表示设置第3块GPU,
边栏推荐
猜你喜欢

Configuration and compilation of mingw-w64, msys and ffmpeg

TP6+Redis+think-queue+Supervisor实现进程常驻消息队列/job任务

【***数组***】

C language learning summary

QT designer cannot modify the window size, and cannot change the size by dragging the window with the mouse

How to migrate virtual machines from VirtualBox to hype-v

Traversal of binary tree and related knowledge

Run typescript code directly using TS node

云原生落地进入深水区,博云容器云产品族释放四大价值

SSTable详解
随机推荐
306. 累加数
Unet代码实现
406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)
[STL] unordered of associated container_ Map Usage Summary
407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)
[project training] change of linear arrow
npm下载报错npm ERR code ERESOLVE
Interpreting the spirit of unity and cooperation in maker Education
MySQL Redo log Redo log
306. Addenda
316. remove duplicate letters
Don't look for [12 super easy-to-use Google plug-ins are here] (are you sure you want to take a look?)
Pspnet complete code implementation
TP6+Redis+think-queue+Supervisor实现进程常驻消息队列/job任务
产品-Axure9(英文版),原型设计后台动态二级菜单显示内容
redux Actions may not have an undefined “type“ property. Have you misspelled a constant?
TP6 安装拓展
MySQL redo log redo log
Regular expression graph and text ultra detailed summary without rote memorization (Part 1)
100 GIS practical application cases (79) - key points of making multi plan integrated base map