【问题标题】:Python Neural Networking: running 10 iterationsPython 神经网络:运行 10 次迭代
【发布时间】:2014-03-25 12:50:51
【问题描述】:

所以我定义了一个 Node 和 Connection 类。我创建了 3 个节点,并为每个节点分配了启动激活。然后我假设运行 10 次迭代,看看会发生什么。但我不完全知道那是什么意思。我以前从未编程过,这是我的第一语言,所以如果这真的很简单而且我不理解,请多多包涵。我确实尝试过类似的东西..

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

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

############################################################################################
# 
#                               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 

【问题讨论】:

    标签: python for-loop iteration neural-network


    【解决方案1】:

    首先,在底部明确引用 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,然后您试图在 update_activation() 函数中从中减去 0.5。

    你不能从 None 中减去 0.5 :)

    【讨论】:

    • 哦,感谢您对 thing.update 的修复。我不知道他们必须是同一件事。这解释了很多。而且,我将如何将 3 个节点连接在一起以使整个系统正常工作?我在 Node 类中定义了一个“addconnection”方法,但节点没有相互连接?我在这里错过了一步吗?
    • 对不起,澄清一下,因为我希望这 3 个节点连接起来,这样一个节点就会激活另一个节点,等等。但是现在,我得到的只是相同的输出值。
    • 我没有看到在这段代码中使用了 addConnection() 函数或 Connection 类。这可能是一个很好的起点:)
    猜你喜欢
    • 2014-03-28
    • 2018-03-26
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多