2014-02-25 59 views
0

我是新來編碼,所以我有一些簡單的問題。當我運行10次迭代時,我得到了相同的數字.. -0.5爲激活和0.0爲輸入,即使在底部我設置開始激活的1.0,1.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):

既然你調用Connection類__init__
self.connections.append(Connection(self,sender,weight))
你實際上通過它的重量值:您在addconnection方法指定的默認0.0。因此,所有連接將具有默認權重0.0。這有效地鎖定了輸入的所有值爲0.0,激活爲-0.5。

要改變這一點,你也許可以在線路75指定權重,其中使用addconnection方法,和/或只讓addconnection有重量的默認值(並讓它爲1.0),而Connection類__init__方法應該總是需要一個weight值,沒有默認值。這是我在下面的代碼中所做的,加上一些__str__方法來檢查事情。

(這與addconnection1.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

哦,感謝我看看我現在做錯了什麼。我認爲它會以另一種方式覆蓋它。嗯,但我得到的價值都是相同的,即使我將激活設置爲1.0,1.0和0.0。我不知道爲什麼所有3個值都是一樣的。 – Averruncus

+0

噢 - 這是因爲你爲每個節點(你還沒有初始化)的'self.net_input'具有默認值0.0時立即調用'update_activation',所以所有激活設置爲-0.5。 你應該首先初始化'net_input'(我想''update_input''),然後才調用'update_activation'。 – Roberto

+0

我在腳本中添加了一個'net_input'的初始化,看看這是否是預期的行爲! – Roberto