0

I was making a neuron project on Python.

In a previous post, I asked how I could make the neurons fire to each other. I used my Dad's account for that post, but I and only I asked that question. I put all accounts with my name on it :|

If you haven't, please read the last post.

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?

  • 2
    Please see [ask] and the [help]. While I encourage you to keep pursuing this, StackOverflow is not meant to be your personal tutoring service. There are guidelines about the nature of questions here, they must be self-contained, for starters. This is not a discussion forum. For questions seeking help debugging code, you must provide a [mcve] – juanpa.arrivillaga Dec 10 '21 at 07:55
  • 1
    For a forum that allows for more broad questions and discussion, I suggest checking out an appropriate subreddit, e.g. https://www.reddit.com/r/learnpython/ where you can have an open ended back-and-forth. – juanpa.arrivillaga Dec 10 '21 at 07:58
  • You may want to consider implementing a time window such that if a neuron receives multiple events within the last N seconds, then you sum the events – Justas Dec 10 '21 at 18:14

1 Answers1

1

I got this answer from reddit.com/r/learnpython as you recommended. Thank you!

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()