如何解决如何从保存的 xgboost 模型中替换参数?
这是我保存xgboost模型时的环境和xgboost参数。
环境
- xgboost 0.90 版
参数
- 目标:gpu:binary:logistic
我想在 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\x00\x00\x00\x00\x00\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00gpu:binary6\logistic\x0 x00\x00\x00\x00\x00\x00\x00gbtree\xfb\x00\x00\x00\x01\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\x0 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\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\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\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:
前缀的长度)。