2014-02-25 5 views
0

코딩에 익숙하지 않아 간단한 문제가 발생했습니다. 내가 10 번의 반복을 실행할 때, 시작점 활성화를 노드 목록의 각 해당 노드에 1.0, 1.0 및 0.0으로 설정 했는데도 맨 아래에 활성화와 0.0의 동일한 숫자가 있습니다.Python 신경 회로망 : 10 반복을 실행하지만 동일한 출력을 얻었습니다

초기 상태를 설정하여 생각했습니다. 그들은 다른 노드에 입력을 보낸다. 이것은 sender.activation * 1의 가중치이다. 나는 새로운 입력 값을 가져야한다. 그러면 활성화에 적용되어 -0.5로 노드를 새로 활성화 할 수 있습니다.

적어도 내가 그렇게하려고하는 것입니다. 그리고 어쨌든 그것은 단지 0.0과 -0.5를 뱉어냅니다.

# 
#        Preparations 
# 

nodes=[] 
NUMNODES=3 

# 
#         Defining Node Class 
# 

class Node(object): 

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

    def addconnection(self,sender,weight=0.0): 
     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 

# 
#         Defining 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(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 
# 
#           Setting Activations 
# 
set_activations([1.0,1.0,0.0]) 

# 
#          Running 10 Iterations 
# 

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

답변

0

그래서, 당신은

def addconnection(self,sender,weight=0.0): 
    self.connections.append(Connection(self,sender,weight)) 
    print "Node", str(self), "now contains", str(self.connections[-1]) 

은 당신이 당신이 바로 여기에 무게를 지정하지

nodes[i].addconnection(nodes[j]) #connects the nodes together 

함께 부르는 코딩. 따라서 Connections 클래스의 기본값 인 weight = 1.0을 사용하고 있다고 생각할 수도 있지만 그렇지 않습니다.
자세히 살펴 보려면 addconnection을 정의 할 때 기본 매개 변수로 weight = 0.0을 지정 하시겠습니까? :
def addconnection(self,sender,weight=0.0):

그리고 당신은
self.connections.append(Connection(self,sender,weight))
와 연결 클래스 __init__ 메소드를 호출하기 때문에 당신이 실제로 그것을 무게 값을 전달할 : 당신이 addconnection 방법에 지정된 기본 0.0을. 따라서 모든 연결에는 기본 가중치 0.0이 있습니다. 이것은 0.0의 입력 값과 -0.5의 활성화 값을 효과적으로 잠급니다.

이를 변경하려면, 당신은 아마 당신이 addconnection 방법을 사용하는 경우 라인 (75)에 무게를 지정, 및/또는 전용 addconnection을 할 수있는 체중에 대한 기본 값이 (그리고 1.0하자) 동안 연결 클래스 __init__ 방법은한다 값이 항상 weight이어야하며 기본값은 없습니다. 이것은 아래 코드에서 수행 한 것입니다. 어떤 것을 확인하기 위해 어떤 __str__ 메소드를 더한 것입니다.

:

는 (이 addconnection에서 1.0의 디폴트 값을 가진 버전과 연결 __init__ 없음 기본값) [편집 :. 추가 net_input의 제 초기화]

# 
#        Preparations 
# 

nodes=[] 
NUMNODES=3 

# 
#         Defining Node Class 
# 

class Node(object): 

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

    def __str__(self): 
     return self.name 

    def addconnection(self,sender,weight=1.0): 
     self.connections.append(Connection(self,sender,weight)) 
     print "Node", str(self), "now contains", str(self.connections[-1]) 

    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 for node', str(self), 'is', self.net_input 

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

# 
#         Defining Connection Class 
# 

class Connection(object): 
    def __init__(self, sender, reciever, weight): 
     self.weight=weight 
     self.sender=sender 
     self.reciever=reciever 
     sender.outgoing_connections.append(self) 
     reciever.incoming_connections.append(self) 
     print 'Created', str(self) 

    def __str__(self): 
     string = "Connection from " + str(self.sender) + " to " + str(self.reciever) + ", weight = " + str(self.weight) 
     return string 
# 
#         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(str(i))) 
    print "Created node:", nodes[i] 


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 
# 
#           Setting Activations 
# 
set_activations([1.0,1.0,0.0]) 

# 
#          Running 10 Iterations 
# 
for thing in nodes: 
    thing.update_input() #initializing inputs 

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

오 덕분 I 내가 지금 잘못한 것을 보아라. 나는 그것을 다른 방법으로 덮어 쓸 것이라고 생각했다. 하지만 활성화 값을 1.0, 1.0 및 0.0으로 설정하더라도 값은 모두 같습니다. 왜 3 가지 값이 모두 같은지 모르겠습니다. – Averruncus

+0

아 - 그것은'update_activation'을 즉시 호출하기 때문에,'self.net_input'가 모든 노드에 대해 0.0의 기본값을 가졌을 때 (아직 초기화하지 않았기 때문에) 모든 활성화가 -0.5로 설정됩니다. 먼저'net_input'을 먼저 초기화해야합니다 ('update_input'으로 가정)?'update_activation' 만 호출하십시오. – Roberto

+0

스크립트에'net_input'의 초기화를 추가했습니다. 이것이 예상 된 동작인지 확인하십시오! – Roberto

관련 문제