簡體   English   中英

Brain.js返回NaN

[英]Brain.js returns NaN

我想用brain.js在node.js中建立一個神經網絡。 它應該將功率提高一些。 是的,我知道,無需使用神經網絡就可以做到這一點。 但我正在學習。

我只是不知道該怎么辦

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

我等待輸出[1,0,0,1,0,0](第二個輸出)。 但是我得到了:

[1,1,0,0]
NaN

問題是什么?

用例有點棘手,因為在這種情況下norm返回了不同大小的數組( NeuralNetwork和前饋神經網絡通常不支持該數組),而console.log(parseInt(output,2))正在投射數組( output )為整數。 這是一個工作示例,關鍵是規范輸入(這是實時版本: 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]);
}

對此的一種變化是僅使用原始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 } }
];

您甚至可以使用LSTM遞歸神經網絡。 這是一個人學習的一堆數學示例: https : //github.com/BrainJS/brain.js/blob/master/examples/javascript/learn-math.js

您沒有做錯任何事。 神經網絡無法正確近似平方運算。 brainjs自述文件所示,它也無法正確進行XOR。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM