简体   繁体   English

从json导入LSTM网络的brain.js问题

[英]brain.js problem with importing LSTM network from json

I've made a brain.js LSTM network, predicting addition.我制作了一个brain.js LSTM 网络,预测加法。 Then I saved it to a .json file using net.toJSON() However, it doesn't seem to work.然后我使用net.toJSON()将它保存到一个net.toJSON()文件中,但是,它似乎不起作用。 The traindata.json was successfully created, no errors. traindata.json已成功创建,没有错误。 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.另外,这是我在 pastebin 上的traindata.json文件的链接: https : traindata.json我不知道如何解决这个错误。 It seems like it's an error with brain.js.这似乎是brain.js 的错误。

I have also tried net.run() after importing the file, but it also didn't work.导入文件后我也尝试过net.run() ,但它也不起作用。

I think what might be going on is that what you're passing into BrainJS is not actually JSON.我认为可能发生的情况是您传递给 BrainJS 的实际上并不是 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.使用JSON.parse()似乎解决了这个问题。

In other words, when I changed net.fromJSON(data);换句话说,当我改变net.fromJSON(data); to net.fromJSON(JSON.parse(data));net.fromJSON(JSON.parse(data)); , the error disappeared. ,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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