Python 用遗传算法求解异或问题
我试图用神经网络解决异或问题。为了训练,我使用遗传算法 人口:200 最高世代数:10000 交叉率:0,8 突变率:0.1 重量:9 激活功能:乙状结肠 选择方法:高百分比选择最适合的 代码: 一些输入:Python 用遗传算法求解异或问题,python,numpy,genetic-algorithm,xor,Python,Numpy,Genetic Algorithm,Xor,我试图用神经网络解决异或问题。为了训练,我使用遗传算法 人口:200 最高世代数:10000 交叉率:0,8 突变率:0.1 重量:9 激活功能:乙状结肠 选择方法:高百分比选择最适合的 代码: 一些输入: XOR是一个简单的问题。通过几百次随机初始化,您应该有一些幸运的人能够立即解决它(如果“已解决”意味着他们在执行阈值后输出是正确的)。这是一个很好的测试,可以查看您的初始化和前馈过程是否正确,而无需一次性调试整个GA。或者你可以手工制作正确的重量和偏差,看看是否有效 你的初始重量(均匀-4
- XOR是一个简单的问题。通过几百次随机初始化,您应该有一些幸运的人能够立即解决它(如果“已解决”意味着他们在执行阈值后输出是正确的)。这是一个很好的测试,可以查看您的初始化和前馈过程是否正确,而无需一次性调试整个GA。或者你可以手工制作正确的重量和偏差,看看是否有效
- 你的初始重量(均匀-40…+40)太大了。我想对于XOR来说,这可能没问题。但初始权值应确保大多数神经元不会饱和,但也不会完全位于乙状结肠的线性区域
- 在你的实现工作完成后,看看这个神经网络的例子,看看如何用更少的代码来实现它
def crossover(self,wfather,wmother):
r = np.random.random()
if r <= self.crossover_perc:
new_weight= self.crossover_perc*wfather+(1-self.crossover_perc)*wmother
new_weight2=self.crossover_perc*wmother+(1-self.crossover_perc)*wfather
return new_weight,new_weight2
else:
return wfather,wmother
def select(self,fits):
percentuais = np.array(fits) / float(sum(fits))
vet = [percentuais[0]]
for p in percentuais[1:]:
vet.append(vet[-1] + p)
r = np.random.random()
#print(len(vet), r)
for i in range(len(vet)):
if r <= vet[i]:
return i
def mutate(self, weight):
r = np.random.random()
if r <= self.mut_perc:
mutr=np.random.randint(self.number_weights)
weight[mutr] = weight[mutr] + np.random.normal()
return weight
def activation_fuction(self, net):
return 1 / (1 + math.exp(-net))
def create_initial_population(self):
population = np.random.uniform(-40, 40, [self.population_size, self.number_weights])
return population
def feedforward(self, inp1, inp2, weights):
bias = 1
x = self.activation_fuction(bias * weights[0] + (inp1 * weights[1]) + (inp2 * weights[2]))
x2 = self.activation_fuction(bias * weights[3] + (inp1 * weights[4]) + (inp2 * weights[5]))
out = self.activation_fuction(bias * weights[6] + (x * weights[7]) + (x2 * weights[8]))
print(inp1, inp2, out)
return out
def fitness(self, weights):
y1 = abs(0.0 - self.feedforward(0.0, 0.0, weights))
y2 = abs(1.0 - self.feedforward(0.0, 1.0, weights))
y3 = abs(1.0 - self.feedforward(1.0, 0.0, weights))
y4 = abs(0.0 - self.feedforward(1.0, 1.0, weights))
error = (y1 + y2 + y3 + y4) ** 2
# print("Error: ", 1/error)
return 1 / error
def sortpopbest(self, pop):
pop_with_fit = [(weights,self.fitness(weights)) for weights in pop]
sorted_population=sorted(pop_with_fit, key=lambda weights_fit: weights_fit[1]) #Worst->Best One
fits = []
pop = []
for i in sorted_population:
pop.append(i[0])
fits.append(i[1])
return pop,fits
def execute(self):
pop = self.create_initial_population()
for g in range(self.max_generations): # maximo de geracoes
pop, fits = self.sortpopbest(pop)
nova_pop=[]
for c in range(int(self.population_size/2)):
weights = pop[self.select(fits)]
weights2 = pop[self.select(fits)]
new_weights,new_weights2=self.crossover(weights,weights2)
new_weights=self.mutate(new_weights)
new_weights2=self.mutate(new_weights2)
#print(fits)
nova_pop.append(new_weights) # adiciona na nova_pop
nova_pop.append(new_weights2)
pop = nova_pop
print(len(fits),fits)