def __init__(self, api, populationSize=10, mutationRate=0.1, \ maxGenerations=0, showResults=True, keepBestProportion=0.1, \ keepRandomProportion=0.1): """ initalizes an evolver """ self.populationSize = populationSize self.api = api self.originalMutationRate = mutationRate self.mutationRate = mutationRate self.spamStart = True self.spamDirection = False self.generationCount = 1 self.maxGenerations = maxGenerations self.population = [] for i in range(self.populationSize): self.population.append(nn.NeuralNetwork(10, 4)) self.currentNetwork = self.population[0] self.currentNetworkIndex = 0 self.lastLifeCount = 3 self.initialFitness = 0 self.bestFitness = 0 self.startTime = time.time() self.showResults = showResults self.keepBestProportion = keepBestProportion self.keepRandomProportion = keepRandomProportion self.frameCount = 0
def __init__(self,size=50,width=600,height=600,graphics=True,brain=None): self.size = size self.rotation_angle = 0 self.x, self.y = width / 2, height / 2 vertices = [(0.5,0),(-0.25,-0.25),(-0.25,0.25)] #convert x-y vertices to polar self.points = [pc.to_polar(v) for v in vertices] #scale up ship by self.size self.points = [[self.size*r,theta] for [r,theta] in self.points] self.color = GREEN self.score = 0 #game score self.alive = True self.life = 0 #number of frames survived self.dpoints = [0]*8 #[[0,0,0]] * 8 #points for testing distance measurements self.create_asteroids = False self.shoot_bullet = False #True when key pressed or AI chooses self.createBullet = True #Off when bullet has been created, creates delay self.bullet_wait = 0 self.graphics = graphics self.fitness = 0 #for neural net if brain: self.brain = brain.replicate() else: self.brain = nn.NeuralNetwork(8, 12, 3, 0.3) self.inputs = [0] * 5 self.output = numpy.zeros(3) #for tracking improvements self.mutated = 0 self.crossovers = 0
def test_saveNeuralNetwork(): #Req 7 """ The Pac-Man AI shall stop playing and save the state of the neural network with the highest fitness when the user has specified for it to stop. """ neuralNetwork = nn.NeuralNetwork(4, 4) assert neuralNetwork.printSynapses()
def __init__(self, size, inputNum, hiddenNum, outputNum): self.size = size self.population = [] self.fitness = [] for i in range(size): self.population.append( nn.NeuralNetwork(inputNum, hiddenNum, outputNum)) self.fitness.append(0)
def initialize(): """ Initializes the population. """ global pop_size, population, pop_fitness, structure # Initialize population. population = [] pop_fitness = [] for i in range(pop_size): # Create neural net individual. if structure != None: individual = neuralNetwork.NeuralNetwork(structure) else: s = genRandomStructure() individual = neuralNetwork.NeuralNetwork(s) population.append(individual) pop_fitness.append(0) # every inidividual starts with a fitness of 0.
def __init__(self,brain=None): self.y = height/2 self.x = 64 self.w = 32 self.gravity = 0.8 self.lift = -16 self.velocity = 0 self.score = 0 self.fitness = 0 if brain: self.brain = brain.copy() self.brain = nn.NeuralNetwork(5,6,1,0.3) self.inputs = [0]*5 self.output = []
def __init__(self, player, width, height): self.obstacleTimer = 0 self.obstacles = [] # constants self.minObsInterval = 100 self.width = width self.height = height self.coll = False self.score = 0 # assets self.ground = loadImage(con.parent + "\\ground.png") # Neural network self.NeuralNet = neuralNetwork.NeuralNetwork(1, 3, 1)
def regression(): Xtrain, Ytrain = reader.readRegressionFile('regression\data.square.train.100.csv') Xtest, Ytest = reader.readRegressionFile('regression\data.square.test.100.csv') net = nn.NeuralNetwork(momentumSize=1, seed=100) net.addLayer(1, 2, F.polly, bias=True) net.addLayer(2, 1, F.linear, bias=True) net.setCostFunction(L.l1) #define own validation and train sets # net.trainAndValidate(Xtrain[20:], Ytrain[20:], Xtrain[0:20], Ytrain[0:20], epochs=10, learningRate=0.1, showError=True, showNodes=False) #automatic k-folds validation method net.kFoldsTrainAndValidate(Xtrain, Ytrain, k=20, batchSize=1, epochs=2000, learningRate=6e-4, momentumRate=2e-5, showError=True, showNodes=False, print = lambda : printer.print_regression(net, Xtrain, Ytrain, size=5, dx=0.25), showEvery= 10) mean, std, error = net.validate(Xtest, Ytest) print(mean, std)
def classification3Classes(): Xtrain, Ytrain = reader.readClassification3ClassesFile('classification\data.three_gauss.train.100.csv') Xtest, Ytest = reader.readClassification3ClassesFile('classification\data.three_gauss.test.100.csv') net = nn.NeuralNetwork(momentumSize=0, seed=0) net.addLayer(2, 20, F.LReLU, False) net.addLayer(20, 3, F.softmax, False) net.setCostFunction(L.crossEntropy) #define own validation and train sets # net.trainAndValidate(Xtrain[20:], Ytrain[20:], Xtrain[0:20], Ytrain[0:20], epochs=10, learningRate=0.1, showError=True, showNodes=False) #automatic k-folds validation method net.kFoldsTrainAndValidate(Xtrain, Ytrain, k=5, epochs=50, learningRate=0.1, showError=True, showNodes=True, print=lambda e : printer.print_classification(net, Xtrain, Ytrain, size=1.5, dS=0.1), showEvery=5) mean, std, error = net.validate(Xtest, Ytest) print(mean, std) printer.print_accuracy(net, Xtest, Ytest)
def classification2Classes(): Xtrain, Ytrain = reader.readClassificationFile('classification\data.XOR.train.100.csv') Xtest, Ytest = reader.readClassificationFile('classification\data.XOR.test.100.csv') net = nn.NeuralNetwork(momentumSize=1, seed=0) net.addLayer(2, 10, F.tanh, True) net.addLayer(10, 1, F.sigmoid, True) net.setCostFunction(L.l1) #define own validation and train sets # net.trainAndValidate(Xtrain[20:], Ytrain[20:], Xtrain[0:20], Ytrain[0:20], epochs=10, learningRate=0.1, showError=True, showNodes=False) #automatic k-folds validation method net.kFoldsTrainAndValidate(Xtrain, Ytrain, k=5, epochs=500, learningRate=0.5, showError=True, showNodes=False, print=lambda : printer.print_classification(net, Xtest, Ytest, size=1.1, dS=0.1), showEvery=10) mean, std, error = net.validate(Xtest, Ytest) print(mean, std) printer.print_accuracy(net, Xtest, Ytest)
def test0(): name = 'test0' Xtrain, Ytrain = reader.readmnistAsOneLineTraining('mnist/train.csv') print('Training set ready, size: ', len(Xtrain)) net = nn.NeuralNetwork(momentumSize=0) net.addLayer(784, 10, F.softmax, True) net.setCostFunction(L.crossEntropy) print('Starting training...') Xtest = reader.readmnistAsOneLineTest('mnist/test.csv') net.kFoldsTrainAndValidate(Xtrain, Ytrain, k=6, epochs=100, learningRate=1e-2, batchSize=100, showError=True, showNodes=False, print= lambda e: _test(e, Xtest, net, name), showEvery=5, name = name)
def test3(): _, Ytrain = reader.readmnistAsOneLineTraining('mnist\\train.csv') Xtrain = reader.readmnistAsOneLinePCA('mnist\\trainPCA300.csv') Xtest = reader.readmnistAsOneLinePCA('mnist\\testPCA300.csv') name = '3test' print('Training set ready, size: ', len(Xtrain)) net = nn.NeuralNetwork(momentumSize=0) net.addLayer(300, 150, F.tanh, True) net.addLayer(150, 70, F.LReLU, True) net.addLayer(70, 10, F.softmax, True) net.setCostFunction(L.crossEntropy) print('Starting training...') net.kFoldsTrainAndValidate(Xtrain, Ytrain, k=6, epochs=100, learningRate=1e-2, batchSize=100, showError=True, showNodes=False, print=lambda e: _test(e, Xtest, net, name), showEvery=25, name=name)
def __init__(self, weightFile=None): self.neuralnetwork = {} self.neuralnetwork['W'] = neuralNetwork.NeuralNetwork() self.neuralnetwork['L'] = neuralNetwork.NeuralNetwork() self.neuralnetwork['E'] = neuralNetwork.NeuralNetwork() self.neuralnetwork['A'] = neuralNetwork.NeuralNetwork() self.neuralnetwork['F'] = neuralNetwork.NeuralNetwork() self.neuralnetwork['T'] = neuralNetwork.NeuralNetwork() self.neuralnetwork['N'] = neuralNetwork.NeuralNetwork() print self.neuralnetwork self.emotionlist = [] self.filebuffer = open("emotion.normal", "wb") #self.trainer = neuralNetwork.Trainer(self.neuralnetwork) self.emotionconversion = { 'W': ['Anger'], 'L': ['Boredom'], 'E': ['Disgust'], 'A': ['Anxiety'], 'F': ['Happiness'], 'T': ['Sadness'], 'N': ['Neutral'] } self.trainednetwork = trainingData.Training(self.neuralnetwork)
import neuralNetwork as nn import numpy as np layer_sizes = (3, 5, 10) x = np.ones((layer_sizes[0], 1)) net = nn.NeuralNetwork(layer_sizes) prediction = net.predict(x)
# Load swing data swing_data = open("swingdataupdated.txt", "r") input_array = np.loadtxt(swing_data, delimiter=';') print("NN training data successfully opened") # Load corrisponding swing type swing_type = open("punchtype.txt", "r") y_array = np.loadtxt(swing_type, delimiter=';') print("Swingtype expected outputs succesfully opened") # load data into three separate arrays to make use of network xarr = np.array(input_array[0:49]) yarr = np.array(input_array[50:99]) zarr = np.array(input_array[100:149]) x = np.array([xarr, yarr, zarr]) y = np.zeros((3, 1)) nn = neuralNetwork.NeuralNetwork(x, y) # iterate through epochs print("Initializing Neural Network") printProgressBar(0, epochs, prefix='NN Training Progress:', suffix='Complete', length=50) for i in range(epochs): # during each epoch, train net with entire data set for j in range(1, 59): # Calculate relative 0 of x array b_index = j * 150 yval = y_array[j] # Generate x and y arrays from dat files
def Flappy_Bird_Game(): # Global Game Variables scoreTime = 0 gameStarted = False score = 0 jumpSpeed = -10 fallingConst = 0.4 # How fast does the user get points scoreRate = 20 # The x position of the background speedOfBg = 3 xOfBg = 0 # Position of our first bird gameOver = False xOfBird = 130 yOfBird = 400 vertSpeed = 0 # Position of the blue bird gameOver2 = False xOfBird2 = 250 yOfBird2 = 400 vertSpeed2 = 0 # The distance between the blocks difference = 400 # True: Logs everything for the machine learning algoritm logger = False logFile = open("trainingData.txt", "a") # Obstacles list blockList = [] def initBlocks(): # Setting the x values of the blocks for i in range(len(blockList)): blockList[i].xValue = 1300 + difference * i # There will be 5 obstacles in total blockSpeed = 5 # Our window seetings size = [1024, 718] screen = pygame.display.set_mode(size) pygame.display.set_caption("Flappy Bird") # Starting the pygame module pygame.init() # Loading assets and convering them for faster results bgPic = pygame.image.load("bg.png").convert() birdPic = pygame.image.load("birdy.png").convert_alpha() birdBluePic = pygame.image.load("birdy_blue.png").convert_alpha() upperPic = pygame.image.load("upper.png").convert_alpha() downerPic = pygame.image.load("downer.png").convert_alpha() gameOverPic = pygame.image.load("gameOver.png").convert_alpha() # Scaling up the images loaded birdPic = pygame.transform.scale2x(birdPic) birdBluePic = pygame.transform.scale2x(birdBluePic) upperPic = pygame.transform.scale2x(upperPic) downerPic = pygame.transform.scale2x(downerPic) gameOverPic = pygame.transform.scale2x(gameOverPic) orjBird = birdPic orjBirdBlue = birdBluePic # Appending blocks to blockList to access them easily blockList.append(Block(upperPic, downerPic)) blockList.append(Block(upperPic, downerPic)) blockList.append(Block(upperPic, downerPic)) blockList.append(Block(upperPic, downerPic)) initBlocks() # Is the game finished done = False clock = pygame.time.Clock() pygame.time.set_timer(pygame.USEREVENT + 1, 1500) # initialize the neural network and load a pre-trained network from a file nn = neuralNetwork.NeuralNetwork("trainingData.txt", [4, 3, 1]) nn.loadNetwork("network.txt") # Main game loop while not done: isButtonPressed = False # Checking the events in pygame for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Checking for keyboard inputs if event.type == pygame.KEYDOWN: # Press 'q' for quiting the game. if event.key == pygame.K_q: pygame.quit() # Press 'Space' for jumping in the game. # IF the game hasn't started, it will start the game if event.key == pygame.K_SPACE: isButtonPressed = True if gameStarted == False and not gameOver: # Starting the game gameStarted = True vertSpeed = jumpSpeed elif gameStarted and gameOver == False: # If the game has already started, just jump. vertSpeed = jumpSpeed else: isButtonPressed = False # Press x for the second player if event.key == pygame.K_x: if gameStarted == False and not gameOver: # Starting the game gameStarted = True vertSpeed2 = jumpSpeed elif gameStarted and not gameOver2: # If the game has already started, just jump. vertSpeed2 = jumpSpeed # Logging variables for machine learning # Finding the distance of the next obstacle nextBlock = None nextObDistance = float('inf') for block in blockList: if block.xValue + 104 > xOfBird: if block.xValue + 104 < nextObDistance: nextObDistance = block.xValue + 104 nextBlock = block if logger: if isButtonPressed: logFile.write("{} {} {} {} {}\n".format( yOfBird, vertSpeed, nextObDistance, nextBlock.yValue, "1")) else: logFile.write("{} {} {} {} {}\n".format( yOfBird, vertSpeed, nextObDistance, nextBlock.yValue, "0")) # print("{} {} {} {} {}\n".format(yOfBird, vertSpeed, nextObDistance, block.yValue, isButtonPressed)) # ask the neural network if the bird should jump based on the bird's height and speed, and the next obstacle's distance and height if gameStarted and not gameOver and round( nn.singleForwardPropogation([ yOfBird, vertSpeed, nextObDistance, 770 + nextBlock.yValue, 1 ])[0]) == 1: vertSpeed = jumpSpeed # Printing the background images screen.blit(bgPic, [xOfBg, 0]) screen.blit(bgPic, [xOfBg + 401, 0]) screen.blit(bgPic, [xOfBg + 802, 0]) screen.blit(bgPic, [xOfBg + 1203, 0]) # Moving the background image xOfBg -= speedOfBg # Checking if the background is out of screen if (xOfBg < -401): # We move the background back to the right xOfBg = 0 # Rotating the bird according to its velocity birdPic = pygame.transform.rotate( orjBird, math.degrees(math.atan(-vertSpeed / 20))) birdBluePic = pygame.transform.rotate( orjBirdBlue, math.degrees(math.atan(-vertSpeed2 / 20))) # Updating our bird picture on pyGame screen.blit(birdPic, [xOfBird, yOfBird]) screen.blit(birdBluePic, [xOfBird2, yOfBird2]) # Drawing the rectangle of the bird # pygame.draw.rect(screen, BLACK, (xOfBird, yOfBird, 64, 48), 1) # pygame.draw.rect(screen, RED, (xOfBird2, yOfBird2, 64, 48), 1) # Handling all the blocks in the game. for block in blockList: # Moving the blocks if gameStarted: block.xValue -= blockSpeed # Drawing the block block.draw(screen) # If the blick has passed the left side of the screen by 300 # We send it to the right side of the screen if block.xValue < -300: block.xValue = 1300 # Checking if the bird is touching the obstacle if block.check(pygame.Rect(xOfBird, yOfBird, 64, 48)): gameOver = True if block.check(pygame.Rect(xOfBird2, yOfBird2, 64, 48)): gameOver2 = True if yOfBird < -100: gameOver = True if yOfBird2 < -100: gameOver = True # Increasing points if bird is still alive if (scoreTime >= scoreRate) and gameStarted == True and ( gameOver == False or gameOver2 == False): score += 1 scoreTime = 0 # Increasing the scoreTime to give points while flying scoreTime += 1 # Checking if bird has fallen if yOfBird > 725: gameOver = True if yOfBird2 > 725: gameOver2 = True if gameOver and gameOver2: # Applying the settings for game over. gameStarted = False # Printing out the score and the gameover text. menuFont = pygame.font.SysFont('Arial', 60, True, False) screen.blit(menuFont.render("Score: " + str(score), True, WHITE), [400, 350]) screen.blit(gameOverPic, [340, 250]) pygame.display.flip() # Waiting for 5 seconds pygame.time.delay(4000) # Moving the bird back to its initial postion. yOfBird = 400 xOfBird = 130 gameOver = False score = 0 # Moving the bird back to its initial postion. yOfBird2 = 400 xOfBird2 = 250 gameOver = False gameOver2 = False score = 0 # Resetting the obstacles initBlocks() # If the game is over, send the bird back if gameOver: xOfBird -= blockSpeed if gameOver2: xOfBird2 -= blockSpeed # If the game has started, we print the ingame score if gameStarted and (not gameOver or not gameOver2): printScore(screen, score) # If the game hasn't started, we show instructions elif not gameStarted and not gameOver: printIns(screen) pygame.display.flip() # Moving the second bird yOfBird2 += vertSpeed2 vertSpeed2 += fallingConst # Moving the first bird yOfBird += vertSpeed vertSpeed += fallingConst if not gameStarted and not gameOver: if yOfBird >= 400: vertSpeed = jumpSpeed if yOfBird2 >= 400: vertSpeed2 = jumpSpeed # FPS of the game clock.tick(99990) # print(score) pygame.quit()
def networkLearningIter(self, PrevIterNeuralNetwork=None, silent=False, images=None, ssdSavingNum=0): NetworkList = [] rightNetworkList = [] rightNetworkSumValuesList = [] for i in range(8): # 8 random weight networks if PrevIterNeuralNetwork is None: Network = neuralNetwork.NeuralNetwork( [256, 64, 8, 4], None, i) # [256, 64, 4] send img else: Network = copyObjNetwork(PrevIterNeuralNetwork) if i != 0: # Network.mutation(0.1, 0.1) Network.mutation(0.1, 0.2) sumQualityNetwork = 0 # all photos send to neural network for k in images: Network.changeInputVals(k[1]) calcRes = Network.showChosenLetter() if calcRes[0] == k[0]: rightNetworkList.append(int(i)) sumQualityNetwork += Network.outputResQuality(k[0]) NetworkList.append(copy.deepcopy(Network)) NetworkList[len(NetworkList) - 1].initLayers( Network.returnLayers()) # hard copy of neurones Layers del Network rightNetworkSumValuesList.append( sumQualityNetwork ) # add sum for chosing best network is more networks weren't detected countedList = Counter(rightNetworkList) # return [1: 5, a: 87, 3: 8] valsD = countedList.values() # get val maxValues = max(valsD) rightNetworkSumValuesMaxList = [] tmpList1 = [] for key, val in countedList.items( ): # return one key of element with max val if val == maxValues: rightNetworkSumValuesMaxList.append(key) log("network[" + '\033[92m' + str(key) + '\033[0m' + ']' + ',', "detected:", '\033[95m' + str(val)) log("Iter Time:", time.time() - self.startLearningTime) # D.log("progress: " + str(toFixed(currentIter/self.iterAmm * 100, 2)) + '%') for i in rightNetworkSumValuesMaxList: tmpList1.append( rightNetworkSumValuesList[i]) # best ammong sumQualityNetwork tmp123 = extrameListVal(tmpList1, False) # return index of max element log("best sumQualityNetworks:", max(tmpList1)) # log("index:", rightNetworkSumValuesList.index(tmpList1[tmp123])) bestNeuralNetworkNumber = rightNetworkSumValuesList.index( tmpList1[tmp123]) ### crutch 04 04 19 if val > ssdSavingNum: # ssd saving import datetime timestr = f"{datetime.datetime.now():%Y-%m-%d %H_%M_%S_%f}" bestS = SaveObj("bestNetworks/" + str(val) + "_" + str(timestr) + ".dat") bestS.save(NetworkList[bestNeuralNetworkNumber] ) # serialize NeuralNetwork ### crutch # send best score to my telegram self.Tmsg(val) return copyObjNetwork(NetworkList[bestNeuralNetworkNumber])
import decisionTree as dt import sys if __name__ == '__main__': raw_data = input.loadFile().sample(frac=1) #Load and shuffle data kf_data = input.kFold(10, raw_data) error = [] for n, i in enumerate(kf_data): #each fold if 0 <= n < 10: print('%2d :: Train size : %d | Test size : %d' % (n + 1, len(i[0]), len(i[1]))) print('%s' % (''.ljust(50, '-'))) if sys.argv[1] == 'nn': # Neuron network nn = neuralNetwork.NeuralNetwork(len(i[0]) - 1, 0.01) nn.addHidden(21) # Hidden layers nn.addHidden(18) nn.addHidden(15) nn.addHidden(13) nn.addHidden(10) nn.addHidden(21) # output layer input.normalize(i[0]) input.normalize(i[1]) for r in range(20): # epoch print(r) for j in range(len(i[0])): #feed each row
import dill import scipy.ndimage BASE_FOLDER = os.path.abspath(os.path.dirname(__file__)) mnist_train_full = os.path.join(BASE_FOLDER, "resources/mnist_train.csv") with open(mnist_train_full, "r") as f: data_train_list = f.readlines() input_nodes = 784 hidden_nodes = 200 output_nodes = 10 learning_rate = 0.1 n = nn.NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate) times = 5 for i in range(times): for record in data_train_list: all_values = record.split(',') lable = int(all_values[0]) inputs = (np.asfarray(all_values[1:]) / 255 * 0.99) + 0.01 targets = np.zeros(output_nodes) + 0.01 targets[lable] = 0.99 n.train(inputs, targets) ## create rotated variations # rotated anticlockwise by x degrees +10 inputs_plusx_img = scipy.ndimage.interpolation.rotate(inputs.reshape( 28, 28),
import inputData as inData import trainer as trainer import neuralNetwork as network import neuralNetwork2 as network2 import numpy as np #Train network with inputData net1 = False net2 = True if net1: data = inData.Input() NN = network.NeuralNetwork(Lambda=0.0001) T = trainer.Trainer(NN) T.train(data.trainX, data.trainY, data.testX, data.testY) elif net2: data = inData.Input() NN = network2.NeuralNetwork2() #yhat = NN.forward(data.trainX) T = trainer.Trainer(NN) T.train(data.trainX, data.trainY, data.testX, data.testY) #print(yhat) #print(data.trainY) else: li = [1, 2, 3] a = 5 li = [a] + li print(li)
import logisticRegression import neuralNetwork reg = logisticRegression.LogReg() reg.run() reg = neuralNetwork.NeuralNetwork() reg.run()
print("X_test.shape=", X_test.shape, " y_test.shape=", y_test.shape) tours = 20 batch_siz = 100 # number of neurons in layers. last is output layer size. # number of neurons in the input layer is decided on the # first call of forward() num_neurons = [ 300, 100, # Comment this line out to run with one hidden layer. 10 ] beta = 0.8 learn_rate = 0.005 nn = NN.NeuralNetwork(num_neu=num_neurons, act_func="softmax", momentum=beta, learn_rate=learn_rate) plot_y = [] # The main loop. for tour in range(tours): ###################################################### # This is where the testing begins. We just throw all # the test data at it at once in one big matrix using # the function forward(). ###################################################### # construct the batch x_test_in = X_test y = np.zeros((x_test_in.shape[0], num_neurons[-1]))
Y_test.append(Y.pop(idx)) X_train = X Y_train = Y epochs = 100 num_neurons = [ 5, # Hidden layer neurons 10, 1 ] # Output layer neurons beta = 0.8 learn_rate = 1 nn = NN.NeuralNetwork(num_neu=num_neurons, act_func='sigmoid', momentum=beta, learn_rate=learn_rate) nn.forward(X_test[0:2]) print(nn.getWeights()) accuracies = [] for epoch in range(epochs): ######################################## # Test how well the neural network does ######################################## yhat = nn.forward(X_test)
def showGameOverScreen(): """ Instead of showing the game over screen, we will now perform our selection, mutation and crossover using fitness proportional selection. """ global pop_fitness global generation global population new_gen = [] # Make elite clones. elites = [] if num_elites <= pop_size: for i in range(num_elites): # Find max fitness individual and add it to the next generation. elite_index = -1 for j in range(pop_size): if j not in elites and (elite_index == -1 or pop_fitness[j] > pop_fitness[elite_index]): elite_index = j if elite_index == -1: print 'Invalid elite clone.' sys.exit() elites.append(elite_index) # Acutally copy the elites. for i in elites: elite_copy = neuralNetwork.NeuralNetwork(population[i].getStructure(), True) w, b = population[i].getWeightsAndBias() elite_copy.setWeightsAndBias(w, b) new_gen.append(elite_copy) # Calculate proportional fitness percentages. total_fitness = float(sum(pop_fitness)) prop_fitness = [] for i in range(pop_size): prop_fitness.append(pop_fitness[i] / total_fitness) if i > 0: prop_fitness[i] += prop_fitness[i - 1] # Select parents and create offspring by performing crossover and # mutation. while len(new_gen) < pop_size: parent_percent_1 = random.uniform(0, 1) parent_percent_2 = random.uniform(0, 1) parent1 = None parent2 = None # Find a pair of parents. for i in range(pop_size): if parent_percent_1 <= prop_fitness[i] and parent1 == None: parent1 = population[i] if parent_percent_2 <= prop_fitness[i] and parent2 == None: parent2 = population[i] if parent1 != None and parent2 != None: break # Now that we have selected parents, we do crossover and mutation. if strategy == 0: values1, values2 = crossover(parent1, parent2) values1 = mutate(values1) values2 = mutate(values2) else: values1 = parent1.getWeightsAndBias() values2 = parent2.getWeightsAndBias() # Add new individuals to the next generation. parent1_new = neuralNetwork.NeuralNetwork(parent1.getStructure(), True) parent2_new = neuralNetwork.NeuralNetwork(parent2.getStructure(), True) parent1_new.setWeightsAndBias(values1[0], values1[1]) parent2_new.setWeightsAndBias(values2[0], values2[1]) new_gen.append(parent1_new) new_gen.append(parent2_new) # Check if we have too many individuals in the next generation. This can # be the case because we added both offspring to the population. if len(new_gen) != pop_size: new_gen = new_gen[:-1] if print_stats: saveResultsToFile() # reset fitness and update the population of the neural networks. for i in range(pop_size): pop_fitness[i] = 0 population[i] = new_gen[i] generation += 1 # now that we have updated the fitnesses, we save the population. if save_gen != 0 and generation % save_gen == 0: savePopulation() return
import neuralNetwork as nt import numpy as np training_input = np.array([[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]) training_output = np.array([[0, 0, 0, 1, 1, 1, 1]]).T neural_network = nt.NeuralNetwork() neural_network.networkInformation() # gives information about the network a = str(input("To start the network, please enter 'go': ")) if (a == "go"): print("Random synaptic weights in layer 0: ") print(neural_network.synaptic_weights0, "\n") print("Random synaptic weights in layer 1: ") print(neural_network.synaptic_weights1, "\n") neural_network.train(training_input, training_output, 50000) # actual training of the network print("Synaptic weights after Training in layer 0: ") print(neural_network.synaptic_weights0, "\n") print("Synaptic weights after Training in layer 1: ") print(neural_network.synaptic_weights1, "\n") neural_network.userInput() # for custom inputs to test
def process(data, args): if args.save_input_csv: save_out_csv(data) dm = dataManage.ManageData(data) dm.prepare(args.remove_duplicate, args.min_sample_size, args.filter_taxon_rank) x_train, x_test, y_train, y_test = dm.split_train_test() util.write_data_by_name( dm.tab, dm.y, dm.idx2label) # util.get_idx2label(tab)) /!\ to move! # util.write_data(x, y, util.get_idx2label(tab)) info_run = { 'init_tab': len(data), 'x_train': len(x_train), 'x_test': len(x_test), 'nb_classes': dm.nb_labels } #'x':len(x), info_run.update(vars(args)) info_run.update({ "inputFields": conf.inputFields, "outputField": conf.selectedField }) info_run.update({"network": conf.network}) print("Before balance: ") dataManage.describe(y_train) if args.do_standardization: print("Make Standardization") x_train = dm.make_standardization(x_train) # , x_test) # x_train = x_train[:1000] # y_train = y_train[:1000] x_train_data = dataManage.getInputColumn(x_train) if args.do_balance_smote: x_train_data, y_train = balanceTool.smote(x_train_data, y_train) print("After balance: ") dataManage.describe(y_train) if args.run_multiple_config: msp = gridSearch.MultiSearchParam() grid_results, best_params = msp.run_search(x_train_data, y_train, dm.nb_labels) # clf = grid_results.best_estimator_ # Retrain and Evaluate on Test data with the best network params = best_params best_model = gridSearch.create_best_model(dm.nb_labels, params) best_model.fit(x_train_data, y_train, epochs=params['epochs'], batch_size=params['batch_size']) # Write results for all configs gridSearch.write_report(info_run, grid_results) else: #just train one config ! # pass nn = neuralNetwork.NeuralNetwork(dm.nb_labels) nn.train(x_train_data, y_train, epochs=args.epochs) best_model = nn.model #if load_model # best_model = util.load_model('model-2018-11-16_155757', 'data/bestModel/model2') # # 2018-11-29_181913', 'data/bestModel/model3') # util.plot_model(best_model) # Run on test data ! get_report_test_on_best_model(x_test, y_test, best_model, dm.idx2label, dm.scaler, info_run, has_saved_network=args.save_model)
num_elites = 2 perfect_game = False pipe_next_dist = None pipe_next_height = None population = [] pop_fitness = [] pop_size = 50 print_stats = False save_gen = 1 strategy = 0 structure = [3, 7, 1] # Initialize population. for i in range(pop_size): # Create neural net individual. individual = neuralNetwork.NeuralNetwork(structure) population.append(individual) pop_fitness.append(0) # every inidividual starts with a fitness of 0. def main(): global SCREEN, FPSCLOCK global pop_fitness pygame.init() FPSCLOCK = pygame.time.Clock() SCREEN = pygame.display.set_mode((int(SCREENWIDTH), int(SCREENHEIGHT))) pygame.display.set_caption('Flappy Bird') # numbers sprites for score display IMAGES['numbers'] = ( pygame.image.load('assets/sprites/0.png').convert_alpha(), pygame.image.load('assets/sprites/1.png').convert_alpha(),