mljs / libsvm

LIBSVM for the browser and nodejs :fire:
https://mljs.github.io/libsvm/
BSD 3-Clause "New" or "Revised" License
82 stars 14 forks source link

How to save and load a libsvm model with nodejs? #21

Closed JiayiHong closed 2 years ago

JiayiHong commented 2 years ago

Hello!

I am wondering whether I could save and load a libsvm model with nodeJS? I have trained a libsvm model online: let svm = new SVM(options); svm.train(trainingSet, targetSet); I could see that from the svm that only few properties exist: "{\"options\":{\"kernel\":\"1\",\"degree\":3},\"model\":....,\"problem\":...}" I tried to save the model into a json file and load the model from it so that every user could access the model. However, since I only stored the svm model: const model = JSON.stringify(svm); exportJsonFile("svm", model); When I load the model, I cannot do the prediction because libsvm cannot detect the model.

Also, I tried to load OPLS model with PLS.OPLS.load(obj) but it has an error like below: OPLS.js:355 Uncaught (in promise) TypeError: model.wOrth.transpose is not a function in OPLS.js:355.

Could you please give me some instructions about how to achieve this? Thanks in advance!

stropitek commented 2 years ago

I will answer here about libsvm. Please open another issue in the OPLS repo for your second question: https://github.com/mljs/pls

With libsvm, models are stored by using svm.serializeModel() and reloaded with SVM.load(serializedModel)

Please refer to the repo Readme which has some documentation

JiayiHong commented 2 years ago

Thank you so much for your help! svm.serializeModel() works well!