├── example.py ├── example_recurrent.py ├── example_func.py ├── example_recurrent_func.py └── README.md /example.py: -------------------------------------------------------------------------------- 1 | import neuralfit as nf 2 | import numpy as np 3 | 4 | # Define dataset 5 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 6 | y = np.asarray([[0], [1], [1], [0]]) 7 | 8 | # Define model (2 inputs, 1 output) 9 | model = nf.Model(2, 1) 10 | 11 | # Compile and evolve 12 | model.compile(loss='mse', monitors=['size']) 13 | model.evolve(x,y) 14 | 15 | # Make predictions 16 | print(model.predict(x)) 17 | 18 | # Save model 19 | model.save('model.nf') 20 | 21 | # Export model to Keras 22 | keras_model = model.to_keras() 23 | -------------------------------------------------------------------------------- /example_recurrent.py: -------------------------------------------------------------------------------- 1 | import neuralfit as nf 2 | import numpy as np 3 | 4 | # Define dataset 5 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 6 | y = np.asarray([[0], [1], [1], [0]]) 7 | 8 | # Reshape to timeseries format 9 | # 4 samples of length 2 with 1 feature as input 10 | # 4 samples of length 1 with 1 feature as output 11 | x = np.reshape(x, (4,2,1)) 12 | y = np.reshape(y, (4,1,1)) 13 | 14 | # Define model (1 input, 1 output) 15 | model = nf.Model(1,1, recurrent = True) 16 | 17 | # Compile and evolve 18 | model.compile(loss='mse', monitors=['size']) 19 | model.evolve(x, y, epochs=500) 20 | 21 | # Make predictions, take last prediction of each series 22 | print(model.predict(x)[:,-1,0]) 23 | 24 | # Save model 25 | model.save('model.nf') 26 | -------------------------------------------------------------------------------- /example_func.py: -------------------------------------------------------------------------------- 1 | import neuralfit as nf 2 | import numpy as np 3 | 4 | # Define dataset 5 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 6 | y = np.asarray([[0], [1], [1], [0]]) 7 | 8 | # Define evaluation function (MSE) 9 | def evaluate (genomes): 10 | losses = np.zeros(len(genomes)) 11 | 12 | for i in range(len(genomes)): 13 | for j in range(x.shape[0]): 14 | result = genomes[i].predict(x[j]) 15 | losses[i] += (result - y[j])**2 16 | 17 | return losses/x.shape[0] 18 | 19 | # Define model (2 inputs, 1 output) 20 | model = nf.Model(2, 1) 21 | 22 | # Compile and evolve 23 | model.compile(monitors=['size']) 24 | model.func_evolve(evaluate) 25 | 26 | # Make predictions 27 | print(model.predict(x)) 28 | 29 | # Save model 30 | model.save('model.nf') 31 | 32 | # Export model to Keras 33 | keras_model = model.to_keras() 34 | -------------------------------------------------------------------------------- /example_recurrent_func.py: -------------------------------------------------------------------------------- 1 | import neuralfit as nf 2 | import numpy as np 3 | 4 | # Define dataset 5 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 6 | y = np.asarray([[0], [1], [1], [0]]) 7 | 8 | # Reshape to timeseries format 9 | # 4 samples of length 2 with 1 feature as input 10 | # 4 samples of length 1 with 1 feature as output 11 | x = np.reshape(x, (4,2,1)) 12 | y = np.reshape(y, (4,1,1)) 13 | 14 | # Define evaluation function (MSE) 15 | def evaluate (genomes): 16 | losses = np.zeros(len(genomes)) 17 | 18 | for i in range(len(genomes)): 19 | for j in range(x.shape[0]): 20 | genomes[i].clear() 21 | result = genomes[i].predict(x[j])[-1] 22 | losses[i] += (result - y[j])**2 23 | 24 | return losses/x.shape[0] 25 | 26 | # Define model (1 input, 1 output) 27 | model = nf.Model(1, 1, recurrent=True) 28 | 29 | # Compile and evolve 30 | model.compile(monitors=['size']) 31 | model.func_evolve(evaluate, epochs=500) 32 | 33 | # Make predictions 34 | print(model.predict(x)[:,-1,0]) 35 | 36 | # Save model 37 | model.save('model.nf') 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

PyPI version Alpha

