Ejemplo n.º 1
0
def generate(generations, population, nn_param_population, dataset):
    
    optimizer = Optimizer(nn_param_population)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info(i + 1, generations)

        # Train and get accuracy for networks.
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info(average_accuracy * 100)
        logging.info('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)
    print_networks(networks[:5])
Ejemplo n.º 2
0
def generate(generations, population_size, parameters_to_optimize, dataset):
    optimizer = Optimizer(parameters_to_optimize)
    networks = optimizer.create_population(population_size)

    for i in range(generations):
        # Train and get accuracy for networks.
        logging.info("Generation %d" % (i + 1))
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)
        average_loss = get_average_loss(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average accuracy: %.2f%%" %
                     (average_accuracy * 100))
        logging.info("Generation average loss: %.2f%%" % (average_loss))
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

# Print out the top network.
        print_best_networks(networks)
Ejemplo n.º 3
0
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)
    # network object = {paramters}
    # Evolve the generation.
    for i in range(generations):
        print("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset)
        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)
        # Print out the average accuracy each generation.
        print("Generation average: %.4f%%" % (average_accuracy * 100))
        print('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 4
0
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.
    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating
    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)
Ejemplo n.º 5
0
def generate(generations, population, nn_param_choices, datasets, nepochs):
    """
    Generate a network with the genetic algorithm.

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))
        print("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, datasets, nepochs)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        print_networks(networks[:5], "")
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:25], "Final Results")
    print("Output is log.txt")
Ejemplo n.º 6
0
    def evolve(self, x_train, y_train, x_val, y_val, x_test, y_test):
        """
        Takes data for traning and data for test and iterate thought generations to find parameters with lowest error
        :param x_train array: array with features for traning
        :param y_train array: array with real values for traning
        :param x_val array: numpy array with features for validation
        :param y_val array: numpy array with labels for validation
        :param x_test array: array with features for test
        :param y_test array: array with real values for test
        :return: None

        """
        optimizer = Optimizer(self._params)
        self._networks = list(optimizer.create_population(self._population))

        for i in range(self._generations - 1):
            self._train_networks(x_train, y_train, x_val, y_val, x_test,
                                 y_test)
            self._networks = optimizer.evolve(self._networks)

        self._networks = sorted(self._networks,
                                key=lambda x: x.accuracy,
                                reverse=True)
        self.best_params = self._networks[0]
        logger.info("best accuracy: {}, best params: {}".format(
            self.best_params.accuracy, self.best_params.network))
Ejemplo n.º 7
0
def generate(generations, population, nn_param_choices, file_name, train_set):
    logging.info("***Evolving %d generations with population %d***" %
                 (generations, population))
    """Generate a network with the genetic algorithm.
    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        file_name (str): project name
        train_set : Dataset to use for training/evaluating
    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)
    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))
        # Train and get accuracy for networks.
        train_networks(networks, file_name, train_set)
        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)
        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-' * 80)
        # Evolve, except on the last iteration.
        if i != generations - 1:
            logging.info(" # Do the evolution.")
            networks = optimizer.evolve(networks)
        else:
            logging.info(" # Do not do the evolution.", i, generations - 1)
    logging.info(" # Sorting our final population.")
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)
    logging.info(" # our final population is sorted.")
    # Print out the top 5 networks.
    print_networks(networks[:3])
    return networks[0]
Ejemplo n.º 8
0
    def evolve(self, model_path, output):
        """
        Takes data for traning and data for test and iterate thought generations to find parameters with lowest error
        :param x_train array: array with features for traning
        :param y_train array: array with real values for traning
        :param x_test array: array with features for test
        :param y_test array: array with real values for test
        :return: None

        """
        optimizer = Optimizer(self._params)
        self._networks = list(optimizer.create_population(self._population))

        for i in range(self._generations - 1):
            self._train_networks(model_path, output)
            self._networks = optimizer.evolve(self._networks)

        self._networks = sorted(self._networks,
                                key=lambda x: x.loss,
                                reverse=False)
        self.best_params = self._networks[0]

        self.dataframe = self.dataframe.append(
            {
                'name': self.best_params.name,
                'output_fixed': output,
                'loss': self.best_params.loss,
                'params': self.best_params.network
            },
            ignore_index=True)

        return self._networks[0], self.dataframe
