我是新的编码,所以我有一些简单的问题。当我运行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()
所以,你把它编码了
你把它叫做
这里没有指定重量。因此,您可能认为您使用的是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
的第一次初始化。]相关问题 更多 >
编程相关推荐