machine_learning icon indicating copy to clipboard operation
machine_learning copied to clipboard

Save and load?

Open 26medias opened this issue 9 years ago • 17 comments

Hi,

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

Thanks.

26medias avatar Aug 04 '15 21:08 26medias

This would be really useful to know.

Thanks

DomVinyard avatar May 06 '16 16:05 DomVinyard

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

johansen avatar Jun 17 '16 19:06 johansen

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.

joerobmunoz avatar Jul 28 '16 17:07 joerobmunoz

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

DrorSegev avatar Aug 03 '16 08:08 DrorSegev

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.

joerobmunoz avatar Aug 10 '16 15:08 joerobmunoz

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 .

DrorSegev avatar Aug 10 '16 18:08 DrorSegev

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.)

joerobmunoz avatar Aug 10 '16 18:08 joerobmunoz

Sweet. Thanks.

Dor On Aug 10, 2016 9:16 PM, "joerobmunoz" [email protected] 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 .

DrorSegev avatar Aug 10 '16 18:08 DrorSegev

What about restoring a saved net?

26medias avatar Aug 10 '16 19:08 26medias

What do you mean by saved net? On Aug 10, 2016 10:39 PM, "Twenty-Six medias, Inc." < [email protected]> 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 .

DrorSegev avatar Aug 10 '16 19:08 DrorSegev

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.

26medias avatar Aug 10 '16 19:08 26medias

I believe you should just use var svm= JSON.parse(str) On Aug 10, 2016 10:49 PM, "Twenty-Six medias, Inc." < [email protected]> 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 .

DrorSegev avatar Aug 10 '16 20:08 DrorSegev

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.

26medias avatar Aug 10 '16 20:08 26medias

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 avatar Aug 22 '16 02:08 openSourceBugs

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.

openSourceBugs avatar Aug 22 '16 02:08 openSourceBugs

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

ryango avatar Nov 11 '16 23:11 ryango

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

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.

Revision18 avatar Dec 02 '16 10:12 Revision18