def multiplication(neurona, neuronb): neuronAplusBcarre = neuron(funcarre) neuronAcarre = neuron(funcarre) neuronBcarre = neuron(funcarre) neurona.bindTo(neuronAplusBcarre,1) neuronb.bindTo(neuronAplusBcarre,1) neurona.bindTo(neuronAcarre,1) neuronb.bindTo(neuronBcarre,1) neuronMult = neuron(fununit) neuronAplusBcarre.bindTo(neuronMult, 0.5) neuronAcarre.bindTo(neuronMult, -0.5) neuronBcarre.bindTo(neuronMult, -0.5) return neuronMult
def division(neurona,neuronx): neuronb = neuron(fundiv) neuronx.bindTo(neuronb,1) neuronAplusBcarre = neuron(funcarre) neuronAcarre = neuron(funcarre) neuronBcarre = neuron(funcarre) neurona.bindTo(neuronAplusBcarre,1) neuronb.bindTo(neuronAplusBcarre,1) neurona.bindTo(neuronAcarre,1) neuronb.bindTo(neuronBcarre,1) neuronDiv = neuron(fununit) neuronAplusBcarre.bindTo(neuronDiv, 0.5) neuronAcarre.bindTo(neuronDiv, -0.5) neuronBcarre.bindTo(neuronDiv, -0.5) return neuronDiv
def gaussienne(neuronx, neuronmu, neuronsigma): neuronnumerateur = neuron(funcarre) # binding x et mu neuronx.bindTo(neuronnumerateur, 1) neuronmu.bindTo(neuronnumerateur, -1) ## sigma carre neuronsigmacarre = neuron(funcarre) neuronsigma.bindTo(neuronsigmacarre, 1) # neuron division neurondivision = division(neuronnumerateur, neuronsigmacarre) neuronOutGaussienne = neuron(funexp) neurondivision.bindTo(neuronOutGaussienne, -1.0/2) return neuronOutGaussienne
def compute_local_gradient(x): #Object to store the inputs received _input = [] #Convert the data read to float for i in range(len(x)): _input.append(float(x[i])) #Calculate the local gradient for the output neurons for j in range(len(neuronListL2)): #The sigmoid Prime value for an is gotten from applying the sigmoid_function_prime to the same #input as was fed into the feedforward network sigmoidPrimeValue = neuron().sigmoid_function_prime(layer2Outputs[j]) #Multiply the sigmoid Prime value by the errors computed for a neuron localGradientL2.append(errors[j] * sigmoidPrimeValue) for k in range(len(neuronListL1)): #The sigmoid Prime value for a hidden layer is gotten the same way as it is for a outer layer neuron sigmoidPrimeValue = neuron().sigmoid_function_prime(layer1Outputs[k]) sumOutputLocalGradients = 0 #Multiply the sigmoid Prime Value by the sum of the local gradients of the output layer for _n in range(len(neuronListL2)): sumOutputLocalGradients += neuronListL2[_n].weights[ k] * localGradientL2[_n] localGradientL1.append(sumOutputLocalGradients * sigmoidPrimeValue)
def __init__(self, DNA, x, y, ID): self.DNA = DNA self.age = 0 self.birthDateTime = datetime.datetime.now() self.HP = 10 self.x = x self.y = y self.ID = ID self.appearance = [self.DNA[3], self.DNA[4], self.DNA[5]] self.neuron = neuron(self.DNA, 0, 0, 0) self.entityType = 2 self.inputs = [0, 0, 0, 0] self.updateCount = 0
def makeStimulus(neuron): print neuron VecAxon = h.Vector() VecAxon.record(neuron(0.5)._ref_v) stim = h.IClamp(neuron(0)) stim.dur = 50 vecCurrent = h.Vector() vecCurrent.record(stim._ref_i) time = h.Vector() time.record(h._ref_t) listOfSines = [] listOfTimes = [] for i in range(0, stim.dur): if random() > 0.75: listOfSines.append(10) else: listOfSines.append(0) listOfTimes.append(i) VecT = h.Vector(listOfTimes) VecStim = h.Vector(listOfSines) VecStim.play(stim._ref_amp, VecT, 1) return (VecStim, VecT, stim)
def remember(self, img, label): #img=self.sdr(img) nmax = self.look(img) # found mutipy? #remember every thing diffrence if (nmax!=None): lb=nmax.axon.outneurons[0].label if lb == label: #allow mistake ,train twice return nmax #if nmax.dendritic.value==self.ROWS*self.COLS: # return nmax #else: # print(lb,label,nmax.dendritic.value) if (self.knowledges.__contains__(label)): # print("Already in,create dendritic only", label) nlb = self.knowledges[label] n=neuron() self.pallium.append(n) nlb.inaxon.append(n) n.axon.outneurons.append(nlb) d=n.dendritic else: nlb = neuron() self.knowledges[label]=nlb nlb.label=label n=neuron() self.pallium.append(n) nlb.inaxon.append(n) n.axon.outneurons.append(nlb) d=n.dendritic r, c = img.shape for i in range(r): for j in range(c): if (img[i][j] > 0):#positive d.connectfrom(self.neurons[i, j].axon, 1) else:#negative d.connectfrom(self.neurons[i, j].axon, -1) return n
def makeStimulus(neuron, simTime): VecAxon = h.Vector() VecAxon.record(neuron(0.5)._ref_v) stim = h.IClamp(neuron(0)) stim.dur = simTime vecCurrent = h.Vector() vecCurrent.record(stim._ref_i) time = h.Vector() time.record(h._ref_t) listOfSines = [] listOfTimes = [] for i in range(0, simTime): a = random.randint(1, 100) if a > 95: listOfSines.append(15) else: listOfSines.append(0) listOfTimes.append(i) print listOfSines VecT = h.Vector(listOfTimes) VecStim = h.Vector(listOfSines) VecStim.play(stim._ref_amp, VecT, 1) return (VecStim, VecT, stim)
def conv(self,img): self.input(img) ROWS, COLS = img.shape #SDR->COND3x3->SDR-COND3X3 self.layer1 = np.array([[neuron() for ii in range(COLS//3)] for jj in range(ROWS//3)]) # brains cimg=np.zeros((ROWS//3,COLS//3)) for i in range(ROWS//3): for j in range(COLS//3): n=self.layer1[i, j] #connect pre for x in range(3): for y in range(3): n.dendritic.connectfrom(self.neurons[3*i+y,3*j+x].axon,1) n.calcValue() print(n.value) if n.value>4: cimg[i,j]=1 return cimg
from neuron import * from dendr import * #test n0, n1, n2, n3, n4, n5 = neuron("n0"), neuron("n1"), neuron("n2"), neuron( "n3"), neuron("n4"), neuron("n5") n5.connect_entry_neuron(n2) n5.connect_entry_neuron(n3) n5.connect_entry_neuron(n4) n5.dendrs[0].set_weight(1) n5.dendrs[1].set_weight(2) n5.dendrs[2].set_weight(4) n4.connect_entry_neuron(n1) n4.connect_entry_neuron(n0) n4.dendrs[0].set_weight(-2) n4.dendrs[1].set_weight(3) n3.connect_entry_neuron(n1) n3.connect_entry_neuron(n0) n3.dendrs[0].set_weight(1) n3.dendrs[1].set_weight(1) n2.connect_entry_neuron(n1) n2.connect_entry_neuron(n0) n2.dendrs[0].set_weight(2) n2.dendrs[1].set_weight(-1) n5.show(0)
from snn.var_th import threshold import os #potentials of output neurons pot_arrays = [] for i in range(par.n): pot_arrays.append([]) #time series time = np.arange(1, par.T + 1, 1) layer2 = [] # creating the hidden layer of neurons for i in range(par.n): a = neuron() layer2.append(a) #synapse matrix initialization synapse = np.zeros((par.n, par.m)) for i in range(par.n): for j in range(par.m): synapse[i][j] = random.uniform(0, par.w_max * 0.5) spike_probe = [] for i in range(par.n): spike_probe.append([(0, 0)]) for k in range(par.epoch): for i in range(3):
def create_layer1_neurons(): neuronListL1.clear() n10 = neuron() n10.layer = 1 n10.number = 0 n10.get_weight_params() n10.get_bias() n10.add_input_neurons(neuronListL0) neuronListL1.append(n10) n11 = neuron() n11.layer = 1 n11.number = 1 n11.get_weight_params() n11.get_bias() n11.add_input_neurons(neuronListL0) neuronListL1.append(n11) n12 = neuron() n12.layer = 1 n12.number = 2 n12.get_weight_params() n12.get_bias() n12.add_input_neurons(neuronListL0) neuronListL1.append(n12) n13 = neuron() n13.layer = 1 n13.number = 3 n13.get_weight_params() n13.get_bias() n13.add_input_neurons(neuronListL0) neuronListL1.append(n13) n14 = neuron() n14.layer = 1 n14.number = 4 n14.get_weight_params() n14.get_bias() n14.add_input_neurons(neuronListL0) neuronListL1.append(n14) n15 = neuron() n15.layer = 1 n15.number = 5 n15.get_weight_params() n15.get_bias() n15.add_input_neurons(neuronListL0) neuronListL1.append(n15) n16 = neuron() n16.layer = 1 n16.number = 6 n16.get_weight_params() n16.get_bias() n16.add_input_neurons(neuronListL0) neuronListL1.append(n16) n17 = neuron() n17.layer = 1 n17.number = 7 n17.get_weight_params() n17.get_bias() n17.add_input_neurons(neuronListL0) neuronListL1.append(n17) n18 = neuron() n18.layer = 1 n18.number = 8 n18.get_weight_params() n18.get_bias() n18.add_input_neurons(neuronListL0) neuronListL1.append(n18) n19 = neuron() n19.layer = 1 n19.number = 9 n19.get_weight_params() n19.get_bias() n19.add_input_neurons(neuronListL0) neuronListL1.append(n19) n110 = neuron() n110.layer = 1 n110.number = 10 n110.get_weight_params() n110.get_bias() n110.add_input_neurons(neuronListL0) neuronListL1.append(n110)
def reform(self): dictaxonpolarity = {} dictlen = {} dictdendritic = {} dictset={} for i in range(self.ROWS): for j in range(self.COLS): # print("R,C",i,j) axon = self.neurons[i, j].axon if (len(axon.synapses) == 0): continue #calc axon,polarity in set for s in axon.synapses: axonpolarity = (axon, s.polarity) if (axonpolarity in dictaxonpolarity): dictaxonpolarity[axonpolarity].append(s.dendritic) else: dictaxonpolarity[axonpolarity] = [s.dendritic] for n in self.pallium: axon = n.axon if (len(axon.synapses) == 0): continue # calc axon,polarity in set for s in axon.synapses: axonpolarity = (axon, s.polarity) if (axonpolarity in dictaxonpolarity): dictaxonpolarity[axonpolarity].append(s.dendritic) else: dictaxonpolarity[axonpolarity] = [s.dendritic] for axonpolarity in dictaxonpolarity: dictlen[str(set(dictaxonpolarity[axonpolarity]))] = len(set(dictaxonpolarity[axonpolarity])) dictdendritic[str(set(dictaxonpolarity[axonpolarity]))] = set(dictaxonpolarity[axonpolarity]) if str(set(dictaxonpolarity[axonpolarity])) in dictset: dictset[str(set(dictaxonpolarity[axonpolarity]))].append(axonpolarity) else: dictset[str(set(dictaxonpolarity[axonpolarity]))] = [axonpolarity] list1 = sorted(dictlen.items(), key=lambda x: x[1], reverse=True) # print(list1) for (k, v) in list1: # #v=dendrtic cnt if (v < 2):# 4: 2*4s+4d => 2s+1d+4s+1d+1n break dset = dictset[k] ncnt=len(dset) if(ncnt<2):# dendritic must have up 2 synapses continue if(ncnt*v<8): continue ds = dictdendritic[k] n = neuron() self.pallium.append(n) for (axon,polarity) in dset: n.dendritic.connectfrom(axon,polarity) for d in ds: d.connectfrom(n.axon,1) for (axon, polarity) in dset: d.disconnectfrom(axon,polarity) self.sortpallium()
def learn_20190102(self, img, label):#digui is slow self.feel(img) self.actived=[] alike=[] newn = neuron() d = newn.dendritic if (self.knowledges.__contains__(label)): # print("Already in,create dendritic only", label) nlb = self.knowledges[label] else: nlb = neuron() self.knowledges[label] = nlb nlb.label = label lenactived=0 for ilayer in range(1,len(self.layers)+1,1):#skip a lay ,only sdr layer=self.layers[-ilayer] #pltshow(self.outputlayer(layer)) R, C = layer.shape for r in range(R): for c in range(C): n = layer[r, c] d.connectfrom(n.axon, n.value) #self.conductlayer(layer,self.actived) #for r in range(R):#too slowly # for c in range(C): # layer[r, c].conduct(self.actived) for n in self.infrneurons[-ilayer]: n.calcValue() if n.dendritic.value>=len(n.dendritic.synapses):#actived if n.axon.outneurons != []: self.actived.append(n) #if found actived label already in,return #else if found actived but not it history need renew and remember this label #else :no actived, add memory if(len(self.actived)==lenactived): self.infrneurons[-ilayer].append(newn) nlb.inaxon.append(newn) newn.axon.outneurons.append(nlb) #remember img,label , maybe already in memory #newn.memory = [self.remember(img, label)] newn.memory=[self.remember(img,label)] # #1.remember this #R,C=layer.shape break# out else: #print(len(self.actived)) #actived new one if len(self.actived)==lenactived+1: act = self.actived[lenactived] #and it has one outneurons and label is me if len(act.axon.outneurons) == 1 \ and act.axon.outneurons[0].label == label: # found nhistory = self.remember(img, label) if nhistory not in self.actived[0].memory: self.actived[0].memory.append(nhistory) del (newn) # self.actived[0].memory.append(img) break #other else renew memory for ia in range(lenactived,len(self.actived)): act = self.actived[ia] if len(act.axon.outneurons)==1 and len(act.memory)>0: #memory need renew alike = alike+act.memory act.memory.clear() if nlb not in act.axon.outneurons: act.axon.outneurons.append(nlb) lenactived = len(self.actived) if len(alike)>1 and ilayer>len(self.layers): print("same img have two diffrent labels!",len(alike)) #for a in alike: # a.memory.clear() return alike
def feel_org(self, img): self.input(img) ROWS, COLS = img.shape self.layers=[] lastlayer = self.neurons #sdr layer = np.array([[neuron() for ii in range(COLS)] for jj in range(ROWS)]) # brains self.layers.append(layer) for i in range(1, ROWS - 1): for j in range(1, COLS - 1): n = layer[i, j] # connect pre for x in range(-1, 2): for y in range(-1, 2): n.dendritic.connectfrom(lastlayer[i + x, j + y].axon, 1) if (x == 0 and y == 0): # max continue n.indendritics.append(layer[i + x, j + y].dendritic) n.nagativeaxons.append(layer[i + x, j + y].axon) cimg = np.zeros((ROWS, COLS)) for i in range(ROWS): for j in range(COLS): n = layer[i, j] n.calcDendritic() # hengxiangyizhi n.value =1 for i in range(ROWS): for j in range(COLS): n = layer[i, j] if n.indendritics == [] or n.dendritic.value == 0: continue vmax = max(n.indendritics, key=lambda v: v.value) if n.dendritic.value >= vmax.value: n.value = n.dendritic.value cimg[i, j] = 1 pltshow(cimg) # sdr cimg = np.zeros((ROWS, COLS)) for i in range(ROWS): for j in range(COLS): n = layer[i, j] if n.nagativeaxons == [] or n.value == 0: continue vmax = max(n.nagativeaxons, key=lambda v: v.connectedNeuron.actived) if vmax.connectedNeuron.actived: continue n.actived = True cimg[i, j] = 1 # n.dendritic.value pltshow(cimg) # conv2 while ROWS>=6: ROWS = ROWS // 3 COLS = COLS // 3 lastlayer=layer layer = np.array([[neuron() for ii in range(COLS)] for jj in range(ROWS)]) # brains self.layers.append(layer) cimg = np.zeros((ROWS, COLS)) for i in range(1, ROWS - 1): for j in range(1, COLS - 1): n = layer[i, j] # connect pre for x in range(-1, 2): for y in range(-1, 2): n.dendritic.connectfrom(lastlayer[3 * i + y, 3 * j + x].axon, 1) if (x == 0 and y == 0): # max continue n.indendritics.append(layer[i + x, j + y].dendritic) n.nagativeaxons.append(layer[i + x, j + y].axon) n.calcDendritic() if n.dendritic.value > 0: n.value = 1 cimg[i, j] = 1 pltshow(cimg) # sdr cimg = np.zeros((ROWS, COLS)) for i in range(ROWS): for j in range(COLS): n = layer[i, j] if n.nagativeaxons == [] or n.value == 0: continue vmax = max(n.nagativeaxons, key=lambda v: v.connectedNeuron.actived) if vmax.connectedNeuron.actived: continue n.actived = True cimg[i, j] = 1 # n.dendritic.value pltshow(cimg) return cimg
def Extraction(self,la,ra,ca,wa,ha,lb,rb,cb): for c in range(1,wa-1): for r in range(1,ha-1): nla = self.getNeuron(la, ra + r , ca + c ) nlb = self.getNeuron(lb, rb + r , cb + c ) n4=neuron(-1,-1,-1) n4.connectfrom(self.getNeuron(la,ra+r,ca+c)) n4.connectfrom(self.getNeuron(la,ra+r-1,ca+c)) n4.connectfrom(self.getNeuron(la,ra+r+1,ca+c)) n4.connectfrom(self.getNeuron(la,ra+r,ca+c-1)) n4.connectfrom(self.getNeuron(la,ra+r,ca+c+1)) n4.axon.criticalvalue=5 m1=neuron(-1,-1,-1) #0/1/1/0 m2=neuron(-1,-1,-1) #0-1-1-0 m11=neuron(-1,-1,-1) #0/1/1/0 m22=neuron(-1,-1,-1) #0-1-1-0 m3=neuron(-1,-1,-1) #0/1/0 m4=neuron(-1,-1,-1) #0-1-0 self.connectMat(m1,la,ra+r,ca+c,[[0],[1],[1],[0]])#????? self.connectMat(m11,la,ra+r+1,ca+c,[[0],[1],[1],[0]]) self.connectMat(m2,la,ra+r,ca+c,[[0,1,1,0]]) self.connectMat(m22,la,ra+r,ca+c+1,[[0,1,1,0]]) self.connectMat(m3,la,ra+r,ca+c,[[0],[1],[0]]) self.connectMat(m4,la,ra+r,ca+c,[[0,1,0]]) m=neuron(-1,-1,-1) m.connectfrom(m1) m.connectfrom(m11) m.connectfrom(m2) m.connectfrom(m22) m.connectfrom(m3) m.connectfrom(m4) m.axon.criticalvalue=1 # 0 1 1 0 # 1 0 0 1 m5=neuron(-1,-1,-5) #0/1/0 m6=neuron(-1,-1,-6) #0-1-0 m55=neuron(-1,-1,-55) #0/1/0 m66=neuron(-1,-1,-66) #0-1-0 self.connectMat(m5,la,ra+r,ca+c,[[1,0],[0,1]]) self.connectMat(m55,la,ra+r+1,ca+c+1,[[1,0],[0,1]]) self.connectMat(m6,la,ra+r,ca+c+1,[[0,1],[1,0]]) self.connectMat(m66,la,ra+r+1,ca+c+1,[[0,1],[1,0]]) m.connectfrom(m5) m.connectfrom(m55) m.connectfrom(m6) m.connectfrom(m66) # 0 1 x # 1 1 0 m7=neuron(-1,-1,-7) m77=neuron(-1,-1,-77) self.connectMat(m7,la,ra+r,ca+c,[[0,1,1],[1,1,0]]) self.connectMat(m77,la,ra+r,ca+c,[[0,1,0],[1,1,0]]) m.connectfrom(m7) m.connectfrom(m77) # - - - # 0 1 1 # x 1 0 m8=neuron(-1,-1,-7) m88=neuron(-1,-1,-77) self.connectMat(m8,la,ra+r+1,ca+c,[[0,1,1],[1,1,0]]) self.connectMat(m88,la,ra+r+1,ca+c,[[0,1,1],[0,1,0]]) m.connectfrom(m8) m.connectfrom(m88) #(n4>0 zxd ) or m>0 ### Double xiangsu n4.connectto(nlb) m.connectto(nlb) nlb.axon.criticalvalue=1
def __init__(self): self.neurons=np.array([[neuron() for ii in range(COLS)] for jj in range(ROWS)])# brains #self.pallium=[] # 促发记忆主要由大脑皮层控制 self.knowledges={} self.dendritics=[]