遗传算法训练神经网络

    科技2022-07-12  111

    遗传算法训练神经网络

    Many people use genetic algorithms as unsupervised algorithms, to optimize agents in certain environments, but do not realize that the implementation of neural networks into the agents as a possibility.

    许多人将遗传算法用作无监督算法,以在某些环境中优化代理,但没有意识到将神经网络实施到代理中的可能性。

    什么是遗传算法? (What are genetic algorithms?)

    Genetic Algorithms are a type of learning algorithm, that uses the idea that crossing over the weights of two good neural networks, would result in a better neural network.

    遗传算法是一种学习算法,其使用这样的思想:跨越两个良好的神经网络的权重将产生一个更好的神经网络。

    The reason that genetic algorithms are so effective is because there is no direct optimization algorithm, allowing for the possibility to have extremely varied results. Additionally, they often come up with very interesting solutions that often give valuable insight into the problem.

    遗传算法之所以如此有效,是因为没有直接的优化算法,从而可能产生极为不同的结果。 此外,他们经常想出非常有趣的解决方案,这些解决方案通常可以为问题提供有价值的见解。

    它们如何工作? (How do they work?)

    A set of random weights are generated. This is the neural network of the first agent. A set of tests are performed on the agent. The agent receives a score based on the tests. Repeat this several times to create a population.Select the top 10% of the population to be available to crossover. Two random parents are chosen from the top 10% and their weights are crossover. Every time a crossover occurs, there is a small chance of mutation: That is a random value that is in neither of the parent’s weights.

    生成一组随机权重。 这是第一个代理的神经网络。 在代理上执行了一组测试。 代理会根据测试获得分数。 重复几次以创建种群。选择种群的前10%以进行交叉。 从最高的10%中选择两个随机的父母,他们的权重是交叉的。 每次发生交叉时,发生突变的可能性都很小:这是一个随机值,它不受父母的影响。

    This process slowly optimizes the agent’s performance, as the agents slowly adapt to the environment.

    随着代理慢慢适应环境,此过程将缓慢优化代理的性能。

    的优点和缺点: (Advantages and Disadvantages:)

    Advantages:

    优点:

    Computationally not intensive

    计算不密集

    There are no linear algebra calculations to be done. The only machine learning calculations necessary are forward passes through the neural networks. Because of this, the system requirements are very broad, as compared to Deep Neural Networks.

    没有线性代数计算要完成。 唯一必要的机器学习计算是通过神经网络的正向传递。 因此,与深度神经网络相比,系统要求非常广泛。

    Adaptable

    适应性强

    One could adapt and insert many different tests and ways to manipulate the flexible nature of genetic algorithms. One could create a GAN within a Genetic algorithm, by making the agents propagate Generator networks, and the tests being the discriminators. This is a critical benefit, that persuades me that the use of genetic algorithm will be more widespread in the future.

    可以改编并插入许多不同的测试和方法来操纵遗传算法的灵活性。 可以通过使代理传播生成器网络并将测试作为鉴别器,从而在遗传算法内创建GAN。 这是一项至关重要的好处,这使我相信将来遗传算法的使用将更加广泛。

    Understandable

    可以理解的

    For normal neural networks, the learning patterns of the algorithm are enigmatic at best. For genetic algorithms it is easy to understand why some things come about: For example, when a genetic algorithm is given the Tic-Tac-Toe environment, certain recognizable strategies slowly develop. This is a large benefit, as the use of machine learning is to use technology to help us gain insight on important matters.

    对于普通的神经网络,该算法的学习模式充其量是神秘的。 对于遗传算法,很容易理解为什么会发生某些事情:例如,当给遗传算法提供Tic-Tac-Toe环境时,某些可识别的策略就会慢慢发展。 这是一个很大的好处,因为使用机器学习就是使用技术来帮助我们了解重要事项。

    Disadvantages:

    缺点:

    Takes a long period of time

    需要很长时间

    Unlucky crossovers and Mutations could result in a negative effect on the program’s accuracy, and therefore make the program slower to converge or reach a certain loss threshold.

    不幸的交叉和变异可能会对程序的准确性造成负面影响,从而使程序收敛或达到某个损失阈值的速度变慢。

    代码: (The Code:)

    Now that you have a reasonably comprehensive understanding of genetic algorithms, and its strengths and its limitations, I am now able to show you the program:

    既然您对遗传算法及其优势和局限性有了相当全面的了解,现在我可以向您展示该程序:

    import randomimport numpy as np

    These are only two dependencies for this program. This is as the neural network infrastructure implemented is a simple version that I created myself. To implement more complex networks, you can import keras or tensorflow.

    这些只是该程序的两个依赖项。 这是因为实现的神经网络基础结构是我自己创建的简单版本。 要实现更复杂的网络,可以导入keras或tensorflow。

    class genetic_algorithm: def execute(pop_size,generations,threshold,X,y,network): class Agent: def __init__(self,network):

    This is the creation of the class “genetic_algorithm” that holds all the functions that concerns the genetic algorithm and how it is supposed to function. The main function is the execute function, that takes pop_size,generations,threshold,X,y,network as parameters. pop_size is the size of the generated population, generations is the term for epochs, threshold is the loss value that you are satisfied with. X and y are for applications of genetic algorithms for labelled data. You can remove all instances of X and y for problems with no data or unlabelled data. Network is the network structure of the neural network.

    这是“ genetic_algorithm”类的创建,其中包含与遗传算法有关的所有功能以及其应如何发挥作用。 主要功能是执行功能,它以pop_size,generations,threshold,X,y,network为参数。 pop_size是生成的种群的大小,generation是时代的术语,threshold是您满意的损失值。 X和y用于标记数据的遗传算法。 对于没有数据或未标记数据的问题,您可以删除X和y的所有实例。 网络是神经网络的网络结构。

    class neural_network: def __init__(self,network): self.weights = [] self.activations = [] for layer in network: if layer[0] != None: input_size = layer[0] else: input_size = network[network.index(layer)-1][1] output_size = layer[1] activation = layer[2] self.weights.append(np.random.randn(input_size,output_size)) self.activations.append(activation) def propagate(self,data): input_data = data for i in range(len(self.weights)): z = np.dot(input_data,self.weights[i]) a = self.activations[i](z) input_data = a yhat = a return yhat self.neural_network = neural_network(network) self.fitness = 0

    This script describes the initialization of the weights and the propagation of the network for each agent’s neural network.

    该脚本描述了每个代理的神经网络的权重初始化和网络传播。

    def generate_agents(population, network): return [Agent(network) for _ in range(population)]

    This function creates the first population of agents that will be tested.

    此功能创建将要测试的第一批代理。

    def fitness(agents,X,y): for agent in agents: yhat = agent.neural_network.propagate(X) cost = (yhat - y)**2 agent.fitness = sum(cost) return agents

    As the example that I am using utilizes labelled data. The fitness function is merely calculating the MSE or the cost function for the predictions.

    作为示例,我使用的是带标签的数据。 适应度函数只是为预测计算MSE或成本函数。

    def selection(agents): agents = sorted(agents, key=lambda agent: agent.fitness, reverse=False) print('\n'.join(map(str, agents))) agents = agents[:int(0.2 * len(agents))] return agents

    This function mimics the theory of selection in evolution: The best survive while the others are left to die. In this case, their data is forgotten and is not used again.

    此功能模仿了进化中的选择理论:最佳选择生存,而其他选择则死掉。 在这种情况下,它们的数据将被遗忘并且不再使用。

    def unflatten(flattened,shapes): newarray = [] index = 0 for shape in shapes: size = np.product(shape) newarray.append(flattened[index : index + size].reshape(shape)) index += size return newarray

    To execute the crossover and mutation functions, the weights need to be flattened and unflattened into the original shapes.

    要执行交叉和变异功能,需要将权重展平和展平成原始形状。

    def crossover(agents,network,pop_size): offspring = [] for _ in range((pop_size - len(agents)) // 2): parent1 = random.choice(agents) parent2 = random.choice(agents) child1 = Agent(network) child2 = Agent(network) shapes = [a.shape for a in parent1.neural_network.weights] genes1 = np.concatenate([a.flatten() for a in parent1.neural_network.weights]) genes2 = np.concatenate([a.flatten() for a in parent2.neural_network.weights]) split = random.ragendint(0,len(genes1)-1)child1_genes = np.asrray(genes1[0:split].tolist() + genes2[split:].tolist()) child2_genes = np.array(genes1[0:split].tolist() + genes2[split:].tolist()) child1.neural_network.weights = unflatten(child1_genes,shapes) child2.neural_network.weights = unflatten(child2_genes,shapes) offspring.append(child1) offspring.append(child2) agents.extend(offspring) return agents

    The crossover function is one of the most complicated functions in the program. It generates two new “children” agents, whose weights that are replaced as a crossover of wo randomly generated parents. This is the process of creating the weights:

    交叉功能是程序中最复杂的功能之一。 它生成两个新的“子代”代理,其权重被替换为wo随机生成的父代的交叉。 这是创建权重的过程:

    Flatten the weights of the parents

    减轻父母的负担 Generate two splitting points

    产生两个分裂点 Use the splitting points as indices to set the weights of the two children agents

    使用拆分点作为索引来设置两个子代理的权重

    This is the full process of the crossover of agents.

    这是代理交叉的完整过程。

    def mutation(agents): for agent in agents: if random.uniform(0.0, 1.0) <= 0.1: weights = agent.neural_network.weights shapes = [a.shape for a in weights]flattened = np.concatenate([a.flatten() for a in weights]) randint = random.randint(0,len(flattened)-1) flattened[randint] = np.random.randn()newarray = [a ] indeweights = 0 for shape in shapes: size = np.product(shape) newarray.append(flattened[indeweights : indeweights + size].reshape(shape)) indeweights += size agent.neural_network.weights = newarray return agents

    This is the mutation function. The flattening is the same as the crossover function. Instead of splitting the points, a random point is chosen, to be replaced with a random value.

    这是突变功能。 展平与交叉功能相同。 代替分割点,而是选择随机点,以将其替换为随机值。

    for i in range(generations): print('Generation',str(i),':') agents = generate_agents(pop_size,network) agents = fitness(agents,X,y) agents = selection(agents) agents = crossover(agents,network,pop_size) agents = mutation(agents) agents = fitness(agents,X,y) if any(agent.fitness < threshold for agent in agents): print('Threshold met at generation '+str(i)+' !') if i % 100: clear_output() return agents[0]

    This is the last part of the execute function, that executes all the functions that have been defined.

    这是execute函数的最后一部分,它执行所有已定义的函数。

    X = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])y = np.array([[0, 1, 1, 0]]).Tnetwork = [[3,10,sigmoid],[None,1,sigmoid]]ga = genetic_algorithmagent = ga.execute(100,5000,0.1,X,y,network)weights = agent.neural_network.weightsagent.fitnessagent.neural_network.propagate(X)

    This executes the whole genetic algorithm. For the network variable, each nested list holds the input neuron numbers, the output neuron numbers and the activation functions. The execute function returns the best agent.

    这将执行整个遗传算法。 对于网络变量,每个嵌套列表都包含输入神经元编号,输出神经元编号和激活函数。 execute函数返回最佳代理。

    感谢您阅读我的文章! (Thank you for reading my article!)

    翻译自: https://towardsdatascience.com/using-genetic-algorithms-to-train-neural-networks-b5ffe0d51321

    遗传算法训练神经网络

    相关资源:Python-SnakeAI使用遗传算法训练一个神经网络来玩贪吃蛇
    Processed: 0.013, SQL: 8