Brain.js: fromJSON not Working (I think I solved it)

Created on 24 Feb 2019  Â·  6Comments  Â·  Source: BrainJS/brain.js


Me in a Nutshell

DISCLAIMER: I think I solved it, explanation below. If I just swapped a problem by another, please inform me.

What is wrong?

When I am trying to load a trained NN from a file, I get an error saying that I cannot read property 'bias' of undefined.

Where does it happen?

In the fromJSON function

How do we replicate the issue?

I replicated it by just trying to load a trained model from memory.

How important is this (1-5)?

It prevented me from using trained models, which is a big deal, so I guess it's, like, 4 or 5.
I may be stupid tho, maybe I messed up saving and reading the JSON somehow.

Expected behavior (i.e. solution)

It should've loaded the model.

Here's how I fixed it (there is probably a nicer way, but I didn't want to get into how the library works and stuff) :
I figured by debugging that the loop for(var j in nodes) goes for some reason to the 398th property of the object (the layer has only 397, and I figured that's one too much). All I did then was swapping the for ... in ... format to a standard loop: for (var t = 0; t < nodes.length; t++) and then just assigning var j = t.toString() since t was the number as a string.

Most helpful comment

Are you going to open a PR for this issue? We'd love to gain you as a contributor!

All 6 comments

Are you going to open a PR for this issue? We'd love to gain you as a contributor!

Are you going to open a PR for this issue? We'd love to gain you as a contributor!

I am a massive noob, honestly. I don't think I have the knowledge to seriously mess with most of the library. btw, I am not even sure that the problem I stated is an actual problem and not my misuse, so I'll hold on to that pr for now - maybe someone more experienced can confirm that it is a bug.

Hi @omer54463 — I'd be happy to take a look, confirm if there's a bug, and help you open your first PR. I'll need to be able to reproduce the issue at my end. Can you share:

  1. Your neural network code
  2. The file you saved the model to.

Looking forward!

Note that the source of these errors - when loading a saved model - is often caused by a mismatch in the network type used to train the model vs. the network being used to load the model. They must match. e.g. saving an LSTM -> loading into a GRU will fail with "cannot read property 'columns' of undefined".

It's also important to make sure you're calling JSON.parse after loading, to decode bytes -> an object.

Sharing your save+load code - all of it - will help debug this.

Maybe a good option is to restore network from the saved state? i.e. Store network type in the json too, whihc means when importing json, brain.js should auto create relevant network and load data in it. @robertleeplummerjr

const lstm = new brain.recurrent.LSTM();
lstm.fromJSON(myjson);
const result = lstm.train(trainingData, {
iterations: 15000,
log: details => console.log(details),
errorThresh: 0.011
});

const updatedjson = lstm.toJSON();

//, { flag: 'a+' }
fs.writeFile("results.json", JSON.stringify(updatedjson), function (err) {
if (err) throw err;
console.log('The "data to write" was appended to file!');
});

result.json file needs to have train data for new training and old training

how can i do this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

panzad3 picture panzad3  Â·  5Comments

tyt34 picture tyt34  Â·  5Comments

nezzard picture nezzard  Â·  5Comments

shyamaprasadpatra picture shyamaprasadpatra  Â·  4Comments

grigori-gru picture grigori-gru  Â·  4Comments