I'm trying to create a genetic algorithm to train neural networks (because I'm to bad at back-propagation), and it works well until generation 18, where the loss stops to decrease and gets constant. The loss gets from 112 to about 90 in 18 generations and almost doesn't change:
Here is the genetic algorithm (I think the problem doesn't come from the neural network algorithm as it's very basic) in python :
# This is my reproduce function :
def reproduce(first,other,mutate=0):
child = Entity() # Creates a new Neural Network (class Entity is the class of the Neural Networks)
# Only two synapse matrices, each get the mean of its two parents
child.syn0 = (first.syn0 + other.syn0)/2
child.syn1 = (first.syn1 + other.syn1)/2
if mutate:
# Add some mutation if it's enabled, but it didn't work without it
child.syn0 += child.syn0*(np.random.random()*mutate-mutate/2)
child.syn1 += child.syn1*(np.random.random()*mutate-mutate/2)
return child
ent = [Entity() for _ in range(20)] # Creates 20 new Neural Networks with random weigths on their synapses
nb_dna = 1
gen = 0
scores = []
max_gen = 100
while gen < max_gen:
np.random.seed(int(time.time()))
# The Neural Networks goes forward.
for i,e in enumerate(ent):
e.set_input(int(np.floor(np.random.random()*nb_dna)))
e.forward()
# In the forward function, the score is calculated as the result of a squared error cost function
if gen < (max_gen-1):
# Do natural selection
ent.sort(key=lambda x: x.score) # Sort my ascending score (the lowest the score is, the closest the NN is to the expected result
ent = ent[:4] # Kill all entities except the five 1st
# Reproduce the winners
for i in range(4):
for j in range(4-i):
# 1st reproduces with 2nd, 3rd, 4th and 5th,
# 2nd with 3rd, 4th and 5th,
# 3rd with 4th and 5th,
# and then 4th with 5th
new = reproduce(ent[i],ent[j],mutate=0.2) # Changing the mutation rate "mutate" will only change the convergence speed
ent.append(new)
gen += 1
Can you tell me if I'm doing something wrong, or if I'm going the wrong way ? Else, is that the best I can get ? It's actually the first "real" genetic algorithm I create (previous ones were easy JS algorithm to try to find a target word, starting with random letters)