gpt4 book ai didi

Python神经网络: running 10 iterations

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:35 26 4
gpt4 key购买 nike

所以我定义了一个 Node 和 Connection 类。我创建了 3 个节点,并为每个节点分配了一个起始激活。然后我打算运行 10 次迭代,看看会发生什么。但我不太清楚这意味着什么。我以前从未编程过,这是我的第一语言,所以如果这实际上非常简单并且我不是不理解,请耐心等待。我确实尝试过类似的事情..

for i in xrange(10):
for thing in nodes:
Node.update_activation

但这给了我一个未绑定(bind)的变量?所以我完全迷失了。

############################################################################################
#
# Preparations
#
############################################################################################
nodes=[]
NUMNODES=3

############################################################################################
#
# Node
#
############################################################################################

class Node(object):

def __init__(self,name=None):
self.name=name
self.activation_threshold=0.0
self.net_input=None
self.outgoing_connections=[]
self.incoming_connections=[]
self.activation=None

def addconnection(self,sender,weight=0.0):
self.connections.append(Connection(self,sender,weight))
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

def update_input(self):
self.net_input=0.0
for conn in self.connections:
self.net_input += conn.wt * 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

############################################################################################
#
# Connection
#
###########################################################################################

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(10):
for thing in nodes:
Node.update_activation
Node.update_input

最佳答案

首先,在底部显式引用 Node 类:

for i in xrange(10): 
for thing in nodes:
Node.update_activation
Node.update_input

您根本没有使用thing变量。 thing 保存您正在迭代的列表中的当前节点。

尝试:

for i in xrange(10): 
for thing in nodes:
thing.update_activation()
thing.update_input()

另请注意,我在您的函数中添加了括号。括号使程序实际调用您创建的函数。例如,thing.update_activation() 正在调用当前保存在 thing 变量中的节点中的 update_activation() 函数。

此外,修复后我收到错误:看起来您正在 Node 类中将 self.net_input 设置为 None,然后您尝试减去 0.5从 update_activation() 函数中获取。

您不能从 None 中减去 0.5 :)

关于Python神经网络: running 10 iterations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21977930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com