自定义模型的自定义转换器¶
当 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_140503414016832.<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 可作为示例。