Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleImputer: Unexpected runtime error when preceded by CastTransformer #1133

Open
ag-tcm opened this issue Oct 22, 2024 · 1 comment
Open

Comments

@ag-tcm
Copy link

ag-tcm commented Oct 22, 2024

skl2onnx Version: 1.16.0

I have a simple model where the input is numpy.float64 and the model pipeline looks like this:

model3 = Pipeline(
    [
        ("cast32", CastTransformer(dtype=numpy.float32)),
        ("imputer", SimpleImputer()),
        ("dt", DecisionTreeRegressor(max_depth=max_depth)),
    ]
)
...
onx3 = to_onnx(
    model3,
    initial_types=[('X', DoubleTensorType([None, 10]))], final_types=[('y', FloatTensorType([None, 1]))],
)

When I call to_onnx I get a runtime error Inputs and outputs should have the same type (stack trace below).

SimpleImputer should expect a FloatTensorType as its input and output type due to the transformer, but it does not.
If SimpleImputer is swapped for a StandardScaler instead, the code runs fine.

(For a full reproducible example, my sample code is based on the example here: http://onnx.ai/sklearn-onnx/auto_tutorial/plot_ebegin_float_double.html#casttransformer.)

Traceback (most recent call last):
  File ".\src\strategy\onnx_convert_test.py", line 36, in <module>
    onx3 = to_onnx(
  File ".\lib\site-packages\skl2onnx\convert.py", line 306, in to_onnx
    return convert_sklearn(
  File ".\lib\site-packages\skl2onnx\convert.py", line 208, in convert_sklearn
    onnx_model = convert_topology(
  File ".\lib\site-packages\skl2onnx\common\_topology.py", line 1532, in convert_topology
    topology.convert_operators(container=container, verbose=verbose)
  File ".\lib\site-packages\skl2onnx\common\_topology.py", line 1349, in convert_operators
    self.call_shape_calculator(operator)
  File ".\lib\site-packages\skl2onnx\common\_topology.py", line 1164, in call_shape_calculator
    operator.infer_types()
  File ".\lib\site-packages\skl2onnx\common\_topology.py", line 653, in infer_types
    shape_calc(self)
  File ".\lib\site-packages\skl2onnx\shape_calculators\imputer.py", line 39, in calculate_sklearn_imputer_output_shapes
    raise RuntimeError(
RuntimeError: Inputs and outputs should have the same type <class 'onnxconverter_common.data_types.FloatTensorType'> != <class 'onnxconverter_common.data_types.DoubleTensorType'>.
@xadupre
Copy link
Collaborator

xadupre commented Nov 14, 2024

Did you try to move the CastTransformer after SimpleImputer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants