def generate(self): logging.info( "***generate(generations, population, all_possible_genes, dataset)***" ) t_start = datetime.datetime.now() t = time.time() evolver = Evolver(self.params) genomes = evolver.create_population(self.population) ofile = open(self.path + '/result.csv', "w") writer = csv.writer(ofile, delimiter=',') table_head = list() for p in self.params: table_head.append(str(p)) table_head.append("accuracy") row = table_head writer.writerow(row) # Evolve the generation. for i in range(self.generations): logging.info("***Now in generation %d of %d***" % (i + 1, self.generations)) self.print_genomes(genomes) # Train and get accuracy for networks/genomes. self.train_genomes(genomes, writer) # Get the average accuracy for this generation. average_accuracy = self.get_average_accuracy(genomes) # Print out the average accuracy each generation. logging.info("Generation average: %.2f%%" % (average_accuracy * 100)) logging.info('-' * 80) # Evolve, except on the last iteration. if i != self.generations - 1: genomes = evolver.evolve(genomes) # Sort our final population according to performance. genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True) # Print out the top 5 networks/genomes. self.print_genomes(genomes[:5]) ofile.close() total = time.time() - t m, s = divmod(total, 60) h, m = divmod(m, 60) d, h = divmod(h, 24) t_stop = datetime.datetime.now() file = open(self.path + '/total_time.txt', 'w') file.write('Start : ' + str(t_start) + '\n') file.write('Stop : ' + str(t_stop) + '\n') file.write('Total :' + "%d days, %d:%02d:%02d" % (d, h, m, s) + '\n') file.close()
def generate(generations, population, all_possible_genes, dataset): """Generate a network with the genetic algorithm. Args: generations (int): Number of times to evolve the population population (int): Number of networks in each generation all_possible_genes (dict): Parameter choices for networks dataset (str): Dataset to use for training/evaluating """ logging.info( "***generate(generations, population, all_possible_genes, dataset)***") evolver = Evolver(all_possible_genes) genomes = evolver.create_population(population) # Evolve the generation. for i in range(generations): logging.info("***Now in generation %d of %d***" % (i + 1, generations)) print_genomes(genomes) # Train and get accuracy for networks/genomes. train_genomes(genomes, dataset) # Get the average accuracy for this generation. average_accuracy = get_average_accuracy(genomes) # Print out the average accuracy each generation. logging.info("Generation average: %.2f%%" % (average_accuracy * 100)) logging.info('-' * 80) #----------- # Evolve, except on the last iteration. if i != generations - 1: # Evolve! genomes = evolver.evolve(genomes) # Sort our final population according to performance. genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True) # Print out the top 5 networks/genomes. print_genomes(genomes[:5])
def generate(generations, population, all_possible_genes, dataset): """Generate a network with the genetic algorithm. Args: generations (int): Number of times to evolve the population population (int): Number of networks in each generation all_possible_genes (dict): Parameter choices for networks dataset (str): Dataset to use for training/evaluating """ logging.info("***generate(generations, population, all_possible_genes, dataset)***") evolver = Evolver(all_possible_genes) genomes = evolver.create_population(population) # Evolve the generation. for i in range( generations ): logging.info("***Now in generation %d of %d***" % (i + 1, generations)) print_genomes(genomes) # Train and get accuracy for networks/genomes. train_genomes(genomes, dataset) # Get the average accuracy for this generation. average_accuracy = get_average_accuracy(genomes) # Print out the average accuracy each generation. logging.info("Generation average: %.2f%%" % (average_accuracy * 100)) logging.info('-'*80) #----------- # Evolve, except on the last iteration. if i != generations - 1: # Evolve! genomes = evolver.evolve(genomes) # Sort our final population according to performance. genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True) # Print out the top 5 networks/genomes. print_genomes(genomes[:5])