我有一个训练有素的 scikit-learn 模型,它使用多输出决策树(作为 a RandomForestRegressor
)。没有明确对随机森林回归模型进行自定义配置以启用多输出行为,因为内置了多输出行为。基本上,只要将多输出训练数据拟合到模型中,模型就会在幕后切换到多输出模式。
此外,它RandomForestRegressor
是 CoreML 转换脚本提供的受支持的转换器。但是,在转换过程中,我收到了带有堆栈跟踪的此错误:
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.
简单的转换代码如下:
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")
我可以做些什么来启用 CoreML 转换脚本来处理多输出决策树?我可以对现有脚本进行更改,而无需完全重新发明轮子吗?