进化张量流keras神经网络的遗传算法。

deepevolution的Python项目详细描述


深度进化

https://badge.fury.io/py/deepevolution.svg

tensorflow-keras模型进化算法的实现。它允许进化出一个keras神经网络模型,用于不可能存在可微损失函数的任务。在

安装

可通过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损失用作适应度函数(默认行为)。另一方面,精度指标 用于发展网络。在

https://raw.githubusercontent.com/ipazc/deepevolution/master/fitness_evolution.png

引擎盖下

调用evolve时发生了什么?进化过程由以下步骤组成:

  1. 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.
  2. 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).
  3. Elitism is practiced: the top_k (by default 4) models are selected for reproduction from the population, the rest are discarded.
  4. 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%).
  5. 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

这种进化算法的一个主要优点是适应度函数不需要是可微的损失函数。它可以是任何函数,返回模型在给定任务或场景下的工作情况,使其适合于强化学习问题。返回的数字必须是一个浮点数,越高,性能越好。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法使用JSF访问托管bean方法   java是制作具有多值类型的HashMap的正确方法   javafx中TicTacToe的java更新UI   windows Java文件。getCanonicalFile()无法处理冒号“:”   java在一个布局屏幕中创建多个(26)按钮   java Android Studio:Gradle构建完成,有251个错误   我们如何在Java上为callfireapiclient编写单元/集成测试?   java无法将1715UTC转换为本地/gmt类型   具有已定义的数字序列的JAVA循环   Java程序正在netbeans中编译,但未在CMD中编译,包不存在   java Android构造函数和onCreate()之间有什么区别?   java配置弹性搜索结果评分   java LibGDX纹理是否可绘制?   java如何在Android中设置应用程序默认打开pdf   java是否有一种创造性的方法将多个参数传递给contentEquals()方法?   java在Android上存储Ed25519私钥