我正在學習做一個基本的神經網絡,我試圖做一個可以在Python中添加數字,但我不斷收到此錯誤:Python - AttributeError:'元組'對象沒有屬性'火'
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
在此先感謝。
這不是Java。你不需要把所有東西都放在課堂上。而且,在類作用域中定義變量會創建類變量,而不是實例變量。 – user2357112
是的,我已經習慣了Java,而且我還沒有用Python編寫太多的代碼 – valkyrd
你認爲'i.fire()'會做什麼?你爲什麼期望'我'有一個'火'的方法? – user2357112