Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How do I save and load a model? #53

Open
siddheshkrishnan1 opened this issue Jul 24, 2019 · 1 comment
Open

How do I save and load a model? #53

siddheshkrishnan1 opened this issue Jul 24, 2019 · 1 comment

Comments

@siddheshkrishnan1
Copy link

The github example does not help btw. I have trained a model using the RBF kernel and now I want to save it so I can use it in another file. How do I do so?

@loretoparisi
Copy link

@siddheshkrishnan1 I did few functions to load / save the model

/**
         * Serialize a SVM model
         * @param {*} modelPath 
         * @param {*} model 
         */
        SVMClassifier.prototype.saveModel = function (modelPath, model) {
            var self = this;
            return new Promise((resolve, reject) => {
                var wstream = fs.createWriteStream(modelPath);
                wstream.on('finish', function () {
                    return resolve(true);
                });
                try {
                    var modelStr =  JSON.stringify(model);
                    wstream.write(modelStr);
                    wstream.end();
                } catch (error) {
                    return reject(error);
                }
            });
        }//saveModel

        /**
         * Deserialize a saved model
         * @param {*} modelPath 
         */
        SVMClassifier.prototype.loadModel = function (modelPath) {
            var self = this;
            return new Promise((resolve, reject) => {
                fileToBuffer(modelPath, (err, buff) => {
                    if (err) {
                        return reject(error);
                    } else {
                        try {
                            var model = JSON.parse(buff.toString('utf-8'));
                            return resolve(model);
                        } catch (error) {
                            return reject(error);
                        }
                    }
                });
            });
        }//loadModel

        /**
         * Validate a saved model
         * @param {*} modelPath 
         */
        SVMClassifier.prototype.isValidModel = function (modelPath) {
            var self = this;
            return new Promise((resolve, reject) => {
                fs.access(modelPath, (error) => {
                    if (error) reject(error);
                    else resolve(true);
                });
            });
        }//isValidModel

but currently the problem is that this project does not compile anymore in node 12.x
#54

If you have any idea how to fix this, we could fork into another project, I have wrote a simple wrapper for the classifier like

var xor = [
        [[0, 0], 0],
        [[0, 1], 1],
        [[1, 0], 1],
        [[1, 1], 0]
    ];

    var svmClassifier = new MXMSVMClassifier({});
    // validate model file if any
    svmClassifier.isValidModel('./xor_model')
        .then(() => {
            console.log('svmClassifier.isValidModel model ok.');
            // load saved model file
            return svmClassifier.loadModel('./xor_model');
        })
        .then(model => {
            var newClf = MXMSVMClassifier.restore(model);
            console.log('svmClassifier.saveModel loaded XOR with new Classifier.');
            xor.forEach(function (ex) {
                var prediction = newClf.predictSync(ex[0]);
                console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], prediction);
            });
        })
        .catch(error => {
            var clf = new MXMSVMClassifier.CSVC();
            console.log('Train XOR with new Classifier...');
            clf.train(xor)
            .progress(function(rate){
                console.log('svmClassifier.progress:'+rate);
            })
            .spread(function (model, report) {
                // save model to file
                svmClassifier.saveModel('./xor_model',model)
                .then(() => {
                    console.log('svmClassifier.saveModel done.');
                })
                .catch(error => {
                    console.error('svmClassifier.saveModel',error);
                });
            });
        });

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants