Ejemplo n.º 1
0
 def __init__(self, a, b, countTeachElement,
              countNeurons, sumError, functionText="math.sin(x)", debug=False):
     self.debug = debug
     self.countTeachElement = countTeachElement
     self.countNeurons = countNeurons
     self.maxError = sumError
     for i in range(0, countNeurons):
         self.state.layer1.append(neuron(1))
     self.state.layer2.append(neuron(countNeurons))
     self.__functionText = functionText
     self.__inputCollection = self.__buildInputCollection(a, b, countTeachElement)
     self.__teachCollection = self.__buildTeachCollection(self.__inputCollection)
     self.__scaleCollection = self.__buildScaleCollection(self.__teachCollection)
Ejemplo n.º 2
0
    def create_empty_red(self, order=[1, 3, 1]):
        for i in range(len(order)):
            if i == 0:  # Capa de entrada
                layer = []
                for z in range(order[i]
                               ):  # se crea la cantidad de neuronas en la capa
                    layer.append(neuron(None, None, None, 0, z))
                layer.append(neuron(None, None, None, None,
                                    None))  #se le añade el bias neuron
                self.red.append(layer)
                logging.info("Se creo capa de entrada con %s neuronas",
                             order[i])

                continue

            if i < (len(order) - 1):  # Capa Oculta
                layer = []
                for z in range(order[i]
                               ):  # se crea la cantidad de neuronas en la capa
                    layer.append(
                        neuron(
                            None,
                            np.random.uniform(low=0,
                                              high=1,
                                              size=(len(self.red[-1]))), None,
                            0, z))
                layer.append(neuron(None, len(self.red[-1]), None, None,
                                    None))  # se le añade el bias neuron
                self.red.append(layer)  # conecta la capa actual a NN
                logging.info("Se creo capa de oculta con %s neuronas",
                             order[i])
                continue

            if i == len(order) - 1:  # Capa de Salida
                layer = []
                for z in range(order[i]
                               ):  # se crea la cantidad de neuronas en la capa
                    layer.append(
                        neuron(
                            None,
                            np.random.uniform(low=0,
                                              high=1,
                                              size=(len(self.red[-1]))), None,
                            0, z))
                self.red.append(layer)  # conecta la capa actual a NN
                logging.info("Se creo capa de Salida con %s neuronas",
                             order[i])
                continue
        self.connect_layers()
Ejemplo n.º 3
0
def create_list_of_neurons(size, start_label, layer_type, prev_layer_size,
                           activation_function, learning_rate):
    l = []
    for ii in range(0, size):
        l.append(
            neuron.neuron(start_label + ii, layer_type, prev_layer_size,
                          activation_function, learning_rate))
    return l
Ejemplo n.º 4
0
    def __init__(self, input_len, hidden_len, hidden_num, output_len):
        self.input_len = input_len
        self.hidden_num = hidden_num
        self.hidden_len = hidden_len
        self.output_len = output_len

        self.input_layer = [-999 for i in range(0, input_len)]
        self.hidden_layers = [[
            neuron('Relu', input_len) for i in range(hidden_len)
        ]]
        if hidden_num != 0:
            for k in range(hidden_num - 1):
                self.hidden_layers.append(
                    [neuron('Relu', hidden_len) for i in range(hidden_len)])
                # Commented below is half-broken code for hidden layer initiliazation with varying hidden layer lengths
                #self.hidden_layers.append([neuron('Relu',len(self.hidden_layers[j])) for k in range(0,hidden_len)])
        self.output_layer = [
            neuron('sigmoid', hidden_len) for i in range(0, output_len)
        ]
Ejemplo n.º 5
0
    def __buildLayerNeurons(self):
        layer = np.empty(self.numberOfBiasNeurons + self.numberOfNeurons,
                         dtype=object)

        for i in range(self.numberOfBiasNeurons):
            layer[i] = neuron(layerName=self.layerName,
                              layerNeuronNumber=i + 1,
                              isBiasNeuron=True)
            pass

        for i in range(self.numberOfNeurons):
            if self.isInputLayer:
                if isinstance(self.inputLayerInputs, type(None)):
                    layer[i + self.numberOfBiasNeurons] = neuron(
                        layerName=self.layerName,
                        layerNeuronNumber=i + self.numberOfBiasNeurons + 1,
                        isInputNeuron=self.isInputLayer)
                    pass
                else:
                    layer[i + self.numberOfBiasNeurons] = neuron(
                        layerName=self.layerName,
                        layerNeuronNumber=i + self.numberOfBiasNeurons + 1,
                        isInputNeuron=self.isInputLayer,
                        input=self.inputLayerInputs[i])
                    pass
                pass
            elif self.isOutputLayer:
                # Outputlayer has no bias Neurons
                layer[i] = neuron(layerName=self.layerName,
                                  layerNeuronNumber=i + 1,
                                  isOutputNeuron=True)
                pass
            else:
                layer[i + self.numberOfBiasNeurons] = neuron(
                    layerName=self.layerName,
                    layerNeuronNumber=i + self.numberOfBiasNeurons + 1)
                pass
            pass

        return layer
        pass
