JavaScript中的神经网络
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中的神经网络相关的知识,希望对你有一定的参考价值。
我的神经网络有点麻烦。我已将其设置为生成一个包含5个值的数组; 0
或1
,即[1,1,0,1,0]
。并使用Node.js我控制台记录随机数组,如果我回复y
它将使用正确的输出将其添加到训练中,反之亦然。一旦我做出回应,genRan()
就会运行并创建一个新的随机数组并将“猜测”保存到var guess
。然而,在第一次运行后,它不再给我一个猜测值,而是:[object Object]
。
这是代码:
var brain = require('brain.js');
var net = new brain.NeuralNetwork();
const readline = require('readline');
const r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var ca = 0,
wa = 0;
net.train([
{input: [0,0,0,0,0], output: [0]}
]);
function genRan(){
var a,b,c,d,e;
var array = [];
a = Math.round(Math.random());
b = Math.round(Math.random());
c = Math.round(Math.random());
d = Math.round(Math.random());
e = Math.round(Math.random());
array.push(a,b,c,d,e);
var guess = net.run(array);
ask(array,guess);
}
function ask(a,b){
var array = a,
guess = b;
r1.question((wa+ca) + ") input: " + array + " We think: " + guess + ". Am I correct? (Y/N)", (answer) => {
if(answer == "Y" || answer == "y"){
ca++;
net.train([
{input : array, output : Math.round(guess)}
]);
}else if(answer == "N" || answer == "n"){
wa++;
var roundGuess = Math.round(guess);
var opposite;
switch (roundGuess){
case 1:
opposite = 0;
break;
case 0:
opposite = 1;
break;
default:
opposite = null
}
net.train([
{input : array, output : opposite}
]);
}
console.log("Success percent: " + (100 *ca/(ca+wa)) + "% " + (ca+wa) +" attempts
");
genRan();
})
}
genRan();
第一个问题很好,并提出这个问题:
0) input: 0,0,0,0,0 We think: 0.07046. Am I correct? (Y/N)
当我回复时,我得到:
Success percent: 100% 1 attempts
1) input 1,1,1,0,1 We think: [object Object]. Am I correct? (Y/N)
出于某种原因,当它“猜测”它并没有给我一个价值。有什么想法吗?
答案
它出错的原因是双重的
net.run
的输出是一个数组 - 你可能想要它的第一个项目。output
中net.train
的输入是一个数组 - 你传递的是一个独特的值
通过一些更改,您的代码就像(我认为)您期望的那样:
- 在你的
guess[0]
方法中使用ask
- 将
oposite
变量包裹在方括号中,使其成为一个数组net.train([ {input : array, output : [opposite]} ]);
下面的工作代码供参考(虽然不能在stacksnippet中工作)
var brain = require('brain.js');
var net = new brain.NeuralNetwork();
const readline = require('readline');
const r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var ca = 0,
wa = 0;
net.train([
{input: [0,0,0,0,0], output: [0]}
]);
function genRan(){
var a,b,c,d,e;
var array = [];
a = Math.round(Math.random());
b = Math.round(Math.random());
c = Math.round(Math.random());
d = Math.round(Math.random());
e = Math.round(Math.random());
array.push(a,b,c,d,e);
//console.log(array);
var guess = net.run(array);
ask(array,guess);
}
function ask(a,b){
var array = a,
guess = b;
r1.question((wa+ca) + ") input: " + array + " We think: " + guess[0] + ". Am I correct? (Y/N)", (answer) => {
if(answer == "Y" || answer == "y"){
ca++;
net.train([
{input : array, output : Math.round(guess[0])}
]);
}else if(answer == "N" || answer == "n"){
wa++;
var roundGuess = Math.round(guess[0]);
var opposite;
switch (roundGuess){
case 1:
opposite = 0;
break;
case 0:
opposite = 1;
break;
default:
opposite = null
}
net.train([
{input : array, output : [opposite]}
]);
}
console.log("Success percent: " + (100 *ca/(ca+wa)) + "% " + (ca+wa) +" attempts
");
genRan();
})
}
genRan();
以上是关于JavaScript中的神经网络的主要内容,如果未能解决你的问题,请参考以下文章