[英]Python: AttributeError: 'tuple' object has no attribute 'read'
[英]Python - AttributeError: 'tuple' object has no attribute 'fire'
我正在學習制作一個基本的神經網絡,並且正在嘗試制作一個可以在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”作為元組。 然后將元組添加到您的列表中。 您應該考慮使用extend 。
剛意識到錯誤:
而不是i.fire()我應該使用i.origin.fire(),而不是self.inputs.append(synapse)我應該使用self.inputs.extend(synapse)
您的i.fire()
無法正常工作,因為Neuron
類的self.inputs[]
包含Synapses
,而不包含其他Neurons
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.