def test_getOutput(self): nn = neuralNet.NeuralNet(4, 4, 1, 6) numberOfWeights = (4 + 1) * 6 + (6 + 1) * 4 weights = [1 for i in range(numberOfWeights)] nn.putWeights(weights) in_ = [1, 1, 1, 1] self.assertEqual(len(nn.getOutput(in_)), 4)
def main(): start = time.clock() numInput, numOutput, numHiddenLayer, neuronPerLayer = 8, 1, 1, 2 net = NN.NeuralNet(numInput, numOutput, numHiddenLayer, neuronPerLayer) # ga = GA.GenAlg() with open('ga_tftrain_sig.pickle', 'rb') as fp: print(fp.name) ga = pickle.load(fp) end = time.clock() runtime = end - start print("init net and GA: " + str(runtime)) width = 8 # of board height = 8 # of board games = 100 player1wins = 0 for i in range(0, games): print("game num: " + str(i)) start = time.clock() if play(player1, player2, width, height, ga, net) == 1: player1wins += 1 end = time.clock() runtime = end - start print("runtime is " + str(runtime)) # pickle.dump(ga, open('ga-data_step.pickle', 'wb')) return 'Player 1 won ' + str(player1wins) + ' out of ' + str( games) + ' games of size ' + str(height) + 'x' + str( width) + '. Total Game Time: ' + str(runtime) + ' sec'
def createNetwork(createFile, trainFromFile, nnParameters): ''' TRAIN DATA PROCESSING ''' train_data_file_name = 'data/spotifyTrainData.csv' print "Train from file: ", trainFromFile, not trainFromFile if not trainFromFile: access_token = waitForAccessToken() sa.crawlSpotifyData(access_token) sa.labelSpotifyData(access_token) if createFile: createDataFileFromSpotify(train_data_file_name) else: if trainFromFile: train_data, train_label = getTrainDataFromFile( train_data_file_name) else: train_data, train_label = getTrainDataFromSpotify() ''' NEURAL NETWORK ''' nn = NN.NeuralNet(numInputs=nnParameters[0], layers=nnParameters[1], numOutputs=nnParameters[2], learningRate=nnParameters[3], momentum=nnParameters[4]) nn.printArchitecture() nn.train(train_data[:800], train_label[:800], num_epoch=nnParameters[5]) nn.test(train_data[800:], train_label[800:])
def test_putWeights(self): nn = neuralNet.NeuralNet(4, 4, 1, 6) numberOfWeights = (4 + 1) * 6 + (6 + 1) * 4 weights = [1 for i in range(numberOfWeights)] nn.putWeights(weights) for neuron in nn.layers[0].neurons: self.assertEqual(len(neuron.weights), 5) for neuron in nn.layers[1].neurons: self.assertEqual(len(neuron.weights), 7)
def train1(numOfTraining): width = 8 # of board height = 8 # of board with open('true.pickle', 'rb') as fp: trueboard = list(pickle.load(fp)) with open('false.pickle', 'rb') as fp: falseboard = list(pickle.load(fp)) numInput, numOutput, numHiddenLayer, neuronPerLayer = 8, 1, 1, 8 net = NN.NeuralNet(numInput, numOutput, numHiddenLayer, neuronPerLayer) ga = GA.GenAlg() # with open('ga-data_step.pickle', 'rb') as fp: # print(fp.name) # ga = pickle.load(fp) for j in range(numOfTraining): start = time.clock() print("iteration #: " + str(j)) board = initial_board(width, height) winboard.detranslate(random.choice(falseboard), board) for genome in ga: net.putWeights(genome.weights) move1 = net.move(board, True) genome.fitness = ga.Fitness(0.0, move1[0]) for genome in ga: print(genome.fitness, end=" ") print() ga.Epoch() winboard.detranslate(random.choice(trueboard), board) for genome in ga: net.putWeights(genome.weights) move1 = net.move(board, True) genome.fitness = ga.Fitness(1.0, move1[0]) for genome in ga: print(genome.fitness, end=" ") print() ga.Epoch() end = time.clock() runtime = end - start print("runtime is " + str(runtime)) pickle.dump(ga, open('ga_tftrain_sig.pickle', 'wb'))
def __init__(self,population,numOfInputs,numOfOutputs, numOfHiddenLayers,neuronsPerHiddenLayer, crossoverRate,mutationRate, geneInit = chromosome.geneFunction): self.population = population self.generation = 1 self.chromosomeLength = neuronsPerHiddenLayer*(numOfInputs+1) self.chromosomeLength += (neuronsPerHiddenLayer+1)*numOfOutputs self.chromosomeLength += sum([neuronsPerHiddenLayer*(neuronsPerHiddenLayer+1) for i in xrange(1,numOfHiddenLayers)]) self.geneticAlg = geneAlg.GeneticAlgorithm(population, crossoverRate,mutationRate,self.chromosomeLength,geneInit) self.neuralNets = [neuralNet.NeuralNet(numOfInputs,numOfOutputs, numOfHiddenLayers,neuronsPerHiddenLayer) for i in xrange(population)] for nNet, chromo in zip(self.neuralNets,self.geneticAlg.chromosomes): nNet.putWeights(chromo.genes)
def main(): with open('true.pickle', 'rb') as fp: trueboard = list(pickle.load(fp)) with open('false.pickle', 'rb') as fp: falseboard = list(pickle.load(fp)) print(len(trueboard), len(falseboard)) nn = neuralNet.NeuralNet(0, 0, 0, 0) data = [] for board in trueboard: temp_board = training.initial_board(8, 8) winboard.detranslate(board, temp_board) data.append(np.array(nn.translate(temp_board))) for board in falseboard: temp_board = training.initial_board(8, 8) winboard.detranslate(board, temp_board) data.append(np.array(nn.translate(temp_board))) print(len(data)) data = np.array(data) k = 2 km = Kmeans() clusters = km.assignment(data, k, trueboard, falseboard, nn) i = 0 while not clusters.sholdStop(): km.oneIter(data, clusters) print("iter num " + str(i)) # if i%10 == 0: # clusters.plot("C:/Users/Adiel/Desktop/figures/" + "fig_" + str(i) + ".png") i += 1 pickle.dump(clusters, open("clusters.pickle", "wb")) for cluster in clusters: print(len(cluster))
import neuralNet as nn import tester as test # create a new neural net input_nodes = 3 output_nodes = 3 hidden_nodes = 3 learning_rate = 0.3 net = nn.NeuralNet(input_nodes, output_nodes, hidden_nodes, learning_rate)
def test_numberOfLayers(self): nn = neuralNet.NeuralNet(4, 4, 1, 6) self.assertEqual(len(nn.layers), 2) nn = neuralNet.NeuralNet(4, 4, 3, 6) self.assertEqual(len(nn.layers), 4)
def test_NeuralNetCreate(self): nn = neuralNet.NeuralNet(4, 4, 1, 6)
def test_numberOfNeurons(self): nn = neuralNet.NeuralNet(4, 4, 1, 6) self.assertEqual(len(nn.layers[0].neurons), 6) self.assertEqual(len(nn.layers[1].neurons), 4)
import numpy as np import pickle # from sklearn import svm from sklearn import linear_model import random import neuralNet as NN import training as trn import winboard with open('true.pickle', 'rb') as fp: trueboard = list(pickle.load(fp)) with open('false.pickle', 'rb') as fp: falseboard = list(pickle.load(fp)) nn = NN.NeuralNet(0, 0, 0, 0) model = linear_model.Perceptron(penalty=None, alpha=0.0001, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, eta0=1.0, n_jobs=1, random_state=0, class_weight=None, warm_start=False) # with open('model.pickle', 'rb') as fp: # model = pickle.load(fp) boards = [] tag = [] width, height = 8, 8 i = 0
#%% import numpy as np import neuralNet as n import matplotlib.pyplot as plt X = np.array([[0,0,1,0,1,0,1],[0,1,1,0,0,0,0],[1,0,1,0,0,1,0],[1,1,1,1,1,1,1]]) y = np.array([[0],[1],[1],[0]]) nn = n.NeuralNet(7,1,8) print(y) nn.trainLoop(X,y,2000) show=False if(show): plt.plot(nn.plotPointsX[10:],nn.plotPointsY1[10:],c='red') plt.plot(nn.plotPointsX[10:],nn.plotPointsY2[10:],c='blue') for ind,o in enumerate(nn.output): print("the answer "+str(ind)+" is "+str(o[0])) plt.show() nn.answer([0,1,1,0,0,0,0])
def train3(numOfTraining): width = 8 # of board height = 8 # of board with open('true.pickle', 'rb') as fp: trueboard = list(pickle.load(fp)) with open('false.pickle', 'rb') as fp: falseboard = list(pickle.load(fp)) # with open('net.pickle', 'rb') as fp: # net = pickle.load(fp) numInput, numOutput, numHiddenLayer, neuronPerLayer = 8, 1, 1, 48 net = NN.NeuralNet(numInput, numOutput, numHiddenLayer, neuronPerLayer) for j in range(numOfTraining): # start = time.clock() print("iteration #: " + str(j)) trueboards = random.sample(trueboard, 400) falseboards = random.sample(falseboard, 100) for i in range(400): tb = initial_board(width, height) winboard.detranslate(trueboards[i], tb) net.train(tb, [1.0]) if (i % 4): fb = initial_board(width, height) winboard.detranslate(falseboards[i % 100], fb) net.train(fb, [0.0]) # end = time.clock() # runtime = end - start # print("runtime is " + str(runtime)) # trueboards = random.sample(trueboard, 100) # falseboards = random.sample(falseboard, 100) avg_true = 0 avg_false = 0 avg_ans_false = 0 avg_ans_true = 0 trueboards = random.sample(trueboard, 100) falseboards = random.sample(falseboard, 100) for i in range(100): tb = initial_board(width, height) fb = initial_board(width, height) winboard.detranslate(trueboards[i], tb) winboard.detranslate(falseboards[i], fb) move1 = net.move(tb, True) move2 = net.move(fb, True) avg_ans_true += move1[0] avg_ans_false += move2[0] if math.fabs(move1[0] - 1.0) <= 0.1: avg_true += 1 if math.fabs(move2[0] - 0.0) <= 0.1: avg_false += 1 print("avg_true: " + str(avg_true / 100) + " avg_false: " + str(avg_false / 100)) print("avg_true_ans: " + str(avg_ans_true / 100) + " avg_false_ans: " + str(avg_ans_false / 100)) pickle.dump(net, open('net.pickle', 'wb'))
def train2(numOfTraining): width = 8 # of board height = 8 # of board with open('true.pickle', 'rb') as fp: trueboard = list(pickle.load(fp)) with open('false.pickle', 'rb') as fp: falseboard = list(pickle.load(fp)) numInput, numOutput, numHiddenLayer, neuronPerLayer = 8, 1, 1, 2 net = NN.NeuralNet(numInput, numOutput, numHiddenLayer, neuronPerLayer) ga = GA.GenAlg() # elite = set() # with open('elite.pickle', 'rb') as fp: # print(fp.name) # print(sorted(pickle.load(fp).population, key=GA.Genome.getFitness)[-1:-101]) # ga.population = sorted(pickle.load(fp).population, key = GA.Genome.getFitness)[-1:-101] # random.shuffle(ga.population) # for genome in ga.population: # genome.fitness = 0.0 for j in range(numOfTraining): start = time.clock() print("iteration #: " + str(j)) trueboards = random.sample(trueboard, 100) falseboards = random.sample(falseboard, 100) for i in range(100): tb = initial_board(width, height) fb = initial_board(width, height) winboard.detranslate(trueboards[i], tb) winboard.detranslate(falseboards[i], fb) for genome in ga: net.putWeights(genome.weights) move1 = net.move(tb, False) move2 = net.move(fb, False) if move1[0] == 1.0: genome.fitness += 1 if move2[0] == 0.0: genome.fitness += 1 ga.population.sort(key=GA.Genome.getFitness) ga.population.reverse() for genome in ga: print(genome.fitness, end=" ") if genome.fitness == 200: print("solution is genome " + str(ga.population.index(genome))) pickle.dump(ga, open('ga_data_tftrain_step.pickle', 'wb')) # ga.population = list(elite) # pickle.dump(ga, open('elite.pickle', 'wb')) exit() # elif genome.fitness > 100: # elite.add(genome.copy()) # print() # print(len(elite)) if j == numOfTraining - 1: pickle.dump(ga, open('ga_data_tftrain_step.pickle', 'wb')) # print(len(elite)) # ga.population = list(elite) # pickle.dump(ga, open('elite.pickle', 'wb')) exit() ga.Epoch() end = time.clock() runtime = end - start print("runtime is " + str(runtime))