2014-02-25 116 views
0

我是編碼新手,我對這個「簡單」程序有很多麻煩。Python神經網絡:將節點連接在一起

所以我試圖連接我一起創建的「節點」列表中的3個節點。在Node類中,我定義了一個add連接,並且創建了一個Connection類來將這些連接在一起。

但是,當我嘗試「Node.addconnection」它給了我「未綁定的方法」。如果我試圖「Node.addconnection(Node(),Node())」我以爲這會連接2個節點。但它給了我一個無限循環的錯誤。

# 
#        Preparations 
# 

nodes=[] 
NUMNODES=3 

# 
#         Node Class 
# 

class Node(object): 

    def __init__(self,name=None): 
     self.name=name 
     self.activation_threshold=0.0 
     self.net_input=0.0 
     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 
# 

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: 
     thing.update_activation 
     thing.update_input 

回答

0

您的問題是,您嘗試通過調用... addconnection調用「addconnection」時將所有節點連接在一起。因此,遞歸。

如果你試圖在你的「其他計劃」的一部分所有節點連接,它會更好:

# 
#        Preparations 
# 

nodes=[] 
NUMNODES=3 

# 
#         Node Class 
# 

class Node(object): 

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

    def addconnection(self,sender,weight=0.0): 
     #just add the connection 
     self.connections.append(Connection(self,sender,weight)) 

    def update_input(self): 
     self.net_input=0.0 
     for conn in self.connections: 
      self.net_input += conn.weight * 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 
# 

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

#do the connections here 
from random import random 
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],random())#connects the nodes together 

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


... 
Updated Activation is -0.5 
Updated Input is -0.452698580538 
Updated Activation is -0.5 
Updated Input is -0.336733968936 
Updated Activation is -0.5 
Updated Input is -0.204908511332 
Updated Activation is -0.952698580538 
Updated Input is -0.862570590181 
Updated Activation is -0.836733968936 
Updated Input is -0.563513500607 
Updated Activation is -0.704908511332 
Updated Input is -0.288883507365 
Updated Activation is -1.36257059018 
... 
+0

哦,因爲我是在節點連接到本身,它只是不停地循環周圍。謝謝! – Averruncus