final_types 对于 RandomForestClassifier skl2onnx

final_types for RandomForestClassifier skl2onnx

如何为 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 的过程中更改它们吗?

我得到了修复:

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

时的错误