karpathy / convnetjs

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.
MIT License
10.86k stars 2.04k forks source link

Brain fromJSON #54

Open ghost opened 8 years ago

ghost commented 8 years ago

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

libraua commented 8 years ago

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);
brezinajn commented 8 years ago

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

libraua commented 8 years ago

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

BurakDev commented 8 years ago

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

brezinajn commented 8 years ago

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

craigmcmillan01 commented 8 years ago

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.

libraua commented 8 years ago

@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);
}
craigmcmillan01 commented 8 years ago

@libraua Thanks! that was exactly the issue.

robertleeplummerjr commented 8 years ago

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

BusinessDuck commented 7 years ago

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

efolio commented 6 years ago

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…

robertleeplummerjr commented 6 years ago

Looking into a fix right now for this.

robertleeplummerjr commented 6 years ago

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

efolio commented 6 years ago

Nope, same error with v1.1.1…

Thank you for your help anyway! Any other idea?

robertleeplummerjr commented 6 years ago

Can you share the reproduced error on jsfiddle or similar?

efolio commented 6 years ago

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 commented 6 years ago

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

efolio commented 6 years ago

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

robertleeplummerjr commented 6 years ago

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

robertleeplummerjr commented 6 years ago

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

robertleeplummerjr commented 6 years ago

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

thegamecat commented 6 years ago

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

robertleeplummerjr commented 6 years ago

"we all look the same" 😛

tripolskypetr commented 3 years ago

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