Brain.js: Import/Export of JSON for model is not working

Created on 28 Mar 2018  路  8Comments  路  Source: BrainJS/brain.js


What is wrong?

Export/Import functionality of the library is not working, the JSON is not in the correct format to be considered JSON.

Where does it happen?

Windows 10 node version 8

How do we replicate the issue?

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

var net = new brain.recurrent.RNN();

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

var output = net.run([0, 0]); // [0]
output = net.run([0, 1]); // [1]
output = net.run([1, 0]); // [1]
output = net.run([1, 1]); // [0]

console.log(net.toJSON()) // JSON is in incorrect format, am i missing something?
`

How important is this (1-5)?

4 - love this library but can't use it for Production use cases until i can export/import

Expected behavior (i.e. solution)

JSON is exported in valid format

Other Comments

N/A - love this Library tho

Most helpful comment

net.fromJSON(JSON.parse("input-output-trainedmodel.json"));

May be the problem. JSON.parse reads a string in, you want to pass the string of the source of input-output-trainedmodel.json

Is there a trick to reading it in from an external source?

Yea, just use

const fs = require('fs');

and then later:

console.log(json) // JSON is in different format
fs.writeFileSync("input-output-trainedmodel.json", JSON.stringify(json));
var net = new brain.recurrent.RNN();
net.fromJSON(
  JSON.parse(
    fs.readFileSync("input-output-trainedmodel.json").toString()
  )
);

All 8 comments

Very important issue. We unit tests here: https://github.com/BrainJS/brain.js/blob/d7508dd706d82a1d864cb74ae52fa4df6070a486/test/recurrent/rnn.js#L323

Will look at this on my lunch break.

Ah ok, I see: https://jsfiddle.net/robertleeplummerjr/zwyhxL8f/6/

The json for a recurrent network brain.recurrent.RNN and the json from a feed forward neural network brain.NeuralNetwork are simply different. The above reproduces the functionality and should clarify. I don't believe this to be a bug.

Can you take a look?

Thanks for the code.

I've tried it and your version works, when you pass the stored json from the variable in the previously trained network to the new one, however, when i try and read in the json from a file and parse it, it has a problem reading it?

CODE:

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

var net = new brain.recurrent.RNN();

net.train([{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [0]}],
{logPeriod:1, log:true, iterations:2000});

console.log(net.run([0, 0])); // [0]
console.log(net.run([0, 1])); // [1]
console.log(net.run([1, 0])); // [1]
console.log(net.run([1, 1])); // [0]

var json = JSON.stringify(net.toJSON());
console.log(json) // JSON is in different format
var net = new brain.recurrent.RNN();
net.fromJSON(JSON.parse("input-output-trainedmodel.json"));

console.log(net.run([0, 0])); // [0]
console.log(net.run([0, 1])); // [1]
console.log(net.run([1, 0])); // [1]
console.log(net.run([1, 1])); // [0]

ERROR:

index.js:143
var matrix = new Matrix(json.rows, json.columns);
TypeError: Cannot read property 'rows' of undefined

Is there a trick to reading it in from an external source?

Also, I think either the README needs to change to specify that the exported JSON model needs to be stringified before use, or the return function needs to stringify it before it returns the JSON.

net.fromJSON(JSON.parse("input-output-trainedmodel.json"));

May be the problem. JSON.parse reads a string in, you want to pass the string of the source of input-output-trainedmodel.json

Is there a trick to reading it in from an external source?

Yea, just use

const fs = require('fs');

and then later:

console.log(json) // JSON is in different format
fs.writeFileSync("input-output-trainedmodel.json", JSON.stringify(json));
var net = new brain.recurrent.RNN();
net.fromJSON(
  JSON.parse(
    fs.readFileSync("input-output-trainedmodel.json").toString()
  )
);

Yes you are correct. Thanks so much! Sorry, I knew that the reading in was a javascript issue not a brain js issue I just didn't know how to achieve it. I'm a .NET developer by trade so little rusty with the ol' JS side of things.

Thanks again.

No problem! Always a pleasure to have a visitor from .net. I started there, and began to dabble a little. You'll be surprised how fast you go from "little rusty" to a well oiled machine.

chow!

I recommend stringify before save and then just read file, parse it using JSON, as I did here:
Save: https://github.com/mubaidr/image-parsing/blob/master/src/index.js#L76
Read: https://github.com/mubaidr/image-parsing/blob/master/scripts/test.js#L10

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shyamaprasadpatra picture shyamaprasadpatra  路  4Comments

nezzard picture nezzard  路  5Comments

TomDobbelaere picture TomDobbelaere  路  6Comments

akii0008 picture akii0008  路  5Comments

ximik753 picture ximik753  路  3Comments