自定义模型自定义转换器

sklearn-onnx 转换 scikit-learn 管道时,它会检查每个转换器和预测器,并获取相关的转换器。生成的 ONNX 图将每个转换器的结果组合成一个单独的图。如果模型没有转换器,它会显示一条错误消息,表明缺少转换器。

<<<

import numpy
from sklearn.linear_model import LogisticRegression
from skl2onnx import to_onnx


class MyLogisticRegression(LogisticRegression):
    pass


X = numpy.array([[0, 0.1]])
try:
    to_onnx(MyLogisticRegression(), X)
except Exception as e:
    print(e)

>>>

    Unable to find a shape calculator for type '<class 'sphinx_runpython.runpython.sphinx_runpython_extension.run_python_script_131728071632320.<locals>.MyLogisticRegression'>'.
    It usually means the pipeline being converted contains a
    transformer or a predictor with no corresponding converter
    implemented in sklearn-onnx. If the converted is implemented
    in another library, you need to register
    the converted so that it can be used by sklearn-onnx (function
    update_registered_converter). If the model is not yet covered
    by sklearn-onnx, you may raise an issue to
    https://github.com/onnx/sklearn-onnx/issues
    to get the converter implemented or even contribute to the
    project. If the model is a custom model, a new converter must
    be implemented. Examples can be found in the gallery.

接下来的部分将介绍如何创建自定义转换器。它假设新的转换器不是要添加到此包中,而只是要注册并在转换管道时使用。要贡献并为 scikit-learn 模型添加转换器,逻辑仍然相同,只有转换器注册会发生变化。可以参考 PR 737 作为示例。