我刚开始编写代码,所以我有一些简单的问题。当我运行10次迭代时,我得到相同的数字。-0.5表示激活,0.0用于输入,尽管在底部,我将起始激活设置为1.0、1.0和0.0,用于节点列表中的每个相应节点。
我想通过设定初始状态。他们向另一个节点发送一个输入: 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()发布于 2014-02-26 00:49:30
所以,你编码了
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,但您没有使用。
如果仔细观察,就会在定义weight = 0.0时指定addconnection作为默认参数,对吗?:
def addconnection(self,sender,weight=0.0):
因为您调用连接类__init__方法
self.connections.append(Connection(self,sender,weight))
您实际上向它传递了一个权重值:在addconnection方法中指定的默认0.0。所以所有的连接都有默认的权重0.0。这有效地将输入的所有值锁定在0.0,激活锁定在-0.5。
要改变这一点,您也许可以在第75行指定一个权重,在这里使用addconnection方法,并且/或只让addconnection有一个默认值(并让它是1.0),而连接类__init__方法应该总是需要一个weight值,而没有默认值。这就是我在下面的代码中所做的,加上一些__str__方法来检查事情。
(这是一个默认值为1.0的addconnection版本,而在连接__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()https://stackoverflow.com/questions/22028573
复制相似问题