遗传算法训练神经⽹络_使⽤遗传算法训练神经⽹络
遗传算法训练神经⽹络
Many people u genetic algorithms as unsupervid 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 us 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 becau 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 t of random weights are generated. This is the neural network of the first agent. A t of tests are performed on the agent. The agent receives a score bad on the tests. Repeat this veral times to create a population.Select the top 10% of the population to be available to crossover. Two random parents are chon 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 pass through the neural networks. Becau of this, the system requirements are very broad, as compared to Deep Neural Networks.
没有线性代数计算要完成。 唯⼀必要的机器学习计算是通过神经⽹络的正向传递。 因此,与深度神经⽹络相⽐,系统要求⾮常⼴泛。
Adaptable
适应性强
One could adapt and inrt 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 u 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 i
s a large benefit, as the u of machine learning is to u 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 random
拔剑而起import numpy as np
The are only two dependencies for this program. This is as the neural network infrastructure implemented is a simple version that I created mylf. 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__(lf,network):
This is the creation of the class “genetic_algorithm” that holds all the functions that concerns the genetic algorithm and how it is suppod 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__(lf,network):
lf.weights = []
lf.activations = []
for layer in network:
if layer[0] != None:
input_size = layer[0]
el:
input_size = network[network.index(layer)-1][1]
output_size = layer[1]
activation = layer[2]
lf.weights.append(np.random.randn(input_size,output_size))
lf.activations.append(activation)
def propagate(lf,data):
input_data = data
for i in range(len(lf.weights)):
z = np.dot(input_data,lf.weights[i])
a = lf.activations[i](z)
input_data = a
yhat = a
return yhat
lf.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 = ural_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 lection(agents):
agents = sorted(agents, key=lambda agent: agent.fitness, rever=Fal)
观赏虾怎么养
print('\n'.join(map(str, agents)))
agents = agents[:int(0.2 * len(agents))]梦见钓了好多鱼是什么意思
return agents
This function mimics the theory of lection in evolution: The best survive while the others are left to die. In this ca, their data is forgotten and is not ud 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 ural_network.weights]
genes1 = np.concatenate([a.flatten() for a ural_network.weights])
genes2 = np.concatenate([a.flatten() for a ural_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())
绿萝的样子描写ural_network.weights = unflatten(child1_genes,shapes)
offspring.append(child1)
offspring.append(child2)
return agents
The crossover function is one of the most complicated functions in the program. It generates two new “children” agents, who weights that are replaced as a crossover of wo randomly generated parents. This is the process of creating the weights:
交叉功能是程序中最复杂的功能之⼀。 它⽣成两个新的“⼦代”代理,其权重被替换为wo随机⽣成的⽗代的交叉。 这是创建权重的过程:
1. Flatten the weights of the parents
减轻⽗母的负担
2. Generate two splitting points
产⽣两个分裂点
3. U the splitting points as indices to t 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 = ural_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
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 chon, 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 = lection(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]]).T
network = [[3,10,sigmoid],[None,1,sigmoid]]
ga = genetic_algorithm
agent = ga.execute(100,5000,0.1,X,y,network)
丝巾的各种系法
weights = ural_network.weights
agent.fitness
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!)