cazala / synaptic

architecture-free neural network library for node.js and the browser
http://caza.la/synaptic
Other
6.92k stars 666 forks source link

NaN only #291

Open Kim-Vallee opened 6 years ago

Kim-Vallee commented 6 years ago

Hello, I am doing an AI for the tic tac toe game (player vs machine) So in basic everything works, its an AI with 9 inputs and 9 outputs that are all numbers between 0 and 1 but at some point the AI.activate(numbers...) only returns me an array of NaN whatever I input Any Idea ? (see below my piece of code and ask for anything unclear)

let boardForAi = [];
  board.map(x => x.map(y => boardForAi.push(y))); // converting [[a,b,c],[d,e,f],[g,h,i]] to [a,b,c,d,e,f,g,h,i]
  // Now the board is a simple array that we can give to the AI
  let res = ttt_AI.activate(boardForAi).map(x => Math.round(x*5)/5);
  /** What does the AI decides to do ?
  * Replacing values inferior to 0.1 by 0 (10% error)
  * And superior to .9 by 1 (10% error)
  **/
  let max = res.reduce(function(a,b) {
    return Math.max(a, b);
  }); // Finding the highest probability to play
  let whereToPlay = res.indexOf(max);
  // I sometimes encounter problems here where res === [NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]
  while(boardForAi[whereToPlay] !== 0){
    // Then we should train ai to never play there because it is already taken
    let corrected_res = res;
    corrected_res[whereToPlay] = 0; // Replacing the probability of playing here to 0
    let training_set = [{
                        input : boardForAi,
                        output : corrected_res
                       }]
    trainer.train(training_set,{
      rate : .3,
      iterations: 200,
      error : .1,
      cost: Trainer.cost.CROSS_ENTROPY
    }) // Training
    res = ttt_AI.activate(boardForAi).map(x => Math.round(x*5)/5);
    max = res.reduce(function(a,b) {
      return Math.max(a, b);
    });
    whereToPlay = res.indexOf(max);
    // I sometimes encounter problems here where res === [NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]
}

Etilawin

ghost commented 6 years ago

You refer an array for input & output in the trainingset. Works in theory, but I recommend to write out the 9 inputs and 9 outputs instead and build the trainingset by looping as many times as required to fill the trainingset with your data.

Example with 27 input and 1 output

    trainingSet[i] = {
        input: [
            parseFloat(arrayx[x][0]),  
            parseFloat(arrayx[x][1]),  
            parseFloat(arrayx[x][2]),  
            parseFloat(arrayx[x][3]),  
            parseFloat(arrayx[x][4]),  
            parseFloat(arrayx[x][5]),      
            parseFloat(arrayx[x][6]), 
            parseFloat(arrayx[x][7]),  
            parseFloat(arrayx[x][8]),  
            parseFloat(arrayx[x][9]),  
            parseFloat(arrayx[x][10]), 
            parseFloat(arrayx[x][11]), 
            parseFloat(arrayx[x][12]), 
            parseFloat(arrayx[x][13]), 
            parseFloat(arrayx[x][14]),
            parseFloat(arrayx[x][15]),
            parseFloat(arrayx[x][16]),
            parseFloat(arrayx[x][17]),
            parseFloat(arrayx[x][18]),
            parseFloat(arrayx[x][19]),
            parseFloat(arrayx[x][20]),
            parseFloat(arrayx[x][21]),
            parseFloat(arrayx[x][22]),
            parseFloat(arrayx[x][23]),
            parseFloat(arrayx[x][24]),
            parseFloat(arrayx[x][25]),
            parseFloat(arrayx[x][26])
        ],
        output: [parseFloat(arrayx[x + timejump][0])]
Kim-Vallee commented 6 years ago

Thanks I am trying that, I also found out that I never trained it to do the right moves but only not to do the wrong ones, which arrived at 0 probability on every output, probably made it bug too