I am making a neuron project on Python. In a previous post, I asked how I could make the neurons fire to each other.
The circuit was successful. I made a new class called "Muscle", that flexes when receiving a signal from a neuron.
But there is a big flaw: When I fire two neurons, say N1 and N2, that are connected to another neuron, N3, then N3 wouldn't sum up the signals it received by both N1 and N2. Instead, N3 does them separately, in different times.
How could I overcome that problem?
I got this answer from https://reddit.com/r/learnpython as juanpa.arrivillaga recommended:
class Neuron: def __init__(self,name,threshold=3): self.name = name self.threshold = threshold self.in_signal = 0 self.out_signal = 0 self.connect_axon = [] self.connect_dendrite = [] def connect_to_neuron(self, neuron): self.connect_dendrite.append(neuron) neuron.connect_axon.append(self) def fire(self, strength): self.out_signal = strength print('neuron',self.name,'fired with strength',self.out_signal) def reset(self): self.in_signal = 0 self.out_signal = 0 def update(self): for neuron in self.connect_dendrite: self.in_signal += neuron.out_signal if self.in_signal >= self.threshold: self.out_signal = self.in_signal print('neuron',self.name,'fired with strength',self.out_signal) class Brain: def __init__(self): self.neurons = [] def add_neuron(self, neuron): self.neurons.append(neuron) def update(self): for neuron in self.neurons: neuron.update() for neuron in self.neurons: neuron.reset() n1 = Neuron('n1') n2 = Neuron('n2') n3 = Neuron('n3',threshold=5) n3.connect_to_neuron(n1) n3.connect_to_neuron(n2) tiny_brain = Brain() tiny_brain.add_neuron(n1) tiny_brain.add_neuron(n2) tiny_brain.add_neuron(n3) n1.fire(4) n2.fire(4) tiny_brain.update()
Source: https://www.reddit.com/r/learnpython/comments/rd3wts/neuron_project/hnz8hxy/