2014-02-25 1 views
0

나는 코딩에 익숙하지 않으며이 "간단한"프로그램에 많은 문제가 있습니다.파이썬 신경망 : 노드를 함께 연결

그래서 나는 함께 만든 "노드"목록에서 3 개의 노드를 연결하려고합니다. Node 클래스에서 추가 연결을 정의하고 이들을 모두 바인딩하는 Connection 클래스를 만들었습니다.

하지만 "Node.addconnection"을 시도하면 "언 바운드 방식"이됩니다. 그리고 "Node.addconnection (Node(), Node())"하면 두 개의 노드를 함께 연결한다고 생각했습니다. 그러나 그것은 나에게 무한 루프의 오류를 준다.

# 
#        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