def simulate_generations(self, num_generations, print_best): file_path = "csv/ESN_Results.csv" dw = DataWriter() dr = DataReader() dw.init_table(file_path) p = Predictor() mapping = dr.get_mapping() images = dr.get_images(112800, 28, 28) # 112800 images in data set scale_factor = 10 for i in range(num_generations): sum = 0 best_score = -100 best_accuracy = -100 best_net = [] engines = [] for net in self.networks: engine = [net, 0, 0] engines.append(engine) p.make_predictions(engines, mapping, images, scale_factor) for j in range(len(engines)): self.networks[j].fitness = engines[j][1] if engines[j][2] > best_score: best_score = engines[j][2] best_net = self.networks[j] if engines[j][1] > best_accuracy: best_accuracy = engines[j][1] avg_accuracy = self.avg_fitness(self.networks) # avg accuracy for j in range(len(engines)): self.networks[j].fitness = engines[j][2] # change fitness to score avg_score = self.avg_fitness(self.networks) # avg accuracy avg_size = self.avg_network_size() if print_best: best_net.show_net() print("-----------------------------------\t\t\t\t\t\t\n Generation " + str(i+1) + " results\n-----------------------------------\n", end='\n') print("Highest accuracy: " + str(best_accuracy*100) + "%\nHighest score: " + str(best_score**(1.0/scale_factor)) + "\nAverage accuracy: " + str(avg_accuracy*100) + "%\nAverage score: " + str(avg_score**(1.0/scale_factor)) + "\nNum species: " + str(len(self.species)) + "\nInnovs tried: " + str(self.networks[0].master_innov[0]) + "\nAverage connections per network: " + str(avg_size) + "\n") non_jit = self.construct_non_jit(best_net) pickle.dump(non_jit, open("neural_net.txt", "wb")) dw.write_row(file_path, [i+1, best_accuracy*100, avg_accuracy*100, best_score**(1.0/scale_factor), avg_score**(1.0/scale_factor), avg_size]) if i != num_generations-1: self.prepare_next_gen(math.ceil(self.pop_size/10)) print("\nStarting Generation " + str(i+2) + ": Species = " + str(len(self.species)) + ", Innovs = " + str(self.networks[0].master_innov[0]), end='\n') print("Finished simulation!")
p = Population(784, 47, 1, master_connection_dict, innov_list, 0) dr = DataReader() mapping = dr.get_mapping() images = dr.get_images( 112800, 28, 28) # 112800 images in data set, each image 28x28 pixels mp = p.networks[0] for q in range(200): print("Adding connection " + str(q + 1), end='\r') mp.add_connection() #mp.randomize_all_bias() print("\nStarting predictions...") engines = [[mp, 0, 0]] pr.make_predictions(engines, mapping, images, 1) base = engines[0][2] start_score = base for i in range(1000): print("\nEpoch " + str(i + 1)) pr.make_predictions(engines, mapping, images, 1) non_jit = p.construct_non_jit(mp) pickle.dump(non_jit, open("neural_net.txt", "wb")) #end_score = engines[0][2] #print("\nStart score = " + str(start_score) + ", End score = " + str(end_score)) #input("Done!") ##len(mp.connection_list) #start = 5000 #next start = previous end = 5000