Andrew Lushin
Andrew Lushin

Reputation: 23

TypeError in node.js: cannot read property '0' of undefined

I wanted to make a neural network in node.js with brain.js. It should raise to the power some number. Yes, I know, that I can do it without using the neural network. But I am learning.

I just haven't an idea what to do

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

net.train([
    {input: [1 / 3], output: [1 / 9]}, 
    {input: [1 / 9], output: [1 / 81]},
    {input: [1 / 6], output: [1 / 36]},
    {input: [1 / 8], output: [1 / 64]}
]);

var input = 1/6;
console.log(input);
var output = net.run(input);
console.log(output);

I waited for output about 0.027777777777777776. But I got this:

0.16666666666666666
[ NaN ]

What is the problem?

Upvotes: 0

Views: 533

Answers (3)

flyingPenguin
flyingPenguin

Reputation: 151

i am also learning ML i might be able to help you first of all you should type "brain.js" in the first line instead of "brainjs"

var brain = require('brain.js');

the way the neural networks work is they train on the given data which is the input and the output so you don't have to define the formula for the input which you did

you can also limit the iterations of the network to run so you don't have to wait for very long (with small number of iterations you don't expect the output to be very accurate)which is alright if we're int the learning mode also check out "Brad Traversy" s youtube channel he has a very nice crash course on brain.js

here's the code that worked for me and also you should lookup LSTM etc. for learning purposes hope this helps

var brain = require('brain.js');
// var net = new brain.NeuralNetwork();
var net = new brain.recurrent.LSTM();

net.train([
    {input: [0.9525741268224331], output: [0.9998766054240137]}, 
    {input: [0.9998766054240137], output: [0.9996646498695336]},
    {input: [0.9996646498695336], output: [1]},
    {input: [0.9990889488055994], output: [1]}
]);

// var input = 1/(1+Math.pow(Math.E, -6));
// var output = net.run();

var output = net.run([/* insert the input here for which you want the network to guess the value for   like  */0.99 ])

console.log(output);// gives output = 1 or close figure to it

Upvotes: 2

ThomasThiebaud
ThomasThiebaud

Reputation: 11969

You need to provide input as an array like that

net.train([
    {input: [1 / 3], output: [1 / 9]}, 
    {input: [1 / 9], output: [1 / 81]},
    {input: [1 / 6], output: [1 / 36]},
    {input: [1 / 8], output: [1 / 64]}
]);

var output = net.run([input]);

or change the training to not use arrays at all

net.train([
    {input: 1 / 3, output: 1 / 9}, 
    {input: 1 / 9, output: 1 / 81},
    {input: 1 / 6, output: 1 / 36},
    {input: 1 / 8, output: 1 / 64}
]);

var output = net.run(input);

Upvotes: 1

dmbarreiro
dmbarreiro

Reputation: 154

You have to provide your input as an array, like this

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

net.train([
    {input: [1 / 3], output: [1 / 9]}, 
    {input: [1 / 9], output: [1 / 81]},
    {input: [1 / 6], output: [1 / 36]},
    {input: [1 / 8], output: [1 / 64]}
]);

var input = 1/6;
console.log(input);
var output = net.run([input]);
console.log(output);

Upvotes: 1

Related Questions