import math from decimal import * import inspect import random class Neuron: actual = 510 expected = 72 number_of_neurons = 0 def __init__(self,inputs,weights,bias): """ Initializes a new Neuron Its inputs are self inputs -> A list of Neurons or Integers weights -> A list of integers bias -> An Integer """ self.inputs = inputs self.weights = weights self.bias = bias self.current_value = 0 def __str__(self): return ("The neuron's inputs are %s:, The neuron's weights are :%s, The neurons inputs and weights \ combined are :%s its bias is %s and its current value is %s" %(self.inputs,self.weights, \ zip(self.inputs,self.weights), self.bias, self.current_value)) def change_weights(self): """ Applies the backpropogation squared error alteration to each weight in the Neuron """ Neuron.learning_rate = Decimal( "%.3f" % (Decimal(1)/Decimal(35))) if isinstance(self.inputs[0],Neuron): for y in range(len(self.weights)): for x in range(len(self.inputs)): self.weights[y] += ((self.inputs[x].current_value)*Neuron.learning_rate*\ (Neuron.actual-Neuron.expected)) else: for y in range(len(self.weights)): for x in range(len(self.inputs)): self.weights[y] += ((self.inputs[x])*Neuron.learning_rate*\ (Neuron.actual-Neuron.expected)) def printer(self): if isinstance(self,Neuron): for x in range(len(self.inputs)): printer(self.inputs[x]) print self def load_data(position,data,neurons,): """ Takes a list of tuples of a list of integers and an integer. The list of integers represent the data point inputs and the integer the actual value. list[(list[int],int),(list[int],int)] It iteratively assigns these values in the list of integers to the list of Neurons. """ for x in range(len(data[position][0])): neurons[x].inputs = [data[position][0][x]] Neuron.actual = data[position][1] class Linear_Neuron(Neuron): def calculate_value(self): i = 0 self.current_value = 0 if isinstance(self.inputs[0],Neuron): for x in self.inputs: self.current_value += x.current_value*self.weights[i] i += 1 self.current_value += self.bias else: for x in self.inputs: self.current_value += x*self.weights[i] i += 1 self.current_value += self.bias print self class Binary_Neuron(Neuron): def calculate_value(self): self.current_value = 0 for x,y in zip(self.inputs,self.weights): self.current_value += x.current_value*y self.current_value += self.bias if (self.current_value >= 0 ): self.current_value = 1 else: self.current_value = 0 class Rectified_Linear_Neuron(Neuron): def calculate_value(self): self.current_value = 0 for x,y in zip(self.inputs,self.weights): self.current_value += x.current_value*y self.current_value += self.bias if (self.current_value < 0 ): self.current_value = 0 def print_contents(neuron): for x in neuron.inputs: print x print neuron.current_value def calculate_network_value(self,): if isinstance(self,Neuron): for x in range(len(self.inputs)): self.inputs[x].calculate_value() self.calculate_value() Neuron.expected = output_neuron.current_value def backpropogate(self,): if isinstance(self,Neuron): for x in range(len(self.inputs)): backpropogate(self.inputs[x]) self.change_weights() class Sigmoid_Neuron(Neuron): def calculate_value(self): self.current_value = 0 for x,y in zip(self.inputs,self.weights): self.current_value += x.current_value*y self.current_value += self.bias self.current_value = 1 / ( 1 + math.pow(math.e,self.current_value)) ketchup = Linear_Neuron([],[100],0) fish = Linear_Neuron([],[25],0) chips = Linear_Neuron([],[111],0) row1 = [ketchup,fish,chips] output_neuron = Linear_Neuron([],[1,1,1],0) for x in row1: output_neuron.inputs.append(x) dataset = [] for x in range(0,10): x = [random.randint(0,10),random.randint(0,10),random.randint(0,10)] value = 150*x[0] + 50*x[1] + 100*x[2] newtup = (x,value) dataset.append(newtup) for x in range(len(dataset)): load_data(x,dataset,row1) print "Before" calculate_network_value(output_neuron) print "Neuron Expected ", Neuron.expected print "Neuron Actual " , Neuron.actual backpropogate(output_neuron) output_neuron.weights = [1,1,1] calculate_network_value(output_neuron) print "After" print_contents(output_neuron) print "Neuron Expected, ", Neuron.expected print "Neuron Actual, " ,Neuron.actual print "___________ END OF THIS OPERATION ___________"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question