junku901 / machine_learning

Machine learning library for Node.js
http://joonku.com/project/machine_learning
735 stars 132 forks source link

Save and load? #2

Open 26medias opened 9 years ago

26medias commented 9 years ago

Hi,

How do you save and then re-open a trained network? It doesn't seem to be explained anywhere in the doc.

Thanks.

DomVinyard commented 8 years ago

This would be really useful to know.

Thanks

johansen commented 8 years ago

Also wondering about this. It would be nice not to have to retrain every time you want to use a model.

joerobmunoz commented 8 years ago

The actual models that you train are JSON instances, so it really depends on how you use them. If you're transferring them to the front-end, you'll have to use local storage or something domain specific. Otherwise, you might consider standard node IO or a database.

DrorSegev commented 8 years ago

Its not the saving, but it the time takes to train the data. Will it be possible to save and load the trained dataset?

joerobmunoz commented 8 years ago

I think I may be misunderstanding the question, so bear with me a sec.

There should be three principal tasks for this:

  1. Create a learner type.
  2. Train the data (this does not involve changing the actual data).
  3. Run a new, prediction data set through the trained model.

The only two things that you wouldn't have saved after completing these tasks is either the model (which you could save as a standard object) or the data that has yet to be run through the predictor.

DrorSegev commented 8 years ago

Thanks. I meant how do you save the trained model? Dor

I think I may be misunderstanding the question, so bear with me a sec.

There should be three principal tasks for this:

  1. Create a learner type.
  2. Train the data (this does not involve changing the actual data).
  3. Run a new, prediction data set through the trained model.

The only two things that you wouldn't have saved after completing these tasks is either the model (which you could save as a standard object) or the data that has yet to be run through the predictor.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/junku901/machine_learning/issues/2#issuecomment-238906009, or mute the thread https://github.com/notifications/unsubscribe-auth/ALn1x7Ukq9ibLJ_l6oX4THIisLSDmdjIks5qee-jgaJpZM4Flph2 .

joerobmunoz commented 8 years ago

Gotcha.

Suppose you create a new learner and train it: var learner = ml.kmeans.cluster({ // whatever })

You might save it by:

// 1. Stringify it var learnerAsAString = JSON.stringify(learner); // 2. Save to whatever medium (e.g. file, db, etc.)

DrorSegev commented 8 years ago

Sweet. Thanks.

Dor On Aug 10, 2016 9:16 PM, "joerobmunoz" notifications@github.com wrote:

Gotcha.

Suppose you create a new learner and train it:

var learner = ml.kmeans.cluster({ // whatever })

You might save it by:

// 1. Stringify it var learnerAsAString = JSON.stringify(learner); // 2. Save to whatever medium (e.g. file, db, etc.)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/junku901/machine_learning/issues/2#issuecomment-238955199, or mute the thread https://github.com/notifications/unsubscribe-auth/ALn1xzH60mF6Qx4wgnwb0iFNwqbcgF2bks5qehWCgaJpZM4Flph2 .

26medias commented 8 years ago

What about restoring a saved net?

DrorSegev commented 8 years ago

What do you mean by saved net? On Aug 10, 2016 10:39 PM, "Twenty-Six medias, Inc." < notifications@github.com> wrote:

What about restoring a saved net?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/junku901/machine_learning/issues/2#issuecomment-238980209, or mute the thread https://github.com/notifications/unsubscribe-auth/ALn1xzs4FiSrF1w8kkbTfNNmpL8yp2o6ks5qeij5gaJpZM4Flph2 .

26medias commented 8 years ago

You've explained how to export to a string so that it can be saved, but not how to load that string back so that we can work with it again in the future.

DrorSegev commented 8 years ago

I believe you should just use var svm= JSON.parse(str) On Aug 10, 2016 10:49 PM, "Twenty-Six medias, Inc." < notifications@github.com> wrote:

You've explained how to export to a string so that it can be saved, but not how to load that string back so that we can work with it again in the future.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/junku901/machine_learning/issues/2#issuecomment-238982824, or mute the thread https://github.com/notifications/unsubscribe-auth/ALn1x_D_tfUB4_zKIhEwE2thFWLhOqEkks5qeitKgaJpZM4Flph2 .

26medias commented 8 years ago

JSON.stringify() returns a JSON object, containing only the data without circular references or methods.

When doing var svm= JSON.parse(str), svm is an object, without any methods besides the default Object methods.

openSourceBugs commented 8 years ago

This feature is totally unsupported at this time. Passing in a new object into mlp.MLP(JSON.parse(str)) will take you down a rabbit hole of "undefined" errors.

openSourceBugs commented 8 years ago

Looking at mlp.js:

                'W': (typeof settings['w_array'] === 'undefined') ? undefined : settings['w_array'][i],
                'b': (typeof settings['b_array'] === 'undefined') ? undefined : settings['b_array'][i]

b_array and w_array are unspecified anywhere in this entire project. Passing in an object with data to mlp.MLP will necessarily fail because b_array and w_array are dead code with no reference anywhere in the project.

ryango commented 8 years ago

looks like you can set any property on these instances, so you could save to json and then reload the data, setting the properties. definitely not ideal though as you need to know which properties to set. could also object.assign

Revision18 commented 7 years ago

This is how I serialized and deserialized a MLP. I will cover only browser code as that is my interest, and only MLP as that is my interest, code should be easy adaptable to other types this library provides.

The concept is that you will use each type constructor to ensure we end with the correct object interface, and then iterate over all properties serialized as JSON and restore them. In the case of the MLP, you will use two constructors, the MLP constructor and the HiddenLayer constructor, HiddenLayer is used internally by MLP when you call predict();

Create a index.html to train your MLP, refer to the MLP example to see how to train one. Then serialize using JSON.stringify to a