def __init__(self, brain = None): super(creature, self).__init__() self.brain = brain self.r = np.zeros(2) self.v = np.ones(2) self.v/= np.sum(self.v*self.v) if brain == None: self.brain = network([4,10,2])
def __init__(self, world, brain = None): super(ant, self).__init__() self.world = world self.brain = brain if brain == None: self.brain = network([5+200,100,2]) self.energy = 1 self.position = [0,0] self.velocity = [0,0] self.timeAlive = 0 self.size = [1,2] self.mass = .1
def breed(self,other): childsBrain = [] for selfLayer,otherLayer in zip(self.brain.layers,other.brain.layers): shape = selfLayer.shape mask = np.rint(np.random.random(shape)) inverseMask = np.abs(mask-1) childLayer = selfLayer*mask+otherLayer*inverseMask if np.random.random()>.95: x = np.random.randint(childLayer.shape[0]) y = np.random.randint(childLayer.shape[1]) childLayer[x,y] = np.random.randn() childsBrain.append(selfLayer*mask+otherLayer*inverseMask) return(ant(self.world,brain = network([],layers = childsBrain)))
def breed(n1,n2): c1 = [] c2 = [] for layer1,layer2 in zip(n1.layers,n2.layers): mask = np.rint(np.random.random(layer1.shape)) inverse_mask = np.abs(mask-1) c = layer2*mask +layer1*inverse_mask x = np.random.randint(0,c.shape[0],20) y = np.random.randint(0,c.shape[1],20) c[x,y] = np.logical_not(c[x,y]) c1.append(c) c = layer1*mask +layer2*inverse_mask x = np.random.randint(0,c.shape[0],20) y = np.random.randint(0,c.shape[1],20) c[x,y] = np.logical_not(c[x,y]) c2.append(c) c1 = network('',layers = c1) c2 = network('',layers = c2) return([c1,c2])
import neuralNetwork as nn netarr = [2, 2] testInpts = [[10.0, 2.0], [5.0, 5.0], [5.0, 6.0], [4.0, 3.0], [1.0, 2.0]] outs = [[1.0 / 12, 1.0 / 12], [1.0 / 10, 1.0 / 10], [1.0 / 11, 1.0 / 11], [1.0 / 7, 1.0 / 7], [1.0 / 3, 1.0 / 3]] learningRate = .001 net = nn.network(netarr, learningRate) c = 0 while c < 20000: for a, b in zip(testInpts, outs): net.train(a, b) c += 1 correct = 0.0 testInpts = [[11.0, 3.0], [7.0, 3.0]] outs = [[1.0 / 14], [1.0 / 10]] for a, b in zip(testInpts, outs): pos = net.predict(a) if pos[0] > .5 and b[0] == 1: correct += 1.0 if pos[0] < .5 and b[0] == 0: correct += 1.0 print str(a) + " EXPECTED" + str(b) + " OUT " + str(pos) print correct / len(outs)
import numpy as np import sys sys.path.insert(0, '../../../network') from neuralNetwork import network import matplotlib.pyplot as plt numGenerations = 100 populationSize = 100 population = [network([2,100,2]) for i in range(populationSize)] def getPath(n,ri = [0,0],vi = [0,0],tFinal = 1,dt = .1): r = [ri] tInitial = 0 v = [vi] cost = 0 i = 0 while np.abs(r[-1][0])<1 and np.abs(r[-1][1])<1 and i<500: i+=1 a = 10*(n.feedForward(r[-1])[-1]-.5) v.append(v[-1]+a*dt) r.append(r[-1]+v[-1]*dt) r = np.array(r) return(r) def breed(n1,n2): c1 = [] c2 = [] for layer1,layer2 in zip(n1.layers,n2.layers):
import numpy as np import matplotlib.pyplot as plt """ Trains many neural networks in a row to invert a binary array and calculates the acuracy of that network and the creates a histogram of the acuracy. """ acuracy = [] length = 10 # the length of the binary array #each iteration generates and tests a neural network for i in range(2): #generate the network inverter = network([length,100,length]) #generate some training data trainingData = [] # for i in range(int(2**length/25)): for i in range(5): inData = np.floor(np.random.random(length)*2) outData = np.abs(inData-1) trainingData.append([inData,outData]) # print train #give the network the training data inverter.train(trainingData) #calculate how acutrate the netowrk is tot = 0 for i in range(1000):
f.extend(filenames) break for i,fileName in enumerate(f): img = Image.open(path+'/'+fileName) rsize = img.resize((N,N)) # Use Pillow to resize rsize = rsize.convert(mode = "L") rsizeArr = np.asarray(rsize).flatten() # Get array back if rsizeArr.shape == (N*N,): trainingData.append((rsizeArr,[0])) #generate the network meme_recogniser = network([N*N,1000,1]) #give the network the training data meme_recogniser.train(trainingData,errorThreshold = .04,eta = .2,verbose = True) numberOfImages = len(trainingData) numberOfCorrectGuesses = 0 for d in trainingData: inpt = d[0] otpt = d[1] guess = np.rint(meme_recogniser.feedForward(inpt)[-1]) if guess == otpt: numberOfCorrectGuesses+=1. print "test memes classified with "+str(numberOfCorrectGuesses/numberOfImages*100)+"% \acuracy" testData = []
import sys sys.path.insert(0, '../../network') from neuralNetwork import network import numpy as np import matplotlib.pyplot as plt trainingData = [] values = [0,0,0,1,1,1,1,0] prevStates = [[i,j,k] for i in range(2) for j in range(2) for k in range(2)] prevStates = np.array(prevStates)[::-1] for i in range(8): trainingData.append([prevStates[i],[values[i]]]) automota = network([3,7,1]) automota.train(trainingData) initialState = np.zeros(301) initialState[151] = 1 states = [initialState] for i in range(300): # print i nextState = [0] for j in range(1,len(initialState)-1): out = automota.feedForward(initialState[j-1:j+2]) nextState.append(out[-1][0]) nextState.append(0)
import numpy as np import sys sys.path.insert(0, '../../../network') from neuralNetwork import network import matplotlib matplotlib.use('TKAGG') import matplotlib.pyplot as plt import matplotlib.animation as animation IDX = 0 dt=.1 numGenerations = 50 populationSize = 50 mating_pool_size= populationSize/4 population = [network([5,100,1]) for i in range(populationSize)] def getCost(n): # a = getCostOneIter(n) # print 'cost is '+str(a) t= 0 for i in range(100): t+=getCostOneIter(n) # print t return(t/100.) getCost = np.vectorize(getCost) def getCostOneIter(n): # print 'evaluting a cost'