Ejemplo n.º 6
0
 def __init__(self,
              a,
              b,
              countTeachElement,
              countNeurons,
              sumError,
              functionText="math.sin(x)",
              debug=False):
     self.debug = debug
     self.countTeachElement = countTeachElement
     self.countNeurons = countNeurons
     self.maxError = sumError
     for i in range(0, countNeurons):
         self.state.layer1.append(neuron(1))
     self.state.layer2.append(neuron(countNeurons))
     self.__functionText = functionText
     self.__inputCollection = self.__buildInputCollection(
         a, b, countTeachElement)
     self.__teachCollection = self.__buildTeachCollection(
         self.__inputCollection)
     self.__scaleCollection = self.__buildScaleCollection(
         self.__teachCollection)
Ejemplo n.º 7
0
def atquestion(request, userid, questionid):
    userid = int(userid)
    questionid = int(questionid)
    print(questionid)
    if request.POST.has_key("choice") == 0:
        return get_response(403, '{"message":"no choice"}', {})
    p = Question.objects.filter(id=questionid)
    if p.count() == 0:
        return get_response(403, '{"message":"no question"}', {})
    e = Users.objects.filter(id=userid)
    if e.count() == 0:
        return get_response(403, '{"message":"no use"}', {})
    userid = Users.objects.filter(id=userid)[0]
    questionid = Question.objects.filter(id=questionid)[0]
    if request.POST["choice"] == p[0].answer:
        l = "right"
    else:
        l = "wrong"

    e = UserQuestion.objects.filter(questionid=questionid,
                                    userid=userid,
                                    righte=0)
    if e.count() == 0:
        z = UserQuestion(righte=1,
                         questionid=questionid,
                         userid=userid,
                         answer=request.POST["choice"],
                         time=datetime.now(),
                         right=p[0].answer,
                         correct=l)
        z.save()

    if p[0].answer == request.POST["choice"]:
        e = neuron(userid, questionid)
        return get_response(200, '{"message":"right"}', {})
    else:
        w = neuron(userid, questionid)
        return get_response(200, '{"message":"wrong"}', {})
Ejemplo n.º 8
0
    def __init__(self, DNA, xPos, yPos):
        #this is a boolean representing if the agent is dead or alive, 1 means alive, 0 means dead
        #if this is zero, the agents AI will stop running, effectively killing the agent
        self.alive = bool
        self.alive = 1

        #this is the DNA of the agent, an array which describes all the properties of an agent which can change, effectively defining this agent
        #and allowing it to be recreated elswhere in the program. The array contains, in order:
        #the colour of the agent in RGB, the spread of the agent's eyes,
        #the 2x3-array representing the default charge of each neuron, and the 3x3x3-array representing the mesh between the two layers of neurons
        #and the outputs
        self.DNA = self.limit(DNA)

        #this holds the list of points which define the shape of the agent (a triangle)
        self.pointList = [(xPos, yPos), (xPos - 15, yPos + 5),
                          (xPos - 15, yPos - 5)]

        #these are important enough to exist outside pointlist, since they are used all over the place and referenceing an index in poitslist
        #would be a pain for the processor
        self.xPos = xPos
        self.yPos = yPos

        #these variables will be useful later
        #the speed the agent is moving
        self.vel = 0
        #the angle the agent is at (relative to horizontal)
        self.theta = 0

        #this is the eyes of the organism/agent/whatever, ititialized at its min so the organism doesn't freak out
        self.eyes = [0, 0, 0]
        #this is the angle of spread between the middle eyes and the two outer ones
        self.spread = DNA[1]

        #here's where the fancy AI stuff starts
        #so the brian of the agent is divided into two layers of neurons with inputs and outputs on wither side
        #the inputs are the agent's eyes, which give info to the first layer of three neurons
        #the first layer of neurons then multiplies the data by a certain constant and then passes it on to the second layer
        #the second layer then does the same things and controls the three outputs, one to make the agent move forward and backward,
        #and two to turn left and right

        #this is the collection of neurons which make up the agent's brain
        # 0 is colour 1 is spread, 2 is defaults, 3 is mesh
        #self.brain = [[neuron(0,(0,0,1)),neuron(0,(0,0,0)),neuron(0,(1,0,0))],[neuron(0,(1,0,0)),neuron(1,(0,1,0)),neuron(0,(0,0,1))]]
        self.brain = [[
            neuron(DNA[2][0][0], DNA[3][1][0]),
            neuron(DNA[2][0][1], DNA[3][1][1]),
            neuron(DNA[2][0][2], DNA[3][1][2])
        ],
                      [
                          neuron(DNA[2][1][0], DNA[3][2][0]),
                          neuron(DNA[2][1][1], DNA[3][2][1]),
                          neuron(DNA[2][1][2], DNA[3][2][2])
                      ]]

        #this is the mesh which controls how data is passed from the inputs (eyes) to the first layer of neurons
        #this is done because treating eyes like neurons would be an unneccessary amount of overhead
        #self.meshOne = [[1,0,0],[0,1,0],[0,0,1]]
        self.meshOne = DNA[3][0]
