brain.js icon indicating copy to clipboard operation
brain.js copied to clipboard

Storing trained net

Open thegamecat opened this issue 9 years ago • 24 comments

Loving Brain.js - how do I store the trained nn and then re-use it in other scenarios?

thegamecat avatar Sep 25 '17 13:09 thegamecat

After training, you can do: net.toFunction() This gives you a zero dependency function of your trained net. To further that thought, you can:

import fs from 'fs';
fs.writeFileSync('trained-net.js', `export default ${ net.toFunction().toString() };`);

Which gives you a 100% es6 file you can just import and run, like so:

Then later:

import trainedNet from './trained-net';
trainedNet(data);

Loving Brain.js

Wait till we land gpu support in v1, and tons of layers in v2!

robertleeplummerjr avatar Sep 25 '17 14:09 robertleeplummerjr

You can also export and import via json: https://github.com/BrainJS/brain.js#json

robertleeplummerjr avatar Sep 25 '17 14:09 robertleeplummerjr

You are a hero!!!!

Gonna try this now. Thank you so much!

thegamecat avatar Sep 25 '17 17:09 thegamecat

Any ideas?

import trainedNet from logpath+'/trained-net.js'; ^^^^^^ SyntaxError: Unexpected token import at Object.exports.runInThisContext (vm.js:73:16)

thegamecat avatar Sep 25 '17 17:09 thegamecat

Just try non-es6

var trainedNet = require('./trained-net');
trainedNet(data);

robertleeplummerjr avatar Sep 25 '17 17:09 robertleeplummerjr

ah yes, thanks. Will the export approach in the stored nn work with node7.6?

thegamecat avatar Sep 25 '17 21:09 thegamecat

I don't see any reason why it wouldn't work.

robertleeplummerjr avatar Sep 25 '17 23:09 robertleeplummerjr

Thanks for your help so far Robert.

A few things I've found.

I'm on Node 7.6 and I've had to change the trained-net file to read like this: module.exports = {

getnn: function (input /**/) { return {'do':1/(1+1/Math.exp(1.424093361048118+0.693914741575579*(1/(1+1/Math.exp(0.21477080727104217-0.13510833411671558*(input['price'])+0.002514424937632802*(input['rsi'])+0.12644871083363154*(input['ema20']))))+0.666723146370773*(1/(1+1/Math.exp(0.13917289595446591+0.10253334351161274*(input['price'])+0.020849410810439374*(input['rsi'])+0.12377127023405728*(input['ema20']))))+0.7305866887169579*(1/(1+1/Math.exp(0.11302968130848308-0.015291197107745113*(input['price'])+0.23504038301431582*(input['rsi'])-0.17232474989736501*(input['ema20']))))))} }}

Which I think looks right.

thegamecat avatar Sep 26 '17 15:09 thegamecat

.

thegamecat avatar Sep 26 '17 15:09 thegamecat

I ended up using the JSON method. Took some playing but the application is working, now trying to work out how to optimize to improve the training which is failing at the moment.

Thanks for your help.

thegamecat avatar Sep 27 '17 09:09 thegamecat

I know I'm super late to this, but honestly, just wanted to say that brainjs is truly amazing! Thank you guys for this awesome library!

michaelonubogu avatar Apr 05 '19 15:04 michaelonubogu

I'm having quite a bit of trouble with this, I'm training a somewhat sizeable nn with brain.js in node, offline, and it trains perfectly fine, but I can't seem to save it at all.

const json = net.toJSON()
net.fromJSON(json)

doesn't save the json file to my file directory?

and

import fs from 'fs';
fs.writeFileSync('trained-net.js', `export default ${ net.toFunction().toString() };);

var trainedNet = require('./trained-net');
trainedNet(data);

doesn't work either. The file is saved to my directory, but I can't actually run the model with it. I get TypeError: trainedNet is not a function when I use this.

I just want to train and save this model offline, then load and use it for predictions later, online.

ambroserb3 avatar Dec 24 '19 05:12 ambroserb3

// get json data
const json = net.toJSON()
// write to file system
fs.writeFileSync('trained-net.json', json);

then you can read json later:

const json= JSON.parse(fs.readFileSync('trained-net.json'))
const net = new brain.NeuralNetwork()
net.fromJSON(json)
net.run(input)

mubaidr avatar Dec 24 '19 05:12 mubaidr

I feel silly for forgetting to actually write it. This worked, I just also had to use JSON.stringify on the file when writing or it gave me [object Object].

However, when load the file with net.fromJSON(json)

It gave me the error: Sizes must be set up before initializing.

ambroserb3 avatar Dec 24 '19 16:12 ambroserb3

That may actually be a bug.

robertleeplummerjr avatar Dec 24 '19 16:12 robertleeplummerjr

Hey @robertleeplummerjr what a great library thank you for this. I seem to be running into the same issue.

If you need the stack trace please shout. I don't mind sharing.

cameronmoodley avatar Dec 28 '19 22:12 cameronmoodley

If you trying to write json to file you need first convert it to string: :arrow_down:

const json = network.toJSON();

fs.writeFileSync(__dirname + '/data/trained-net.json', JSON.stringify(json), 'utf8');

FDiskas avatar Mar 01 '20 21:03 FDiskas

That exports as this: export default {"type":"LSTM","options":{"inputSize":12,"inputRange":12,"hi....

I can't seem to get a normal json file out of it.

Shroototem avatar Jan 14 '21 02:01 Shroototem

Can you share your full example?

robertleeplummerjr avatar Jan 14 '21 02:01 robertleeplummerjr

Hi all, complete noob here. Really loving the intuitive feel of brain.js, just having problems saving a trained neural network like the others. I tried the method listed above and got the same response "Sizes must be set before initializing." I have an index.js file, NN.js and a trained-net.json file in use, it loaded the data in the new JSON file but I can't seem to get it from my new NN.js file.

Is there any alternative solutions I could use? Thanks. Here's my code.

//index.js//

const fs = require('fs');

let brain = require("brain.js"); let net = new brain.recurrent.LSTM();

const trainingData = [ 'Jane saw Doug.', 'Doug saw Jane', ];

net.train(trainingData, { iterations: 500, errorThresh: 0.011, log: (data) => console.log(data) });

const json = net.toJSON(); fs.writeFileSync('trained-net.json', JSON.stringify(json));


//NN.js [what I am trying to load it from] //

const fs = require('fs') const brain = require('brain.js') const json= JSON.parse(fs.readFileSync('trained-net.json')) const net = new brain.NeuralNetwork() net.fromJSON(json) net.run("Jane")

polystate avatar Jan 14 '21 09:01 polystate