0

My use case is I am training machine learners in python to find which model is most performant. But once I have finished choosing a model, I need to recapitulate that model in C for the shippable product.

What we have been planning to do is pick apart the model in python, save all its parameters to a file, and load these in to a different model with the same structure in C. But this strikes me as unnecessarily complex if there is actually a way to save a trained model as some kind of black-box, compiled library with a predict() function.

So is there a way to compile an object with all its data to a machine-code library? All I find about saving python objects is pickling them, which is clearly not what I need.

Pavel Komarov
  • 1,153
  • 12
  • 26
  • So you need to move something from python to c? Why don't you use python entirely or C entirely? – user202729 Jan 21 '18 at 14:10
  • No, there is not. You need to treat behaviour (code) and data separate. You could compile your code with [Cython](http://cython.org/), preferably written with the Cython dialect to begin with. – Martijn Pieters Jan 21 '18 at 14:14
  • @user202729 It is not as convenient to code in C to start with because it does not have scikit-learn and the like, and we would have to completely define all models ourselves. Better to do hyperparameter and model search in python and then recapitulate only what we select as best. And we need to ship in C because this is going on a bird with power/space constraints. – Pavel Komarov Jan 21 '18 at 14:45
  • @Martijn Pieters So I could potentially compile all the python classes on which the chosen model relies and then load it with parameters manually? That would still save me the time of recoding all those classes in C – Pavel Komarov Jan 21 '18 at 14:48
  • What about pickling the trained model from the big script, reading it back in to an other smaller one, compiling that smaller one+dependencies with cython, and then putting the pickled trained model+the compiled smaller script that reads it in in my C project? – Pavel Komarov Jan 21 '18 at 18:50

0 Answers0