1
$\begingroup$

I have a situation where my data can only be read from in a hosted Python environment, due to data security reasons. However, I am constrained to run ML models in a Rust environment due to work-related factors such as the expertise of the team. Does it make sense to do ML model training in scikit-learn, and then use those parameters in Rust. I have 2 types of models I need to use: logistic regression and SVM.

Based on the documentation for both scikit-learn and linfa, I think I can simply return the trained parameters, then manually copy over the parameters into my rust system and run inference using linfa. I want to know if anyone else has faced this problem before, and whether there could be a more streamlined way to do this?

linfa examples:

  1. Logistic regression https://github.com/rust-ml/linfa/blob/master/algorithms/linfa-logistic/examples/logistic_cv.rs#L17

  2. SVM https://github.com/rust-ml/linfa/blob/master/algorithms/linfa-svm/examples/winequality.rs#L16

Proposal: Derive optimal model parameters in scikit-learn > Save these parameters in a file > Read these parameters into Rust code > Initialize linfa model using parameters > Store linfa model

From what I understand, there might be a problem in storing and loading linfa models: https://github.com/rust-ml/linfa/issues/67

Would be keen to hear from people's experience on this.

$\endgroup$
1
  • $\begingroup$ can you accept/upvote my answer if it helped you? :) $\endgroup$ Nov 20, 2023 at 9:35

2 Answers 2

1
$\begingroup$

In theory, if you were to pass the same data through a logistic regression or a SVM implemented in different packages, you should get the same results. And the same should apply when tuning a model in package A and re-creating the same one in package B with the optimal parameters found in package A.

However, in practice, there will likely be some differences due to details that are implementation-specific (logistic regression or SVM might be implemented differently in Scikit-Learn and Linfa). Off the top of my head, the following might affect your experiment:

  • Randomisation (can be controlled with random generator seeds).
  • Default settings that are default in one library but not the other (can be controlled by explicitly specifying as many parameters as possible).
  • Different parameters, as in some parameters might be named differently (e.g., learning rate is named learning_rate in Scikit-Learn but it's called lr in PyTorch) or might be non-existent depending on the implementation of the package in question.

To make sure your experience is a success, you should check all parameters that are controllable between Scikit-Learn and Linfa and manually set as many as possible so you are sure you can replicate it in Linfa (while making sure the naming is the same, and if it's different map them correctly when you save them and load them in R). If you explicitly set all the parameters, use the same random seed, and have the same data with the same splits, then the results should be very close if not identical.

$\endgroup$
1
  • $\begingroup$ On Randomisation, from what I understand the training of Logistic Regression and SVM models is not random, so given the same dataset it should produce the same parameters. The rest are valid though, thank you! $\endgroup$
    – wtwtwt
    Nov 16, 2023 at 17:09
0
$\begingroup$

Or, to make things easier and robust, you can export the trained scikit-learn model in a cross-platform format, and import it in Rust.

2 suggested format by scikit-learn are ONNX and PMML. Chances are you can find a way to import them to linfa without having to touch the parameters directly.

So the steps go as: Train model in scikit -> export to ONNX -> Load the model in linfa.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.