Brain.js: Storing trained net

Created on 25 Sep 2017  路  24Comments  路  Source: BrainJS/brain.js

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

Most helpful comment

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!

All 24 comments

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!

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

You are a hero!!!!

Gonna try this now. Thank you so much!

Any ideas?

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

Just try non-es6

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

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

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

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.

.

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.

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!

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.

// 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)

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.

That may actually be a bug.

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.

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');

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.

Can you share your full example?

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")

In the above example, you are using two different neural networks. Stick to the LSTM network for strings.

My issue was fixed. I had some small mistake and now everything's fine.

Thanks! It worked.

Someday when my programming skills advance I would like to contribute to this project.

Can you make a sophisticated chatbot with this? I assume you would have to use the LSTM-type NN right?

@dock-adams Please follow this thread for information regarding this topic: https://github.com/BrainJS/brain.js/issues/535

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iSumitBanik picture iSumitBanik  路  4Comments

TomDobbelaere picture TomDobbelaere  路  6Comments

timendez picture timendez  路  3Comments

lynxionxs picture lynxionxs  路  5Comments

grigori-gru picture grigori-gru  路  4Comments