onnxmltools icon indicating copy to clipboard operation
onnxmltools copied to clipboard

AttributeError: 'RandomForestClassificationModel' object has no attribute 'coefficients'

Open thongvhoang opened this issue 1 year ago • 0 comments

Hello,

I'm trying to convert a pyspark.ml pipeline to onnx , but I get the following error.

from onnxmltools import convert_sparkml
from skl2onnx.common.data_types import FloatTensorType
onnx_model = convert_sparkml(model_multi, 'My Sparkml Pipeline', [
            ('sum_point', FloatTensorType([1, 1])),
            ('count_point', FloatTensorType([1, 1])),
            ('brandCode', StringTensorType([1, 1]))
        ])

My error is

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[206], line 3
      1 from onnxmltools import convert_sparkml
      2 from skl2onnx.common.data_types import FloatTensorType
----> 3 onnx_model = convert_sparkml(model_multi, 'My Sparkml Pipeline', [
      4             ('sum_point', FloatTensorType([1, 1])),
      5             ('count_point', FloatTensorType([1, 1])),
      6             ('brandCode', StringTensorType([1, 1]))
      7         ])

File [~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/main.py:166](https://vscode-remote+ssh-002dremote-002b192-002e168-002e1-002e4.vscode-resource.vscode-cdn.net/home/devops/lemuck-airflow-etl_1/dags/scripts/~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/main.py:166), in convert_sparkml(model, name, initial_types, doc_string, target_opset, targeted_onnx, custom_conversion_functions, custom_shape_calculators, spark_session)
    163     raise RuntimeError('Spark is not installed. Please install Spark to use this feature.')
    165 from .sparkml.convert import convert
--> 166 return convert(model, name, initial_types, doc_string, target_opset, targeted_onnx,
    167                custom_conversion_functions, custom_shape_calculators, spark_session)

File [~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/sparkml/convert.py:71](https://vscode-remote+ssh-002dremote-002b192-002e168-002e1-002e4.vscode-resource.vscode-cdn.net/home/devops/lemuck-airflow-etl_1/dags/scripts/~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/sparkml/convert.py:71), in convert(model, name, initial_types, doc_string, target_opset, targeted_onnx, custom_conversion_functions, custom_shape_calculators, spark_session)
     68 topology.compile()
     70 # Convert our Topology object into ONNX. The outcome is an ONNX model.
---> 71 onnx_model = convert_topology(topology, name, doc_string, target_opset, targeted_onnx)
     73 return onnx_model

File [~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxconverter_common/topology.py:776](https://vscode-remote+ssh-002dremote-002b192-002e168-002e1-002e4.vscode-resource.vscode-cdn.net/home/devops/lemuck-airflow-etl_1/dags/scripts/~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxconverter_common/topology.py:776), in convert_topology(topology, model_name, doc_string, target_opset, targeted_onnx, channel_first_inputs)
    773         topology.custom_conversion_functions[operator.type](scope, operator, container)
    774     else:
    775         # Convert the selected operator into some ONNX objects and save them into the container
--> 776         get_converter(operator.type)(scope, operator, container)
    778 # When calling ModelComponentContainer's add_initializer(...), nothing is added into the input list.
    779 # However, for ONNX target opset < 9, initializers should also be model's (GraphProto) inputs.
    780 # Thus, we create ValueInfoProto objects from initializers (type: TensorProto) directly and
    781 # then add them into model's input list.
    782 extra_inputs = []  # ValueInfoProto list of the initializers

File [~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/sparkml/operator_converters/one_vs_rest.py:28](https://vscode-remote+ssh-002dremote-002b192-002e168-002e1-002e4.vscode-resource.vscode-cdn.net/home/devops/lemuck-airflow-etl_1/dags/scripts/~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/sparkml/operator_converters/one_vs_rest.py:28), in convert_one_vs_rest(scope, operator, container)
     26 classifier_op.outputs.append(classifier_prediction_output)
     27 classifier_op.outputs.append(classifier_probability_output)
---> 28 convert_sparkml_linear_classifier(scope, classifier_op, container)
     29 classifier_op.is_evaluated = True
     30 single_feature_tensor = scope.get_unique_variable_name('single_feature_tensor')

File [~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/sparkml/operator_converters/linear_classifier.py:16](https://vscode-remote+ssh-002dremote-002b192-002e168-002e1-002e4.vscode-resource.vscode-cdn.net/home/devops/lemuck-airflow-etl_1/dags/scripts/~/anaconda3/envs/machine-learning/lib/python3.10/site-packages/onnxmltools/convert/sparkml/operator_converters/linear_classifier.py:16), in convert_sparkml_linear_classifier(scope, operator, container)
     13 attrs = {'name': scope.get_unique_operator_name(op_type)}
     15 if op.numClasses == 2:
---> 16     coefficients = op.coefficients.toArray().tolist()
     17     intercepts = [op.intercept]
     18     coefficients = list(map(lambda x: -1 * x, coefficients)) + coefficients

AttributeError: 'RandomForestClassificationModel' object has no attribute 'coefficients'

I have checked for other models, with Logistic regression being successful but RandomForest failing, but I suppose the same issue can be raised if applicable.

Best regards, Thong Vo

thongvhoang avatar Mar 09 '23 09:03 thongvhoang