2017-10-07 3 views
1

EDIT:私は簡単な例https://github.com/developer239/neural-network-playgroundシナプス/ NeatapticシンプルNEAT XORソリューション

のカップルを一緒に得ることができた誰もがXORまたはいくつかの他の同様の問題を解決するためにどのようにネットを教える簡単なきちんとした例で私を助けてもらえますか?しかし、トレーニングデータセットを指定する必要がないように、NEATテクニックを使用していますか?

使用Javascriptを:https://github.com/cazala/synapticまたはhttps://github.com/wagenaartje/neataptic

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

これは非常に参考になります。同じteqniqueは、ここで使用されている:

https://github.com/ivanseidel/IAMDinosaur

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

は、私はソースコードを行ってきましたが、多くのものが起こってする方法があります。私は一般的な考えを理解しています。しかし、私はどのようにソリューションを実装するか分かりません。

ありがとうございました:) Neatapticの README.md上の例であり

答えて

0

私は自分で解決策を書くことができました。 https://github.com/developer239/neural-network-playground/tree/master/neatXOR

ドキュメントの解決策との主な違いは、genetic.jsでは学習プロセスを動的に変更できることです。

これは、エントリ・ファイルです:

const genetic = require('./genetic') 


genetic.generateRandomPopulation() 

for (let iteration = 0; iteration < 1000; iteration += 1) { 
    genetic.live() 
    genetic.evolve() 
} 

const genom = genetic.neat.population[0] 

console.log(` 
    Result for genom with index 0 in the newest population. Note that selection/mutation happened 
    after we called last evolve function so this is not necessarily the best genome in the population. 

    [0, 0] = ${genom.activate([0, 0])} (should be 0) 
    [1, 1] = ${genom.activate([1, 1])} (should be 0) 
    [0, 1] = ${genom.activate([0, 1])} (should be 1) 
    [1, 0] = ${genom.activate([1, 0])} (should be 1) 
`) 

これはgenetic.jsファイルです:

const { Neat, architect } = require('neataptic') 


module.exports = { 
    neat: null, // https://wagenaartje.github.io/neataptic/docs/neat/ 
    possibleInputs: [ 
    [0, 0], // expected output 0 
    [1, 1], // expected output 0 
    [0, 1], // expected output 1 
    [1, 0], // expected output 1 
    ], 
    generateRandomPopulation: function() { 
    this.neat = new Neat(
     2, // number of inputs 
     1, // number of outputs 
     null, // fitnessFunction - in this example we are calculating fitness inside live method 
     { 
     elitism: 5, // this sets how many genomes in population will be passed into next generation without mutation https://www.researchgate.net/post/What_is_meant_by_the_term_Elitism_in_the_Genetic_Algorithm 
     mutationRate: 0.3, // sets the mutation rate. If set to 0.3, 30% of the new population will be mutated. Default is 0.3 
     network: // https://wagenaartje.github.io/neataptic/docs/architecture/network/ 
      new architect.Random(
      2, 
      3, 
      1, 
     ), 
     }, 
    ) 
    }, 
    // the closer the output gets to expectedOutput the better 
    // note that optimal fitness in this example is 0 neural network seems to work fine though 
    calculateFitness: function (expectedOutput, output) { 
    let closeCount = Math.abs(expectedOutput - output) 
    let fitness = closeCount * -1 
    return fitness 
    }, 
    live: function() { 
    // increment generation index 
    this.neat.generation += 1 

    // loop through each genome 
    for (let genomeIndex in this.neat.population) { 
     const possibleInputs = this.possibleInputs 
     const genome = this.neat.population[genomeIndex] 
     genome.score = 0 

     // loop through each input 
     for (let i = 0; i < possibleInputs.length; i += 1) { 
     let input = possibleInputs[i] 
     // test each input 
     let output = genome.activate(input)[0] 

     // calculate fitness for each output 
     // we have 4 different inputs so the total score is sum of 4 different fitness values 
     if (i <= 1) { 
      genome.score += this.calculateFitness(0, output) 
     } else { 
      genome.score += this.calculateFitness(1, output) 
     } 
     } 
    } 
    }, 
    evolve: function() { 
    const neat = this.neat 
    console.log(`[generation ${neat.generation}] Average score: ${neat.getAverage()} (the closer to zero the better)`) 

    // sort by genome.score in descending order 
    neat.sort() 

    // our new population will be here 
    let newPopulation = [] 

    // we want to push neat.elitism number of best genomes into the new population automatically 
    for (let i = 0; i < neat.elitism; i++) { 
     newPopulation.push(neat.population[i]) 
    } 

    // we want to get offspring from the current population and push it into the new population 
    for (let i = 0; i < neat.popsize - neat.elitism; i++) { 
     newPopulation.push(neat.getOffspring()) 
    } 

    // set new population 
    neat.population = newPopulation 
    // mutate the population 
    neat.mutate() 
    }, 
} 
1

// 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 
}); 

Neatapticにはすべてが組み込まれているため、データセットはすべて提供する必要があります。これがどのように設定されたかについての詳しい情報が必要な場合は、read this article

ダイナミックソリューションの問題については、カスタムループとフィットネス機能を実装する必要があります。

関連する問題