Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Brain.js返回NaN_Javascript_Node.js_Neural Network - Fatal编程技术网

Javascript Brain.js返回NaN

Javascript Brain.js返回NaN,javascript,node.js,neural-network,Javascript,Node.js,Neural Network,我想用brain.js在node.js中创建一个神经网络。它应该提高到某个数字的权力。是的,我知道,我不用神经网络也能做到。但我正在学习 我只是不知道该怎么办 var brain=require('brainjs'); var net=新的brain.NeuralNetwork(); 函数范数(inp){ var istr=输入到串(2); var out=[]; 对于(假设i=0;i你没有做错任何事情。神经网络无法正确地近似平方运算。如中所示,它也无法正确地进行异或运算。你没有做错任何事情。神

我想用brain.js在node.js中创建一个神经网络。它应该提高到某个数字的权力。是的,我知道,我不用神经网络也能做到。但我正在学习

我只是不知道该怎么办

var brain=require('brainjs');
var net=新的brain.NeuralNetwork();
函数范数(inp){
var istr=输入到串(2);
var out=[];

对于(假设i=0;i你没有做错任何事情。神经网络无法正确地近似平方运算。如中所示,它也无法正确地进行异或运算。

你没有做错任何事情。神经网络无法正确地近似平方运算。如中所示,它也无法正确地进行异或运算

这个用例有点棘手,因为在本例中,
norm
返回不同大小的数组(
NeuralNetwork
,而前馈神经网络一般不支持),并且
console.log(parseInt(output,2))
正在投射数组(
output
)这是一个工作示例,关键是规范化输入(这是一个实时版本:):


您甚至可以使用LSTM递归神经网络。下面是一个由一个人学习的一组数学的示例:

用例有点棘手,因为在这种情况下,
norm
返回不同大小的数组(
NeuralNetwork
,前馈神经网络通常不支持这一点)和
console.log(parseInt(output,2))
正在将数组(
output
)强制转换为整数。下面是一个工作示例,关键是规范化输入(这里是一个实时版本:):


您甚至可以使用LSTM递归神经网络。下面是一个由一个人学习的一组数学示例:

我没有使用brain.js,但据我所知,“net.run(input);”这部分没有返回正确的值。如果您在parint中使用base 2,“output”应该是二进制值。请检查从“net.run(input);”返回的内容我没有使用brain.js,但据我所知,“net.run(input);”这部分没有返回正确的值。如果在parint中使用base 2,“output”应该是二进制值。请检查“net.run(input);”返回的内容
norm
函数返回不同长度的数组,这些数组与
NeuralNetwork
和行
console.log(parseInt(输出,2))不兼容;
正在将数组解析为整数。
norm
函数返回不同长度的数组,这些数组与
NeuralNetwork
不兼容,行
console.log(parseInt(output,2));
正在将数组解析为整数。
[1,1,0,0]
NaN

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