Skip to main content

An all-purpose pythonic genetic algorithm

Project description

NaturalSelection

An all-purpose pythonic genetic algorithm, which also has built-in hyperparameter tuning support for neural networks.

Installation

$ pip install naturalselection

Usage

Here is a toy example optimising a pair of numbers with respect to division.

>>> import naturalselection as ns
>>>
>>> Pair = ns.Genus(x = range(1, 10000), y = range(1, 10000))
>>> def division(number):
...   return number.x / number.y
...
>>> pairs = ns.Population(genus = Pair, size = 10, fitness_fn = division)
>>> history = pairs.evolve(generations = 100, progress_bars = 1)
Evolving population: 100%|███████████████████| 100/100 [00:19<00:00,  5.03it/s]
>>>
>>> history.fittest
{'genome': {'x': 9527, 'y': 190}, 'fitness': 50.142105263157895}
>>>
>>> history.plot()

Alt

Here is an example of finding a feed-forward neural network to model MNIST.

>>> import naturalselection as ns
>>> from tensorflow.keras.utils import to_categorical
>>> import mnist
>>>
>>> # Standard training and testing sets for MNIST
>>> X_train = ((mnist.train_images() / 255) - 0.5).reshape((-1, 784))
>>> Y_train = to_categorical(mnist.train_labels())
>>> X_val = ((mnist.test_images() / 255) - 0.5).reshape((-1, 784))
>>> Y_val = to_categorical(mnist.test_labels())
>>>
>>> fitness_fn = ns.get_nn_fitness_fn(
...   kind = 'fnn',
...   train_val_sets = (X_train, Y_train, X_val, Y_val),
...   loss_fn = 'binary_crossentropy',
...   score = 'accuracy',
...   output_activation = 'softmax',
...   max_training_time = 60
...   )
>>>
>>> fnns = ns.Population(
...   genus = ns.FNN(),
...   fitness_fn = fitness_fn,
...   size = 50
...   )
>>> history = fnns.evolve(generations = 10)
Evolving population: 100%|██████████████████| 10/10 [2:20:14<00:00, 699.72s/it]
Computing fitness for generation: 100%|████████| 23/23 [06:57<00:00, 20.05s/it]
>>> 
>>> history.fittest
{'genome': {'optimizer': 'nadam', 'hidden_activation': 'relu',
'batch_size': 256, 'initializer': 'glorot_uniform', 'input_dropout': 0.2,
'layer0': 64, 'dropout0': 0.0, 'layer1': 256, 'dropout1': 0.0, 'layer2': 256,
'dropout2': 0.1, 'layer3': 128, 'dropout3': 0.0, 'layer4': 32, 'dropout4': 0.2}, 'fitness': 0.9646}
>>> 
>>> history.plot(
...   title = "Average accuracy by generation",
...   ylabel = "Average accuracy"
...   )

Alt

License

This project is licensed under the MIT License.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

naturalselection-0.2.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file naturalselection-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: naturalselection-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for naturalselection-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa92683a1dd1809af7a009bafe831c63d302caed773cc4ea525d1e3422095799
MD5 d9beb3f3fa7469fbd2c08968232cdcef
BLAKE2b-256 aa48fa7ab8fec464e59898395769ef15692ed491f2eb700447ea713a2977a286

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