def test_logging_levels(self): old_level = logging.get_verbosity() logging.set_verbosity(logging.DEBUG) self.assertEquals(logging.get_verbosity(), logging.DEBUG) self.assertTrue(logging.level_debug()) self.assertTrue(logging.level_info()) self.assertTrue(logging.level_warning()) self.assertTrue(logging.level_error()) logging.set_verbosity(logging.INFO) self.assertEquals(logging.get_verbosity(), logging.INFO) self.assertFalse(logging.level_debug()) self.assertTrue(logging.level_info()) self.assertTrue(logging.level_warning()) self.assertTrue(logging.level_error()) logging.set_verbosity(logging.WARNING) self.assertEquals(logging.get_verbosity(), logging.WARNING) self.assertFalse(logging.level_debug()) self.assertFalse(logging.level_info()) self.assertTrue(logging.level_warning()) self.assertTrue(logging.level_error()) logging.set_verbosity(logging.ERROR) self.assertEquals(logging.get_verbosity(), logging.ERROR) self.assertFalse(logging.level_debug()) self.assertFalse(logging.level_info()) self.assertTrue(logging.level_error()) logging.set_verbosity(old_level)
def test_logging_levels(self): old_level = logging.get_verbosity() logging.set_verbosity(logging.DEBUG) self.assert_(logging.get_verbosity() == logging.DEBUG) self.assert_(logging.level_debug()) self.assert_(logging.level_info()) self.assert_(logging.level_warn()) self.assert_(logging.level_error()) logging.set_verbosity(logging.INFO) self.assert_(logging.get_verbosity() == logging.INFO) self.assert_(not logging.level_debug()) self.assert_(logging.level_info()) self.assert_(logging.level_warn()) self.assert_(logging.level_error()) logging.set_verbosity(logging.WARN) self.assert_(logging.get_verbosity() == logging.WARN) self.assert_(not logging.level_debug()) self.assert_(not logging.level_info()) self.assert_(logging.level_warn()) self.assert_(logging.level_error()) logging.set_verbosity(logging.ERROR) self.assert_(logging.get_verbosity() == logging.ERROR) self.assert_(not logging.level_debug()) self.assert_(not logging.level_info()) self.assert_(not logging.level_warn()) self.assert_(logging.level_error()) logging.set_verbosity(old_level)
def GetOrAdd(session: sql.orm.session.Session, model, defaults: typing.Dict[str, object] = None, **kwargs): """Instantiate a mapped database object. If the object is not in the database, add it. Note that no change is written to disk until commit() is called on the session. Args: session: The database session. model: The database table class. defaults: Default values for mapped objects. kwargs: The values for the table row. Returns: An instance of the model class, with the values specified. """ instance = session.query(model).filter_by(**kwargs).first() if not instance: params = {k: v for k, v in kwargs.items() if not isinstance(v, sql.sql.expression.ClauseElement)} params.update(defaults or {}) instance = model(**params) session.add(instance) if logging.level_debug(): logging.debug('New record: %s(%s)', model.__name__, ', '.join([f'{k}={v}' for k, v in params.items()])) return instance
def __init__(self, ne_algorithm, backup_dir_path, num_cpus=None, num_gpus=None, max_generations=None, max_fitness=None): """""" # Register parameters self.ne_algorithm = ne_algorithm self.max_generations = max_generations self.max_fitness = max_fitness print("Using Neuroevolution algorithm: {}".format(ne_algorithm.__class__.__name__)) print("Maximum number of generations to evolve the population: {}".format(max_generations)) print("Maximum fitness value to evolve population up to: {}".format(max_fitness)) # Initiate the Multiprocessing library ray and determine the actually available CPUs and GPUs as well as the # desired verbosity level for the genome evaluation ray.init(num_cpus=num_cpus, num_gpus=num_gpus) self.available_num_cpus = ray.available_resources()['CPU'] self.available_num_gpus = len(ray.get_gpu_ids()) self.verbosity = 0 if not logging.level_debug() else 1 print("Initialized the ray library with {} CPUs and {} GPUs".format(self.available_num_cpus, self.available_num_gpus)) # Initialize the environments through the registered algorithm with the determined parameters for parallel # evaluation and verbosity self.ne_algorithm.initialize_environments(num_cpus=self.available_num_cpus, num_gpus=self.available_num_gpus, verbosity=self.verbosity) # Create the directory into wich the training process will backup the population each generation self.backup_dir_path = os.path.abspath(backup_dir_path) if self.backup_dir_path[-1] != '/': self.backup_dir_path += '/' self.backup_dir_path += datetime.now(tz=datetime.now().astimezone().tzinfo).strftime("run_%Y-%b-%d_%H-%M-%S/") os.makedirs(self.backup_dir_path) print("Backing up population to directory: {}".format(self.backup_dir_path))
def DebugLogging() -> bool: """Return whether debug logging is enabled.""" return absl_logging.level_debug()
def evaluate_population(self, environment) -> (int, float): """ Evaluate the population by building the specified amount of genomes from each blueprint, all having randomly assigned specific modules for the inherent blueprint module species. Set the evaluated fitness of each blueprint and each module as the average fitness achieved by all genomes in which the respective member was invovled in. Return the generational counter as well as the achieved fitness of the best genome. @param environment: instance of the evaluation environment @return: tuple of generation counter and best fitness achieved by best genome """ # Create container collecting the fitness of the genomes that involve specific modules. Calculate the average # fitness of the genomes in which a module is involved in later and assign it as the module's fitness mod_fitnesses_in_genomes = dict() # Initialize population evaluation progress bar. Print notice of evaluation start genome_pop_size = len(self.pop.blueprints) * self.genomes_per_bp genome_eval_counter = 0 genome_eval_counter_div = round(genome_pop_size / 40.0, 4) print("\nEvaluating {} genomes in generation {}...".format( genome_pop_size, self.pop.generation_counter)) print_str = "\r[{:40}] {}/{} Genomes".format("", genome_eval_counter, genome_pop_size) sys.stdout.write(print_str) sys.stdout.flush() # Evaluate each blueprint independent from its species by building 'genomes_per_bp' genomes and averaging out # and assigning the resulting fitness for blueprint in self.pop.blueprints.values(): # Get the species ids of all species present in the blueprint currently evaluated bp_module_species = blueprint.get_species() # Create container collecting the fitness of the genomes that involve that specific blueprint. bp_fitnesses_in_genomes = list() for _ in range(self.genomes_per_bp): # Assemble genome by first uniform randomly choosing a specific module from the species that the # blueprint nodes are referring to. bp_assigned_modules = dict() for i in bp_module_species: chosen_module_id = random.choice(self.pop.mod_species[i]) bp_assigned_modules[i] = self.pop.modules[chosen_module_id] try: # Create genome, using the specific blueprint, a dict of modules for each species, the configured # output layers and input shape as well as the current generation genome_id, genome = self.enc.create_genome( blueprint, bp_assigned_modules, self.output_layers, self.input_shape, self.pop.generation_counter) except ValueError: # Catching build value error, occuring when the supplied layers and parameters do not result in a # valid TF model. See warning string. bp_id = blueprint.get_id() mod_spec_to_id = dict() for spec, mod in bp_assigned_modules.items(): mod_spec_to_id[spec] = mod.get_id() logging.warning( f"CoDeepNEAT tried combining the Blueprint ID {bp_id} with the module assignment " f"{mod_spec_to_id}, resulting in an invalid neural network model. Setting genome " f"fitness to 0.") # Setting genome id and genome to None as referenced later. Setting genome fitness to 0 to # discourage continued use of the blueprint and modules resulting in this faulty model. genome_id, genome = None, None genome_fitness = 0 else: # Now evaluate genome on registered environment and set its fitness # NOTE: As CoDeepNEAT implementation currently only supports 1 eval instance, automatically choose # that instance from the environment list genome_fitness = environment.eval_genome_fitness(genome) genome.set_fitness(genome_fitness) # Print population evaluation progress bar genome_eval_counter += 1 progress_mult = int( round(genome_eval_counter / genome_eval_counter_div, 4)) print_str = "\r[{:40}] {}/{} Genomes | Genome ID {} achieved fitness of {}".format( "=" * progress_mult, genome_eval_counter, genome_pop_size, genome_id, genome_fitness) sys.stdout.write(print_str) sys.stdout.flush() # Add newline after status update when debugging if logging.level_debug(): print("") # Assign the genome fitness to the blueprint and all modules used for the creation of the genome bp_fitnesses_in_genomes.append(genome_fitness) for assigned_module in bp_assigned_modules.values(): module_id = assigned_module.get_id() if module_id in mod_fitnesses_in_genomes: mod_fitnesses_in_genomes[module_id].append( genome_fitness) else: mod_fitnesses_in_genomes[module_id] = [genome_fitness] # Register genome as new best if it exhibits better fitness than the previous best if self.pop.best_fitness is None or genome_fitness > self.pop.best_fitness: self.pop.best_genome = genome self.pop.best_fitness = genome_fitness # Reset models, counters, layers, etc including in the GPU to avoid memory clutter from old models as # most likely only limited gpu memory is available tf.keras.backend.clear_session() # Average out collected fitness of genomes the blueprint was invovled in. Then assign that average fitness # to the blueprint bp_fitnesses_in_genomes_avg = round( statistics.mean(bp_fitnesses_in_genomes), 4) blueprint.set_fitness(bp_fitnesses_in_genomes_avg) # Average out collected fitness of genomes each module was invovled in. Then assign that average fitness to the # module for mod_id, mod_fitness_list in mod_fitnesses_in_genomes.items(): mod_genome_fitness_avg = round(statistics.mean(mod_fitness_list), 4) self.pop.modules[mod_id].set_fitness(mod_genome_fitness_avg) # Calculate average fitness of each module species and add to pop.mod_species_fitness_history for spec_id, spec_mod_ids in self.pop.mod_species.items(): spec_fitness_list = [ self.pop.modules[mod_id].get_fitness() for mod_id in spec_mod_ids ] spec_avg_fitness = round(statistics.mean(spec_fitness_list), 4) if spec_id in self.pop.mod_species_fitness_history: self.pop.mod_species_fitness_history[spec_id][ self.pop.generation_counter] = spec_avg_fitness else: self.pop.mod_species_fitness_history[spec_id] = { self.pop.generation_counter: spec_avg_fitness } # Calculate average fitness of each blueprint species and add to pop.bp_species_fitness_history for spec_id, spec_bp_ids in self.pop.bp_species.items(): spec_fitness_list = [ self.pop.blueprints[bp_id].get_fitness() for bp_id in spec_bp_ids ] spec_avg_fitness = round(statistics.mean(spec_fitness_list), 4) if spec_id in self.pop.bp_species_fitness_history: self.pop.bp_species_fitness_history[spec_id][ self.pop.generation_counter] = spec_avg_fitness else: self.pop.bp_species_fitness_history[spec_id] = { self.pop.generation_counter: spec_avg_fitness } return self.pop.generation_counter, self.pop.best_fitness