Open RenEjima opened 3 years ago
onnxmltools only saves the final tree and does not need to know which cost function was used to trained the tree. It is possible to add any custom information in the metadata after the conversion if you need to save some information about the training:
meta = {'pA': 'one', 'pB': 'two'}
onnx.helper.set_model_props(model_onnx, meta)
Thank you for you reply! I don't need information about history of training and I need only the final tree now. When I want to do so, how can I save LGBMClassifier with custom cost function using onnxmltools?
The converter for lightgbm works the same way. It does not store the cost function but it can be added to the metadata as well after the conversion.
This is my code and I wrote this with reference to http://onnx.ai/sklearn-onnx/auto_examples/plot_pipeline_lightgbm.html
def registerConvONNX_lightGBM():
update_registered_converter(
LGBMClassifier, 'LightGbmLGBMClassifier',
calculate_linear_classifier_output_shapes, convert_lightgbm,
options={'nocl': [True, False]}
)
def buildONNXModel_lightGBM(model):
registerConvONNX_lightGBM()
return convert_sklearn(model, 'lightgbm',[('input', FloatTensorType([None, colExpVarDim]))],target_opset=12)
def saveONNXModel(model_onnx,model_name):
with open(model_name, "wb") as f:
f.write(model_onnx.SerializeToString())
model = LGBMClassifier(boosting_type='gbdt',objective=get_focal_loss,n_estimators=150,metric="custom")
print("training is starting")
model.fit(
X_train,
y_train,
eval_set=[(X_eval, y_eval)],
eval_metric=get_focal_error,
early_stopping_rounds=30,)
model_onnx = buildONNXModel_lightGBM(model)
saveONNXModel(model_onnx,'lightGBM.onnx')
Then, I can't save model because I use custom objective... but I can save model if I use objective='binary'... How can I save only final tree?
I'll see what I can do. The objective is used to guess the type of outputs (regressor, classifier) the converted model should have. I need to see how to retrieve that information in some other way.
The objective is used to guess the type of outputs (regressor, classifier) the converted model should have.
I think it is better to add new input(it describe the model type such as classifier, regressor,or Ranker) when saving onnx model.
Hi @xadupre , is this issue still unresolved? I think custom objective function(for example, Focal loss, IBloss, and so on) is one of the strong way against unbalance data and many realistic case is unbalance. So I hope this issue will be solved soon.
It would be really useful to fix this.
lightgbm models trained with a custom objective function can not be converted to onnx currently. This is weird because why should inference care about the training objective.
I have a question. Can't we use custom objective function in onnx?
I difined "focal_loss" as a objective function.
ONNX couldn't save following LightGBM model,
but ONNX could save following LightGBM model.
If there is any way to use custom objective function, please let me know.