进化张量流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第三方库


热门话题
显示图像的RGB编号的java   java JavaFX画布2D游戏:背景变换vs.绘画   在到达maxElementsInMemory之前创建的java DiskMarker   a4j:ajax可用事件的java详尽列表?   java从批处理文件运行jar文件,如果出现错误,则显示meessage   音频Java在背景音乐之上播放声音   用于在FTP中上载文件的java更改目录   尽管设置了必要的属性,java列表项仍不会保持选中状态   java Stanford Core NLP解析与CSV   java使用缓冲区合并热态和冷态   java无法初始化类javax。加密。JCE安全   对这个Java循环如此困惑的输入   java Spring RabbitMQ SimpleRabbitListenerContainerFactory用法   java如何使用jGrowl创建JSF消息   安装jRebel插件后,Netbeans项目中的java源文件夹不可见?   如何在Java中解析复杂的json字符串   java Spark KafkaUtils CreateRDD在键上应用过滤器   try块中的java代码被忽略,为什么?