Skip to content
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

Save and load? #2

Open
26medias opened this issue Aug 4, 2015 · 17 comments
Open

Save and load? #2

26medias opened this issue Aug 4, 2015 · 17 comments

Comments

@26medias
Copy link

26medias commented Aug 4, 2015

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
Copy link

This would be really useful to know.

Thanks

@johansen
Copy link

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

@joerobmunoz
Copy link

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
Copy link

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
Copy link

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
Copy link

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
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALn1x7Ukq9ibLJ_l6oX4THIisLSDmdjIks5qee-jgaJpZM4Flph2
.

@joerobmunoz
Copy link

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
Copy link

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
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALn1xzH60mF6Qx4wgnwb0iFNwqbcgF2bks5qehWCgaJpZM4Flph2
.

@26medias
Copy link
Author

What about restoring a saved net?

@DrorSegev
Copy link

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
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALn1xzs4FiSrF1w8kkbTfNNmpL8yp2o6ks5qeij5gaJpZM4Flph2
.

@26medias
Copy link
Author

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
Copy link

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
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALn1x_D_tfUB4_zKIhEwE2thFWLhOqEkks5qeitKgaJpZM4Flph2
.

@26medias
Copy link
Author

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
Copy link

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
Copy link

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
Copy link

ryango commented Nov 11, 2016

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
Copy link

Revision18 commented Dec 2, 2016

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 <textarea> element. Copy the content of the <textarea> element and save to a js file, I named the variable LearnedData, add var LearnedData= in front of the JSON so you end with a global variable called LearnedData.

It should look like this:

var LearnedData = {"x":[[0,0,0,0,0,1,...

Then to deserialize:

    // Create a dummy MLP, using its constructor to ensure we end with the correct type,
    // you can use 2 inputs node and 2 output nodes always, even if your LearnedData contains
    // more because we will overwrite its properties later.
    var detector = new ml.MLP({
		'input' : [[1, 0], [0, 1]],
		'label' : [[0, 1], [1, 0]],
		'n_ins' : 2,
		'n_outs' : 2,
		'hidden_layer_sizes' : [5]
    });

    // Then overwrite all properties with what is saved in LearnedData variable    
    detector.x = LearnedData.x; // comment out if you won't train
    detector.y = LearnedData.y; // comment out if you won't train
    detector.nLayers = LearnedData.nLayers;
    detector.settings = LearnedData.settings;
    detector.sigmoidLayers = new Array(LearnedData.sigmoidLayers.length);    
    for (var i in LearnedData.sigmoidLayers)
    {
       // Here you cannot lie, use same values than in detector.settings
       // Create a HiddenLayer use its constructor to ensure we end with the correct type
        detector.sigmoidLayers[i] = new HiddenLayer({
            'n_ins': detector.settings['n_ins'],
            'n_outs': detector.settings['n_outs']
        });
        
       // restore all properties of each HiddenLayer
        for (var p in LearnedData.sigmoidLayers[i])
        {
            detector.sigmoidLayers[i][p] = LearnedData.sigmoidLayers[i][p];
        }
    }

    // Free LearnedData if you won't create more MLP instances
    //delete LearnedData;
    LearnedData = null;

Hint: while you are advised by readme.md file to use http://joonku.com/js/machine_learning.min.js for browsers, when trying to guess how to deserialize it is better to download http://joonku.com/js/machine_learning.js so you can insert breakpoints and see what is happening and what constructors you need to correctly recreate your objects.

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

No branches or pull requests

8 participants