Kristijan Stefanoski
Kristijan Stefanoski

Reputation: 262

Brain.js NaN training result

I am trying to train a network which as input takes a certain string that contains some predefined values which later i will pull from database or JSON. I normalize the data by dividing the ACII value of each char by 1000 which leaves me with an array of values between 0 and 1 of each char of the input string. The error is that when i start the training i get training error : NaN, and the strange thing is that it work if i have only one input, bellow is the code.

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

function normalize(string){
  var input = [];
  for(let i=0; i<string.length; i++){
    input.push(string.charCodeAt(i)/1000);
  }
  return input;
}

function convert_ascii(ascii){
  var string = '';
  for(let i=0; i<ascii.length;i++){
    string += String.fromCharCode(ascii[i]*1000);
  }
  return string;
}

var string1 = normalize('Invoice Number IN-7688998788963');
var string2 = normalize('Invoice Date April 19, 2019');
var string3 =  normalize('Due Date May 3, 2019');
var string4 = normalize('Total Due $104.50');
var string5 = normalize('Sub Total $95.00');
var string6 = normalize('Tax $9.50');
const net = new brain.NeuralNetwork();
net.train([
  { input: string1, output: { invoice_num: 1 } },
  { input: string2, output: { date: 1 } },
  { input: string3, output: { due_date: 1 } },
  { input: string4, output: { total_due: 1 } },
  { input: string5, output: { sub_total: 1 } },
  { input: string6, output: { tax: 1 } }
], {
  log: detail => console.log(detail), iterations: 1500
});

let output = net.run(normalize('Invoice Number 1241341'));

console.log(output);

Bellow code works:

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

function normalize(string){
  var input = [];
  for(let i=0; i<string.length; i++){
    input.push(string.charCodeAt(i)/1000);
  }
  return input;
}

function convert_ascii(ascii){
  var string = '';
  for(let i=0; i<ascii.length;i++){
    string += String.fromCharCode(ascii[i]*1000);
  }
  return string;
}

var string1 = normalize('Invoice Number IN-7688998788963');
var string2 = normalize('Invoice Date April 19, 2019');
var string3 =  normalize('Due Date May 3, 2019');
var string4 = normalize('Total Due $104.50');
var string5 = normalize('Sub Total $95.00');
var string6 = normalize('Tax $9.50');
const net = new brain.NeuralNetwork();
net.train([
  { input: string1, output: { invoice_num: 1 } }
], {
  log: detail => console.log(detail), iterations: 1500
});

let output = net.run(normalize('Invoice Number 1241341'));

console.log(output);

Upvotes: 2

Views: 1629

Answers (2)

asparism
asparism

Reputation: 614

I believe this has to do with the NeuralNetwork object only accepting input when all the neurons are the same length. I think the examples of string input I've seen use LSTM(), so...

I changed:

const net = new brain.NeuralNetwork()
net.train(/* your input / config here */)

to:

const net = new brain.recurrent.LSTM()
const train = net.train(/* your input / config here */)
console.log(train)

which gave me a training error each iteration and logged:

{error: 0.011269339231430813, iterations: 150}

Upvotes: 0

macasas
macasas

Reputation: 572

did you ever solve the NaN issue. I am getting the same in the net.biases, net.changes, net.deltas etc... after training, and after run I always get NaN.

I am converting my string to numbers and then buffering the arrays to make them all the same length. I've seen examples where the text, as object key values, just gets sent straight into the train, but as soon as I come away from the example given and try my own data, NaN.

Interesting that in your 2nd example you are including the title in the normalized string, not just the data value. How does that effect things?

I realise this was some time ago, and you've probably moved on since then, but just perhaps you can shed some light and push me in the right direction before I go mad :-)

Upvotes: 1

Related Questions