Python 3节点神经网络:负值

Python 3节点神经网络:负值,python,connection,neural-network,Python,Connection,Neural Network,所以这里的问题是,当我运行这个代码时。我得到0的起始激活的负值。我假设这是因为没有正确建立连接,但我认为这段代码没有问题。谁能帮我看看吗 ####################################################################################### # # Preparations # #########################################

所以这里的问题是,当我运行这个代码时。我得到0的起始激活的负值。我假设这是因为没有正确建立连接,但我认为这段代码没有问题。谁能帮我看看吗

#######################################################################################
# 
#                               Preparations 
# 
#######################################################################################

import random
import math
import pygame
nodes=[] 
NUMNODES=3

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

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

    def update_weight(self):
        pass

#######################################################################################
# 
#                                 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([0.5,0.0,0.0])

#######################################################################################
#
#                                        Running 10 Iterations
#
#######################################################################################

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

out_file=open('output.txt','w')
out_file.close()

啊,没关系!,我意识到我已将节点连接到自身,因此无法正常运行。重新连接连接,现在工作正常

def addconnection(self,sender,weight=1.0):
    self.connections.append(Connection(sender, self, weight))