karpathy / convnetjs

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Brain fromJSON

opened this issue · comments

Is there any way to read Brain data from stored JSON? toJSON + fromJSON combo doesn't seem to work.

I think it works, at least I used it in node js.
Converting brain to string (t);

var j = brain.value_net.toJSON();
var t = JSON.stringify(j); //brain as string

Loading brain from string (t);

var j = JSON.parse(t);
brain.value_net.fromJSON(j);

I'm trying to save the learning progress and being able to load it later. Process you described seems to save only the network structure.

Edit: I'm the OP. I didn't notice I'm logged in with my work account :)

I probably stupid, but what do you mean by "saving learning progress"?

commented

I think he want for exemple load a trained brain and work with neural network without re-train it

Thanks, that's much better phrasing :). Yes that's exactly what I want to achieve.

Hi, I am having a similar problem.

I save the network to a JSON file using

function savenet(brain) {
    var file = 'network.json';
    var j = brain.value_net.toJSON();
    jsonfile.writeFile(file, j, function(err) {
        if (err === null) {
            console.log("network saved successfully");
        } else {
            console.log("Error Saving Bot: " + err);
        }
    });
}

and load it back up with

function loadnet(brain) {
    var file = 'network.json';
    jsonfile.readFile(file, function(err, obj) {
        if (err === null) {
            console.log("network loaded successfully");
            brain.value_net.fromJSON(obj);
        } else
            console.log("Error Loading Bot: " + err);
    });
}

The file saves the network correctly but after loading it back up and calling

brain.forward(state);

it returns NaN instead of one of the outputs from the network. Not entirely sure why that is the case other than the JSON file not being loaded correctly for some reason although no error is thrown.

@craigmcmillan01 If you using node.js I guess you should try use readFileSync instead. cause as understand your code working asynchronously, which probably mean that in the moment you call for brain.forward(state); brain is still empty.

I used something like that.

if (fs.existsSync("/Path/to/file")) {
    var dataNet = fs.readFileSync("/Path/to/file", 'utf8');
    var j = JSON.parse(dataNet);
    brain.value_net.fromJSON(j);
}

@libraua Thanks! that was exactly the issue.

Are you talking about brain.js data? As in from: https://github.com/harthur-org/brain.js ?

@libraua Looks good, but after load from JSON brain age is 1, is that fine? Should it load age as well from json i mean?

Anyone found a solution?
This issue was opened in 2015…

Doing this:

fs.writeFileSync('trained.json', JSON.stringify(net.toJSON()));

then

const net = new brain.NeuralNetwork();
const jsonFile = fs.readFileSync('trained.json');
const json = JSON.parse(jsonFile);
net.fromJSON(json);

throws:

node_modules/brain.js/dist/neural-network.js:98
          throw new Error('[' + key + ', ' + options[key] + '] is out of normal training range, your network will probably not train.');
          ^

Error: [timeout, null] is out of normal training range, your network will probably not train.
    at node_modules/brain.js/dist/neural-network.js:98:17
    at Array.forEach (<anonymous>)
    at Function._validateTrainingOptions (node_modules/brain.js/dist/neural-network.js:96:48)
    at NeuralNetwork._updateTrainingOptions (node_modules/brain.js/dist/neural-network.js:396:21)
    at NeuralNetwork.fromJSON (node_modules/brain.js/dist/neural-network.js:991:12)
    at Object.<anonymous> (test.js:11:5)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)

It's impossible to use brain.js at the moment…

Looking into a fix right now for this.

I believe the issue that @efolio mentioned is now fixed in: https://github.com/BrainJS/brain.js/releases/tag/1.1.1

Nope, same error with v1.1.1…

Thank you for your help anyway!
Any other idea?

Can you share the reproduced error on jsfiddle or similar?

The source code isn't big:

'use strict';

const brain = require('brain.js');

let net = new brain.NeuralNetwork();

net.train([{input: [0, 0], output: [0]},
           {input: [0, 1], output: [1]},
           {input: [1, 0], output: [1]},
           {input: [1, 1], output: [0]}]);

// save NN
const jsonFileContent = JSON.stringify(net.toJSON());

// load NN
const json = JSON.parse(jsonFileContent);
net = new brain.NeuralNetwork();
net.fromJSON(json); // <--- this throws

const res = brain.likely([0, 1], net);
console.log(res);

There is no error when we don't use JSON.stringify and JSON.parse.
Maybe this would help to figure out the issue?

From what I see, json.trainOpts.timeout is loaded to null instead of Infinity
If I manually put Infinity in it, it works!!

(Infinity is not a value that can be put in JSON format…)

Fantastic! Let me get a test and fix out. Ty for finding this!

ok, @freddyC beat me to a fix. We will have it released shortly, fyi. Likely before end of day.

Why is a brain.js query appearing in this repo?

"we all look the same" 😛

Damn it is it really hard to guess that Infinity is not a serializable value and cut it to hell with this shit-check. 4 Nov 2015...