how to dump model from GradientBoostingRegressor to txt? - scikit-learn

I am using gradient boosting regressor to build a predictive model.
After all the tuning/CV, finally I get my prediction right. I am now thinking about dump the model to a file, so that my production c++ program can load it and use it.
It seem that sklearn provides model persistence through pickle, but I am wondering if there is a way to convert pickle model into txt, like what xgboost has. My production code is c++ so having pickle as media is really not handy
Is there a 'dumpModel' function in the library?
Anyone has any experience ?
Thanks

Related

export an unfitted model to ONNX

I am building an API for training models, and figured I wanted to use ONNX to send the models back and forth.
I am testing with a sklearn XGboost model, and it seems that it is a requirement to fit the model before I can export it to onnx.
I want to define a custom or standard sklearn model, convert to onnx for transport, reopen and train, save in ONNX
Is this feasable at all?
My end goal is to have an API that can accept any sklearn, tensorflow or similar model in an untrained state and then train on the server.
Onnx is used to deliver model results, including pre and post-processing or other manipulations, "in production".
The assumption is the model is already trained and you only need to "predict" (or whatever similar action) on new data.
Sound like what you need is a Python (or other) code that will receive your API calls, translate them into the appropriate models, train the models, and then, if you want to be independent from an MLOps point of view, transform the result into Onnx.

Porting pre-trained keras models and run them on IPU

I am trying to port two pre-trained keras models into the IPU machine. I managed to load and run them using IPUstrategy.scope but I dont know if i am doing it the right way. I have my pre-trained models in .h5 file format.
I load them this way:
def first_model():
model = tf.keras.models.load_model("./model1.h5")
return model
After searching your ipu.keras.models.py file I couldn't find any load methods to load my pre-trained models, and this is why i used tf.keras.models.load_model().
Then i use this code to run:
cfg=ipu.utils.create_ipu_config()
cfg=ipu.utils.auto_select_ipus(cfg, 1)
ipu.utils.configure_ipu_system(cfg)
ipu.utils.move_variable_initialization_to_cpu()
strategy = ipu.ipu_strategy.IPUStrategy()
with strategy.scope():
model = first_model()
print('compile attempt\n')
model.compile("sgd", "categorical_crossentropy", metrics=["accuracy"])
print('compilation completed\n')
print('running attempt\n')
res = model.predict(input_img)[0]
print('run completed\n')
you can see the output here:link
So i have some difficulties to understand how and if the system is working properly.
Basically the model.compile wont compile my model but when i use model.predict then the system first compiles and then is running. Why is that happening? Is there another way to run pre-trained keras models on an IPU chip?
Another question I have is if its possible to load a pre-trained keras model inside an ipu.keras.model and then use model.fit/evaluate to further train and evaluate it and then save it for future use?
One last question I have is about the compilation part of the graph. Is there a way to avoid recompilation of the graph every time i use the model.predict() in a different strategy.scope()?
I use tensorflow2.1.2 wheel
Thank you for your time
To add some context, the Graphcore TensorFlow wheel includes a port of Keras for the IPU, available as tensorflow.python.ipu.keras. You can access the API documentation for IPU Keras at this link. This module contains IPU-specific optimised replacement for TensorFlow Keras classes Model and Sequential, plus more high-performance, multi-IPU classes e.g. PipelineModel and PipelineSequential.
As per your specific issue, you are right when you mention that there are no IPU-specific ways to load pre-trained Keras models at present. I would encourage you, as you appear to have access to IPUs, to reach out to Graphcore Support. When doing so, please attach your pre-trained Keras model model1.h5 and a self-contained reproducer of your code.
Switching topic to the recompilation question: using an executable cache prevents recompilation, you can set that up with environmental variable TF_POPLAR_FLAGS='--executable_cache_path=./cache'. I'd also recommend to take a look into the following resources:
this tutorial gathers several considerations around recompilation and how to avoid it when using TensorFlow2 on the IPU.
Graphcore TensorFlow documentation here explains how to use the pre-compile mode on the IPU.

How to load a sklearn model in Tensorflowjs?

I have a gradient boost model saved in the .pkl format. I have to load this model in tensorflowjs. i can see that there is a way to load a keras model but I can't find a way to load a sklearn model. Is it possible to do this?
It is not possible to load sklearn model in tensorflow.js. Tensorflow.js allows to load models written in tensorflow.
Though, I haven't tried myself, but I think that you can possibly use the scikit learn wrapper to rewrite the classifier in tensorflow. The model can be saved and converted to a format that can be loaded in tensorflow.js.

How to convert Turi Create created CoreML models to Keras?

I'm looking for a way to do the conversion, the only information I've found is how to go from Keras and other to CoreML.
You'll have to write your own code to do this, there is no automated conversion tool for Core ML models to Keras (only the other way around).

Convert scikit-learn SVM model to LibSVM

I have trained a SVM (svc) using scikit-learn over half a terabyte of data. The model is working fine and I need to port it to C, but I don't want to re-train the SVM from scratch because it takes way too long for me. Is there a way to easily export the model generated by scikit-learn and import it into LibSVM? Internally scikit-learn uses LibSVM so theoretically it should be possible, but I haven't been able to find anything in the documentation. Any suggestion?
Is there a way to easily export the model generated by scikit-learn and import it into LibSVM?
No. The scikit-learn version of LIBSVM has been hacked up severely to fit it into the Python environment and the model is stored as NumPy/SciPy data structures.
Your best shot is to study the SVM decision function and reimplement it in C. The support vectors can be obtained from the SVC object as NumPy arrays, which are easily translated to C arrays.

Resources