I was trying to make a neural network with 40 middle neurons.I want to initial Neuron class 40 times by loop and append each iteration to a list and then pass it to final node.but then I've got that when I pass a list like [neuron1,neuron2, ...] it works without any problem but when I pass a list that I've appended it in loop it throws RecursionError: maximum recursion depth exceeded
. here is my network initial code:
W1 = Weight('w1', random_weight())
W2 = Weight('w2', random_weight())
neurons = [None] * 2
A = Neuron('A', [i0, i1], [ W1, W2])
neurons[0] = A
B = Neuron('B', [i0, i1], [W1, W2])
neurons[1] = B
out = Neuron('out', [A,B], [W1, W2])
this one works good. but below code has problem !
W1 = Weight('w1', random_weight())
W2 = Weight('w2', random_weight())
neurons = [None] * 2
A = Neuron('A', [i0, i1], [ W1, W2])
neurons[0] = A
B = Neuron('B', [i0, i1], [W1, W2])
neurons[1] = B
out = Neuron('out', neurons, [W1, W2])
here is my Neuron class implementation.
class Neuron(DifferentiableElement):
def __init__(self, name, inputs, input_weights, use_cache=True):
assert len(inputs)==len(input_weights)
for i in range(len(inputs)):
assert isinstance(inputs[i],(Neuron,Input))
assert isinstance(input_weights[i],Weight)
DifferentiableElement.__init__(self)
self.my_name = name
self.my_inputs = inputs # list of Neuron or Input instances
self.my_weights = input_weights # list of Weight instances
self.use_cache = use_cache
self.clear_cache()
self.my_descendant_weights = None
self.my_direct_weights = None
def output(self):
if self.use_cache:
if self.my_output is None:
self.my_output = self.compute_output()
return self.my_output
return self.compute_output()
def compute_output(self):
output = 0
inputs = self.get_inputs()
weights = self.get_weights()
for i in range(len(inputs)):
output += inputs[i].output() * weights[i].get_value()
output = 1 / (1 + math.exp(-1 * output))
return output