


Creating a simple and efficient genetic algorithm for a neural network with Python and NumPy
It is the first article from course about evolution algorithms in ML.
A genetic algorithm is needed when you know the parameters of your neural network, but do not know what the output should be, for example, this algorithm can be used to play Google Dinosaur or Flappy Bird, because there you do not know what the output should be, but you have the ability to sort the most viable options, for example by time, this is called fitness functions.
I have never been able to find such an algorithm that would work, be simple, and be usable, so I started creating my own lightweight, simple, perfectly working Genetic Algorithm.
My goal is not to drag out the writing of this article, and to torture readers with its length, so let’s get straight to the code. As already mentioned, the code is simple, so most of it does not need to be described in entire essays.
First we need to import the modules:
import numpy as np import random
Then we add Dataset and the answers to it, but not to use the backpropagation algorithm, but simply to count the number of correct answers. Then you can test it on other variants, which are now commented out
x = np.array([[1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1]]) y = np.array([[0],[1],[1], [0], [0], [0], [0], [1], [1]]) #x = np.array([[0, 1, 1], [0, 0, 1], [1, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 0], [0, 0, 0], [1, 1, 0], [1, 1, 1]]) #y = np.array([[1],[0], [0], [1], [0], [1], [0], [1], [1]]) #x = np.array([[1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1]]) #y = np.array([[1],[0],[1], [0], [1], [0], [1], [0], [1]])
Add lists and activation functions. The meaning of the lists will become clear later. The first activation function is the sigmoid, and the second is the threshold.
listNet = [] NewNet = [] goodNET = [] GoodNet0 = [] GoodNet1 = [] GoodNet2 = [] GoodNet3 = [] GoodNet4 = [] GoodNet5 = [] GoodNet6 = [] good = 0 epoch = 0 good = 0 epoch = 0 def sigmoid(x): return 1/(1 + np.exp(-x)) def finfunc(x): if x[0] >= 0.5: x[0] = 1 return x[0] else: x[0] = 0 return x[0]
Next, we will need to create two classes, the first one is needed to create the initial population, and the second one for all subsequent ones, since the first time we will need to randomly create weights, and then only cross and mutate them. The init() function is used to create or add weights, predict() is needed for the algorithm itself and for calculating the best options, and the Fredict() function is different in that it returns the answer and the fitness function to display numbers on the screen and see the training stages. At the output layer, the sigmoid function is first used to bring the answer closer to one of the options, and only then the threshold function.
class Network(): def __init__(self): self.H1 = np.random.randn(3, 6) self.O1 = np.random.randn(6, 1) def predict(self, x, y): t1 = x @ self.H1 t1 = sigmoid(t1) t2 = t1 @ self.O1 t2 = sigmoid(t2) t2 = finfunc(t2) if t2 == y[0]: global good good += 1 def Fpredict(self, x, y): t1 = x @ self.H1 t1 = sigmoid(t1) t2 = t1 @ self.O1 t2 = sigmoid(t2) t2 = finfunc(t2) if t2 == y[0]: global good good += 1 return t2, good class Network1(): def __init__(self, H1, O1): self.H1 = H1 self.O1 = O1 def predict(self, x, y): t1 = x @ self.H1 t1 = sigmoid(t1) t2 = t1 @ self.O1 t2 = sigmoid(t2) t2 = finfunc(t2) if t2 == y[0]: global good good += 1 def Fpredict(self, x, y): t1 = x @ self.H1 t1 = sigmoid(t1) t2 = t1 @ self.O1 t2 = sigmoid(t2) t2 = finfunc(t2) if t2 == y[0]: global good good += 1 return t2, good
We output the first answers and the variable good, which is the fitness function here, then we reset it for the next neural network, the print 'wait0' (you can write whatever you want here) is necessary so as not to get confused about where the answers of different neural networks begin.
import numpy as np import random
The first cycle passes, here and in all subsequent cycles we give only six questions to check how well it will cope with the task, which it has not met, that is, we check it for cramming, and this sometimes happens. And now let's go into more detail: depending on how many answers it answered correctly, we assign it to one of the classes, if a large number are correct, then we must support such a neural network and increase its number, so that with the subsequent mutation there will be more smarter ones, to understand this, you can imagine that for 100 people there is one genius, but it is not enough for everyone, and this means that his genius will fade away in the next generations, this means that either the neural network will learn very slowly, or will not exist at all, to avoid this, we increase the number of neural networks with a large number of correct answers in the cycle. At the end, we empty the main listNet list, assign it new values of the GoodNet lists in order from best to worst, make a cut for the 100 best individuals, for the subsequent mutation.
x = np.array([[1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1]]) y = np.array([[0],[1],[1], [0], [0], [0], [0], [1], [1]]) #x = np.array([[0, 1, 1], [0, 0, 1], [1, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 0], [0, 0, 0], [1, 1, 0], [1, 1, 1]]) #y = np.array([[1],[0], [0], [1], [0], [1], [0], [1], [1]]) #x = np.array([[1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1]]) #y = np.array([[1],[0],[1], [0], [1], [0], [1], [0], [1]])
The crossing and mutation itself: we take one part from the first parent, the second from the second, mutate and we get a child in the NewNet list, so 1000 times.
listNet = [] NewNet = [] goodNET = [] GoodNet0 = [] GoodNet1 = [] GoodNet2 = [] GoodNet3 = [] GoodNet4 = [] GoodNet5 = [] GoodNet6 = [] good = 0 epoch = 0 good = 0 epoch = 0 def sigmoid(x): return 1/(1 + np.exp(-x)) def finfunc(x): if x[0] >= 0.5: x[0] = 1 return x[0] else: x[0] = 0 return x[0]
Starting from the previous part of the code, we use Network1(), since we are now crossing and mutating, but not creating randomly. So we need to repeat 1000 times (this is a hyperparameter, so you can choose the number of epochs yourself, 15 was enough for me), we show the answers on the first epoch and the 1000th is the final version (if you have, for example, 20, then specify 20). Here the code is repeated, so I will not describe it, everything is very clear there.
import numpy as np import random
That's all, the pattern that the neural network should find, this is what number (first, second, third) the final version depends on and ignore the rest. You can do, for example, logical operations (XOR, NOT, AND ...), only in this case in the network class change the input data by two, I also followed the rule neurons in the hidden layer are equal to the input data multiplied by two, it worked, but you can try your options, it is also very important to provide the neural network with the same number of some answers and other answers, so that the number of correct answers, for example "a", would be equal to "b", otherwise the neural network will answer all answers the same way, that is, if there is more a, then it will answer a to everything and nothing will come of it, also give it completely different options in the training sample so that it understands the pattern, for example, if you make an XOR block, then you must add an option with two ones, but in the case of logical operations, you will have to give all the options, because there are too few of them and it will not understand anything.
That's it!!! Next article (must read!): Soon…
Code: https://github.com/LanskoyKirill/GenNumPy.git
My site(it may be undergoing rework): selfrobotics.space
The above is the detailed content of Creating a simple and efficient genetic algorithm for a neural network with Python and NumPy. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