Ejemplo n.º 9
0
def generate(generations, population, nn_param_choices):
    """Generate a network with the genetic algorithm.
    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating
    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get score for networks.
        train_networks(networks)

        # Get the average score for this generation.
        average_score = get_average_score(networks)

        # Print out the average score each generation.
        logging.info("Generation F1-score average: %.2f" % (average_score))
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.score, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 10
0
def generate(filename, generations, starting_gen, population, nn_param_choices,
             x, y):
    optimizer = Optimizer(nn_param_choices, population=population)
    logging.info(filename)
    if filename == "" or not os.path.isfile(filename):
        logging.info("***Randomly creating networks***")
        networks = optimizer.create_population(population)
        filename = "gen-0-pre.json"
        with open(filename, 'w') as outfile:
            json.dump([ob.__dict__ for ob in networks], outfile)
    else:
        logging.info("***Loading Evolution File (%s)***" % filename)
        with open(filename) as json_file:
            data = json.load(json_file)
        networks = optimizer.load_population(data)

        if "-post" in filename:
            networks = optimizer.evolve(networks)

    logging.info(' ')

    for i in range(starting_gen, generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))
        logging.info(' ')

        filename = "gen-" + str(i + 1) + "-pre.json"
        logging.info("Writing file: %s" % filename)
        with open(filename, 'w') as outfile:
            json.dump([ob.__dict__ for ob in networks], outfile)

        for n in networks:
            n.print_network()

        logging.info(' ')

        train_networks(networks, x, y)

        average_accuracy = get_average_accuracy(networks)

        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        for n in networks:
            n.print_network()

        logging.info(' ')
        logging.info('-' * 80)
        logging.info(' ')

        filename = "gen-" + str(i + 1) + "-post.json"
        with open(filename, 'w') as outfile:
            json.dump([ob.__dict__ for ob in networks], outfile)

        if i != generations - 1:
            networks = optimizer.evolve(networks)

    networks = sorted(networks, key=lambda x: x.accuracy, reverse=False)

    print_networks(networks[:5])
Ejemplo n.º 11
0
def generate(generations, population, nn_param_choices):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))

        print("\r***Doing generation %d of %d***" %
                     (i + 1, generations))


        counter = 0
        for network in networks:
            network.network['number'] = counter
            counter += 1

        # Train and get loss for networks.
        train_networks(networks)

        # Get the average loss for this generation.
        np_loss = get_np_losses(networks)

        # Print out the average loss each generation.
        logging.info("Generation average: %.3f" % (np.mean(np_loss)))
        logging.info("Generation maximum: %.3f" % (np.max(np_loss)))
        logging.info("Generation minimum: %.3f" % (np.min(np_loss)))
        logging.info('-'*80)

        print("\rGeneration average: %.3f" % (np.mean(np_loss)))
        print("Generation maximum: %.3f" % (np.max(np_loss)))
        print("Generation minimum: %.3f" % (np.min(np_loss)))
        print('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.loss, reverse=False)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 12
0
def   generate(generations, population, nn_param_choices):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)
    valid_list = split_to_valid_chunks(train_x,train_y)
    avg_acc_list = []
    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, valid_list[i%len(valid_list)])

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)
        avg_acc_list.append(average_accuracy)
        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    epocs_list = list(range(generations))
    plt.plot(epocs_list,avg_acc_list,'red')
    plt.xlabel("generations")
    plt.ylabel("accuracy")
    plt.savefig("accuracy.png")
    plt.clf()

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])


    # test(networks[0].network, networks[0].B,([0,0],[0,1],[1,0],[1,1]))
    test(networks[0].network, networks[0].B,test_x,test_y)
Ejemplo n.º 13
0
def generate(generations, population, nn_param_choices, dataset_dict):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)
    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))
        print("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset_dict)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)
        max_accuracy = get_max_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info("Generation max: %.2f%%" % (max_accuracy * 100))

        logging.info('-' * 80)
        print("***generation %d of %d*** score" % (i + 1, generations))
        print("Generation average: %.2f%%" % (average_accuracy * 100))
        print("Generation max: %.2f%%" % (max_accuracy * 100))
        print('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 3 networks.
    #print_networks(networks[:3])

    # Write the network to file
    if i == generations - 1:
        networks[0].train_final_net(dataset_dict)
        networks[0].WriteModelToFile()
        networks[0].WriteResToFile(dataset_dict, FILE_NAME)
Ejemplo n.º 14
0
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    tf.keras.utils.plot_model(model,
                              to_file='model.png',
                              show_shapes=False,
                              show_layer_names=True,
                              rankdir='TB',
                              expand_nested=False,
                              dpi=96)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))
        print("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-' * 80)
        print("Generation average: %.2f%%" % (average_accuracy * 100))
        print('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 15
0
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    print("The size of the population is {}".format(population))
    networks = optimizer.create_population(population)
    print("These are all the networks created for this population:")
    print(networks)

    # Evolve the generation.
    for i in range(generations):
        os.mkdir('./training_session_gen_{}'.format(i + 1))
        print("Doing generation: {}".format(i + 1))
        os.chdir('./training_session_gen_{}'.format(i + 1))
        logging.info("***Doing generation {} of {}***".format(
            i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset, i)

        # Get the average accuracy for this generation.
        average_accuracy, acc_list = get_average_accuracy(networks)
        print("This is the average_accuracy: {}".format(average_accuracy))

        # Print out the average accuracy each generation.
        logging.info("Generation average: {}".format(average_accuracy * 100))
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 16
0
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("")
        logging.info("")
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))

        print("\n\n\n**************************************")
        print("***Generation %d/%d" % (i + 1, generations))
        print("**************************************\n\n")

        # Train and get accuracy for networks.
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy, average_loss = get_averages(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%% (%.4f)" % (average_accuracy * 100, average_loss ))
        logging.info('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)
            copy_accuracies(networks)


    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 17
0
    def evolve(self, X, Y):
        """
        Takes data for traning and data for test and iterate thought generations to find parameters with lowest error
        :param x_train array: array with features for traning
        :param y_train array: array with real values for traning
        :param x_test array: array with features for test
        :param y_test array: array with real values for test
        :return: None

        """
        optimizer = Optimizer(self._params)
        self._networks = list(optimizer.create_population(self._population))

        for generation in range(self._generations - 1):
            self._train_networks(X, Y, generation)
            self._networks = optimizer.evolve(self._networks)

        self._networks = sorted(self._networks,
                                key=lambda x: x.accuracy,
                                reverse=True)
        self.best_params = self._networks[0]
Ejemplo n.º 18
0
def generate(generations, population, nn_param_choices):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            networks = optimizer.evolve(networks)  # Do the evolution.

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    logging.info('-' * 80)
    for network in networks[:5]:
        network.print_network()
Ejemplo n.º 19
0
def generate(generations, population, param_choices):
    """
    Generate a network with the genetic algorithm
    :param generations: Number times to evolve the population
    :param population: Number of network in each generation
    :param param_choices: parameter vhoices for networks
    :return:
    """
    optimizer = Optimizer(param_choices)
    networks = optimizer.create_population(population)

    # evolve the generation
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))

        # train and get accuracy for networks.
        train_networks(networks)
        print_networks(networks)

        # get the average accuracy for this generation
        average_accuracy = get_average_accuracy(networks)

        # print out the average accuracy for each generation
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-' * 80)

        # evolve, except on the last iteration
        if i != generations - 1:
            networks = optimizer.evolve(networks)

    # Sort our final population
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])
Ejemplo n.º 20
0
def generate(generations, population, nn_param_choices):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)
    games = [Game() for _ in range(4)]
    processes_manager = Manager()
    return_dict = processes_manager.dict()

    # Evolve the generation.
    for i in range(generations):
        print("**Doing generation %d of %d**" % (i + 1, generations))

        for j in range(0, len(networks), 4):
            p1 = Process(target=pool_score,
                         args=(networks[j].network, games[0], j, return_dict))
            p2 = Process(target=pool_score,
                         args=(networks[j + 1].network, games[1], j + 1,
                               return_dict))
            p3 = Process(target=pool_score,
                         args=(networks[j + 2].network, games[2], j + 2,
                               return_dict))
            p4 = Process(target=pool_score,
                         args=(networks[j + 3].network, games[3], j + 3,
                               return_dict))

            p1.start()
            p2.start()
            p3.start()
            p4.start()

            p1.join()
            p2.join()
            p3.join()
            p4.join()

        for k, v in return_dict.items():
            networks[k].score = v

        # Get the average accuracy for this generation.
        average_accuracy = reduce(lambda x, y: x + y,
                                  (n.score for n in networks)) / population

        # Print out the average accuracy each generation.
        print("Generation average: {}".format(average_accuracy))
        print('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)
            for network in networks:
                network._score = None

        if i % 10 == 0:
            with open('./checkpoint.json', 'w') as checkpoint:
                print("Writing checkpoint")
                json.dump([n.network for n in networks], checkpoint)
    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.score, reverse=True)

    # Print out the top 5 networks.
    print([n.network for n in networks[:5]])
Ejemplo n.º 21
0
def gen_population(generations, population, nn_param_choices):
    optimizer = Optimizer(nn_param_choices) #choices are unimportant
    networks = optimizer.create_population(population)
    # Evolve the generation.
    mnist = tf.contrib.learn.datasets.load_dataset("mnist")
    F = open("results_sorted_exp_10.txt", "w")
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))
            
         # Train and get accuracy for networks.
        accuracys, accuracys_mal = train_network(networks, "mnist", population, mnist)
#        accuracys, sess = test_with_weights(networks, accuracys_run, population, mnist)
        for j in range(population):
            
#            print("training in perm")
#            print(net.network)
#            acc, acc_mal = net.train("mnist")

            # get from our calculated
            
            acc = accuracys[j]
            acc_mal = accuracys_mal[j]
            #let the net know how it performed!
            networks[j].set_accuracies(acc, acc_mal)
            F.write("Net accuracy:%.2f\n" % acc)
            F.write("\n")
#            print(acc)
            F.write("Net accuracy mal:%.2f\n" % acc_mal)
            F.write("\n")
	    print("Net accuracy:%.2f\n" % acc)
            print("\n")
#            print(acc)
            print("Net accuracy mal:%.2f\n" % acc_mal)
            print("\n")
            connects = networks[j].get_conns()
            F.write("Connections in net:%.2f\n" % connects)
            F.write("\n")
	    print("Connections in net:%.2f\n" % connects)
            print("\n")
#            print(acc_mal)
            accuracys.append(acc)
            accuracys_mal.append(acc_mal)
         
        # Get the average accuracy for this generation.
        average_accuracy = np.mean(accuracys)
        average_accuracy_mal = np.mean(accuracys_mal)

        # Print out the average accuracy each generation.
        F.write("Generation average: %.2f\n" % (average_accuracy * 100))
        F.write("Generation average mal: %.2f\n" % (average_accuracy_mal * 100))
	print("Generation average: %.2f\n" % (average_accuracy * 100))
        print("Generation average mal: %.2f\n" % (average_accuracy_mal * 100))
#         logging.info('-'*80)

         # Evolve, except on the last iteration.
        if i != generations - 1:
             # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: 1/x.mal_accuracy, reverse=True)
    for net in networks[:5]:
        F.write("acc ")
        F.write('{}'.format(net.accuracy))
        F.write("\n")
        F.write("acc_mal ")
        F.write('{}'.format(net.mal_accuracy))
        F.write("\n")
        F.write("setup ")
        F.write('{}'.format(net.network))
        F.write("\n")
Ejemplo n.º 22
0
def generate(generations, population, nn_param_choices):
    """Generate a network with the genetic algorithm.
    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
    """

    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Prepare an array to record average and best losses.
    loss_t = np.empty((generations, ))
    loss_bt = np.empty((generations, ))

    data = get_stock_data()

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" % (i + 1, generations))

        # Train and get loss for networks.
        train_networks(networks, data)

        # Get and record the average loss for this generation.
        average_loss = get_average_loss(networks)
        loss_t[i] = average_loss

        # Get and record the best loss for this generation.
        best_loss = get_best_loss(networks)
        loss_bt[i] = best_loss

        # Print out the average and best loss of each generation.
        logging.info("Average Generation loss: %.3f" % (average_loss))
        logging.info('-' * 80)
        logging.info("Best Generation loss: %.3f" % (best_loss))
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != (generations - 1):
            # Do the evolution.
            networks = optimizer.evolve(networks)
        else:
            pass

    # Record elapsed time
    end_time = time.time()
    time_elapsed = end_time - start_time
    minutes, seconds = divmod(time_elapsed, 60)
    hours, minutes = divmod(minutes, 60)
    print(" Total running time was that of %d h : %d m : %d s" %
          (hours, minutes, seconds))

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.loss, reverse=False)

    # Print best network
    print "Best Performing Network:"
    print network_arch(networks[0].network)
    print "Network Loss:"
    print networks[0].loss

    # Save best network to hdf5 and JSON
    compile_model(networks[0].network).save("bestGeneticModel.hdf5")
    print("Saved best model to disk as HDF5")
    model_json = compile_model(networks[0].network).to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
    print("Saved best model to disk as JSON")

    # Print out the top 5 networks.
    print "Top 5 Best Performing Networks:"
    print_networks(networks[:5])

    # Make and print plot with average loss history
    plt.figure()
    plt.plot(np.arange(1, generations + 1, 1), loss_t)
    plt.xlabel('Generation')
    plt.ylabel('Average loss')
    plt.grid(True)

    plt.figure()
    plt.plot(np.arange(1, generations + 1, 1), loss_bt)
    plt.xlabel('Generation')
    plt.ylabel('Best loss')
    plt.grid(True)

    plt.show()
Ejemplo n.º 23
0
def generate(generations,
             population,
             nn_param_choices,
             config,
             x_train=None,
             y_train=None,
             x_test=None,
             y_test=None,
             use_cv=False):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    if use_cv:
        folds = []
        skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
        for train, test in skf.split(x_train, y_train):
            y_tn = to_categorical(y_train[train])
            y_tt = to_categorical(y_train[test])

            folds.append((x_train[train], y_tn, x_train[test], y_tt))

    # Evolve the generation.
    for i in range(generations):

        logging.info("***Doing generation %d of %d***" % (i + 1, generations))

        if use_cv:
            choice = random.randint(0, len(folds) - 1)
            x_train, y_train, x_test, y_test = folds[choice]

        # Train and get accuracy for networks.
        train_networks(networks,
                       config,
                       x_train=x_train,
                       y_train=y_train,
                       x_test=x_test,
                       y_test=y_test,
                       gen=i + 1)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-' * 80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)
        logging.info('Done generation %d' % (i + 1))

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5])