1

So what I have is defined a Node and Connection class. I've created 3 nodes and I have assigned each of the nodes a starting activation. Then I'm suppose to run 10 iterations and see what happens. But I don't exactly know what that means. I've never programmed before, and this is my first language so please bear with me if this is actually really simple and I'm not not understanding. I did try something like..

for i in xrange(10):
    for thing in nodes:
        Node.update_activation

but this gave me an unbound variable? So I'm just completely lost.

############################################################################################
# 
#                               Preparations 
# 
############################################################################################
nodes=[] 
NUMNODES=3

############################################################################################
# 
#                                   Node 
# 
############################################################################################

class Node(object): 

    def __init__(self,name=None): 
        self.name=name 
        self.activation_threshold=0.0
        self.net_input=None
        self.outgoing_connections=[] 
        self.incoming_connections=[] 
        self.activation=None

    def addconnection(self,sender,weight=0.0): 
        self.connections.append(Connection(self,sender,weight)) 
        for i in xrange(NUMNODES):#go thru all the nodes calling them i 
            for j in xrange(NUMNODES):#go thru all the nodes calling them j 
                if i!=j:#as long as i and j are not the same 
                    nodes[i].AddConnection(nodes[j])#connects the nodes together 

    def update_input(self): 
        self.net_input=0.0
        for conn in self.connections: 
            self.net_input += conn.wt * conn.sender.activation 
        print 'Updated Input is', self.net_input 

    def update_activation(self): 
        self.activation = self.net_input - 0.5
        print 'Updated Activation is', self.activation 

############################################################################################
# 
#                                   Connection 
# 
########################################################################################### 

class Connection(object): 

    def __init__(self, sender, reciever, weight=1.0): 
        self.weight=weight 
        self.sender=sender 
        self.reciever=reciever 
        sender.outgoing_connections.append(self) 
        reciever.incoming_connections.append(self) 
############################################################################################
# 
#                                 Other Programs 
# 
############################################################################################

def set_activations(act_vector): 
    """Activation vector must be same length as nodes list"""
    for i in xrange(len(act_vector)): 
        nodes[i].activation = act_vector[i] 

for i in xrange(NUMNODES): 
    nodes.append(Node()) 

for i in xrange(10): 
    for thing in nodes: 
        Node.update_activation 
        Node.update_input 

1 Answer 1

1

Firstly, at the bottom you refer to the Node class explicitly:

for i in xrange(10): 
    for thing in nodes: 
        Node.update_activation
        Node.update_input

you are not using the thing variable at all. thing holds the current node in the list that you are iterating through.

Try:

for i in xrange(10): 
    for thing in nodes: 
        thing.update_activation()
        thing.update_input()

Also note that I added parenthesis to your functions. Parenthesis makes the program actually call the function you created. For example, thing.update_activation() is calling the update_activation() function in the node currently held in the thing variable.

Furthermore, I am getting an error after this fix: looks like you are setting self.net_input to None in the Node class, then you are trying to subtract 0.5 from it in the update_activation() function.

You can't subtract 0.5 from None :)

Sign up to request clarification or add additional context in comments.

3 Comments

OH thanks for the fix on the thing.update. I was not aware that they had to be the same thing. That explains a lot. And also, how would I go about connecting the 3 nodes together to make this whole system work? I've defined an "addconnection" method in my Node class but the nodes aren't connecting to each other? Am I missing a step here?
I'm sorry, just to clarify, because I want these 3 nodes connected, so that one node would active the other and etc. But right now, all I get is the same output value.
I do not see any use of the addConnection() function or the Connection class in this code. That may be a good place to start :)

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.