4 | 5 | NeuralFit is a simple neuro-evolution library for Python with an API that is similar to the [Keras](https://keras.io/) 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 🐛. 6 | 7 | ## Installation 8 | 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. 9 | 10 | ``` 11 | pip install neuralfit 12 | ``` 13 | 14 | **If you want to export models to Keras, make sure you also have `tensorflow` installed!** 15 | 16 | ## Get started 17 | As an example, you can evolve a model to learn the [XOR gate](https://en.wikipedia.org/wiki/XOR_gate) as follows. 18 | 19 | ```python 20 | import neuralfit as nf 21 | import numpy as np 22 | 23 | # Define dataset 24 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 25 | y = np.asarray([[0], [1], [1], [0]]) 26 | 27 | # Define model (2 inputs, 1 output) 28 | model = nf.Model(2, 1) 29 | 30 | # Compile and evolve 31 | model.compile(loss='mse', monitors=['size']) 32 | model.evolve(x,y) 33 | ``` 34 | 35 | To verify that your model has fitted the dataset correctly, we can check its predictions. 36 | 37 | ```python 38 | print(model.predict(x)) 39 | ``` 40 | 41 | Done with evolving your model? Simply save the model or export it to Keras! 42 | 43 | ```python 44 | # Save model 45 | model.save('model.nf') 46 | 47 | # Export model to Keras 48 | keras_model = model.to_keras() 49 | ``` 50 | 51 | There is much more to discover about NeuralFit, and new features are being added rapidly. Please visit the official [documentation](https://neuralfit.net/documentation/) for more information or follow one of our [examples](https://neuralfit.net/examples/). One of the examples shows a method to [visualize the evolved model](https://neuralfit.net/examples/visualization/), which results in the pictures below. 52 | 53 |

54 | 55 | 56 |

57 | 58 | ## Recurrent models 59 | 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. 60 | 61 | ```python 62 | import neuralfit as nf 63 | import numpy as np 64 | 65 | # Define dataset 66 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 67 | y = np.asarray([[0], [1], [1], [0]]) 68 | 69 | # Reshape to timeseries format 70 | # 4 samples of length 2 with 1 feature as input 71 | # 4 samples of length 1 with 1 feature as output 72 | x = np.reshape(x, (4,2,1)) 73 | y = np.reshape(y, (4,1,1)) 74 | 75 | # Define model (1 input, 1 output) 76 | model = nf.Model(1,1, recurrent = True) 77 | 78 | # Compile and evolve 79 | model.compile(loss='mse', monitors=['size']) 80 | model.evolve(x, y, epochs=500) 81 | ``` 82 | 83 | After the model has evolved, we can give it timeseries and extract the last output to see what the model predicts. 84 | 85 | ```python 86 | # Make predictions 87 | print(model.predict(x)[:,-1,0]) 88 | ``` 89 | 90 | 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](https://neuralfit.net/examples/), as more recurrent examples will be added soon. Again, it is possible to [visualize the model](https://neuralfit.net/examples/visualization/), which will now likely include backwards and self-connections. 91 | 92 |

93 | 94 | 95 |

96 | 97 | ## Custom evaluation functions 98 | 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: 99 | 100 | ```python 101 | # Define dataset 102 | x = np.asarray([[0,0], [0,1], [1,0], [1,1]]) 103 | y = np.asarray([[0], [1], [1], [0]]) 104 | 105 | # Define evaluation function (MSE) 106 | def evaluate (genomes): 107 | losses = np.zeros(len(genomes)) 108 | 109 | for i in range(len(genomes)): 110 | for j in range(x.shape[0]): 111 | result = genomes[i].predict(x[j]) 112 | losses[i] += (result - y[j])**2 113 | 114 | return losses/x.shape[0] 115 | 116 | # Define model (2 inputs, 1 output) 117 | model = nf.Model(2, 1) 118 | 119 | # Compile and evolve 120 | model.compile(monitors=['size']) 121 | model.func_evolve(evaluate) 122 | ``` 123 | 124 | 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](https://github.com/neural-fit/neuralfit/blob/main/example_recurrent_func.py). 125 | 126 | ## Issues? 127 | Found a bug? Want to suggest a feature? Or have a general question about the workings of NeuralFit? Head over to the [issues section](https://github.com/neural-fit/neuralfit/issues) and feel free to create an issue. Note that any questions about licenses or payments should be sent to info@neuralfit.net. 128 | 129 | ## Become a supporter 130 | 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](https://neuralfit.net/licenses/), 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 💚. 131 | 132 | --------------------------------------------------------------------------------