自定义模型的自定义转换器

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_140489269341504.<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 可以用作示例。