Python神经网络:运行10次迭代,但我得到相同的输出

2024-04-19 19:47:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的编码,所以我有一些简单的问题。当我运行10次迭代时,我得到相同的数字。。-0.5表示激活,0.0表示输入,即使在底部,我为节点列表中的每个对应节点设置了启动激活为1.0、1.0和0.0。你知道吗

我想通过设置初始状态。它们向另一个节点发送一个输入:即发件人激活*重量为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()

Tags: inselfforinputnet节点defconnections
1条回答
网友
1楼 · 发布于 2024-04-19 19:47:51

所以,你把它编码了

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):

既然用
调用连接类__init__方法 self.connections.append(Connection(self,sender,weight))
实际上,您传递了一个weight值:您在addconnection方法中指定的默认值0.0。因此,所有连接都将具有默认的权重0.0。这将有效地将所有输入值锁定为0.0,并将激活值锁定为-0.5。你知道吗

要改变这一点,您可以在第75行指定一个权重,在该行中使用addconnection方法,和/或只让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()

相关问题 更多 >