0

I'm new to python, but fairly familiar with OO languages, and I'm struggling with a compilation error that is clear but that I don't understand how to fix.

Here is my class

class Neuron(): 
    def __init__(self):
        self.__output = 0
        self.__inputNeurons = []

    def initInputNeurons(neuronArray, initialWeight=0):
        for neuron in neuronArray:
            __inputNeurons.append((neuron, initialWeight))

    def activateFromInput(value):
        output = value;

    def activateFromNeurons(activationFunction, threshold=1): 
        sumWeightedInputs = 0
        for connection in __inputNeurons: 
            sumWeightedInputs += connection[0].output * connection[1]
        output = activationFunction(sumWeightedInputs, threshold)

Its fairly simple with an init function and a couple options for activation. I initialize the neural network with the following code after reading in my training data .

inputNeurons = []
for value in trainingCharacterVecs[0]:
    inputNeurons.append(Neuron())

outputNeurons = []
for value in range(7):
    print(value);
    neuron = Neuron()
    Neuron.initInputNeurons(inputNeurons, value)
    outputNeurons.append(neuron)

My error occurs when initializing the outputNeuron array, specifically when running

Neuron.initInputNeurons(inputNeurons, value)

I get the following error

Traceback (most recent call last):
    File "./simpleNet.py", line 88, in <module>
Neuron.initInputNeurons(inputNeurons, value)
    File "./simpleNet.py", line 62, in initInputNeurons
__inputNeurons.append((neuron, initialWeight))
NameError: name '_Neuron__inputNeurons' is not defined

The error is telling me that __inputNeurons does not exist. This is also the case when I define _inputNeurons outside the __init__ function as a static variable.

How should I be defining and accessing class variables in python?

1
  • Note that in Python the term "class method" is distinct from a regular method on a class. Commented Sep 24, 2017 at 18:02

1 Answer 1

3

With Python you need to explicitly reference self for each of the references. This means it is your first parameter AND it needs to be referenced through dot notation.

Try this:

# self is the first parameter
def initInputNeurons(self, neuronArray, initialWeight=0):
    for neuron in neuronArray:
        # direct reference to self
        self.__inputNeurons.append((neuron, initialWeight))

# ... LATER ... 
def activateFromNeurons(self, activationFunction, threshold=1): 
    sumWeightedInputs = 0
    # Note the reference to `self.`
    for connection in self.__inputNeurons: 
        sumWeightedInputs += connection[0].output * connection[1]
    output = activationFunction(sumWeightedInputs, threshold)

# and at the bottom:

    neuron = Neuron()
    # marking this lower case, to match your variable name.
    # otherwise `self` doesn't get passed.
    neuron.initInputNeurons(inputNeurons, value)
    outputNeurons.append(neuron)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.