【问题标题】:final_types for RandomForestClassifier skl2onnxRandomForestClassifier skl2onnx 的 final_types
【发布时间】:2021-11-19 21:32:27
【问题描述】:

为 RandomForestClassifier 定义 final_types 的最佳方式是什么?

如果我执行以下操作:

initial_type = [('input', FloatTensorType([None, 13]))]
final_type = [('output', FloatTensorType([None, 1]))]

sklonnx = convert_sklearn(rfc, initial_types=initial_type, final_types=final_type)
with open("sklrfc.onnx", "wb") as f:
    f.write(sklonnx.SerializeToString())

我收到以下错误:

RuntimeError: Number of declared outputs is unexpected, declared 'output' found 'output_label, output_probability'.

所以我把 final_type 改成:

initial_type = [('input', FloatTensorType([None, 13]))]
final_type = [('label', Int64TensorType([None, 1])),
              ('output', FloatTensorType([None, 1]))]

sklonnx = convert_sklearn(rfc, initial_types=initial_type, final_types=final_type)
with open("sklrfc.onnx", "wb") as f:
    f.write(sklonnx.SerializeToString())

这不会产生任何错误,但是当我运行 InferenceSession 时:

import onnxruntime as rt
sess = rt.InferenceSession("sklrfc.onnx")
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
pred_onx = sess.run([label_name], {input_name: X_test.astype(np.float32)})[0]

我得到了这个错误:

InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : Load model from sklrfc.onnx failed:This is an invalid model. Type Error: Type 'seq(map(int64,tensor(float)))' of input parameter (output_probability) of operator (Cast) in node (Cast2) is invalid.

我必须在我的模型中或在将模型转换为 onnx 的过程中更改它们吗?

【问题讨论】:

    标签: python scikit-learn random-forest onnx onnxruntime


    【解决方案1】:

    我得到了修复:

    initial_type = [('input', FloatTensorType([None, 13]))]
    final_type = [('label', Int64TensorType([None, 1])),
                  ('output', FloatTensorType([None, 1]))]
    
    sklonnx = convert_sklearn(rfc, initial_types=initial_type, final_types=final_type, **options={'zipmap': False}**)
    with open("sklrfc.onnx", "wb") as f:
        f.write(sklonnx.SerializeToString())
    

    在我运行 InferenceSession 时修复了错误

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 2016-11-06
      • 2016-01-26
      • 2019-10-22
      • 2023-03-22
      • 2014-04-20
      • 1970-01-01
      • 2019-08-03
      • 2019-08-18
      相关资源
      最近更新 更多