【问题标题】:Python Neural Networking: Run 10 iterations but I get the same outputPython 神经网络:运行 10 次迭代,但我得到相同的输出
【发布时间】:2014-03-28 12:35:30
【问题描述】:

我是编码新手,所以我遇到了一些简单的问题。当我运行 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()

【问题讨论】:

    标签: python input iteration neural-network activation


    【解决方案1】:

    所以,你编码了

    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,但实际上并非如此。
    如果您仔细观察,您确实在定义addconnection 时将weight = 0.0 指定为默认参数,对吧? :
    def addconnection(self,sender,weight=0.0):

    并且由于您使用
    self.connections.append(Connection(self,sender,weight))
    调用 Connection 类 __init__ 方法 你实际上传递了一个权重值:你在 addconnection 方法中指定的默认 0.0。所以所有连接都将具有默认权重0.0。这有效地将输入的所有值锁定在 0.0,激活锁定在 -0.5。

    要改变这一点,你也许可以在第 75 行指定一个权重,在那里你使用addconnection 方法,和/或只让addconnection 有一个权重的默认值(并让它为 1.0),而 Connection 类__init__ 方法应该总是需要一个 weight 值,没有默认值。这就是我在下面的代码中所做的,加上一些__str__ 方法来检查。

    (这是addconnection中默认值为1.0的版本,而连接__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()
    

    【讨论】:

    • 哦,谢谢,我知道我现在做错了什么。我以为它会以另一种方式覆盖它。嗯,但即使我将激活设置为 1.0、1.0 和 0.0,我得到的值也是一样的。我不知道为什么所有 3 个值都相同。
    • 哦-这是因为您立即调用update_activation,而self.net_input的每个节点的默认值都是0.0(您还没有初始化它),所以所有激活都设置为- 0.5。你应该首先初始化net_input(我想是update_input?),然后才调用update_activation
    • 我在脚本中添加了net_input的初始化,看看这是否是预期的行为!
    • 啊!这很有意义,而且更干净。让我知道哪个节点正在执行什么更新。哇我现在爱你哈哈。 “str”是数字吗?
    • 我使用nodes.append(Node(str(i)))i 变成一个字符串,并将其用作您在Node __init__ 方法中指定的name 参数:) 从那时起,每个节点都有它的self.name,并且这个名称包含在__str__ 方法的返回中:)
    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 2021-01-07
    • 2018-02-21
    • 2017-09-21
    • 2013-04-03
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多