convnetjs icon indicating copy to clipboard operation
convnetjs copied to clipboard

Brain fromJSON

Open ghost opened this issue 10 years ago • 24 comments

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

ghost avatar Nov 03 '15 23:11 ghost

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

libraua avatar Nov 29 '15 15:11 libraua

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

brezinajn avatar Dec 01 '15 18:12 brezinajn

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

libraua avatar Dec 01 '15 20:12 libraua

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

BurakDev avatar Dec 04 '15 17:12 BurakDev

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

brezinajn avatar Dec 06 '15 16:12 brezinajn

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 avatar Dec 06 '15 21:12 craigmcmillan01

@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 avatar Dec 06 '15 22:12 libraua

@libraua Thanks! that was exactly the issue.

craigmcmillan01 avatar Dec 06 '15 22:12 craigmcmillan01

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

robertleeplummerjr avatar Sep 17 '16 22:09 robertleeplummerjr

@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?

BusinessDuck avatar Sep 13 '17 13:09 BusinessDuck

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…

efolio avatar Mar 04 '18 20:03 efolio

Looking into a fix right now for this.

robertleeplummerjr avatar Mar 04 '18 20:03 robertleeplummerjr

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

robertleeplummerjr avatar Mar 04 '18 20:03 robertleeplummerjr

Nope, same error with v1.1.1…

Thank you for your help anyway! Any other idea?

efolio avatar Mar 04 '18 21:03 efolio

Can you share the reproduced error on jsfiddle or similar?

robertleeplummerjr avatar Mar 04 '18 21:03 robertleeplummerjr

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

efolio avatar Mar 04 '18 22:03 efolio

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

efolio avatar Mar 04 '18 22:03 efolio

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

efolio avatar Mar 04 '18 22:03 efolio

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

robertleeplummerjr avatar Mar 05 '18 02:03 robertleeplummerjr

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

robertleeplummerjr avatar Mar 05 '18 14:03 robertleeplummerjr

Try https://github.com/BrainJS/brain.js/releases/tag/1.1.2

robertleeplummerjr avatar Mar 05 '18 18:03 robertleeplummerjr

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

thegamecat avatar Apr 07 '18 21:04 thegamecat

"we all look the same" 😛

robertleeplummerjr avatar Apr 07 '18 22:04 robertleeplummerjr

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

tripolskypetr avatar Mar 07 '21 14:03 tripolskypetr