0

我是新來的編程,目前有一些簡單的問題,試圖訓練我的hopfield網絡,但我試圖計算連接的權重時,不斷收到此錯誤。也許我不懂如何「訓練」網絡,或者我錯過了某個地方或某個步驟。但我所定義的節點類下面的功能:蟒蛇Hopfield網絡:培訓網絡 - 權重錯誤

def update_weight(self): 
    for i in self.incoming_connections: 
     i.weight += (2*self.activation - 1)*(2*i.sender.activation-1) 

這應該是正確的,但是當我更新的權重,然後輸入,然後激活(位於末尾)。我收到一個錯誤,說我的更新權重函數「不受支持的操作數類型」,我不明白。有人能幫我看看我的問題似乎是什麼嗎?

# 
#        Preparations 
# 

import random 
import math 
import pygame 
nodes=[] 
training=[] 
NUMNODES=16 

# 
#         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.activation=None 

    def __str__(self): 
     return self.name 

    def addconnection(self,sender,weight=0.0): 
     self.incoming_connections.append(Connection(sender,self,weight)) 

    def update_input(self): 
     self.net_input=0.0 
     for conn in self.incoming_connections: 
      self.net_input += conn.weight * conn.sender.activation 
     print 'Updated Input for node', str(self), 'is', self.net_input 

    def update_activation(self): 
     if self.net_input > self.activation_threshold: 
      self.activation = 1.0 
      print 'Node', str(self), 'is activated : ', self.activation 
     elif self.net_input <= self.activation_threshold: 
      self.activation = 0.0 
      print 'Node', str(self), 'is not activated : ', self.activation 

    def update_training(self): 
     Node = random.choice(nodes) 

    def update_weight(self): 
     for i in self.incoming_connections: 
      i.weight += (2*self.activation - 1)*(2*i.sender.activation-1) 
      print 'Weight is now set' 

# 
#         Connection Class 
# 

class Connection(object): 
    def __init__(self, sender, reciever, weight): 
     self.weight=weight 
     self.sender=sender 
     self.reciever=reciever 

    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): 
    for i in xrange(len(act_vector)): 
     nodes[i].activation = act_vector[i] 

for i in xrange(NUMNODES): 
    nodes.append(Node(str(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 

# 
#           Training Patterns 
# 

train1=(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0) 
training.append(train1) 
train2=(1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0) 
training.append(train2) 
train3=(1.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0) 
training.append(train3) 
train4=(1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0) 
training.append(train4) 

set_activations=(train1) 

# 
#          Running 10 Iterations 
# 

for i in xrange(10): 
    print '      *********** Iteration', str(i+1), '***********' 
    for thing in nodes: 
     thing.update_weight() 
    for thing in nodes: 
     thing.update_input() 
    for thing in nodes: 
     thing.update_activation() 

out_file=open('output.txt','w') 
out_file.close() 

回答

0

也許這些activation屬性之一是None

i.weight += (2*self.activation - 1)*(2*i.sender.activation-1) 

這是一件好事(而不是默默不及格),因爲它表明一個節點尚未建立正確的地方的錯誤。

如果您發佈實際的回溯,即使它對您沒有意義,它也會有所幫助。

編輯

看起來這是一個錯誤

set_activations=(train1) 

,你應該可以調用set_activations(train1)呢?

+0

哦,我看起來就是這個問題所在。我做了改變,你建議等號不應該在那裏,錯誤是固定的!非常感謝!現在嘗試「訓練」網絡。 – Averruncus