model_onnx = convert_sklearn(
pipe,
"pipeline_catboost",
[("input", DoubleTensorType([None, 33]))],
target_opset={"": 12, "ai.onnx.ml": 2}, final_types = [("output_label", Int64TensorType([None,])),("output_probability1", DoubleTensorType([None,2]))]
)
可能会遇到的报错:
Load model from pipeline_lightgbm.onnx failed:Type Error: Type (seq(map(int64,tensor(double)))) of output arg (output_probability) of node (ZipMap) does not match expected type (seq(map(int64,tensor(float)))).
然后尝试将
final_types = [(“output_label”, Int64TensorType([None,])),(“output_probability1”, DoubleTensorType([None,2]))]
改成 final_types = [(“output_label”, Int64TensorType([None,])),(“output_probability1”, FloatTensorType([None,2]))]
仍报错
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.
加上 在convert_sklearn函数中加上 options={‘zipmap’: False}即解决
大概的代码:
model_onnx = convert_sklearn(
pipe,
"pipeline_catboost",
[("input", DoubleTensorType([None, 33]))],
target_opset={"": 12, "ai.onnx.ml": 2}, final_types = [("output_label", Int64TensorType([None,])),("output_probability1", FloatTensorType([None,2]))]
, options={'zipmap': False}
)
sess = rt.InferenceSession("pipeline_catboost.onnx", providers=["CPUExecutionProvider"])
pred_onx = sess.run(None, {"input": X[:5].values.astype(numpy.double)})