Ejemplo n.º 9
0
	def __init__(self, num_rows, num_columns, data_set, name_of_data_set, num_nodes, end_of_num_layers_array, layerNumber, learning_rate):
		self.data_set = data_set
		self.num_rows = num_rows
		self.num_columns = num_columns
		self.list_of_neurons = []
		for row in range(num_rows):
			inputs = []
			for column in range(num_columns):
				if column != num_columns - 1:
					inputs.append(data_set[row][column])
			for i in range(num_nodes):
				the_neuron = n.neuron(inputs, end_of_num_layers_array, layerNumber, learning_rate)
				self.list_of_neurons.append(the_neuron)

		self.name_of_data_set = name_of_data_set
		self.results_of_run = []
		self.num_nodes = num_nodes
		self.str_results_of_run = []
Ejemplo n.º 10
0
    def __init__(self,
                 neuralNetSkeleton,
                 inputVecRank,
                 InitialWeight=0,
                 activationType="THRES"):
        """ Intializes a neuralnetwork of N layers

        NeuralNet = [ (1,(neuron(), neuron(),..., neuron())),
                      (2,(neuron(), neuron(),..., neuron())),
                       ...
                      (n,(neuron(), neuron(),..., neuron())),


        Args:
            neuralNetSkeleton:  Tupule list of (layer, Number of Neurons)
                                e.g ((1, 7 ), (2, 3), (3, 2)) is a three layer with the first layer having 7 neurons,
                                the second layer of three neurons and the third layer of two neurons

            inputVecRank:       the dimension of the input samples

            InitialWeight:      the initial weights for the neural network

            activationType:     Three kinds of activation functions "THRES", "LINEAR", "SIGMOID"


        """
        self.__neuralNetSkeleton = neuralNetSkeleton
        self.__neuralNet = []
        self.__inputVecRank = inputVecRank

        for layerNumber, noNeuronsInThisLayer in self.__neuralNetSkeleton:
            weightVectorForThisLayer = [InitialWeight
                                        ] + [InitialWeight] * inputVecRank
            neuronListForThisLayer = tuple([
                neuron(weightVector=weightVectorForThisLayer,
                       actFuncType=activationType)
                for i in range(0, noNeuronsInThisLayer)
            ])
            self.__neuralNet.append([layerNumber, neuronListForThisLayer])
            # the input rank or dimension for the next layer is the output dimension of this layer
            # assuming that we have a completely connected feedforward network
            inputVecRank = noNeuronsInThisLayer
        self.__neuralNet.sort()
Ejemplo n.º 11
0
 def __init__(self, ins, outs):
 
     # Undo related
     self.last_neuron = None
     self.last_bias   = self.last_bias2  = None
     self.last_weight = None
     self.last_post   = None
     
     # Store a copy of the parameters
     self.ins = ins; self.outs = outs
     self.curr = 0               # Current Neuron in creation progress
     
     # Create lattice
     self.cont = []; cnt = 0
     for aa in range(ins):
         self.cont.append([])
         for bb in range(outs):  
             self.cont[aa].append(neuron.neuron(self.ins, cnt, self.curr))
             self.curr += 1
         cnt += 1       
Ejemplo n.º 12
0
    def __init__(self, num_rows, num_columns, data_set, name_of_data_set,
                 num_nodes, end_of_num_layers_array, layerNumber,
                 learning_rate):
        self.data_set = data_set
        self.num_rows = num_rows
        self.num_columns = num_columns
        self.list_of_neurons = []
        for row in range(num_rows):
            inputs = []
            for column in range(num_columns):
                if column != num_columns - 1:
                    inputs.append(data_set[row][column])
            for i in range(num_nodes):
                the_neuron = n.neuron(inputs, end_of_num_layers_array,
                                      layerNumber, learning_rate)
                self.list_of_neurons.append(the_neuron)

        self.name_of_data_set = name_of_data_set
        self.results_of_run = []
        self.num_nodes = num_nodes
        self.str_results_of_run = []
Ejemplo n.º 13
0
import neuron as nr

inputA = nr.neuron(0)
inputB = nr.neuron(0)
inputC = nr.neuron(0)

norGate = nr.neuron([inputA, inputB, inputC], [-1, -1, -1], 0)
print(norGate.getOutput())
Ejemplo n.º 14
0
import numpy as np
import math
from neuralsystem import neuralsystem
from neuron import neuron

if __name__ == '__main__':
    print "hello"
    actthreshold = 0.3
    smell1 = neuron(0, 'inputneuron', actthreshold)
    smell2 = neuron(1, 'inputneuron', actthreshold)
    pro1 = neuron(2, 'proneuron', actthreshold)
    pro2 = neuron(3, 'proneuron', actthreshold)
    pro3 = neuron(4, 'proneuron', actthreshold)
    out = neuron(5, 'outneuron', actthreshold)

    templist = [pro1, pro2, pro3]
    for neuron in templist:
        smell1.add_outconnects([neuron, 0.4])
        smell2.add_outconnects([neuron, 0.4])
        neuron.add_inconnects([smell1, 0.4])
        neuron.add_inconnects([smell2, 0.4])
        neuron.add_outconnects([out, 0.2])
        out.add_inconnects([neuron, 0.2])
        for neuron2 in templist:
            neuron.add_outconnects([neuron2, 0.3])
            neuron.add_inconnects([neuron2, 0.3])
    templist = [smell1, smell2, pro1, pro2, pro3, out]
    network = neuralsystem('network1', templist)
    network.birth(10)
