首页 > 解决方案 > 如何从保存的 xgboost 模型中替换参数?

问题描述

这是我保存 xgboost 模型时的 xgboost 环境和参数。

环境

范围

我想在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:logisticbinary: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

我无法重新训练我的模型。出于这个原因,我必须从保存的模型中解决这个问题。

如何解决这个问题?

标签: pythonmachine-learningmodelxgboost

解决方案


目标函数的名称以字符串形式存储在模型文件中。只需在您的模型文件上执行二进制感知替换gpu:binary:logistic-> !binary:logistic

应该可以使用命令行工具,例如看这个 SO 线程:binary sed replacement

成功替换后,模型文件的大小应缩小 4 个字节(gpu:前缀的长度)。


推荐阅读