def codeepneat_xor_example(_):
    """
    This Example evolves a CoDeepNEAT population on the XOR problem for 100 generations, using fixed speciation for
    the modules and blueprints. Subsequently the best genome is trained for a final 100 epochs and its genotype and
    Tensorflow model are backed up.
    """
    # Set standard configuration specific to TFNE but not the neuroevolution process
    logging_level = logging.INFO
    config_file_path = './codeepneat_xor_fixed_example_config.cfg'
    backup_dir_path = './tfne_state_backups/'
    max_generations = 100
    max_fitness = None

    # Read in optionally supplied flags, changing the just set standard configuration
    if flags.FLAGS.logging_level is not None:
        logging_level = flags.FLAGS.logging_level
    if flags.FLAGS.config_file is not None:
        config_file_path = flags.FLAGS.config_file
    if flags.FLAGS.backup_dir is not None:
        backup_dir_path = flags.FLAGS.backup_dir
    if flags.FLAGS.max_generations is not None:
        max_generations = flags.FLAGS.max_generations
    if flags.FLAGS.max_fitness is not None:
        max_fitness = flags.FLAGS.max_fitness

    # Set logging, parse config
    logging.set_verbosity(logging_level)
    config = tfne.parse_configuration(config_file_path)

    # Initialize the environment and the specific NE algorithm
    environment = tfne.environments.XOREnvironment(weight_training=True,
                                                   config=config,
                                                   verbosity=logging_level)
    ne_algorithm = tfne.algorithms.CoDeepNEAT(config)

    # Initialize evolution engine and supply config as well as initialized NE algorithm and evaluation environment.
    engine = tfne.EvolutionEngine(ne_algorithm=ne_algorithm,
                                  environment=environment,
                                  backup_dir_path=backup_dir_path,
                                  max_generations=max_generations,
                                  max_fitness=max_fitness)

    # Start training process, returning the best genome when training ends
    best_genome = engine.train()
    print("Best genome returned by evolution:\n")
    print(best_genome)

    # Increase epoch count in environment for a final training of the best genome. Train the genome and then replay it.
    print("Training best genome for 100 epochs...\n")
    environment.epochs = 100
    environment.eval_genome_fitness(best_genome)
    environment.replay_genome(best_genome)

    # Serialize and save genotype and Tensorflow model to demonstrate serialization
    best_genome.save_genotype(save_dir_path='./best_genome_genotype/')
    best_genome.save_model(file_path='./best_genome_model/')
def codeepneat_xor_example(_):
    """"""
    # Set standard configuration specific to TFNE but not the neuroevolution process
    logging_level = logging.INFO
    config_file_path = './codeepneat_xor_example_config.cfg'
    backup_dir_path = './population_backups/'
    num_cpus = None
    num_gpus = None
    max_generations = 100
    max_fitness = None

    # Read in optionally supplied flags, changing the just set standard configuration
    if flags.FLAGS.logging_level is not None:
        logging_level = flags.FLAGS.logging_level
    if flags.FLAGS.config_file is not None:
        config_file_path = flags.FLAGS.config_file
    if flags.FLAGS.backup_dir is not None:
        backup_dir_path = flags.FLAGS.backup_dir
    if flags.FLAGS.num_cpus is not None:
        num_cpus = flags.FLAGS.num_cpus
    if flags.FLAGS.num_gpus is not None:
        num_gpus = flags.FLAGS.num_gpus
    if flags.FLAGS.max_generations is not None:
        max_generations = flags.FLAGS.max_generations
    if flags.FLAGS.max_fitness is not None:
        max_fitness = flags.FLAGS.max_fitness

    # Set logging, parse config
    logging.set_verbosity(logging_level)
    config = tfne.parse_configuration(config_file_path)

    # Set (not initialize) the environment and initialize the specific NE algorithm
    environment = tfne.environments.XOREnvironment
    ne_algorithm = tfne.CoDeepNEAT(config, environment)

    # Initialize evolution engine and supply config as well as initialized NE elements
    engine = tfne.EvolutionEngine(ne_algorithm=ne_algorithm,
                                  backup_dir_path=backup_dir_path,
                                  num_cpus=num_cpus,
                                  num_gpus=num_gpus,
                                  max_generations=max_generations,
                                  max_fitness=max_fitness)

    # Start training process, returning the best genome when training ends
    best_genome = engine.train()

    # Show string representation of best genome, visualize it and then save it
    print("Best Genome returned by evolution:\n")
    print(best_genome)
    best_genome.save_genotype(save_dir_path='./')
    best_genome.save_model(save_dir_path='./')
Ejemplo n.º 3
0
def test_codeepneat_5():
    # Create test config
    config = tfne.parse_configuration(
        os.path.dirname(__file__) + '/test_codeepneat_4_config.cfg')
    environment = tfne.environments.CIFAR10Environment(weight_training=True,
                                                       config=config)
    ne_algorithm = tfne.algorithms.CoDeepNEAT(config)

    # Start test
    engine = tfne.EvolutionEngine(ne_algorithm=ne_algorithm,
                                  environment=environment,
                                  backup_dir_path=tempfile.gettempdir(),
                                  max_generations=2,
                                  max_fitness=None)
    engine.train()

    # Sanity check state of the algorithm
    sanity_check_algorithm_state(ne_algorithm)