I'm using Brain.js in a node.js project. Training now takes too long to run every time i run the program to update things so i'm saving the network in a json file, using the intended method mentioned in the Github page. it seems to save properly but when i try to load it it says "node_modules/brain/lib/neuralnetwork.js:341 var size = json.layers.length; " i'm probably doing something wrong as i haven't found this problem on Github or here, any help would be appreciated. not put the entire file as most of it is repeats of code to load in the files. this all works properly not had any errors until loading the file. If more code is required i'll post it
main file
function run(){
console.log("training network...");
var trainingNet0 = net0.train([
{input: pixels0, output: [1]},
{input: pixels1, output: [1]},
{input: pixels2, output: [1]},
{input: pixels3, output: [1]},
{input: pixels6, output: [0]},
{input: pixels7, output: [0]},
{input: pixels8, output: [0]},
{input: pixels9, output: [0]}],
{
errorThresh: 5, // error threshold to reach
iterations: 1000, // maximum training iterations
log: false, // console.log() progress periodically
logPeriod: 10, // number of iterations between logging
learningRate: 0.003 // learning rate);
})
console.log(net0.run(pixels4))
console.log(net0.run(pixels5))
console.log(net0.run(pixels6))
console.log(net0.run(pixels3))
saveFile();
}
function saveFile(){
fs.writeFile("network.json", net0.toJSON(), function(err) {
if(err)
return console.log(err);
console.log("The file was saved!");
loadFile()
});
console.log(net0.toJSON())
}
function loadFile(){
fs.readFile('network.json', function (err, data) {
if (err)
throw err;
console.log(data.layers);
net0.fromJSON(data);
console.log("file loaded");
});
}
the contents of the json file that is being saved too
[object Object]
I believe you need to pass a string for the data you want to write into fs.writeFile
. So JSON.stringify() the data you pass in.
function saveFile(){
fs.writeFile("network.json", JSON.stringify(net0.toJSON()), function(err) {
if(err)
return console.log(err);
console.log("The file was saved!");
loadFile()
});
}
Don't forget, you're going to have to JSON.parse the data after you fs.readFile()
var obj = JSON.parse(fs.readFileSync('network.json', 'utf8'));
or you can use this node module to save json to files: https://github.com/jprichardson/node-jsonfile