我的神经网络有点麻烦。我已经设置好了,它会生成一个包含5个值的数组; 01,即[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\n\r");
        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的输出是一个数组-您可能需要其中的第一项。
outputnet.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\n\r");
        genRan();
    })

}
genRan();

关于javascript - JavaScript中的神经网络,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49073052/

10-12 16:43