简体   繁体   English

Brain.js NaN 训练结果

[英]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.我正在尝试训练一个网络,该网络作为输入采用包含一些预定义值的特定字符串,稍后我将从数据库或 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.我通过将每个字符的 ACII 值除以 1000 来对数据进行归一化,这使我得到输入字符串的每个字符的 0 到 1 之间的值数组。 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.错误是当我开始训练时出现训练错误:NaN,奇怪的是如果我只有一个输入它就可以工作,下面是代码。

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

did you ever solve the NaN issue.你有没有解决过 NaN 问题。 I am getting the same in the net.biases, net.changes, net.deltas etc... after training, and after run I always get NaN.我在 net.biases、net.changes、net.deltas 等中得到了相同的...训练后,运行后我总是得到 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.我见过文本作为对象键值直接发送到火车中的示例,但是一旦我离开给出的示例并尝试我自己的数据,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:-)我意识到这是很久以前的事了,从那时起你可能已经继续前进了,但也许你可以在我发疯之前阐明一些观点并把我推向正确的方向:-)

I believe this has to do with the NeuralNetwork object only accepting input when all the neurons are the same length.我相信这与 NeuralNetwork 对象仅在所有神经元长度相同时才接受输入有关。 I think the examples of string input I've seen use LSTM(), so...我认为我见过的字符串输入示例使用的是 LSTM(),所以...

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM