[英]scikit-learn: Convert multi-output decision tree to CoreML model
I have a trained scikit-learn model that uses a multi-output decision tree (as a RandomForestRegressor
). 我有一个训练有素的scikit-learn模型,该模型使用多输出决策树(作为
RandomForestRegressor
)。 No custom configuration was explicitly made to the Random Forest Regression model to enable multi-output behavior, as multi-output behavior is built-in. 由于内置了多输出行为,因此没有对“随机森林回归”模型明确地进行自定义配置以启用多输出行为。 Basically, as long as you fit multi-output training data to the model, the model will switch to multi-output mode behind the scenes.
基本上,只要您将多输出训练数据拟合到模型,模型就会在后台切换到多输出模式。
Additionally, the RandomForestRegressor
is a supported transformer that the CoreML conversion scripts supply. 此外,
RandomForestRegressor
是CoreML转换脚本提供的受支持的转换器。 However, during the conversion, I get this error w/ stack trace: 但是,在转换过程中,出现堆栈跟踪错误:
ValueError: Expected only 1 output in the scikit-learn tree.
ValueError:在scikit-learn树中预期仅输出1。
Traceback (most recent call last):
File "/Users/user0/Desktop/model_convert.py", line 7, in <module>
coreml_model = sklearn_to_ml.convert(model)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_converter.py", line 146, in convert
sk_obj, input_features, output_feature_names, class_labels = None)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_converter_internal.py", line 297, in _convert_sklearn_model
last_spec = last_sk_m.convert(last_sk_obj, current_input_features, output_features)._spec
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_random_forest_regressor.py", line 53, in convert
return _MLModel(_convert_tree_ensemble(model, feature_names, target))
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 195, in convert_tree_ensemble
scaling = scaling, mode = mode, n_classes = n_classes, tree_index = tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 75, in _recurse
raise ValueError('Expected only 1 output in the scikit-learn tree.')
ValueError: Expected only 1 output in the scikit-learn tree.
The simple conversion code is below: 简单的转换代码如下:
from coremltools.converters import sklearn as sklearn_to_ml
from sklearn.externals import joblib
model = joblib.load("ms5000.pkl")
print("Converting model")
coreml_model = sklearn_to_ml.convert(model)
print("Saving CoreML model")
coreml_model.save("ms5000.mlmodel")
What can I do to enable the CoreML conversion script to handle multi-output decision trees? 如何使CoreML转换脚本能够处理多输出决策树? Is there a change I can make to the existing scripts without reinventing the wheel with a new script entirely?
我可以对现有脚本进行更改,而不必完全用新脚本重新发明轮子吗?
CoreML is (right now) a brand new thing, so there is currently not any known sources of third-party conversion scripts. CoreML(现在)是一个崭新的事物,因此目前尚无任何已知的第三方转换脚本来源。
The "Models" section of the coremltools
documentation provides extensive documentation of how to use Python to produce a CoreML model. coremltools
文档的“模型”部分提供了有关如何使用Python生成CoreML模型的大量文档。 That being said, you can translate any machine learning model to a CoreML model using the model interfaces provided in the documentation. 话虽如此,您可以使用文档中提供的模型接口将任何机器学习模型转换为CoreML模型。
At the moment, coremltools
does not support multi-output regression models. 目前,
coremltools
不支持多输出回归模型。 If you don't wish to reinvent the wheel, you will need to convert the model to a single output model by introducing a new input that corresponds to which output the current prediction is for. 如果您不想重新发明轮子,则需要通过引入新输入将模型转换为单个输出模型,该输入对应于当前预测所针对的输出。
Either way, the documentation is there so that should get you started. 无论哪种方式,都可以找到文档,以便您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.