cazala / synaptic

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

NEAT example #273

Closed developer239 closed 6 years ago

developer239 commented 6 years ago

Could anyone please write simple neat example teaching the net how to solve XOR or some other similar problem? But using the NEAT technique so that I would not have to specify training data set?

1. Initialize network
2. Generate generation
3. Go through each genome in generation and evaluate its fitness (how good it is)
4. Take 2 best genomes from generation
5. Merge genomes 50 / 50 at random
6. Mutate final genome
7. Generate second generation

This would be extremely helpful. Same teqnique is being used here:

https://github.com/ivanseidel/IAMDinosaur https://www.youtube.com/watch?v=P7XHzqZjXQs

I went through the source code but there is WAY to much stuff going on. I understand the general idea. However I have no idea how to implement the solution.

Thank you :)

developer239 commented 6 years ago

I managed to create example following https://github.com/ssusnic/Machine-Learning-Flappy-Bird GA class source code. However it does not work properly.

After 1000+ iterations or so it returns values that are close to 1 or 0 but it does not return the values at the right time.

I created simple repo here: https://github.com/developer239/neural-network-playground/tree/master/neatXOR

Maybe my fitness function is wrong?

calculateFitness: function (input, output) {
    let fitness = 0

    let stringifiedInput = input.join('')

    if (stringifiedInput === '00' || stringifiedInput === '11') {
      fitness = (output - 1) * -1
    }

    if (stringifiedInput === '01' || stringifiedInput === '10') {
      fitness = output
    }

    return fitness
},
wagenaartje commented 6 years ago
// this network learns the XOR gate (through neuro-evolution)
var network = new Network(2,1);

var trainingSet = [
  { input: [0,0], output: [0] },
  { input: [0,1], output: [1] },
  { input: [1,0], output: [1] },
  { input: [1,1], output: [0] }
];

await network.evolve(trainingSet, {
  equal: true,
  error: 0.03
 });

That's all there is to it. The network does not know the data set, it is merely used to do fitness calculation.

function (genome) {
  return -genome.test(set, cost).error;
}

Where genome.test is a function that tests how well a genome is performing. See the function here.

This library has been developed in such a way that it is possible for anyone to use neuro-evolution. You are overthinking how it works. Neataptic has everything set up for you (fitness function, genetic algorithm, etc.).

developer239 commented 6 years ago

Thanks to the wagenaartje I got the exampel working. I believe that more javascript developers will be interested in this type of programming in the future.

If you are from the future and if you want to learn from working examples you can look here: https://github.com/developer239/neural-network-playground/tree/master/neatXOR