Python神经网络:连接节点

2024-06-02 07:46:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的编码,我有很多麻烦,这个“简单”的程序。你知道吗

因此,我试图连接“节点”列表中的3个节点,我已经创建在一起。在Node类中,我定义了一个add连接,并创建了一个连接类来将这些连接绑定在一起。你知道吗

但当我试图节点.addconnection它给了我“未绑定方法”。如果我想”节点.addconnection(Node(),Node())“我以为这会把两个节点连接在一起。但它给了我一个无限的错误循环。你知道吗

# 
#                               Preparations 
# 

nodes=[] 
NUMNODES=3

# 
#                                   Node Class
# 

class Node(object): 

    def __init__(self,name=None): 
        self.name=name 
        self.activation_threshold=0.0
        self.net_input=0.0
        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
# 

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: 
        thing.update_activation 
        thing.update_input

Tags: inselfnodeforinputnet节点def
1条回答
网友
1楼 · 发布于 2024-06-02 07:46:23

您的问题是,在调用“addconnection”时,您试图通过调用…addconnection将所有节点连接在一起。因此,递归。你知道吗

如果尝试连接“其他程序”部分中的所有节点,则效果会更好:

# 
#                               Preparations 
# 

nodes=[] 
NUMNODES=3

# 
#                                   Node Class
# 

class Node(object): 

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

    def addconnection(self,sender,weight=0.0): 
        #just add the connection
        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 

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

#do the connections here
from random import random
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],random())#connects the nodes together 

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


...
Updated Activation is -0.5
Updated Input is -0.452698580538
Updated Activation is -0.5
Updated Input is -0.336733968936
Updated Activation is -0.5
Updated Input is -0.204908511332
Updated Activation is -0.952698580538
Updated Input is -0.862570590181
Updated Activation is -0.836733968936
Updated Input is -0.563513500607
Updated Activation is -0.704908511332
Updated Input is -0.288883507365
Updated Activation is -1.36257059018
...

相关问题 更多 >