Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from neuralNetwork import *
- import math
- import random
- import copy
- popSize = 200
- inputSize = 2
- hiddenSize = 2
- outputSize = 1
- class xor():
- def __init__(self):
- self.nn = NeuralNetwork(inputSize,hiddenSize,outputSize)
- self.score = 0
- input = [[[1,0],1],
- [[0,1],1],
- [[1,1],0],
- [[0,0],0],
- ]
- arr = []
- for i in range(popSize):
- arr.append(xor())
- bestScore = 0
- while True:
- for i in arr:
- for j in range(4):
- predict = i.nn.Predict2(input[j][0],False)
- res = abs(predict[0] - input[j][1])
- i.score += 1 - res
- index = 0
- arrBest = []
- arrBest.append(arr[0])
- for i in range(popSize-1):
- if arr[index].score < arr[i+1].score:
- index = i+1
- arrBest.append(arr[i+1])
- elif arr[index].score == arr[i+1].score:
- arrBest.append(arr[i+1])
- best = random.choice(arrBest)
- if bestScore < best.score:
- bestScore = best.score
- print(bestScore)
- if arr[index].score >= 4.0:
- break
- newArr = []
- for i in range(popSize):
- r = xor()
- r.nn.mutation(best.nn,0.1)
- newArr.append(r)
- arr = newArr
- print("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement