def test_model_evaluation(self): model = NetworkUnit(params={'epochs': 1}) model.train() evaluator = Evaluator(model) print(evaluator.evaluate_agent())
def load_population(self, population_count): population_params = self.load_population_params() population = [NetworkUnit(params=dict(param)) for param in population_params] if len(population) < population_count: for i in range(population_count - len(population)): population.append(NetworkUnit()) return population
def test_breed(self): NU = NetworkUnit() NU2 = NetworkUnit(params={'lstm_layers_number': 4}) ga = GeneticAlgorithm() NU3 = ga.breed_parents(NU, NU2) print(f"Parent1: {NU}") print(f"Parent2: {NU2}") print(f"Child: {NU3}")
def breed_parents(male, female): print("Breeding!") m_params = male.params f_params = female.params new_params = {} param = random_choice(['lstm_layers_number', 'dense_layers_number', 'polynomial_exponent']) choice = randint(0, 3) if choice == 0: new_params[param] = m_params[param] elif choice == 1: new_params[param] = f_params[param] else: new_params[param] = int((f_params[param] + m_params[param]) / 2) list_param = random_choice(['lstm_neurons', 'dense_neurons']) working_param = m_params[list_param].copy() for idx in range(min(len(m_params[list_param]), len(f_params[list_param]))): choice = randint(0, 3) if choice == 0: working_param[idx] = m_params[list_param][idx] elif choice == 1: working_param[idx] = f_params[list_param][idx] else: working_param[idx] = int((f_params[list_param][idx] + m_params[list_param][idx]) / 2) new_params[list_param] = working_param child = NetworkUnit(params=new_params) print("Breed finished!") return child
def test_save_population(self): population = [NetworkUnit()] ga = GeneticAlgorithm() ga.save_population(population) from_file = ga.load_population() for idx, v in enumerate(from_file): assert str(v) == str(population[idx])
def test_mutate(self): population = [NetworkUnit()] to_mutate = [0] ga = GeneticAlgorithm() ga.mutate_selected(to_mutate, population) print(population[0])
def mutate_selected(to_mutate, parents): print("Mutation!") for idx in to_mutate: original_individual = parents[idx] original_params = original_individual.params param_to_mutate = random_choice(list(original_params.keys())) additional_param = None if param_to_mutate == 'lstm_neurons': additional_param = 'lstm_layers_number' elif param_to_mutate == 'lstm_layers_number': additional_param = 'lstm_neurons' elif param_to_mutate == 'dense_layers_number': additional_param = 'dense_neurons' elif param_to_mutate == 'dense_neurons': additional_param = 'dense_layers_number' new_params = original_params.copy() del new_params[param_to_mutate] if additional_param: del new_params[additional_param] parents[idx] = NetworkUnit(params=new_params) print("Mutation finished!")
def spawn_population(self, count): # Create a number of inviduals # return: a list of individuals return [NetworkUnit() for _ in range(count)]
def test_build_model(self): NU = NetworkUnit() print(NU.model.summary())
def test_randomiser(self): NU = NetworkUnit() print(NU)
def test_model_predictions(self): NU = NetworkUnit(params={'epochs': 1}) NU.train() choice = NU.get_choice_for_vector([300, 300, 123, 124, 424]) print(choice)
def test_model_training(self): NU = NetworkUnit(params={'epochs': 1}) NU.train()
def test_randomiser_set_params(self): NU = NetworkUnit(params={'dense_neurons': [42]}) #assert NU.params['epochs'] == 42 print(NU)