Javascript neural network not converging -


i've been trying evolve neural network prints values converging one, using genetic algorithm.

i've tried debugging code don't know i've messed up.

i'm using fitness chose best "brains" , cross them on (reproduce).

at moment trying evolve "brains" return number. fitness function of difference between returned number , original number.

"use strict";    function sigmoid(x) {      return 1 / (1 + math.e ** -x);  }    function random(min, max) {      return (max - min) * math.random() + min  }    function toss() {      return random(-1, 1)  }    function brain(inputs, hiddens, outputs) {      this.structure = [...arguments];      if (this.structure.length < 3) throw "invalid layer count";      this.layers = [];      this.layers[this.structure.length - 1] = {          nodes: []      };      (var = this.structure.length - 1; i--;) this.layers[i] = {          bias: toss(),          nodes: []      };      (var = 1; < this.structure.length; i++) {          var nodes = this.layers[i].nodes;;          (var j = this.structure[i]; j--;) {              var node = nodes[j] = {                  weights: []              };              (var k = this.structure[i - 1]; k--;) node.weights[k] = toss();          }      };  }    brain.prototype.compute = function() {      if (arguments[0] !== this.structure[0]) throw "invalid input count";      (var = arguments.length; i--;) this.layers[0].nodes[i] = {          value: arguments[i]      };      (var = 1; < this.layers.length - 1; i++) {          var layer = this.layers[i];          var feeder = this.layers[i - 1];          (var j = layer.nodes.length; j--;) {              var node = layer.nodes[j];              var dot = 0;              (var k = node.weights.length; k--;) dot += node.weights[k] * feeder.nodes[k].value;              node.value = sigmoid(dot + feeder.bias);          }      }      var result = [];      var layer = this.layers[this.layers.length - 1];      var feeder = this.layers[this.layers.length - 2];      (var j = layer.nodes.length; j--;) {          var node = layer.nodes[j];          var dot = 0;          (var k = node.weights.length; k--;) dot += node.weights[k] * feeder.nodes[k].value;          result[j] = sigmoid(dot + feeder.bias);      }      return result;  }  brain.prototype.cross = function() {      var newbrain = new brain(...this.structure);      var brains = [this, ...arguments];      (var = 1; < newbrain.layers.length; i++) {          var layer = newbrain.layers[i];          (var j = layer.nodes.length; j--;) {              var node = layer.nodes[j];              (var k = node.weights.length; k--;) node.weights[k] = mutate() ||                  brains[math.floor(math.random() * brains.length)]                  .layers[i].nodes[j].weights[k];          }      }      (var = newbrain.layers.length - 1; i--;) newbrain.layers[i].bias = mutate() ||          brains[math.floor(math.random() * brains.length)]          .layers[i].bias;      return newbrain;  }    function mutate(key, nodes) {      if (math.random() > 0.05) return toss();  }    var brain = new brain(1, 5, 1);  var newbrain = new brain(1, 5, 1)  var result = brain.compute(1);  var cross = brain.cross(newbrain);    var brains = [];  (var node = 45; node--;) brains.push({      brain: new brain(1, 5, 4, 3, 2, 1)  });  (var count = 1000000; count--;) {      brains.push({          brain: new brain(1, 5, 4, 3, 2, 1)      });      (var node = brains.length; node--;) {          var brain = brains[node];          var number = 1;          var target = number;          brain.fitness = 1 / math.abs(number - brain.brain.compute(number));      }      brains.sort((a, b) => a.fitness < b.fitness);      if (count % 10000 === 0) console.log(brains.length, brains[0].fitness);        var newbrains = [];      (var node = 10; node--;)          (var j = node; j--;) newbrains.push({              brain: brains[node].brain.cross(brains[j].brain)          });      brains = newbrains;  }  console.log(brains); 

what need improve/change?

here console log:

46 1.468903884218341  46 1.1881817088540865  46 4.899728181582378  46 1.5494097713447523  46 2.4958253537304644  46 2.4091648830940953  46 1.4000955420478967  46 1.7560836401632383  46 3.3419380735652897  46 2.8290305398668245  46 2.951901023302089  46 2.9400525658126675  46 2.6769575714598948  46 1.55835425177616 

as can see, fitness seems random

some advice...

  1. neural networks take input should somehow related output. couldn't find inputs network? if can't come ideas use xor problem , try solve it.

  2. when checking if population becoming better on time don't @ brains of each generation. remember, intentionally creating randomized networks may or may not @ task. try printing top result of each generation , maybe average score. in working genetic algorithm both values should become better on time. (altough top score way more significant & important)

  3. (not directly adressing issue) don't use javascript. transscribe current code java/c#/c++. these languages execute way faster js.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -