def main(): model_filename = 'network-regression_1.pt' network = FeedForward(n_input=config.n_input, n_output=config.n_output, n_neurons_per_layer=n_neurons_per_layer, n_hidden_layers=1) parameters = torch.load(f'./../../deep_learning/models/{model_filename}') network.load_state_dict(parameters) std = -3.1 genome = get_genome_from_standard_network(network, std=std) evaluation_engine = EvaluationStochasticEngine(testing=False) x, y_true, y_pred, kl_posterior = \ evaluation_engine.evaluate_genome(genome, n_samples=1, is_gpu=False, return_all=True) print() print(f'KL Posterior: {kl_posterior}') x = x.numpy() x = evaluation_engine.dataset.input_scaler.inverse_transform(x) y_pred = evaluation_engine.dataset.output_scaler.inverse_transform( y_pred.numpy()) y_true = evaluation_engine.dataset.output_scaler.inverse_transform( y_true.numpy()) # plot results plt.figure(figsize=(20, 20)) plt.plot(x, y_true, 'r*') plt.plot(x, y_pred, 'b*') plt.show() print(f'MSE: {mean_squared_error(y_true, y_pred) * 100} %')
def regression_problem(x): bias = x[:total_bias_params] weight = x[total_bias_params:] genome = generate_genome_with_hidden_units(n_input=config.n_input, n_output=config.n_output, n_hidden=n_neurons_per_layer) nodes = genome.node_genes connections = genome.connection_genes i = 0 for key, node in nodes.items(): node.bias_mean = bias[i] node.bias_std = bias[i+1] i += 2 i = 0 for key, connection in connections.items(): connection.weight_mean = weight[i] connection.weight_std = weight[i + 1] i += 2 evaluation_engine = EvaluationStochasticEngine(batch_size=50000) loss = \ evaluation_engine.evaluate_genome(genome=genome, n_samples=N_SAMPLES, return_all=False) print(loss) return loss
def _finetune_best(self): best_genome = None best_fitness = -100000 for specie_key, specie in self.species.items(): best_genome_at_specie = specie.get_fittest() if best_genome_at_specie.fitness > best_fitness: best_genome = best_genome_at_specie best_fitness = best_genome_at_specie.fitness best_genome_finetuned = self._finetune_genome(best_genome) self.config.parallel_evaluation = False evaluation_engine = EvaluationStochasticEngine() self.species_best_genome = evaluation_engine.evaluate( population={1: best_genome_finetuned})
def test_happy_path_miso(self): # Multiple-Input Single-Output self.config = create_configuration( filename='/classification-miso.json') self.config.parallel_evaluation = False self.config.n_processes = 1 genome = generate_genome_with_hidden_units( n_input=self.config.n_input, n_output=self.config.n_output) population = {1: genome} evaluation_engine = EvaluationStochasticEngine() population = evaluation_engine.evaluate(population=population) self.assertEqual(type(population.get(1).fitness), float)
def main(): model_filename = 'network-classification.pt' network = FeedForward(n_input=config.n_input, n_output=config.n_output, n_neurons_per_layer=n_neurons_per_layer, n_hidden_layers=1) parameters = torch.load(f'./../../deep_learning/models/{model_filename}') network.load_state_dict(parameters) std = -3.1 genome = get_genome_from_standard_network(network, std=std) # genome = generate_genome_with_hidden_units(2, 2, n_hidden=1) evaluation_engine = EvaluationStochasticEngine(testing=False) x, y_true, y_pred, kl_posterior = \ evaluation_engine.evaluate_genome(genome, n_samples=100, is_gpu=False, return_all=True) print() print(f'KL Posterior: {kl_posterior}') x = x.numpy() x = evaluation_engine.dataset.input_scaler.inverse_transform(x) y_true = y_true.numpy() # plot results y_pred = np.argmax(y_pred.numpy(), 1) df = pd.DataFrame(x, columns=['x1', 'x2']) df['y'] = y_pred x1_limit, x2_limit = evaluation_engine.dataset.get_separation_line() plt.figure() ax = sns.scatterplot(x='x1', y='x2', hue='y', data=df) ax.plot(x1_limit, x2_limit, 'g-', linewidth=2.5) plt.show() print('Confusion Matrix:') print(confusion_matrix(y_true, y_pred)) print(f'Accuracy: {accuracy_score(y_true, y_pred) * 100} %')
def __init__(self, report: EvolutionReport, notifier: Notifier, is_cuda: bool = False): self.report = report self.notifier = notifier self.is_cuda = is_cuda self.population_engine = PopulationEngine( stagnation_engine=Stagnation()) # self.speciation_engine = SpeciationEngine() self.speciation_engine = FixSpeciationEngine() self.evaluation_engine = EvaluationStochasticEngine() self.evolution_configuration = get_configuration() self.n_generations = self.evolution_configuration.n_generations self.population = None self.start_time = time.perf_counter()
def regression_problem_learn_from_nn(): # standard network network = FeedForward(n_input=config.n_input, n_output=config.n_output, n_neurons_per_layer=n_neurons_per_layer, n_hidden_layers=1) parameters = torch.load('./deep_learning/models/network.pt') network.load_state_dict(parameters) genome = prepare_genome(parameters) print(genome) evaluation_engine = EvaluationStochasticEngine(testing=False, batch_size=None) x, y_true, y_pred, kl_posterior = \ evaluation_engine.evaluate_genome(genome, n_samples=100, is_gpu=False, return_all=True) plt.figure(figsize=(20, 20)) plt.plot(x.numpy().reshape(-1), y_true.numpy().reshape(-1), 'b*') plt.plot(x.numpy().reshape(-1), y_pred.numpy().reshape(-1), 'r*') plt.show() print(f'KL Div - Posterior: {kl_posterior}')
class EvolutionEngine: def __init__(self, report: EvolutionReport, notifier: Notifier, is_cuda: bool = False): self.report = report self.notifier = notifier self.is_cuda = is_cuda self.population_engine = PopulationEngine( stagnation_engine=Stagnation()) # self.speciation_engine = SpeciationEngine() self.speciation_engine = FixSpeciationEngine() self.evaluation_engine = EvaluationStochasticEngine() self.evolution_configuration = get_configuration() self.n_generations = self.evolution_configuration.n_generations self.population = None self.start_time = time.perf_counter() @timeit def run(self): logger.info('Started evolutionary process') end_condition = 'normal' # try: # initialize population self.population = self.population_engine.initialize_population() self.speciation_engine.speciate(self.population, generation=0) self.population = self.evaluation_engine.evaluate( population=self.population) # report self.report.report_new_generation( generation=0, population=self.population, species=self.speciation_engine.species) for generation in range(1, self.n_generations + 1): self._run_generation(generation) elapsed = time.perf_counter() - self.start_time if elapsed > TIMEOUT_SECONDS: end_condition = 'timeout' break if self.evolution_configuration.is_fine_tuning: fine_tuner = FineTuner(species=self.speciation_engine.species, config=self.evolution_configuration, is_cuda=self.is_cuda, only_best=False) fine_tuner.run() best_genomes = fine_tuner.species_best_genome # best_genomes = self.evaluation_engine.evaluate(population=best_genomes) self.report.report_fine_tuning(best_genomes) self.evaluation_engine.close() self.report.generate_final_report(end_condition=end_condition)\ .persist_report() self.report.persist_logs() # self.notifier.send(str(self.report.get_best_individual())) self._send_final_message() logger.info('Finished evolutionary process') @timeit def _run_generation(self, generation): # create new generation's population self.population = self.population_engine.reproduce( species=self.speciation_engine.species, pop_size=self.population_engine.pop_size, generation=generation) # evaluate self.population = self.evaluation_engine.evaluate( population=self.population) # create new species based on new population self.speciation_engine.speciate(self.population, generation=generation) # generation report self.report.report_new_generation( generation=generation, population=self.population, species=self.speciation_engine.species) # schedule parameters if generation == self.evolution_configuration.generation_fix_architecture: logger.info('Fixing Architecture') self.evolution_configuration.fix_architecture = True def _send_final_message(self): self.notifier.send( f'-----------------------------------------------------------------\n' f'Dataset: {self.report.dataset}. \nCorrelation id: {self.report.correlation_id}.\n' f'Execution id: {self.report.report.execution_id}') self.notifier.send( f'Best Fitness: {self.report.best_individual.fitness}') if self.report.generic_text is not None: self.notifier.send(str(self.report.generic_text)) self.notifier.send(str(self.report.metrics_best))