2017-10-07 18 views
1

編輯:我設法張羅幾個簡單的例子https://github.com/developer239/neural-network-playground突觸/ Neataptic簡約利落XOR解決方案

誰能幫我簡單利落身教網如何解決XOR或一些其他類似的問題?但是使用NEAT技術,我不需要指定訓練數據集?

使用Javascript:https://github.com/cazala/synaptichttps://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

我通過源代碼去,但有辦法多的東西怎麼回事。我瞭解大概的想法。不過,我不知道如何實施解決方案。

謝謝:)

回答

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

上有Neataptic的README.md一個例子。

// 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

對於動態解決方案的問題,必須實施自定義循環和適應度函數。