【问题标题】:how to replace parameter from a saved xgboost model?如何从保存的 xgboost 模型中替换参数?
【发布时间】:2021-04-05 07:22:44
【问题描述】:

这是我保存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\x13\x00\x00\x00\x00\x00\x00\x00gpu:binary:logistic\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\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\x01\x00\x00\x00\x01\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x00

我无法重新训练我的模型。因此,我必须从已保存的模型中解决此问题。

如何解决这个问题?

【问题讨论】:

    标签: python machine-learning model xgboost


    【解决方案1】:

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

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

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

    【讨论】:

    • 作为您的答案,我在 python 中打开了我的模型并将 `gpu:' 替换为 ''。但是,发生了 OSError。
    • 好吧,我的错。字符串值存储为<int:string length in bytes><byte[]:string content>。除了更改字符串值之外,您还需要更新前导字符串长度指示字段(从当前值中减去 4)。
    • 现在你有:<int:19>gpu:binary:logistic,但你需要有<int 15>binary:logistic
    • 请注意,长度实际上是使用long64数据类型写入的,所以它是字符串值开头之前的8个字节。
    • 一个模型文件是使用int数据类型编写的,但我不确定这个文件是否使用long64数据类型。如何判断是否使用了long64 数据类型?
    猜你喜欢
    • 2017-12-07
    • 2020-01-03
    • 2021-02-20
    • 2017-09-27
    • 2018-11-01
    • 2022-06-20
    • 2020-01-21
    • 2020-02-15
    相关资源
    最近更新 更多