python - 如何从保存的 xgboost 模型中替换参数?
问题描述
这是我保存 xgboost 模型时的 xgboost 环境和参数。
环境
- xgboost 版本 0.90
范围
- 目标:gpu:二进制:逻辑
我想在1.0.0 版本中加载保存的 xgboost 模型。但是,会发生此错误消息。
XGBoostError: [12:51:46] C:\Users\Administrator\workspace\xgboost-win64_release_1.0.0\src\objective\objective.cc:26: Unknown objective function: `gpu:binary:logistic`
这是因为gpu:binary:logistic
现在已弃用。当我在 0.90 版中加载我的模型时,出现了这个警告。
[13:45:14] WARNING: C:/Jenkins/workspace/xgboost-win64_release_0.90/src/objective/regression_obj.cu:170: gpu:binary:logistic is now deprecated, use binary:logistic instead.
我尝试申请set_params
更换gpu:binary:logistic
并binary:logistic
保存了一个模型。但是,它没有用。
import xgboost as xgb
model = xgb.XGBClassifier()
model.load_model('xgb.model')
params = {'objective':'binary:logistic'}
model.set_params(**params)
我试图从保存的模型文件中将 'gpu:binary:logistic' 替换为 'binary:'logistic',但发生了 OSError。
temp = open('test.model','rb')
lines = temp.readlines()
lines[0] = lines[0].replace(b'gpu:',b'')
new_model = open('test_replace.model','wb')
for line in lines:
new_model.write(line)
new_model.close()
加载模型时出现OSError。
model = xgb.XGBClassifier()
model.load_model('test_replace.model')
OSError: [WinError -529697949] Windows Error 0xe06d7363
这是一个保存的 xgboost 模型。此文件已保存为model
格式
b'\x00\x00\x00\x80\x19\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00gpu:二进制:物流\x06\x00\x00\x00\x00\x00\x00\x00gbtree\xfb\x00\x00\x00\x01\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x19\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x19\x00\x00\x00\x00\x00
我无法重新训练我的模型。出于这个原因,我必须从保存的模型中解决这个问题。
如何解决这个问题?
解决方案
目标函数的名称以字符串形式存储在模型文件中。只需在您的模型文件上执行二进制感知替换gpu:binary:logistic
-> !binary:logistic
应该可以使用命令行工具,例如看这个 SO 线程:binary sed replacement
成功替换后,模型文件的大小应缩小 4 个字节(gpu:
前缀的长度)。
推荐阅读
- javascript - 需要,js不同数量的参数
- c - 32 位比较比 64 位比较快吗?
- c# - 正则表达式地址模式
- amazon-web-services - 第二次运行 AWS Rekognition 人脸搜索
- c# - 如何解决 ASP.NET 中的多个端点匹配问题
- php - 如何使用 foreach 循环获取所有内容,除了那些在简单的 html dom.php 中具有某些类的内容
- performance - grep 仅在特定小时内生成的文件
- ruby-on-rails - Rails 自定义模型验证器取决于 check_box 值
- azure-iot-edge - IoT Edge 上的自定义 DPS 注册负载
- python - 选择数据框中列的前 X% 受另一列的影响