Brain.js: Throws a NaN even though dataset / output are just numbers.

Created on 19 Mar 2018  路  9Comments  路  Source: BrainJS/brain.js


A GIF or MEME to give some spice of the internet

What is wrong?

Getting {"error": null, "iterations": 20000} even though training dataset are just numbers.

Where does it happen?

When brain.js trains data in my sample website running in Chrome 65 on Mac

How do we replicate the issue?

Input data: https://gist.github.com/digi0ps/65bebf027876f85b42ab7b4dd2ba49bf
Code:

 let net = new brain.NeuralNetwork();
 net.train(encoded);
 trained  = net.toFunction()

How important is this (1-5)?

  1. Just trying to learn. But I'm curious as to why it's not working

Expected behavior (i.e. solution)

Error shouldn't have been NaN. When I run net.run with an input I get
{english: NaN, tanglish: NaN}
instead of values inside them.

Other Comments

Here is the NeuralNetwork object:
https://gist.github.com/digi0ps/53292e8c55c31b7de8de9dcb06ea22e5

Can you help?
Regards,
Sriram.

enhancement

Most helpful comment

All 9 comments

The issue is that your input array lengths are staggered. A simple fix of the following will bring results:

const net = new brain.NeuralNetwork();
net.train(fixLengths(data));

function fixLengths(data) {
  let maxLengthInput = -1;
  for (let i = 0; i < data.length; i++) {
    if (data[i].input.length > maxLengthInput) {
      maxLengthInput = data[i].input.length;
    }
  }

  for (let i = 0; i < data.length; i++) {
    while (data[i].input.length < maxLengthInput) {
      data[i].input.push(0);
    }
  }

  return data;
}

Thanks for the quick fix!

@robertleeplummerjr
Sorry to bring this up again.
But running an input against the trained function, gives null back again.
Can you check?

Is the data being sent in after trained the same size as the previously sent in data?

@robertleeplummerjr We could do some validation/handling when the training data and results passed into the train methods are different sizes. The question is which would be the best way to go about it, run the training for what we have or throw and stop execution?

Validating size wouldn't be very costly, and would provide useful feedback, I like the suggestion!

Thanks robertleeplummerjr for the code above. I'm getting back into coding and brushing up on JS. I have some code to contribute. Its like yours but for when you need to size a single array, I thought someone might find it useful. It fills or slices according to the second parameter.

fixLengthSingle(data, 190)

function fixLengthSingle(data, max)
{
var element = [];
var lrg = Math.max(max, data.length);

for (let i = 0; i < lrg; i++)
{
if (element.push(data[i]) > lrg)
{
element.push(i);
}
}
for (let i = 0; i < element.length; i++) {
if (element[i] == undefined) {
element[i] = 0;
}
}

if(element.length > max )
{
element = element.slice(0, max);
}
return element;
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nezzard picture nezzard  路  5Comments

iSumitBanik picture iSumitBanik  路  4Comments

tyt34 picture tyt34  路  5Comments

robertleeplummerjr picture robertleeplummerjr  路  6Comments

akii0008 picture akii0008  路  5Comments