[英]Neural Network in JavaScript
I'm having a little trouble with my neural network. 我的神经网络有点麻烦。 I've set it up so it generates an array with 5 values;
我已经设置好了,它会生成一个包含5个值的数组;
0
or 1
, ie [1,1,0,1,0]
. 0
或1
,即[1,1,0,1,0]
。 And using Node.js I console log the random array, and if I reply with y
it will add it to the training with the correct output, and vice versa. 然后使用Node.js控制台记录随机数组,如果我回答
y
,它将以正确的输出将其添加到训练中,反之亦然。 Once I have responded, the genRan()
runs and creates a new random array and saves the "guess" to var guess
. 有一次,我已经回应的
genRan()
运行,并创建一个新的随机排列,并保存了“猜”对var guess
。 However, after the first run, it no longer gives me a guess value, instead: [object Object]
. 但是,在第一次运行后,它不再给我一个猜测值,而是:
[object Object]
。
Here is the code: 这是代码:
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\n\r");
genRan();
})
}
genRan();
The first question works fine, and presents this: 第一个问题工作正常,并提出以下内容:
0) input: 0,0,0,0,0 We think: 0.07046. Am I correct? (Y/N)
When I respond, I get: 当我回复时,我得到:
Success percent: 100% 1 attempts
1) input 1,1,1,0,1 We think: [object Object]. Am I correct? (Y/N)
For some reason, when it goes to "guess" it doesn't give me a value. 出于某种原因,当谈到“猜测”时,它并没有给我任何价值。 Any ideas why?
有什么想法吗?
The reason its gone wrong is twofold 它出错的原因是双重的
net.run
is an array - you probably want the first item from it. net.run
的输出是一个数组-您可能需要其中的第一项。 output
in net.train
is an array - you're passing it a distinct value net.train
output
的输入是一个数组-您正在为其传递一个不同的值 With a few changes your code works as (I think) you expect it: 经过一些更改,您的代码将按您期望的那样工作:
guess[0]
in your ask
method throughout ask
方法中始终使用guess[0]
Wrap the oposite
variable in square braces to make it an array 将
oposite
变量包装在方括号中以使其成为数组
net.train([ {input : array, output : [opposite]} ]);
Working code below for reference (Will not work in stacksnippet though) 以下工作代码供您参考(尽管不会在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\\n\\r"); genRan(); }) } genRan();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.