0
我是編碼新手,我對這個「簡單」程序有很多麻煩。Python神經網絡:將節點連接在一起
所以我試圖連接我一起創建的「節點」列表中的3個節點。在Node類中,我定義了一個add連接,並且創建了一個Connection類來將這些連接在一起。
但是,當我嘗試「Node.addconnection」它給了我「未綁定的方法」。如果我試圖「Node.addconnection(Node(),Node())」我以爲這會連接2個節點。但它給了我一個無限循環的錯誤。
#
# 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
哦,因爲我是在節點連接到本身,它只是不停地循環周圍。謝謝! – Averruncus