2021-06-30

Is it Possible in python, to Pass Variables Within a List, by Reference, in an Argument? [duplicate]

I am trying to create a Neural Network class made up of Neuron objects wired together.

My Neuron class has

  1. Dendrites
    The number of dendrites is specified in the parameters when the class is initialised. The Dendrites are stored in a list whose index stores the voltages of each Dendrite. eg: neuron1.dendrites[2]=0.12 volts.
  2. Activation (Threshold) Potential The sum of all the dendrite potentials provides the neuron potential. If this potential exceeds the Threshold Potential, my Neuron will fire. There are other neurons connected to the axon of my neuron. The Axon of my Neuron connects to the Dendrites of other Neuron objects. Several Dendrites from other Neurons may connect to the Axon of my Neuron. They will all receive a fixed voltage (outputVoltage) when my Neuron fires. It fires in an All-or-Nothing manner.
  3. Firing state
    When the the Activation Potential is reached, the firing state = on (True)
  4. My Neuron class also has a setConnections() method. This method receives a python list of Dendrites. In this method, I wish to iterate through my internal list of external Dendrites and reset their voltage values. This is not working. I cannot figure out why and so seek assistance here.

I provide a stripped down version of my code below:

import threading

 
 class Neuron:
      def __init__(self, dendrites, activation_Pot=0.24):
      """
      Create a dendrites[] array.
      Each element of this array represents the voltage of that dendrite.
      We can then loop through the array to sum up the signal strength.  

      If the signal strength exceeds the Activation potential, then the all-or-nothing threshold has been breached and
      we can transmit our signal along the axon.
      """
      self.dendrites = [0]*dendrites
      self.InputPotential = 0  # This variable will store the sum of all the dendrite voltages.  It is being initialised here.
      self.activation_Pot = activation_Pot
      self.on = True
      self.off = False
      self.voltsOut = 0.12      # This constant dictates the potential of the axon when the neuron is firing.
      self.outputPotential = 0  # This variable  SETS the potential of the single axon when the threshold activation potential of the  neuron has been reached and the neuron is firing.
      self.firing = self.off
      self.axonConnections = []
      
      # Launch a thread to check on a timer the sum of all dendrite inputs and fire when output > Activation Potential.
      t1 = threading.Thread(target = self.start, args=[])
      t1.start()
      
      
      def fire(self):
          self.outputPotential = self.voltsOut
          self.firing = self.on
          print("Neuron is firing!")
          
          for outputDendrites in self.axonConnections:
              outputDendrites = self.outputPotential
              
      def stopFiring(self):
          self.outputPotential = 0
          self.firing = self.off
          print("Neuron has STOPPED firing!")
          
          
      def setActivation_Pot(self, activation_Pot):
          if (activation_Pot >= 0) and (activation_Pot <=1):
              self.activation_Pot = activation_Pot
          else:
              print("activation_Pot value needs to be between 0 and 1")
              print("activation_Pot has not been reset.")
              print("Please consider re-inputting a valid value.")        
              
              
      def getActivation_Pot(self):
          return self.activation_Pot
          
          
      def setAxonConnections(self, axonConnections):
          self.axonConnections = axonConnections
          
      def getAxonConnections(self):
          return self.axonConnections
      
      
      def start(self):
          while True:
              while True:
                  self.InputPotential = 0  
                  for dendrite in self.dendrites:
                      self.InputPotential+=dendrite
                      
                  if self.InputPotential >= self.activation_Pot:
                      self.fire()
                      break
              while True:
                  self.InputPotential = 0
                  for dendrite in self.dendrites:
                      self.InputPotential+=dendrite
                  
                  if self.InputPotential < self.activation_Pot:
                      self.stopFiring()
                      break

Here is the relevant code from the from the main.py script which is testing the Neuron class:

from neuron import Neuron

# Instantiate transmitting neurone...
n0 = Neuron(3, 0.36)

# Instantiate receiving neurones...
n1 = Neuron(3, 0.36)
n2 = Neuron(3, 0.36)
n3 = Neuron(3, 0.36)

# Make the connections: I do this by creating storing my external Dendrites into a list 
# and passing that list to the transmitting neuron for it to update the voltages 
# of each of these neurons.  BUT THESE LIST VARIABLES ARE NOT GETTING UPDATED...
axonConns = [n1.dendrites[0], n2.dendrites[1], n3.dendrites[2]]
n0.setAxonConnections(axonConns) # THE LIST VARIABLES OF THE axonConns LIST ARE NOT GETTING UPDATED!!

n0.fire()  # THE LIST VARIABLES OF THE axonConns LIST ARE NOT GETTING UPDATED by this fire() method!!

I hope that makes sense. In summary: I am passing a list of variables from my main.py script at the line n0.setAxonConnections(axonConns). The variables in this list are not getting updated by the neuron.fire() method of my Neuron class. Can someone please explain why? Forgive me, I am a python newbe!



from Recent Questions - Stack Overflow https://ift.tt/3A6sdLP
https://ift.tt/eA8V8J

No comments:

Post a Comment