Advertisement
Guest User

test.py

a guest
Apr 4th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. from neuralNetwork import *
  2. import math
  3. import random
  4. import copy
  5.  
  6. popSize = 200
  7. inputSize = 2
  8. hiddenSize = 2
  9. outputSize = 1
  10.  
  11.  
  12.  
  13. class xor():
  14.     def __init__(self):
  15.         self.nn = NeuralNetwork(inputSize,hiddenSize,outputSize)
  16.         self.score = 0
  17.    
  18.  
  19. input = [[[1,0],1],
  20.          [[0,1],1],
  21.          [[1,1],0],
  22.          [[0,0],0],
  23. ]
  24. arr = []
  25. for i in range(popSize):
  26.     arr.append(xor())
  27.  
  28. bestScore = 0
  29. while True:
  30.     for i in arr:
  31.         for j in range(4):
  32.             predict = i.nn.Predict2(input[j][0],False)
  33.             res = abs(predict[0] - input[j][1])
  34.             i.score +=  1 - res
  35.  
  36.     index = 0
  37.     arrBest = []
  38.     arrBest.append(arr[0])
  39.     for i in range(popSize-1):
  40.         if arr[index].score < arr[i+1].score:
  41.             index = i+1
  42.             arrBest.append(arr[i+1])
  43.         elif arr[index].score == arr[i+1].score:
  44.             arrBest.append(arr[i+1])
  45.    
  46.     best = random.choice(arrBest)
  47.  
  48.     if bestScore < best.score:
  49.         bestScore = best.score
  50.         print(bestScore)
  51.     if arr[index].score >= 4.0:      
  52.         break
  53.     newArr = []
  54.     for i in range(popSize):
  55.         r = xor()
  56.         r.nn.mutation(best.nn,0.1)
  57.         newArr.append(r)
  58.     arr = newArr
  59.  
  60. print("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement