简体   繁体   中英

brain.js problem with importing LSTM network from json

I've made a brain.js LSTM network, predicting addition. Then I saved it to a .json file using net.toJSON() However, it doesn't seem to work. The traindata.json was successfully created, no errors. However, I get an error:

/home/runner/AngryBlaringTelevision/node_modules/brain.js/dist/brain.js:18102
      var matrix = new Matrix(json.rows, json.columns);
                                   ^

TypeError: Cannot read property 'rows' of undefined
    at Function.fromJSON (/home/runner/AngryBlaringTelevision/node_modules/brain.js/dist/brain.js:18102:36)
    at LSTM.fromJSON (/home/runner/AngryBlaringTelevision/node_modules/brain.js/dist/brain.js:20142:26)
    at /home/runner/AngryBlaringTelevision/index.js:9:7
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)

My code:

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

const LSTM = brain.recurrent.LSTM;
const net = new LSTM();

fs.readFile('traindata.json', function(err, data) {
  if (err) throw err;
  net.fromJSON(data);
  console.log("file loaded");
});

Also, here's the link to my traindata.json file on pastebin: https://pastebin.com/tBZyxq1R I don't have an idea how to solve this error. It seems like it's an error with brain.js.

I have also tried net.run() after importing the file, but it also didn't work.

I think what might be going on is that what you're passing into BrainJS is not actually JSON.

In your code, you have this:

fs.readFile('traindata.json', function(err, data) {
  if (err) throw err;
  net.fromJSON(data);
  console.log("file loaded");
});

When I tried running your code, I saw the same error you saw. Using JSON.parse() seemed to fix the issue.

In other words, when I changed net.fromJSON(data); to net.fromJSON(JSON.parse(data)); , the error disappeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM