如何将LightGBM型号转换为ONXX?

发布于 2025-02-10 16:24:49 字数 712 浏览 2 评论 0原文

我正在尝试保存我的模型,以便可以在ASP.NET程序中使用,我认为ONNX是一个很好的方法。问题在于,即使在检查文档并整天搜索搜索后,我仍然会遇到相同的错误提高ValueError('需要初始类型。 ..) in skl2onnx.convert for details. I have no idea what's going on and any help is greatly appreciated!

My Code

import onnxmltools
from skl2onnx import convert
import lightgbm as lgb
import pandas as pd

parameters = {
    'boosting': 'gbdt',
    'feature_fraction': 0.5,
    'bagging_fraction': 0.5,
    'bagging_freq': 20,
    'num_boost_round': 10000,
    'verbose': -1 #maybe?
}


model_lgbm = lgb.train(parameters, train_data, valid_sets = test_data, early_stopping_rounds = 200);

onnx_model = convert.convert_sklearn(model_lgbm, ???);

I'm trying to save my model so it can be used in a ASP.NET program, and I think that ONNX is a good way to do so. The problem is that even after checking the docs and googling it all day, I still get the same error raise ValueError('Initial types are required. See usage of ' ValueError: Initial types are required. See usage of convert(...) in skl2onnx.convert for details. I have no idea what's going on and any help is greatly appreciated!

My Code

import onnxmltools
from skl2onnx import convert
import lightgbm as lgb
import pandas as pd

parameters = {
    'boosting': 'gbdt',
    'feature_fraction': 0.5,
    'bagging_fraction': 0.5,
    'bagging_freq': 20,
    'num_boost_round': 10000,
    'verbose': -1 #maybe?
}


model_lgbm = lgb.train(parameters, train_data, valid_sets = test_data, early_stopping_rounds = 200);

onnx_model = convert.convert_sklearn(model_lgbm, ???);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2025-02-17 16:24:49

我认为

您必须使用:
onnxmltools.convert_lightgbm
而不是
convert.convert_sklearn

I think this doc will help you.

You have to use :
onnxmltools.convert_lightgbm
and not
convert.convert_sklearn

痕至 2025-02-17 16:24:49
  1. 正如其他答案所提到的:您必须使用:onnxmltools.convert_lightgbm而不是转换。convert_sklearn

  2. 也将引起错误,因为您尚未定义initial_types。 inital_types在 docs 所描述的如下:

假设指定的示例:假设指定的示例Scikit-Learn模型将异质列表作为其输入。如果前5个元素是浮子,而最后10个元素是整数,则需要在下面指定初始类型。 [none,5]中的[无]表示这里的批处理大小是未知的。

from skl2onnx.common.data_types import FloatTensorType, Int64TensorType
initial_type = [('float_input', FloatTensorType([None, 5])),
                ('int64_input', Int64TensorType([None, 10]))]
  1. As the other answer mentions: You have to use : onnxmltools.convert_lightgbm and not convert.convert_sklearn

  2. The error would also be caused since you have not defined the initial_types. The inital_types are in the Docs described as follows:

Example of initial_types: Assume that the specified scikit-learn model takes a heterogeneous list as its input. If the first 5 elements are floats and the last 10 elements are integers, we need to specify initial types as below. The [None] in [None, 5] indicates the batch size here is unknown.

from skl2onnx.common.data_types import FloatTensorType, Int64TensorType
initial_type = [('float_input', FloatTensorType([None, 5])),
                ('int64_input', Int64TensorType([None, 10]))]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文