简体   繁体   English

Brain.js返回NaN

[英]Brain.js returns NaN

I wanted to make a neural network in node.js with brain.js. 我想用brain.js在node.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();

function norm (inp){
    var istr = inp.toString(2);
    var out = [];
    for (let i = 0;i <= istr.length;i++) {
        out[i] = +istr.charAt(i);
    }
    return out;
}

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

var input = norm(6);
console.log(input);
var output = net.run(input);
console.log(parseInt(output,2));

I waited for output about [1,0,0,1,0,0](second output). 我等待输出[1,0,0,1,0,0](第二个输出)。 But I got this: 但是我得到了:

[1,1,0,0]
NaN

What is the problem? 问题是什么?

The use case is a little tricky, because norm in this case is returning different size arrays ( NeuralNetwork , and feedforward neural networks in general, doesn't support that), and console.log(parseInt(output,2)) is casting an array ( output ) to an integer. 用例有点棘手,因为在这种情况下norm返回了不同大小的数组( NeuralNetwork和前馈神经网络通常不支持该数组),而console.log(parseInt(output,2))正在投射数组( output )为整数。 Here is a working example, the key is normalizing inputs (here is a live version: https://jsfiddle.net/robertleeplummerjr/8kh0wurg/2/ ): 这是一个工作示例,关键是规范输入(这是实时版本: https : //jsfiddle.net/robertleeplummerjr/8kh0wurg/2/ ):


const brain = require('./src');
const net = new brain.NeuralNetwork();

const lookupValueTable = {
  3: normKey(3).join(','),
  6: normKey(6).join(','),
  8: normKey(8).join(','),
  9: normKey(9).join(','),
  36: normKey(36).join(','),
  64: normKey(64).join(','),
  81: normKey(81).join(',')
};

const lookupKeyTable = {
  [normKey(3).join(',')]: 3,
  [normKey(6).join(',')]: 6,
  [normKey(8).join(',')]: 8,
  [normKey(9).join(',')]: 9,
  [normKey(36).join(',')]: 36,
  [normKey(64).join(',')]: 64,
  [normKey(81).join(',')]: 81
};

const trainingData = [
  { input: norm(3), output: norm(9) },
  { input: norm(9), output: norm(81) },
  { input: norm(6), output: norm(36) },
  { input: norm(8), output: norm(64) }
];

net.train(trainingData, { errorThresh: 0.0015 });
printResults(3, 9);
printResults(9, 81);
printResults(6, 36);
printResults(8, 64);

function normKey (inp) {
  const limitNumber = 100;
  const limit = limitNumber.toString(2).length;
  const istr = inp.toString(2);
  if (istr.length > limit) throw new Error('Normalizing too large of a value for this neural network');
  const out = [];
  for (let i = 0; i < limit; i++) {
    if (i < istr.length) {
      out[i] = istr[i] === '0' ? '.5' : istr[i];
    } else {
      out[i] = '-';
    }
  }
  return out;
}

function norm (inp) {
  const limitNumber = 100;
  const limit = limitNumber.toString(2).length;
  const istr = inp.toString(2);
  if (istr.length > limit) throw new Error('Normalizing too large of a value for this neural network');
  const out = [];
  for (let i = 0; i < limit; i++) {
    if (i < istr.length) {
      out[i] = istr[i] === '0' ? .5 : +istr[i];
    } else {
      out[i] = 0;
    }
  }
  return out;
}

function keyFromArray(output) {
  return Array.from(output).map(v => {
    if (v > .8) return '1';
    if (v < .6 && v > .4) return '.5';
    return '-';
  }).join(',');
}

function printResults(inputRaw, outputRaw) {
  const input = norm(inputRaw);
  const output = net.run(input);
  const key = keyFromArray(output);

  console.log(`input for ${inputRaw}:`, input);
  console.log(`output for ${inputRaw}:`, output);
  console.log(`lookup key for ${inputRaw}:`, key);
  console.log(`lookup value, should be ${outputRaw}:`, lookupKeyTable[key]);
}

A variation on this would be to simply use the value of the original norm function like this: 对此的一种变化是仅使用原始norm函数的值,如下所示:

const trainingData = [
  { input: { [norm(3)]: 1 }, output: { [norm(9)]: 1 } },
  { input: { [norm(9)]: 1 }, output: { [norm(81)]: 1 } },
  { input: { [norm(6)]: 1 }, output: { [norm(36)]: 1 } },
  { input: { [norm(8)]: 1 }, output: { [norm(64)]: 1 } }
];

You could even use a LSTM recurrent neural network. 您甚至可以使用LSTM递归神经网络。 Here is an example of a bunch of math learned by one: https://github.com/BrainJS/brain.js/blob/master/examples/javascript/learn-math.js 这是一个人学习的一堆数学示例: https : //github.com/BrainJS/brain.js/blob/master/examples/javascript/learn-math.js

You are not doing anything wrong. 您没有做错任何事。 The neural network is not able to approximate square operation correctly. 神经网络无法正确近似平方运算。 As shown in the readme of brainjs , it's not able to XOR also correctly. brainjs自述文件所示,它也无法正确进行XOR。

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

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