0

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
Maede
  • 174
  • 12
  • 3
    It's a little difficult to tell, because you seem to have named several things similarly, but it _looks_ like your `compute_output()` function internally calls your `output()` function, which internally turn calls your `compute_output()` function which calls your `output()` function which... – G. Anderson Jan 03 '20 at 20:08
  • I just confused what's the difference between append element to list or pass it like [A,..] and how can I solve it? – Maede Jan 03 '20 at 20:17

1 Answers1

0

You call the output function every time you execute the compute_output function and so on. That's leading to a RecursionError: maximum recursion depth exceeded, becuase there is only a limited time a function can call another function at one direct call. The exact spot in compute_output is:

def compute_output(self):
    …
    for i in range(len(inputs)):
        output += inputs[i].output() * weights[i].get_value()

you call inputs[i].output() at this point

PS: Difference between +/ += and append() is mentioned Here

CDJB
  • 14,043
  • 5
  • 29
  • 55
Cobalt
  • 447
  • 5
  • 9