我正在学习制作一个基本的神经网络,我正在尝试制作一个可以在 python 中将数字相加的网络,但我不断收到此错误:
Traceback (most recent call last):
File "C:/Users/tomas/Desktop/projects/neuralnetwork/main.py", line 37, in <module>
class Main:
File "C:/Users/tomas/Desktop/projects/neuralnetwork/main.py", line 76, in Main
out.fire()
File "C:/Users/tomas/Desktop/projects/neuralnetwork/main.py", line 23, in fire
i.fire()
AttributeError: 'tuple' object has no attribute 'fire'
我是 python 新手,所以如果我犯了一些明显的错误,请原谅我。
代码:
class Synapse:
origin = None
weight, learningRate = 0.5, 0.25
def __init__(self, origin):
self.origin = origin
def adjust_weight(self, result, expected):
self.weight *= self.learningRate * (expected - result)
class Neuron:
threshold, output = None, None
inputs = []
def __init__(self, threshold, output):
self.threshold = threshold
self.output = output
def fire(self):
weighed_input = 0
for i in self.inputs:
i.fire()
weighed_input += i.weight * i.origin.output
if weighed_input >= self.threshold:
self.output = weighed_input
else:
self.output = 0
def connect_synapse(self, *synapse):
self.inputs.append(synapse)
def clear_synapses(self):
self.inputs = []
class Main:
training_data = [[[1, 2], [3]],
[[2, 0], [2]],
[[1, 1], [2]],
[[3, 0], [3]],
[[3, 1], [4]]]
src1 = Neuron(0, None)
src2 = Neuron(0, None)
hid1 = Neuron(1, None)
hid2 = Neuron(1, None)
out = Neuron(0, None)
syn1 = Synapse(src1)
syn2 = Synapse(src2)
syn3 = Synapse(hid1)
syn4 = Synapse(hid2)
while True:
error_count = 0
hid1.clear_synapses()
hid2.clear_synapses()
hid1.connect_synapse(syn1, syn2)
hid2.connect_synapse(syn1, syn2)
out.clear_synapses()
out.connect_synapse(syn3, syn4)
for i in range(0, 5):
input1 = training_data[i][0][0]
input2 = training_data[i][0][1]
expected = training_data[i][1][0]
src1.output = input1
src2.output = input2
out.fire()
result = out.output
if result != expected:
error_count += 1
syn1.adjust_weight(result, expected)
syn2.adjust_weight(result, expected)
print("Expected ", expected, "\nGot ", result, "\nError count: ", error_count, "\n\n")
if error_count == 0:
break
提前致谢。
def connect_synapse(self, *synapse): self.inputs.append(synapse)
这是接收多个参数“*synapse”作为元组。然后该元组将被附加到您的列表中。您应该考虑使用扩展。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句