Ejemplo n.º 15
0
Archivo: net1.py Proyecto: bhamrick/ai2
data = []
for i in range(ndata):
	foo = fin.readline().split()
	for i in range(len(foo)):
		try:
			foo[i]=int(foo[i])
		except:
			pass
	data.append([foo[:inputs],foo[inputs:inputs+outputs]])

layers = []
for l in range(hlayers+1):
	arr = []
	for i in range(nhnodes[l]):
		if l == 0:
			arr.append(neuron.neuron(inputs))
		else:
			arr.append(neuron.neuron(nhnodes[l-1]))
	layers.append(arr)

if not interactive:
	start = time.time()
	sys.stdout.write('Learning...')
	sys.stdout.flush()
	for k in range(niter):
		for i in range(ndata):
			#Feed Forward
			indata = data[i][0]
	#		print indata
			for l in range(len(layers)):
				nextdata = []
import numpy as np
#import torch
import random
from neuron import neuron

input = [0 for i in range(0, 9)]

hidden = [neuron(input) for i in range(0, 5)]

output = [neuron(hidden) for i in range(0, 2)]

cost = np.dot()
Ejemplo n.º 17
0
from lines import *
from neuron import neuron
from rules import *
from spike_train import *

T = 100
dt = .1

dimen = 10

i_inc = .00725
v_decay = .002

neurons = [
    [[[neuron() for x in range(dimen)] for y in range(dimen)]
     for z in range(dimen)],
    [neuron() for x in range(dimen)],
]

weights = [full((dimen, dimen, dimen), 1)]

times = arange(0, T + dt, dt)

for i, net in enumerate(neurons[0]):
    for neuron in net[i]:
        neuron.on = True


def integrate(i, layer, st):
    for j, n in enumerate(neurons[layer]):
Ejemplo n.º 18
0
from neuron import neuron
from gsom import gsom
from gridPosition import gridPosition
import numpy as np

#Test Neuron class
vectorA=[0.1,0.2,0.3]
a = neuron(vectorA,0)
vectorB=[0.2,0.3,0.4]
b=neuron(vectorB,0)
vectorC=[0.3,0.4,0.5]
c= neuron(vectorC,0)
vectorD=[0.5 ,0.6 ,0.7]
d=neuron(vectorD,0)

#Initial neural network



#Initial SOM

som = {}
som[gridPosition(0,0)] = a
som[gridPosition(1,0)] = b
som[gridPosition(0,1)] = c
som[gridPosition(1,1)] = d
for i,value in som.iteritems():
	print value.getweightVector()
