You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GridSearchCV训练GPU版XGBoost出现设备不匹配警告的解决咨询

解决GridSearchCV调用XGBoost GPU训练时的设备不匹配警告

问题背景

单独调用XGBRegressor.fit()进行GPU加速训练时无警告,但通过GridSearchCV做网格搜索训练时,出现设备不匹配警告:

UserWarning: [17:29:04] WARNING:
/workspace/src/common/error_msg.cc:58: Falling back to prediction
using DMatrix due to mismatched devices. This might lead to higher
memory usage and slower performance. XGBoost is running on: cuda:0,
while the input data is on: cpu. Potential solutions:

  • Use a data structure that matches the device ordinal in the booster.
  • Set the device for booster before call to inplace_predict.

原因是GridSearchCV在交叉验证的评分阶段,默认使用CPU进行预测,而模型本身运行在GPU上,导致数据与模型的设备不匹配。

解决方案

在初始化XGBRegressor时添加predictor="gpu_predictor"参数,强制模型使用GPU完成预测环节,避免设备不匹配。

修改后的完整代码

import xgboost as xgb
from sklearn.model_selection import GridSearchCV
import numpy as np

if __name__ == "__main__":
    X = np.random.randn(100,2)
    y = np.random.randn(100)

    # 新增predictor="gpu_predictor"参数,确保预测环节也使用GPU
    reg = xgb.XGBRegressor(
        device="cuda",
        tree_method="hist",
        max_depth=10,
        n_estimators=100,
        predictor="gpu_predictor"
    )
    param_grid = {"gamma": [0.3]}

    gs = GridSearchCV(reg, param_grid, cv=5)
    gs.fit(X,y)  # 不再触发设备不匹配警告

针对pandas数据的说明

如果你的X和y来自pandas读取的HDF5文件(DataFrame或Series格式),无需额外转换数据结构,只要保持上述模型参数设置即可,XGBoost会自动处理数据的设备迁移。

内容的提问来源于stack exchange,提问作者user7431005

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 18:18:11