首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python神经网络:运行10次迭代

Python神经网络:运行10次迭代
EN

Stack Overflow用户
提问于 2014-02-24 02:34:39
回答 1查看 97关注 0票数 1

因此,我定义了一个节点和连接类。我已经创建了3个节点,并为每个节点分配了一个启动激活。然后我应该运行10个迭代,看看会发生什么。但我不太清楚那是什么意思。我从来没有编程,这是我的第一语言,所以请容忍我,如果这实际上是真的很简单,我不理解。我确实试过..。

代码语言:javascript
复制
for i in xrange(10):
    for thing in nodes:
        Node.update_activation

但这给了我一个未绑定变量?所以我完全迷路了。

代码语言:javascript
复制
############################################################################################
# 
#                               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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-24 03:31:58

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

代码语言:javascript
复制
for i in xrange(10): 
    for thing in nodes: 
        Node.update_activation
        Node.update_input

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

尝试:

代码语言:javascript
复制
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,然后在update_activation()函数中尝试从它中减去0.5。

您不能从None中减去0.5 :)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21977930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档