#	print len(i.getNeighbours())
#Test GSOM class
input = np.array(
Ejemplo n.º 19
0
T = 200
time  = np.arange(1, T+1, 1)
t_back = -20
t_fore = 20
Pth = 150 #Should be Pth = 6 for deterministic spike train
m = 784 #Number of neurons in first layer
n = 8 #Number of neurons in second layer
epoch = 1
num_of_images = 6
w_max = 0.5
w_min = -0.5

layer2 = []
# creating the hidden layer of neurons
for i in range(n):
	a = neuron()
	layer2.append(a)

#synapse matrix
synapse = np.zeros((n,m))
#learned weights
weight_matrix = learned_weights()
for i in range (num_of_images):
	synapse[i] = weight_matrix[i]

#random initialization for rest of the synapses
for i in range(num_of_images,n):
	for j in range(m):
		synapse[i][j] = random.uniform(w_min,w_max)

for k in range(epoch):
Ejemplo n.º 20
0
def build_N_learn(lamda):
	tset = os.listdir(directory)
	for n in tset:
		p = picture.Pic(os.path.join(directory, n))
		filename = n.split(".")
		imageVectors[filename[0]] = p.getVector()
	TotalErr = 0.0
	Err = 0
	iteration = 0
	while (Err == 0.0 or TotalErr > 2 and iteration < 2500):
		if iteration % 20 == 0: 
			print "total error till " , iteration ,"th iteration is :", TotalErr

		iteration = iteration+1
		TotalErr = 0

		for k in imageVectors.keys():
			for i in range(firstLayer):
				if len(inputNeurons) < firstLayer:
					f = neuron.neuron(len(imageVectors[k]), lamda)
					f.setWeights()
					inputNeurons.append(f)
				for y in range(len(imageVectors[k])):
					inputNeurons[i].setInput(y+1, imageVectors[k][y])
				inputNeurons[i].computeBaseFunction()
				inputNeurons[i].computeOutput()
			#print k
			for i in range(hiddenLayer):
				if len(hiddenNeurons) < hiddenLayer:
					h = neuron.neuron(firstLayer, lamda)
					h.setWeights()
					hiddenNeurons.append(h)
				for y in range(firstLayer):
					hiddenNeurons[i].setInput(y+1, inputNeurons[y].getOutput())
				hiddenNeurons[i].computeBaseFunction()
				hiddenNeurons[i].computeOutput()

			for i in range(lastLayer):
				if len(lastNeurons) < lastLayer:
					l = neuron.neuron(hiddenLayer, lamda)
					l.setWeights()
					lastNeurons.append(l)
				for y in range(hiddenLayer):
					lastNeurons[i].setInput(y+1, hiddenNeurons[y].getOutput())
				lastNeurons[i].computeBaseFunction()
				lastNeurons[i].computeOutput()

			Err = 0
			for i in range(lastLayer):#delta calculation for o/p layer
				if i == int(k[0]):#check for which o/p neuron should give 1 as o/p
					o=lastNeurons[i].getOutput()
					Err = Err + (1 - o)**2
					lastNeurons[i].setDelta( (1 - o) * lamda * o * (1 - o) )
				else:
					o=lastNeurons[i].getOutput()
					Err = Err + (0 - lastNeurons[i].getOutput())**2
					lastNeurons[i].setDelta( (0 - o) * lamda * o * (1 - o) )
			TotalErr = TotalErr + 0.5 * Err

			####Backpropagation
			for i in range(hiddenLayer):
				sum = 0
				for sd in range(lastLayer):
					sum = sum + lastNeurons[sd].getDelta() * lastNeurons[sd].getWeight(i+1)
				#delta calc for hidden layer propagated from o/p layer
				o=hiddenNeurons[i].getOutput()
				hiddenNeurons[i].setDelta(sum * lamda * o * (1 - o))

			for i in range(firstLayer):
				sum = 0
				for sd in range(hiddenLayer):
					sum = sum+ hiddenNeurons[sd].getDelta() * hiddenNeurons[sd].getWeight(i+1)
				#delta calc for hidden layer propagated from prev hidden  layer
				o=inputNeurons[i].getOutput()
				inputNeurons[i].setDelta(sum * lamda * o * (1 - o))

			for i in range(0, firstLayer):
				for j in range(0, inputNeurons[i].getNumberInput()):
					nw=inputNeurons[i].getWeight(j) + 0.6 * inputNeurons[i].getDelta() * inputNeurons[i].getInput(j)
					inputNeurons[i].setWeight(j, nw)

			for i in range(0, hiddenLayer):
				for j in range(0, hiddenNeurons[i].getNumberInput()):
					nw=hiddenNeurons[i].getWeight(j) + 0.6 * hiddenNeurons[i].getDelta() * hiddenNeurons[i].getInput(j)
					hiddenNeurons[i].setWeight(j, nw)

			for i in range(0, lastLayer):
				for j in range(0, lastNeurons[i].getNumberInput()):
					nw=lastNeurons[i].getWeight(j) + 0.6 * lastNeurons[i].getDelta() * lastNeurons[i].getInput(j)
					lastNeurons[i].setWeight(j, nw)
Ejemplo n.º 21
0
import neuron as nr

inputA = nr.neuron(1)
inputB = nr.neuron(1)
inputCi = nr.neuron(1)

OrGate1 = nr.neuron([inputA, inputB], [1, 1], 1)
NandGate1 = nr.neuron([inputA, inputB], [-1, -1], -1)
XORGate1 = nr.neuron([OrGate1, NandGate1], [1, 1], 2)

OrGate2 = nr.neuron([XORGate1, inputCi], [1, 1], 1)
NandGate2 = nr.neuron([XORGate1, inputCi], [-1, -1], -1)
XORGate2 = nr.neuron([OrGate2, NandGate2], [1, 1], 2)

AndGate1 = nr.neuron([inputCi, XORGate1], [1, 1], 2)
AndGate2 = nr.neuron([inputA, inputB], [1, 1], 2)

OrGate3 = nr.neuron([AndGate1, AndGate2], [1, 1], 1)

# print(OrGate3.getOutput(), XORGate2.getOutput())
print(OrGate3.getOutput() * 2 + XORGate2.getOutput())
Ejemplo n.º 22
0
def learning(learning_path, synapse):
	#potentials of output neurons
	potential_lists = []
	for i in range(par.kSecondLayerNuerons_):
		potential_lists.append([])

	#time series
	time_array  = np.arange(1, par.kTime_+1, 1)

	layer2 = []

	# creating the hidden layer of neurons
	for i in range(par.kSecondLayerNuerons_):
		a = neuron()
		layer2.append(a)

	for epoch in range(1):
		for wave_file in learning_path:
		#for wave_file in ["sounddata\F1\F1SYB01_が.wav"]:
			resemble_print(str(wave_file) + "  " + str(epoch))

			#音声データの読み込み
			splited_sig_array, samplerate = wav_split(str(wave_file))
			resemble_print(str(wave_file))

			splited_sig_array = remove_silence(splited_sig_array)
			print(len(splited_sig_array))
			#スパイクの連結
			#spike_train = wav_split2spike(splited_sig_array, samplerate)
			#spike_connected = np.array(connect_spike(spike_train))
			#for spike_train in spike_connected:
			for signal in splited_sig_array:
				#Generating melspectrum
				f_centers, mel_spectrum = get_log_melspectrum(signal, samplerate)

				#Generating spike train
				spike_train = np.array(encode(np.log10(mel_spectrum)))

				#calculating threshold value for the image
				var_threshold = threshold(spike_train)

				# resemble_print var_threshold
				# synapse_act = np.zeros((par.kSecondLayerNuerons_,par.kFirstLayerNuerons_))
				# var_threshold = 9
				# resemble_print var_threshold
				# var_D = (var_threshold*3)*0.07

				var_D = 0.15 * par.kScale_

				for x in layer2:
					x.initial(var_threshold)

				#flag for lateral inhibition
				flag_spike = 0

				img_win = 100

				active_potential = []
				for index1 in range(par.kSecondLayerNuerons_):
					active_potential.append(0)

				#Leaky integrate and fire neuron dynamics
				for time in time_array:
					for second_layer_position, second_layer_neuron in enumerate(layer2):
						active = []
						if(second_layer_neuron.t_rest < time):
							second_layer_neuron.P = (second_layer_neuron.P
													+ np.dot(
														synapse[second_layer_position], spike_train[:, time]
														)
													)
							#resemble_print("synapse : " + str(synapse[second_layer_position]))
							if(second_layer_neuron.P > par.kPrest_):
								second_layer_neuron.P -= var_D
							active_potential[second_layer_position] = second_layer_neuron.P

						potential_lists[second_layer_position].append(second_layer_neuron.P)

					# Lateral Inhibition
					if(flag_spike==0):
						max_potential = max(active_potential)
						if(max_potential > var_threshold):
							flag_spike = 1
							winner_neuron = np.argmax(active_potential)
							img_win = winner_neuron
							resemble_print("winner is " + str(winner_neuron))
							for s in range(par.kSecondLayerNuerons_):
								if(s != winner_neuron):
									layer2[s].P = par.kMinPotential_

					#Check for spikes and update weights
					for second_layer_position, second_layer_neuron in enumerate(layer2):
						neuron_status = second_layer_neuron.check()
						if(neuron_status == 1):
							second_layer_neuron.t_rest = time + second_layer_neuron.t_ref
							second_layer_neuron.P = par.kPrest_
							for first_layer_position in range(par.kFirstLayerNuerons_):
								#前シナプスの計算
								for back_time in range(-2, par.kTimeBack_-1, -1): #-2 → -20
									if 0 <= time + back_time < par.kTime_ + 1:
										if spike_train[first_layer_position][time + back_time] == 1:
											# resemble_print "weight change by" + str(update(synapse[j][h], rl(t1)))
											synapse[second_layer_position][first_layer_position] = update(
												synapse[second_layer_position][first_layer_position], rl(back_time)
												)
											resemble_print("back : " + str(second_layer_position) + "-" + str(first_layer_position) + " : " + str(synapse[second_layer_position][first_layer_position]))
								#後シナプスの計算
								for fore_time in range(2, par.kTimeFore_+1, 1): # 2 → 20
									if 0 <= time + fore_time<par.kTime_+1:
										if spike_train[first_layer_position][time + fore_time] == 1:
											# resemble_print "weight change by" + str(update(synapse[j][h], rl(t1)))
											synapse[second_layer_position][first_layer_position] = update(
												synapse[second_layer_position][first_layer_position], rl(fore_time)
												)
											resemble_print("fron : " + str(second_layer_position) + "-" + str(first_layer_position) + " : " + str(synapse[second_layer_position][first_layer_position]))


				if(img_win!=100):
					for first_layer_position in range(par.kFirstLayerNuerons_):
						if sum(spike_train[first_layer_position]) == 0:
							synapse[img_win][first_layer_position] -= 0.06 * par.kScale_
							if(synapse[img_win][first_layer_position]<par.kMinWait_):
								synapse[img_win][first_layer_position] = par.kMinWait_

	return potential_lists, synapse, layer2
Ejemplo n.º 23
0
def winner_take_all(synapse, wave_file):
    #potentials of output neurons
    potential_lists = []
    for i in range(par.kSecondLayerNuerons_):
        potential_lists.append([])

    #time series
    time_array = np.arange(1, par.kTime_ + 1, 1)

    layer2 = []

    # creating the hidden layer of neurons
    for i in range(par.kSecondLayerNuerons_):
        a = neuron()
        layer2.append(a)

    neuron_spiked = np.zeros(par.kSecondLayerNuerons_)

    for epoch in range(1):
        resemble_print(str(wave_file) + "  " + str(epoch))

        #音声データの読み込み
        #splited_sig_array, samplerate = wav_split(str(wave_file))
        #resemble_print(wave_file)
        splited_sig_array, samplerate = wav_split(str(wave_file))
        resemble_print(str(wave_file))

        #スパイクの連結
        #spike_train = wav_split2spike(splited_sig_array, samplerate)
        #spike_connected = np.array(connect_spike(spike_train))
        #for spike_train in spike_connected:

        for signal in splited_sig_array:

            #Generating melspectrum
            f_centers, mel_spectrum = get_log_melspectrum(signal, samplerate)

            #Generating spike train
            spike_train = np.array(encode(np.log10(mel_spectrum)))

            #calculating threshold value for the image
            var_threshold = threshold(spike_train)

            # resemble_print var_threshold
            # synapse_act = np.zeros((par.kSecondLayerNuerons_,par.kFirstLayerNuerons_))
            # var_threshold = 9
            # resemble_print var_threshold
            # var_D = (var_threshold*3)*0.07

            var_D = 0.15 * par.kScale_

            for x in layer2:
                x.initial(var_threshold)

            #flag for lateral inhibition
            flag_spike = 0

            img_win = 100

            active_potential = []
            for index1 in range(par.kSecondLayerNuerons_):
                active_potential.append(0)

            #Leaky integrate and fire neuron dynamics
            for time in time_array:
                for second_layer_position, second_layer_neuron in enumerate(
                        layer2):
                    active = []
                    if (second_layer_neuron.t_rest < time):
                        second_layer_neuron.P = (
                            second_layer_neuron.P +
                            np.dot(synapse[second_layer_position],
                                   spike_train[:, time]))
                        #resemble_print("synapse : " + str(synapse[second_layer_position]))
                        if (second_layer_neuron.P > par.kPrest_):
                            second_layer_neuron.P -= var_D
                        active_potential[
                            second_layer_position] = second_layer_neuron.P

                    potential_lists[second_layer_position].append(
                        second_layer_neuron.P)

                # Lateral Inhibition
                if (flag_spike == 0):
                    max_potential = max(active_potential)
                    if (max_potential > var_threshold):
                        flag_spike = 1
                        winner_neuron = np.argmax(active_potential)
                        img_win = winner_neuron
                        neuron_spiked[winner_neuron] += 1
                        resemble_print("winner is " + str(winner_neuron))
                        for s in range(par.kSecondLayerNuerons_):
                            if (s != winner_neuron):
                                layer2[s].P = par.kMinPotential_

    #勝ったニューロンの特定
    resemble_print(neuron_spiked)

    return neuron_spiked
Ejemplo n.º 24
0
def create_list_of_neurons(size, start_label, layer_type, prev_layer_size, activation_function, learning_rate):
	l = []
	for ii in range(0, size):
		l.append(neuron.neuron(start_label+ii, layer_type, prev_layer_size, activation_function, learning_rate))
	return l
Ejemplo n.º 25
0
def learning(learning_or_classify):

    #1 = learning, 0 = classify
    #learning_or_classify = 0
    print learning_or_classify
    if(learning_or_classify == 0):
        print "Starting classify..."
    elif(learning_or_classify == 1):
        print "Starting learning..."
    else:
        print "Error in argument, quitting"
        quit()

    if(learning_or_classify == 0):
        par.epoch = 1

    #potentials of output neurons
    pot_arrays = []
    pot_arrays.append([]) #because 0th layer do not require neuron model
    for i in range(1,par.num_layers):
        pot_arrays_this = []
        for j in range(0,par.num_layer_neurons[i]):
            pot_arrays_this.append([])
        pot_arrays.append(pot_arrays_this)
    print "created potential arrays for each layer..."

    Pth_array = []
    Pth_array.append([]) #because 0th layer do not require neuron model
    for i in range(1,par.num_layers):
        Pth_array_this = []
        for j in range(0,par.num_layer_neurons[i]):
            Pth_array_this.append([])
        Pth_array.append(Pth_array_this)
    print "created potential threshold arrays for each layer..."


    train_all = []
    for i in range(0,par.num_layers):
        train_this = []
        for j in range(0,par.num_layer_neurons[i]):
            train_this.append([])
        train_all.append(train_this)
    print "created spike trains for each layer..."

    #synapse matrix initialization
    synapse = [] #synapse[i] is the matrix for weights from layer i to layer i+1, assuming index from 0
    for i in range(0,par.num_layers-1):
        synapse_this = np.zeros((par.num_layer_neurons[i+1],par.num_layer_neurons[i]))
        synapse.append(synapse_this)

    if(learning_or_classify == 1):
        for layer in range(0,par.num_layers-1):
            for i in range(par.num_layer_neurons[layer+1]):
	        for j in range(par.num_layer_neurons[layer]):
	            synapse[layer][i][j] = random.uniform(0,0.4*par.scale)
    else:
        for layer in range(0,par.num_layers-1):
            for i in range(par.num_layer_neurons[layer+1]):
	        #for j in range(par.num_layer_neurons[layer]):
                filename = "weights/layer_"+str(layer)+"_neuron_"+str(i)+".dat"
                with open(filename,"rb") as f:
	            synapse[layer][i] = pickle.load(f)


    print "created synapse matrices for each layer..."


    #this contains neurons of all layers except first
    layers = [] #layers[i] is the list of neurons from layer i, assuming index from 0
    layer_this = []
    layers.append(layer_this) #0th layer is empty as input layer do not require neuron model

    #time series 
    time  = np.arange(1, par.T+1, 1)

    # creating each layer of neurons
    for i in range(1,par.num_layers):
        layer_this = []
        for i in range(par.num_layer_neurons[i]):
            a = neuron()
            layer_this.append(a)
        layers.append(layer_this)
    print "created neuron for each layer..."


    for k in range(par.epoch):
        for i in range(1,7):
            print "Epoch: ",str(k),", Image: ", str(i)
            if(learning_or_classify == 1):
                img = cv2.imread("training_images/" + str(i) + ".png", 0)
            else:
                img = cv2.imread("training_images/" + str(i) + ".png", 0)
                 

            #Convolving image with receptive field
            pot = rf(img)
            #print pot

            #training layers i and i+1, assuming 0 indexing, thus n layers require n-1 pairs of training 
            for layer in range(0,par.num_layers-1):
                print "Layer: ", str(layer)

                #Generating spike train when the first layer
                #else take the spike train from last layer
                if(layer == 0):
                    train_all[layer] = np.array(encode(pot))
                    train = np.array(encode(pot))
                else:
                    train_all[layer] = np.asarray(train_this_layer)
                    train = np.array(np.asarray(train_this_layer))

                #print train[1]

                #calculating threshold value for the image
                var_threshold = threshold(train)
                #print "var_threshold is ", str(var_threshold)

                # print var_threshold
                # synapse_act = np.zeros((par.n,par.m))
                # var_threshold = 9
                # print var_threshold
                # var_D = (var_threshold*3)*0.07

                var_D = 0.15*par.scale

                for x in layers[layer+1]:
                    x.initial(var_threshold)

                #flag for lateral inhibition
                f_spike = 0

                img_win = 100

                active_pot = []
                train_this_layer = []
                for index1 in range(par.num_layer_neurons[layer+1]):
                    active_pot.append(0)
                    train_this_layer.append([])

                #print synapse[layer].shape, train.shape
                #Leaky integrate and fire neuron dynamics
                for t in time:
                    #print "Time: ", str(t)
                    for j, x in enumerate(layers[layer+1]):
                        active = []	
                        if(x.t_rest<t):
                            x.P = x.P + np.dot(synapse[layer][j], train[:,t])
                            if(x.P>par.Prest):
                                x.P -= var_D
                            active_pot[j] = x.P

                        #pot_arrays[layer+1][j].append(x.P)
                        #Pth_array[layer+1][j].append(x.Pth)

                    # Lateral Inhibition
                    # Occurs in the training of second last and last layer
                    #if(f_spike==0 and layer == par.num_layers - 2 and learning_or_classify == 1):
                    if(f_spike==0 ):
                        high_pot = max(active_pot)
                        if(high_pot>var_threshold):
                            f_spike = 1
                            winner = np.argmax(active_pot)
                            img_win = winner
                            #print "winner is " + str(winner)
                            for s in range(par.num_layer_neurons[layer+1]):
                                if(s!=winner):
                                    layers[layer+1][s].P = par.Pmin

                    #Check for spikes and update weights				
                    for j,x in enumerate(layers[layer+1]):
                        pot_arrays[layer+1][j].append(x.P)
                        Pth_array[layer+1][j].append(x.Pth)
                        s = x.check()
                        train_this_layer[j].append(s)
                        if(learning_or_classify == 1):
                            if(s==1):
                                x.t_rest = t + x.t_ref
                                x.P = par.Prest
                                for h in range(par.num_layer_neurons[layer]):

                                    for t1 in range(-2,par.t_back-1, -1):
                                        if 0<=t+t1<par.T+1:
                                            if train[h][t+t1] == 1:
                                                # print "weight change by" + str(update(synapse[j][h], rl(t1)))
                                                synapse[layer][j][h] = update(synapse[layer][j][h], rl(t1))


                                    for t1 in range(2,par.t_fore+1, 1):
                                        if 0<=t+t1<par.T+1:
                                            if train[h][t+t1] == 1:
                                                # print "weight change by" + str(update(synapse[j][h], rl(t1)))
                                                synapse[layer][j][h] = update(synapse[layer][j][h], rl(t1))

                    for j in range(par.num_layer_neurons[layer+1]):
                        train_this_layer[j].append(0)


                #if(img_win!=100 and layer == par.num_layers - 2 ):
                if(img_win!=100 ):
                    for p in range(par.num_layer_neurons[layer]):
                        if sum(train[p])==0:
                            synapse[layer][img_win][p] -= 0.06*par.scale
                            if(synapse[layer][img_win][p]<par.w_min):
                                synapse[layer][img_win][p] = par.w_min

                #print train_this_layer
                #print synapse[0][0]

            
            train_all[par.num_layers-1] = np.asarray(train_this_layer)

            results_each_layer = 1
            if(results_each_layer):
                for layer in range(par.num_layers-1,par.num_layers):
                    for i in range(par.num_layer_neurons[layer]):
                        print "Layer"+ str(layer) + ", Neuron"+str(i+1)+": "+str(sum(train_all[layer][i]))

    
    #print classification results
#    if(learning_or_classify == 0):
        
    


    plot = 0
    if (plot == 1):
        for layer in range(par.num_layers-1,par.num_layers):
            ttt = np.arange(0,len(pot_arrays[layer][0]),1)
    
            #plotting 
            for i in range(par.num_layer_neurons[layer]):
                axes = plt.gca()
                axes.set_ylim([-20,50])
                plt.plot(ttt,Pth_array[layer][i], 'r' )
                plt.plot(ttt,pot_arrays[layer][i])
                plt.show()

    #Reconstructing weights to analyse training
    reconst = 1
    if(learning_or_classify != 1):
        reconst = 0

    if(reconst == 1):
        for layer in range(par.num_layers-1):
            siz_x = int(par.num_layer_neurons[layer]**(.5))
            siz_y = siz_x
            for i in range(par.num_layer_neurons[layer+1]):
                reconst_weights(synapse[layer][i],i+1,layer,siz_x,siz_y)

    #Dumping trained weights of last layer to file
    dump = 1
    if(learning_or_classify != 1):
        dump = 0
    if(dump == 1):
        for layer in range(par.num_layers-1):
            for i in range(par.num_layer_neurons[layer+1]):
                filename = "weights/"+"layer_"+str(layer)+"_neuron_"+str(i)+".dat"
                with open(filename,'wb') as f:
                    #f.write(str(synapse[layer][i]))
                    pickle.dump(synapse[layer][i],f)