首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Brain.js返回NaN

Brain.js返回NaN
EN

Stack Overflow用户
提问于 2019-08-26 01:52:32
回答 2查看 942关注 0票数 1

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

我只是不知道该怎么做

代码语言:javascript
复制
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的输出。但我得到了这个:

代码语言:javascript
复制
[1,1,0,0]
NaN

有什么问题吗?

EN

回答 2

Stack Overflow用户

发布于 2019-09-08 02:47:56

用例有点棘手,因为在这种情况下,norm将返回不同大小的数组(NeuralNetwork和前馈神经网络通常不支持这种情况),并且console.log(parseInt(output,2))将数组(output)转换为整数。这是一个有效的例子,关键是规范化输入(这里是一个实时版本:https://jsfiddle.net/robertleeplummerjr/8kh0wurg/2/):

代码语言:javascript
复制
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函数的值,如下所示:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2019-08-26 02:31:22

你没有做错任何事。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57648412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档