Coder Social home page Coder Social logo

neural-fit / neuralfit Goto Github PK

View Code? Open in Web Editor NEW
16.0 1.0 0.0 48 KB

πŸ¦– A fast & simple neuro-evolution library for Python

Home Page: https://neuralfit.net

Python 100.00%
keras-tensorflow neural-network neuro-evolution machine-learning python

neuralfit's Introduction

PyPI version Alpha

NeuralFit is a simple neuro-evolution library for Python with an API that is similar to the Keras library. You can train models with evolution instead of backpropagation in just a few lines of code. You can also export evolved models to Keras so you can use them in your existing environments without changing a thing. For more information, please visit https://neuralfit.net/. NeuralFit is currently in the alpha phase and being developed rapidly, expect many bugs and crashes πŸ›.

Installation

NeuralFit is tested and supported on 64-bit machines running Windows or Ubuntu with Python 3.7-3.10 installed. You can install NeuralFit via pip with version 19.3 or higher.

pip install neuralfit

If you want to export models to Keras, make sure you also have tensorflow installed!

Get started

As an example, you can evolve a model to learn the XOR gate as follows.

import neuralfit as nf
import numpy as np

# Define dataset
x = np.asarray([[0,0], [0,1], [1,0], [1,1]])
y = np.asarray([[0], [1], [1], [0]])

# Define model (2 inputs, 1 output)
model = nf.Model(2, 1)

# Compile and evolve
model.compile(loss='mse', monitors=['size'])
model.evolve(x,y)

To verify that your model has fitted the dataset correctly, we can check its predictions.

print(model.predict(x))

Done with evolving your model? Simply save the model or export it to Keras!

# Save model
model.save('model.nf')

# Export model to Keras
keras_model = model.to_keras()

There is much more to discover about NeuralFit, and new features are being added rapidly. Please visit the official documentation for more information or follow one of our examples. One of the examples shows a method to visualize the evolved model, which results in the pictures below.

Recurrent models

It is also possible to evolve recurrent models, although they cannot (yet) be exported to Keras. These models can evolve backwards connections that allow models to have memory, making them ideal for timeseries prediction. The above XOR dataset can be transformed to a timeseries, where each input feature is inputted sequentially instead of all at once.

import neuralfit as nf
import numpy as np

# Define dataset
x = np.asarray([[0,0], [0,1], [1,0], [1,1]])
y = np.asarray([[0], [1], [1], [0]])

# Reshape to timeseries format
# 4 samples of length 2 with 1 feature as input
# 4 samples of length 1 with 1 feature as output
x = np.reshape(x, (4,2,1))
y = np.reshape(y, (4,1,1)) 

# Define model (1 input, 1 output)
model = nf.Model(1,1, recurrent = True)

# Compile and evolve
model.compile(loss='mse', monitors=['size'])
model.evolve(x, y, epochs=500)

After the model has evolved, we can give it timeseries and extract the last output to see what the model predicts.

# Make predictions
print(model.predict(x)[:,-1,0])

It is also possible to define a target output series that has the length of each input series, which is useful for stock market prediction for example. Please keep an eye on our examples page, as more recurrent examples will be added soon. Again, it is possible to visualize the model, which will now likely include backwards and self-connections.

Custom evaluation functions

If your task does not have a supervised dataset, you can pass your own evaluation function to be used in the evolution process. The above feedforward XOR example can be for example adapted as follows:

# Define dataset
x = np.asarray([[0,0], [0,1], [1,0], [1,1]])
y = np.asarray([[0], [1], [1], [0]])

# Define evaluation function (MSE)
def evaluate (genomes):
    losses = np.zeros(len(genomes))

    for i in range(len(genomes)):
        for j in range(x.shape[0]):
            result = genomes[i].predict(x[j])
            losses[i] += (result - y[j])**2
    
    return losses/x.shape[0]

# Define model (2 inputs, 1 output)
model = nf.Model(2, 1)

# Compile and evolve
model.compile(monitors=['size'])
model.func_evolve(evaluate)

This can also be done for tasks where recurrency (i.e. memory) is required. Just make sure to clear() the genomes before each series, as is done in the recurrent XOR example.

Issues?

Found a bug? Want to suggest a feature? Or have a general question about the workings of NeuralFit? Head over to the issues section and feel free to create an issue. Note that any questions about licenses or payments should be sent to [email protected].

Become a supporter

Unfortunately, it is not possible to sustain the research and development for NeuralFit withour your help. Please consider supporting us by buying a license subscription, which also unlocks various extra features. We promise that all NeuralFit features will be available for everyone if other sources of income allow for it πŸ’š.

neuralfit's People

Contributors

wagenaartje avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

neuralfit's Issues

Early stopping

Currently NeuralFit does not implement any πŸ›‘ early stopping techniques. These are especially useful for (1) avoiding overfitting and (2) stopping training at a specified tolerance. The implementation should be similar to Keras.

Feel free to make any additions below!

Callbacks

It should be possible to define a callback function that gets called when certain criteria are satisfied during training (e.g. every X epochs).

Feel free to make any additions below!

N-dimensional arrays

At the moment is only possible to specify 1D inputs and 1D outputs (because the structure of models in NeuralFit does not allow for dimensional symetries). Nevertheless, it would be nice to allow N-D inputs and outputs so that NeuralFit models can be used more seamlessly.

Feel free to make any additions below!

Recurrent neural networks

NeuralFit should be able to evolve ⚑recurrent neural networks.

  • At model creation, it should be specified if the model is recurrent or not.
  • When using Model.evolve, an option should be specified that disables shuffling (for timeseries training).
  • Models should have a function that resets the states of the neurons to zero (to reset memory).

Feel free to make any additions below!

Multithreading

NeuralFit is currently limited to just one core, while there is plenty of oppurtunity for multithreading. In the future it should support multithreading, but an option should remain to disable it.

Feel free to make any additions below!

No scientific notation at low losses

When losses become very low, the epoch status does not switch to scientific notation. As a result, outputs like this can occur:

Epoch 8/10 - 1/1 [==============================] - 3ms 1ms/step - loss: 0.000000 - size: 3

Where no useful information is given about the current loss.

Classification losses

Although classification is currently possible, there are no supporting loss functions for it and output neurons cannot be constrained. Therefore, NeuralFit should

Feel free to make any additions below!

Unable to handle odd pop_sizes

At the moment setting pop_size to some odd number (e.g. 11) causes Model.evolve() or Model.func_evolve() to crash. This will be fixed in the next version, 0.1.7. For now, make sure your population size is a multiple of 4.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.