我正在尝试在python中编写一个用于尖峰神经网络的模拟器。 计划通过神经元和连接构建网络,如下所示:
连接类:
class Connection(object):
connectionType = 0
weight = [[0 for x in range(1)] for x in range(9)]
def __init__():
self.threshold = 0
self.connectionType = 0
神经元课程:
import Connection
class Neuron(object):
ID = 1
OveralPotential = 0
def __init__(self):
global ID
self.input = [[0 for x in range(1)] for x in range(9)]
self.output = [[0 for x in range(1)] for x in range(9)]
self.id = Neuron.ID
Neuron.ID += 1
def connect(srcNeuron, dstNeuron, connectionType):
srcNeuron.output[dstNeuron.id] = 1
dstNeuron.input[srcNeuron.id] = 1
Connection.Connection.connectionType = connectionType
if __name__ == "__main__":
n1 = Neuron()
n2 = Neuron()
n3 = Neuron()
print("ID of the 3rd neuron is {0}".format(n3.id))
n4 = Neuron()
n5 = Neuron()
Neuron.connect(1,3,1)
Neuron.connect(1,4,1)
Neuron.connect(1,5,1)
Neuron.connect(2,3,1)
Neuron.connect(2,4,1)
Neuron.connect(2,5,1)
但这是stacktrace:
ID of the 3rd neuron is 3
Traceback (most recent call last):
File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Mic
rosoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 106, in
exec_file
exec_code(code, file, global_variables)
File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Mic
rosoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 82, in
exec_code
exec(code_obj, global_variables)
File "C:\Users\Matinking\Documents\Visual Studio 2013\Projects\NeuroSimulation
\NeuroSimulation\Neuron.py", line 31, in <module>
Neuron.connect(1,3,1)
File "C:\Users\Matinking\Documents\Visual Studio 2013\Projects\NeuroSimulation
\NeuroSimulation\Neuron.py", line 16, in connect
srcNeuron.output[dstNeuron.id] = 1
AttributeError: 'int' object has no attribute 'output'
在这种情况下,我检查了可能的重复项,例如:
AttributeError: 'int' object has no attribute 'split'
和
AttributeError: 'int' object has no attribute 'write'
但他们都没有给我足够的洞察力来处理上述事情。 我该如何解决?!
提前致谢
答案 0 :(得分:1)
connect
函数需要两个Neuron
类型的对象,而不是神经元ID。您正在传递一个id,即int
,并且python尝试访问输出字段。
答案 1 :(得分:1)
您正在将整数值传递给方法
def connect(srcNeuron, dstNeuron, connectionType):
srcNeuron.output[dstNeuron.id] = 1
dstNeuron.input[srcNeuron.id] = 1
Connection.Connection.connectionType = connectionType
当你调用它时
Neuron.connect(1,3,1)
Neuron.connect(1,4,1)
Neuron.connect(1,5,1)
Neuron.connect(2,3,1)
Neuron.connect(2,4,1)
Neuron.connect(2,5,1)
我认为你实际上是要指定神经元而不是整数
Neuron.connect(n1,n3,n1)
Neuron.connect(n1,n4,n1)
Neuron.connect(n1,n5,n1)
Neuron.connect(n2,n3,n1)
Neuron.connect(n2,n4,n1)
Neuron.connect(n2,n5,n1)