Home Backend Development Python Tutorial Creating a simple and efficient genetic algorithm for a neural network with Python and NumPy

Creating a simple and efficient genetic algorithm for a neural network with Python and NumPy

Dec 13, 2024 am 12:50 AM

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
Copy after login
Copy after login
Copy after login

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]])

Copy after login
Copy after login

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]
Copy after login
Copy after login

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
Copy after login

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
Copy after login
Copy after login
Copy after login

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]])

Copy after login
Copy after login

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]
Copy after login
Copy after login

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
Copy after login
Copy after login
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles