如何查询pyGAD GA实例的最优解?

How do I query the best solution of a pyGAD GA instance?

我使用 pyGAD Python 库提供的遗传算法实现训练了大量神经网络。 到目前为止我写的代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pygad.gann
import time
import pickle

ret = -1
n_sect = 174
population_size = 500
num_parents_mating = 4 
num_generations = 1000
mutation_percent = 5
parent_selection_type = "rank"
crossover_type = "two_points"
mutation_type = "random"
keep_parents = 1
init_range_low = -2
init_range_high = 5
n_div = 15

data = pd.read_csv("delta_results/sub_delta_{}.csv".format(n_sect), index_col=0)
data.index = pd.to_datetime(data.index)
data = list(data["Delta"])

function_inputs = np.array([data[i:i+n_div][:ret] for i in range(0, len(data), n_div)])
required_outputs = np.array([[data[i:i+n_div][ret]] for i in range(0, len(data), n_div)])

input_layer_size = function_inputs.shape[1]
n_hidden_layers = 2
hidden_layer_1_size = input_layer_size - 2
hidden_layer_2_size = input_layer_size - 4
output_layer_size = 1

population = pygad.gann.GANN(
    num_solutions=population_size, 
    num_neurons_input=input_layer_size, 
    num_neurons_output=output_layer_size, 
    num_neurons_hidden_layers=[hidden_layer_1_size, hidden_layer_2_size], # 2 Hidden Layers
    hidden_activations=["relu", "relu"],
    output_activation="None"
)

population_vectors = pygad.gann.population_as_vectors(population_networks=population.population_networks)

initial_population = population_vectors.copy()

def normalize(x):
    return x/np.linalg.norm(x, ord=2, axis=0, keepdims=True)

def fitness(solution, solution_index):
    prediction = pygad.nn.predict(last_layer=population.population_networks[solution_index], data_inputs=function_inputs, problem_type="regression")
    prediction = np.array(prediction)
    error = (prediction+0.0001)-required_outputs
    fitness = np.nan_to_num((np.abs(error)**(-2))).astype(np.float64)
    solution_fitness = np.sum(normalize(fitness))
    return solution_fitness

def on_generation(population_instance):
    global population
    population_matrices = pygad.gann.population_as_matrices(population_networks=population.population_networks, population_vectors=population_instance.population)
    population.update_population_trained_weights(population_trained_weights=population_matrices)

population_instance = pygad.GA(
    num_generations=num_generations,
    num_parents_mating=num_parents_mating,
    initial_population=initial_population,
    fitness_func=fitness,
    mutation_percent_genes=mutation_percent,
    init_range_low=init_range_low,
    init_range_high=init_range_high,
    parent_selection_type=parent_selection_type,
    crossover_type=crossover_type,
    mutation_type=mutation_type,
    keep_parents=keep_parents,
    on_generation=on_generation
)

saved_population = pygad.load(filename=".../population_data_v2")
best_solution = saved_population.best_solution()
print("Population Best Solution Info:\n| Attributes:\n{}\n| Fitness: {}\n| Solution Index: {}".format(best_solution[0], best_solution[1], best_solution[2]))
saved_population.plot_result()

遗传算法为 运行 后,我将种群数据保存到名为 population_data_v2.pkl 的文件中(上面未显示)- 文件创建并成功保存。

但是,一旦我打开文件,我不知道如何从人口中找到最好的神经网络的信息。

我得到的只是解决方案 (best_solution[0]) 的 nd.numpy.array,我不知道如何从中查询,或者如何传入函数输入并查看最佳预测解决方案是。

如有任何帮助,我们将不胜感激!

感谢使用 PyGAD

我看到您正确地构建了示例。您可以通过简单的 3 个步骤轻松使用最佳解决方案进行预测。

请注意,在每一代之后,population 属性由最新的种群更新。这意味着在 PyGAD 完成所有世代之后,最后的种群将保存在 population 属性中。

第 1 步

使用 pygad.load() 函数加载保存的模型后,就像您在适应度函数中所做的那样,您可以使用 population 属性恢复网络的权重,如下所示:

population_matrices = pygad.gann.population_as_matrices(population_networks=population.population_networks, population_vectors=saved_population.population)
population.update_population_trained_weights(population_trained_weights=population_matrices)

第 2 步

best_solution() 方法 returns 3 输出其中第三个代表最佳解决方案的索引。您可以使用它进行如下预测:

best_solution = saved_population.best_solution()
prediction = pygad.nn.predict(last_layer=population.population_networks[best_solution[2]], data_inputs=function_inputs, problem_type="regression")

步骤 3

最后,您可以打印预测值:

prediction = np.array(prediction)
print("Prediction of the best solution: {pred}".format(pred=prediction))

完整代码

在上述讨论中,以下是可让您根据最佳解决方案进行预测的代码:

population_matrices = pygad.gann.population_as_matrices(population_networks=population.population_networks, population_vectors=saved_population.population)
population.update_population_trained_weights(population_trained_weights=population_matrices)

best_solution = saved_population.best_solution()
prediction = pygad.nn.predict(last_layer=population.population_networks[best_solution[2]], data_inputs=function_inputs, problem_type="regression")

prediction = np.array(prediction)
print("Prediction of the best solution: {pred}".format(pred=prediction))

如果出现问题,请告诉我。

再次感谢您使用PyGAD