Skip to main content

Distributed Evolutionary Algorithms in TensorFlow (DEATF) is a framework where networks generated with TensorFlow are evolved via DEAP.

Project description

DEATF

Python TensorFlow DEAP

Distributed Evolutionary Algorithms in TensorFlow (DEATF) is a framework where networks generated with TensorFlow [1] are evolved via DEAP [2]. DEATF is a framework directly based in EvoFlow [3] framework created by Unai Garciarena.

Installation

DEATF has available an easy installation with pip.

pip install deatf

Documentation

In order to facilitate the use of DEATF, there is a User's Guide with the information of all classes, functions and examples provided by the library.

Requirements

DEATF requires a Python version between 3.5 and 3.8. It also depends from other libraries, these are the requirements to use it correctly:

  • TensorFlow (v2.0 or greater)
  • Numpy
  • DEAP
  • Tensorflow-datasets
  • Scikit-learn
  • Pandas

Example

The easiest example of this library (taken from simple.py in the examples folder), where every used parameter is predifined is the following one:

import numpy as np

from deatf.auxiliary_functions import load_fashion
from deatf.network import MLPDescriptor
from deatf.evolution import Evolving

from sklearn.preprocessing import OneHotEncoder

x_train, y_train, x_test, y_test, x_val, y_val = load_fashion()

OHEnc = OneHotEncoder()

y_train = OHEnc.fit_transform(np.reshape(y_train, (-1, 1))).toarray()
y_test = OHEnc.fit_transform(np.reshape(y_test, (-1, 1))).toarray()
y_val = OHEnc.fit_transform(np.reshape(y_val, (-1, 1))).toarray()

e = Evolving(evaluation="XEntropy", desc_list=[MLPDescriptor], compl=False,
         x_trains=[x_train], y_trains=[y_train], x_tests=[x_val], y_tests=[y_val], 
         n_inputs=[[28, 28]], n_outputs=[[10]], batch_size=150, iters=10, 
         population=15, generations=10, max_num_layers=10, max_num_neurons=20,
         seed=0, dropout=False, batch_norm=False, evol_alg='mu_plus_lambda',
         evol_kwargs={'mu':10, 'lambda_':15, 'cxpb':0., "mutpb": 1.},
         sel = 'best')

a = e.evolve()

More complex cases can be created, an example of it (without being to complex) is sequential.py. In this second example, the evaluation function is not predefined; instead it is defined by the user in eval_sequential function. With this option, user can decide how to evaluate the created models.

import tensorflow as tf
import numpy as np

from deatf.auxiliary_functions import accuracy_error, load_fashion
from deatf.network import MLPDescriptor
from deatf.evolution import Evolving

from tensorflow.keras.layers import Input, Flatten
from tensorflow.keras.models import Model
import tensorflow.keras.optimizers as opt
from sklearn.preprocessing import OneHotEncoder

optimizers = [opt.Adadelta, opt.Adagrad, opt.Adam]

def eval_sequential(nets, train_inputs, train_outputs, batch_size, iters, test_inputs, test_outputs, hypers):
   
    inp = Input(shape=train_inputs["i0"].shape[1:])
    out = Flatten()(inp)
    out = nets["n0"].building(out)
    out = nets["n1"].building(out)

    model = Model(inputs=inp, outputs=out)
    
    opt = optimizers[hypers["optimizer"]](learning_rate=hypers["lrate"])
    
    model.compile(loss=tf.nn.softmax_cross_entropy_with_logits, optimizer=opt, metrics=[])
    
    model.fit(train_inputs['i0'], train_outputs['o0'], epochs=iters, batch_size=batch_size, verbose=0)

    pred = model.predict(test_inputs['i0'])
        
    res = tf.nn.softmax(pred)

    return accuracy_error(test_outputs["o0"], res),

x_train, y_train, x_test, y_test, x_val, y_val = load_fashion()

OHEnc = OneHotEncoder()

y_train = OHEnc.fit_transform(np.reshape(y_train, (-1, 1))).toarray()
y_test = OHEnc.fit_transform(np.reshape(y_test, (-1, 1))).toarray()
y_val = OHEnc.fit_transform(np.reshape(y_val, (-1, 1))).toarray()

e = Evolving(evaluation=eval_sequential, desc_list=[MLPDescriptor, MLPDescriptor], 
             x_trains=[x_train], y_trains=[y_train], x_tests=[x_val], y_tests=[y_val], 
             batch_size=150, population=10, generations=10, iters=10, 
             n_inputs=[[28, 28], [10]], n_outputs=[[10], [10]], cxp=0.5, mtp=0.5, 
             hyperparameters={"lrate": [0.1, 0.5, 1], "optimizer": [0, 1, 2]},
             batch_norm=False, dropout=False)
a = e.evolve()

References

[1] Abadi, M., Agarwal, A., Barham, P., Brevdo, E., Chen, Z., Citro, C., ... & Ghemawat, S. (2016). Tensorflow: Large-scale machine learning on heterogeneous distributed systems. arXiv preprint arXiv:1603.04467.

[2] Fortin, F. A., Rainville, F. M. D., Gardner, M. A., Parizeau, M., & Gagné, C. (2012). DEAP: Evolutionary algorithms made easy. Journal of Machine Learning Research, 13(Jul), 2171-2175.

[3] Garciarena, U., Santana, R., & Mendiburu, A. (2018, July). Evolved GANs for generating Pareto set approximations. In Proceedings of the Genetic and Evolutionary Computation Conference (pp. 434-441). ACM.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deatf-0.3.1.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

deatf-0.3.1-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file deatf-0.3.1.tar.gz.

File metadata

  • Download URL: deatf-0.3.1.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.10

File hashes

Hashes for deatf-0.3.1.tar.gz
Algorithm Hash digest
SHA256 32096eb0a7999ce81d421df2605dd8fff541241aa8135677b948c54d197bdec4
MD5 7dcdc16479833e98bec36b7ec3684390
BLAKE2b-256 60d7679d9cfa5713e7043038935af5dbb614a89c31e3b36ae12226a0d9c0cfee

See more details on using hashes here.

File details

Details for the file deatf-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: deatf-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.10

File hashes

Hashes for deatf-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da2c2192ccfca5cd68794c5c9afca3e8c855a7d756acb2933e367a0d5ee06e53
MD5 17cb4433f58fdd73cec20a9e3a02aebe
BLAKE2b-256 8d985e2751415a5fc0d799132aa98e2da14009481e1aa98830eb44d4e6d8a5cb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page