Using Brain.js - brain.NeuralNetwork is not a constructor

hi, did anybody here ever used Brain,js?

i added it with: npm install brain.js --save, and wrote the following code:

const brain = require('brain.js');
var net = new brain.NeuralNetwork();

however i got the following error:

brain.NeuralNetwork is not a constructor

any ideas?

thanks!

That’s an old, unmaintained project. I don’t think there can be any guarantee that it’s going to work as you’d expect (or even work at all).

Edit: I looked at the wrong package. Ignore my post!

OK, just so I’m not being totally unhelpful, I did:

meteor create brain
cd brain
meteor npm i --save brain.js

In server/main.js:

import { Meteor } from 'meteor/meteor';

Meteor.startup(() => {
  import brain from 'brain.js';
  const net = new brain.NeuralNetwork();

  net.train([
    {input: [0, 0], output: [0]},
    {input: [0, 1], output: [1]},
    {input: [1, 0], output: [1]},
    {input: [1, 1], output: [0]},
  ]);

  console.log(net.run([1, 0]));  // [0.987]
});

Then ran meteor:

I20171116-13:40:07.416(0)? Float64Array [ 0.9337321904948577 ]

Which seems to be working, even if not quite the expected result.

5 Likes

hi, thanks a lot!!!

2 questions please:

  1. why did you use the i in the meteor npm i --save brain.js? is it instead of install?
  2. why did you have to import it? why doesn’t require enough?

thanks!!! :slight_smile:

  1. meteor npm i is shorthand for meteor npm install. :slight_smile:
  2. import is the long-term standard that Javascript community is moving to (ES6, away from CommonJS), as determined by the language designers themselves.

Regardless, you may also want to take a look at Synaptic.js. I did a review of the current neural net packages on NPM as part of my Clinical Decision Support class earlier this year, and it’s pretty clear that Synaptic is actively maintained, has about 50% more activity, and probably has a better API for complex networks. For what it’s worth, Synaptic is what’s being rolled into the Clinical track for CDS functionality.

https://www.npmjs.com/package/synaptic

3 Likes

Wow!

So many awesome answers :slight_smile:
thanks a lot for that. and cool i didnt know about this tool.
will take a look now.

Thanks again :slight_smile: