mysamai / natural-brain

A natural language classifier using Node Natural with a BrainJS neural network
MIT License
320 stars 29 forks source link

Can you save/import a net after its been trained? #12

Closed bags307 closed 7 years ago

bags307 commented 7 years ago

both natural and brain allow for serialization (in the case of brain), or actual saving of a trained classifer (natural).

Is there a similar functionality here?

swizardlv commented 7 years ago

it is supported in natural

A classifier can also be persisted and recalled so you can reuse a training

classifier.save('classifier.json', function(err, classifier) { // the classifier is saved to the classifier.json file! }); To recall from the classifier.json saved above:

natural.BayesClassifier.load('classifier.json', null, function(err, classifier) { console.log(classifier.classify('long SUNW')); console.log(classifier.classify('short SUNW')); }); A classifier can also be serialized and deserialized like so:

var classifier = new natural.BayesClassifier(); classifier.addDocument(['sell', 'gold'], 'sell'); classifier.addDocument(['buy', 'silver'], 'buy');

// serialize var raw = JSON.stringify(classifier); // deserialize var restoredClassifier = natural.BayesClassifier.restore(JSON.parse(raw)); console.log(restoredClassifier.classify('i should sell that'));

daffl commented 7 years ago

This should also work here and is tested in https://github.com/mysamai/natural-brain/blob/master/test/baseline.test.js#L111.

A Note: It is still going to train the net every time you load or restore.

swizardlv commented 7 years ago

@daffl

i tried seems no need to retrain

BrainJSClassifier.load('brain_classifier.json', null,function(error,classifier) {
    console.log('loaded');
    console.log(classifier.classify('did the tests pass?')); // -> software
console.log(classifier.classify('did you buy a new drive?')); // -> hardware
console.log(classifier.classify('What is the capacity?')); // -> hardware
console.log(classifier.classify('Lets meet tomorrow?')); // -> meeting
console.log(classifier.classify('Can you play some stuff?')); // -> music
});
daffl commented 7 years ago

What I meant was that it doesn't store the serialized Neural Network, just the Node Natural training data. Glad it is working though, going to close this issue!