进化张量流keras神经网络的遗传算法。
deepevolution的Python项目详细描述
安装
可通过pip安装:
$ pip install deepevolution
它需要tensorflow>;2.0.0。安装方式如下:
^{pr2}$使用
在导入keras模型类之后导入包。deepevolution包自动将fit_evolve()方法绑定到keras模型类,使其立即可用于任何keras模型。其用法如下:
fromtensorflow.keras.modelsimportModelfromdeepevolutionimportwrap_keraswrap_keras()## ... Build a keras model `model`...keras_model.fit_evolve(x_train,y_train,max_generations=100)
默认适应度函数是模型的负损失函数(必须编译)。适合不同的场合 功能,检查高级部分。在
MNIST示例
在存储库(https://github.com/ipazc/deepevolution/tree/master/examples)的文件夹examples/中可以找到具有相同前馈神经网络的MNIST的两个示例。 其中一种方法是将负MSE损失用作适应度函数(默认行为)。另一方面,精度指标 用于发展网络。在
引擎盖下
调用evolve时发生了什么?进化过程由以下步骤组成:
- Network’s weights are duplicated to build a population of N networks (16 by default). Each individual network is differentiated by adding noise sampled from a normal distribution (with a std=0.3 by default) for each weight.
- Weights are evaluated by passing them to the fitness function along with the training data. This fitness function measures how well the network’s weights perform with the given data, and it is totally customizable. More information about crafting a fitness function can be obtained in the ADVANCED section. If not provided, it will use by default a fitness function where the score of the network is measured by its negative loss (using the training data as evaluation).
- Elitism is practiced: the top_k (by default 4) models are selected for reproduction from the population, the rest are discarded.
- The crossover happens between pairs of top models. Each model’s weights are crossed with subsequents models until all combinations are met. The crossover consists of merging the 50% of the weights from one model with the 50% of the other, plus a random mutation sampled from a normal distribution applied to a % of the weights (by default, 20%).
- The generated models after the crossover are mixed with the top_k from the previous generation. The result is a new generation that can be evolved again by jumping to the point 2.
高级
可以为演进设置以下参数:
- max_generations: the number of generations that we will wait for. Similar to “epochs” concept. By default it is 100.
- population: the number of individuals that will form the population of the generation. The higher, the better chances to find a new best-performing individual, but more computer resources are required. By default 16
- top_k: the number of models that will survive the generation (based on its ranking score). They are considered to be the best K models of the generation. By default it is 4
- mutation_rate: percent of weights from the children to mutate on the new generation. By default it is 0.2 (20%).
- mutation_std: the mutation is sampled from a normal distribution with a given std. By default it is 0.03. Higher values implies heavier mutations to the weights.
- verbose: 0 for disabling logging, 1 for traditional logging of progress as messages, 2 for a tqdm bar.
- fitness_func: the function for scoring each model.
适应度函数具有以下原型:
deffitness_func(model,X,Y):# Evaluate the model somehow. Note that it is optional to use X and Y.# This is the default behaviour, but any score can be returnedresult=model.evaluate(X,Y,batch_size=2048)[0]return-1*result
这种进化算法的一个主要优点是适应度函数不需要是可微的损失函数。它可以是任何函数,返回模型在给定任务或场景下的工作情况,使其适合于强化学习问题。返回的数字必须是一个浮点数,越高,性能越好。在
- 项目
标签: