output = net.activate(inputs) error += (output[0] - s) ** 2 return -(error / (N * num_tests)) ** 0.5 # Demonstration of how to add your own custom activation function. def sinc(x): return 1.0 if x == 0 else math.sin(x) / x # This sinc function will be available if my_sinc_function is included in the # config file activation_functions option under the pheotype section. # Note that sinc is not necessarily useful for this example, it was chosen # arbitrarily just to demonstrate adding a custom activation function. activation_functions.add('my_sinc_function', sinc) def run(): local_dir = os.path.dirname(__file__) pop = population.Population(os.path.join(local_dir, 'nn_config')) pe = parallel.ParallelEvaluator(4, eval_fitness) pop.run(pe.evaluate, 1000) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Display the most fit genome. print('\nBest genome:') winner = pop.statistics.best_genome() print(winner)
def test_concurrent_nn(): """This is a stripped-down copy of the `memory` example.""" # num_tests is the number of random examples each network is tested against. num_tests = 16 # N is the length of the test sequence. N = 4 def eval_fitness(genomes): for g in genomes: net = nn.create_recurrent_phenotype(g) error = 0.0 for _ in range(num_tests): # Create a random sequence, and feed it to the network with the # second input set to zero. seq = [random.choice((0, 1)) for _ in range(N)] net.reset() for s in seq: inputs = [s, 0] net.activate(inputs) # Set the second input to one, and get the network output. for s in seq: inputs = [0, 1] output = net.activate(inputs) error += (output[0] - s)**2 g.fitness = -(error / (N * num_tests))**0.5 # Demonstration of how to add your own custom activation function. def sinc(x): return 1.0 if x == 0 else math.sin(x) / x # This sinc function will be available if my_sinc_function is included in the # config file activation_functions option under the pheotype section. # Note that sinc is not necessarily useful for this example, it was chosen # arbitrarily just to demonstrate adding a custom activation function. activation_functions.add('my_sinc_function', sinc) local_dir = os.path.dirname(__file__) pop = population.Population(os.path.join(local_dir, 'recurrent_config')) pop.run(eval_fitness, 10) # Visualize the winner network and plot/log statistics. # visualize.draw_net(winner, view=True, filename="nn_winner.gv") # visualize.draw_net(winner, view=True, filename="nn_winner-enabled.gv", show_disabled=False) # visualize.draw_net(winner, view=True, filename="nn_winner-enabled-pruned.gv", show_disabled=False, prune_unused=True) # visualize.plot_stats(pop.statistics) # visualize.plot_species(pop.statistics) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics) winner = pop.statistics.best_genome() repr(winner) str(winner) for g in winner.node_genes: repr(g) str(g) for g in winner.conn_genes: repr(g) str(g)
def test_concurrent_nn(): """This is a stripped-down copy of the `memory` example.""" # num_tests is the number of random examples each network is tested against. num_tests = 16 # N is the length of the test sequence. N = 4 def eval_fitness(genomes): for g in genomes: net = nn.create_recurrent_phenotype(g) error = 0.0 for _ in range(num_tests): # Create a random sequence, and feed it to the network with the # second input set to zero. seq = [random.choice((0, 1)) for _ in range(N)] net.reset() for s in seq: inputs = [s, 0] net.activate(inputs) # Set the second input to one, and get the network output. for s in seq: inputs = [0, 1] output = net.activate(inputs) error += (output[0] - s) ** 2 g.fitness = -(error / (N * num_tests)) ** 0.5 # Demonstration of how to add your own custom activation function. def sinc(x): return 1.0 if x == 0 else math.sin(x) / x # This sinc function will be available if my_sinc_function is included in the # config file activation_functions option under the pheotype section. # Note that sinc is not necessarily useful for this example, it was chosen # arbitrarily just to demonstrate adding a custom activation function. activation_functions.add('my_sinc_function', sinc) local_dir = os.path.dirname(__file__) pop = population.Population(os.path.join(local_dir, 'recurrent_config')) pop.run(eval_fitness, 10) # Visualize the winner network and plot/log statistics. # visualize.draw_net(winner, view=True, filename="nn_winner.gv") # visualize.draw_net(winner, view=True, filename="nn_winner-enabled.gv", show_disabled=False) # visualize.draw_net(winner, view=True, filename="nn_winner-enabled-pruned.gv", show_disabled=False, prune_unused=True) # visualize.plot_stats(pop.statistics) # visualize.plot_species(pop.statistics) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics) winner = pop.statistics.best_genome() repr(winner) str(winner) for g in winner.node_genes: repr(g) str(g) for g in winner.conn_genes: repr(g) str(g)
output = net.activate(inputs) error += (output[0] - s) ** 2 return -(error / (N * num_tests)) ** 0.5 # Demonstration of how to add your own custom activation function. def sinc(x): return 1.0 if x == 0 else math.sin(x) / x # This sinc function will be available if my_sinc_function is included in the # config file activation_functions option under the pheotype section. # Note that sinc is not necessarily useful for this example, it was chosen # arbitrarily just to demonstrate adding a custom activation function. activation_functions.add('my_sinc_function', sinc) def run(): local_dir = os.path.dirname(__file__) pop = population.Population(os.path.join(local_dir, 'nn_config')) pe = parallel.ParallelEvaluator(4, eval_fitness) pop.run(pe.evaluate, 1000) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Display the most fit genome. print('\nBest genome:') winner = pop.statistics.best_genome() print(winner)
error += (output[0] - s) ** 2 return -(error / (N * num_tests)) ** 0.5 # Demonstration of how to add your own custom activation function. def sinc(x): return 1.0 if x == 0 else math.sin(x) / x # This sinc function will be available if my_sinc_function is included in the # config file activation_functions option under the pheotype section. # Note that sinc is not necessarily useful for this example, it was chosen # arbitrarily just to demonstrate adding a custom activation function. activation_functions.add("my_sinc_function", sinc) def run(): local_dir = os.path.dirname(__file__) pop = population.Population(os.path.join(local_dir, "nn_config")) pe = parallel.ParallelEvaluator(4, eval_fitness) pop.run(pe.evaluate, 1000) # Log statistics. statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics) print("Number of evaluations: {0}".format(pop.total_evaluations))
evolve() c += 1 for i, g in enumerate(group): genomes[g].fitness += countSpecies(gameOfLife.world['space'], i) #results of fights define the fitness for g in genomes: g.fitness = g.fitness / num_runs #adding elu to activation functions def elu(x): return x if x < 0 else np.exp(x) - 1 activation_functions.add('elu', elu) def main(): print("Starting...") pop = population.Population( os.path.join(os.path.dirname(__file__), 'nn_config')) #HINT change checkpoints for new try or reloading pop.load_checkpoint( os.path.join(os.path.dirname(__file__), 'checkpoints/popv1.cpt')) pop.run(eval_fitness_internalfight, 5) pop.save_checkpoint( os.path.join(os.path.dirname(__file__), 'checkpoints/popv1.cpt')) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics)
# divide training results by traincount, bc of padding etc this has to be done # fitness of all below median is set to zero trainfitness = trainfitness/traincount trainfitness[trainfitness < np.median(trainfitness)] = 0 # results of fights define the fitness for k, g in enumerate(allgenomes): g.fitness = trainfitness[k] # adding elu to activation functions def elu(x): return x if x < 0 else np.exp(x) - 1 activation_functions.add('elu', elu) def train(checkpoint, config): print("Starting...") pop = population.Population(config) # HINT change checkpoints for new try or reloading try: pop.load_checkpoint(checkpoint) except: print("Checkpoint not found, starting from scratch: ", checkpoint) trainfunc = partial(eval_fitness_internalfight, num_runs=4, steplength=100, x=16, y=16) pop.run(trainfunc, 20) pop.save_checkpoint(checkpoint) statistics.save_stats(pop.statistics)