Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Hopfield网络:训练网络-权重错误_Python_Connection_Iteration_Neural Network_Weighted Average - Fatal编程技术网

Python Hopfield网络:训练网络-权重错误

Python Hopfield网络:训练网络-权重错误,python,connection,iteration,neural-network,weighted-average,Python,Connection,Iteration,Neural Network,Weighted Average,我是编程新手,目前在训练我的hopfield网络时遇到一些简单的问题,但在计算连接的权重时,我一直遇到这个错误。也许我不了解如何训练网络,或者也许我在某个地方错过了一步。但我已经在node类下定义了函数: def update_weight(self): for i in self.incoming_connections: i.weight += (2*self.activation - 1)*(2*i.sender.activation-1) 这应该是正确的

我是编程新手,目前在训练我的hopfield网络时遇到一些简单的问题,但在计算连接的权重时,我一直遇到这个错误。也许我不了解如何训练网络,或者也许我在某个地方错过了一步。但我已经在node类下定义了函数:

    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()
这些激活属性中可能有一个是None

与静默失败相比,这是一件好事,因为它表明某个节点未在某个位置正确设置

如果您发布实际的回溯,即使它对您来说还没有意义,也会很有帮助

编辑

看来这是个错误

set_activations=(train1)

您是否应该改为调用set\u activationstrain1?

哦,天哪,问题似乎就在这里。我做了你建议的改变,在那里不应该有等号,错误是固定的!谢谢!现在试着训练网络。
set_activations=(train1)