-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I save and load a model? #53
Comments
@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 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);
});
});
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
The text was updated successfully, but these errors were encountered: