在过去的几周里,我一直在空闲时间创建这个神经网络;我一直在喂它两个数字(1或0)并让它学习XOR功能。但是,在经过多次不同的调试尝试之后,我遇到了程序部分的问题,我根据输出是太大还是太小来调整节点和连接的权重。 当输出声明为“太大”且连接器值为1时,则这些值不会改变; “Too small”输出也是如此:它们不会增加-1的连接器值。 以下是完整的代码;之后,我已经删除了重量调整代码,因此更容易查看。 代码长约150行,所以我不希望很快就能找到解决方案,但任何帮助都会非常感激。
我的问题是:我忽略了这部分编码是否有问题,或者您是否有修复?
谢谢!
代码:
import random
from random import randint
GenericArray = [-1,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
Another_Generic_Array = [-0.1,0.1]
Node1_Value = 0
Node2_Value = 0
Output = 0
Generic_Variable = 0
NumberOfQuestions = 0
QuestionsRight = 0
Randomnumber = random.choice(GenericArray)
Connector1_1 = Randomnumber
Randomnumber = random.choice(GenericArray)
Connector1_2 = Randomnumber
Randomnumber = random.choice(GenericArray)
Connector2_1 = Randomnumber
Randomnumber = random.choice(GenericArray)
Connector2_2 = Randomnumber
Randomnumber = random.choice(GenericArray)
Node1 = Randomnumber
Randomnumber = random.choice(GenericArray)
Node2 = Randomnumber
Randomnumber = random.choice(GenericArray)
ConnectorNd1_Out = Randomnumber
Randomnumber = random.choice(GenericArray)
ConnectorNd2_Out = Randomnumber
Decision = "Null"
Spam = 2
i = 0
while True:
if Generic_Variable == 0:
print("ERROR")
i = 0
InputNumber = input("How many numbers do you want inputted?")
while True:
if Connector1_1 == 0:
Connector1_1 = random.choice(Another_Generic_Array)
if Connector1_2 == 0:
Connector1_2 = random.choice(Another_Generic_Array)
if Connector2_1 == 0:
Connector2_1 = random.choice(Another_Generic_Array)
if Connector2_2 == 0:
Connector2_2 = random.choice(Another_Generic_Array)
if Node1 == 0:
Node1 = random.choice(Another_Generic_Array)
if Node2 == 0:
Node2 = random.choice(Another_Generic_Array)
if ConnectorNd1_Out == 0:
ConnectorNd1_Out = random.choice(Another_Generic_Array)
if ConnectorNd2_Out == 0:
ConnectorNd2_Out = random.choice(Another_Generic_Array)
if Connector1_1 > 1:
Connector1_1 = 1
if Connector1_2 > 1:
Connector1_2 = 1
if Connector2_1 > 1:
Connector2_1 = 1
if Connector2_2 > 1:
Connector2_2 = 1
if Node1 > 1:
Node1 = 1
if Node2 > 1:
Node2 = 1
if ConnectorNd1_Out > 1:
ConnectorNd1_Out = 1
if ConnectorNd2_Out > 1:
ConnectorNd2_Out = 1
if Connector1_1 < -1:
Connector1_1 = -1
if Connector1_2 < -1:
Connector1_2 = -1
if Connector2_1 < -1:
Connector2_1 = -1
if Connector2_2 < -1:
Connector2_2 = -1
if Node1 < -1:
Node1 = -1
if Node2 < -1:
Node2 = -1
if ConnectorNd1_Out < -1:
ConnectorNd1_Out = -1
if ConnectorNd2_Out < -1:
ConnectorNd2_Out = -1
Input1 = randint(0,1)
Input2 = randint(0,1)
Node1_Value = (Input1 * Connector1_1) + (Input2 * Connector2_1)
Node2_Value = (Input1 * Connector1_2) + (Input2 * Connector2_2)
Output = (Node1_Value * Node1 * ConnectorNd1_Out) + (Node2_Value * Node2 * ConnectorNd2_Out)
print Connector1_1
print Connector1_2
print Connector2_1
print Connector2_2
print ConnectorNd1_Out
print ConnectorNd2_Out
print Node1
print Node2
if Output == Input1 ^ Input2:
print("The Output was correct")
QuestionsRight += 1
NumberOfQuestions += 1
PercentageCorrect = (QuestionsRight / NumberOfQuestions) * 100
elif Output > Input1 ^ Input2:
print("The Output was too big")
Generic_Variable = (Input1 ^ Input2) - Output
Generic_Variable = 1 / Generic_Variable
Generic_Variable = 1 - Generic_Variable
if Connector1_1 > 0:
Connector1_1 = Connector1_1 - (Generic_Variable * Connector1_1)
if Connector1_2 > 0:
Connector1_2 = Connector1_2 - (Generic_Variable * Connector1_2)
if Connector2_1 > 0:
Connector2_1 = Connector2_1 - (Generic_Variable * Connector2_1)
if Connector2_2 > 0:
Connector2_2 = Connector2_2 - (Generic_Variable * Connector2_2)
if ConnectorNd1_Out > 0:
ConnectorNd1_Out = ConnectorNd1_Out - (Generic_Variable * ConnectorNd1_Out)
if ConnectorNd2_Out > 0:
ConnectorNd2_Out = ConnectorNd2_Out - (Generic_Variable * ConnectorNd2_Out)
if Node1 > 0:
Node1 = Node1 - (Generic_Variable * Node1)
if Node2 > 0:
Node2 = Node2 - (Generic_Variable * Node2)
if Connector1_1 < 0:
Connector1_1 = Connector1_1 + (Generic_Variable * (0 - Connector1_1))
if Connector1_2 < 0:
Connector1_2 = Connector1_2 + (Generic_Variable * (0 - Connector1_2))
if Connector2_1 < 0:
Connector2_1 = Connector2_1 + (Generic_Variable * (0 - Connector2_1))
if Connector2_2 < 0:
Connector2_2 = Connector2_2 + (Generic_Variable * (0 - Connector2_2))
if ConnectorNd1_Out < 0:
ConnectorNd1_Out = ConnectorNd1_Out + (Generic_Variable * (0 - ConnectorNd1_Out))
if ConnectorNd2_Out < 0:
ConnectorNd2_Out = ConnectorNd2_Out + (Generic_Variable * (0 - ConnectorNd2_Out))
if Node1 < 0:
Node1 = Node1 + (Generic_Variable * (0 - Node1))
if Node2 < 0:
Node2 = Node2 + (Generic_Variable * (0 - Node2))
NumberOfQuestions += 1
PercentageCorrect = (QuestionsRight / NumberOfQuestions) * 100
elif Output < Input1 ^ Input2:
print("The Output was too small")
Generic_Variable = (Input1 ^ Input2) - Output
Generic_Variable = 1 / Generic_Variable
Generic_Variable = 1 - Generic_Variable
if Connector1_1 > 0:
Connector1_1 = Connector1_1 - (Generic_Variable * Connector1_1)
if Connector1_2 > 0:
Connector1_2 = Connector1_2 - (Generic_Variable * Connector1_2)
if Connector2_1 > 0:
Connector2_1 = Connector2_1 - (Generic_Variable * Connector2_1)
if Connector2_2 > 0:
Connector2_2 = Connector2_2 - (Generic_Variable * Connector2_2)
if ConnectorNd1_Out > 0:
ConnectorNd1_Out = ConnectorNd1_Out - (Generic_Variable * ConnectorNd1_Out)
if ConnectorNd2_Out > 0:
ConnectorNd2_Out = ConnectorNd2_Out - (Generic_Variable * ConnectorNd2_Out)
if Node1 > 0:
Node1 = Node1 - (Generic_Variable * Node1)
if Node2 > 0:
Node2 = Node2 - (Generic_Variable * Node2)
if Connector1_1 < 0:
Connector1_1 = Connector1_1 + (Generic_Variable * (0 - Connector1_1))
if Connector1_2 < 0:
Connector1_2 = Connector1_2 + (Generic_Variable * (0 - Connector1_2))
if Connector2_1 < 0:
Connector2_1 = Connector2_1 + (Generic_Variable * (0 - Connector2_1))
if Connector2_2 < 0:
Connector2_2 = Connector2_2 + (Generic_Variable * (0 - Connector2_2))
if ConnectorNd1_Out < 0:
ConnectorNd1_Out = ConnectorNd1_Out + (Generic_Variable * (0 - ConnectorNd1_Out))
if ConnectorNd2_Out < 0:
ConnectorNd2_Out = ConnectorNd2_Out + (Generic_Variable * (0 - ConnectorNd2_Out))
if Node1 < 0:
Node1 = Node1 + (Generic_Variable * (0 - Node1))
if Node2 < 0:
Node2 = Node2 + (Generic_Variable * (0 - Node2))
NumberOfQuestions += 1
PercentageCorrect = ((QuestionsRight * 100) / NumberOfQuestions)
round(Connector1_1, 2)
round(Connector1_2, 2)
round(Connector2_1, 2)
round(Connector2_2, 2)
round(ConnectorNd1_Out, 2)
round(ConnectorNd2_Out, 2)
round(Node1, 2)
round(Node2, 2)
i = i+1
if i == InputNumber:
print("We've had " + str(NumberOfQuestions) + " questions asked.")
print(str(QuestionsRight) + " of them were answered correctly.")
break
raw_input = ("Training finished!")
重量调整部分:
elif Output > Input1 ^ Input2:
print("The Output was too big")
Generic_Variable = (Input1 ^ Input2) - Output
Generic_Variable = 1 / Generic_Variable
Generic_Variable = 1 - Generic_Variable
if Connector1_1 > 0:
Connector1_1 = Connector1_1 - (Generic_Variable * Connector1_1)
if Connector1_2 > 0:
Connector1_2 = Connector1_2 - (Generic_Variable * Connector1_2)
if Connector2_1 > 0:
Connector2_1 = Connector2_1 - (Generic_Variable * Connector2_1)
if Connector2_2 > 0:
Connector2_2 = Connector2_2 - (Generic_Variable * Connector2_2)
if ConnectorNd1_Out > 0:
ConnectorNd1_Out = ConnectorNd1_Out - (Generic_Variable * ConnectorNd1_Out)
if ConnectorNd2_Out > 0:
ConnectorNd2_Out = ConnectorNd2_Out - (Generic_Variable * ConnectorNd2_Out)
if Node1 > 0:
Node1 = Node1 - (Generic_Variable * Node1)
if Node2 > 0:
Node2 = Node2 - (Generic_Variable * Node2)
if Connector1_1 < 0:
Connector1_1 = Connector1_1 + (Generic_Variable * (0 - Connector1_1))
if Connector1_2 < 0:
Connector1_2 = Connector1_2 + (Generic_Variable * (0 - Connector1_2))
if Connector2_1 < 0:
Connector2_1 = Connector2_1 + (Generic_Variable * (0 - Connector2_1))
if Connector2_2 < 0:
Connector2_2 = Connector2_2 + (Generic_Variable * (0 - Connector2_2))
if ConnectorNd1_Out < 0:
ConnectorNd1_Out = ConnectorNd1_Out + (Generic_Variable * (0 - ConnectorNd1_Out))
if ConnectorNd2_Out < 0:
ConnectorNd2_Out = ConnectorNd2_Out + (Generic_Variable * (0 - ConnectorNd2_Out))
if Node1 < 0:
Node1 = Node1 + (Generic_Variable * (0 - Node1))
if Node2 < 0:
Node2 = Node2 + (Generic_Variable * (0 - Node2))
NumberOfQuestions += 1
PercentageCorrect = (QuestionsRight / NumberOfQuestions) * 100
elif Output < Input1 ^ Input2:
print("The Output was too small")
Generic_Variable = (Input1 ^ Input2) - Output
Generic_Variable = 1 / Generic_Variable
Generic_Variable = 1 - Generic_Variable
if Connector1_1 > 0:
Connector1_1 = Connector1_1 - (Generic_Variable * Connector1_1)
if Connector1_2 > 0:
Connector1_2 = Connector1_2 - (Generic_Variable * Connector1_2)
if Connector2_1 > 0:
Connector2_1 = Connector2_1 - (Generic_Variable * Connector2_1)
if Connector2_2 > 0:
Connector2_2 = Connector2_2 - (Generic_Variable * Connector2_2)
if ConnectorNd1_Out > 0:
ConnectorNd1_Out = ConnectorNd1_Out - (Generic_Variable * ConnectorNd1_Out)
if ConnectorNd2_Out > 0:
ConnectorNd2_Out = ConnectorNd2_Out - (Generic_Variable * ConnectorNd2_Out)
if Node1 > 0:
Node1 = Node1 - (Generic_Variable * Node1)
if Node2 > 0:
Node2 = Node2 - (Generic_Variable * Node2)
if Connector1_1 < 0:
Connector1_1 = Connector1_1 + (Generic_Variable * (0 - Connector1_1))
if Connector1_2 < 0:
Connector1_2 = Connector1_2 + (Generic_Variable * (0 - Connector1_2))
if Connector2_1 < 0:
Connector2_1 = Connector2_1 + (Generic_Variable * (0 - Connector2_1))
if Connector2_2 < 0:
Connector2_2 = Connector2_2 + (Generic_Variable * (0 - Connector2_2))
if ConnectorNd1_Out < 0:
ConnectorNd1_Out = ConnectorNd1_Out + (Generic_Variable * (0 - ConnectorNd1_Out))
if ConnectorNd2_Out < 0:
ConnectorNd2_Out = ConnectorNd2_Out + (Generic_Variable * (0 - ConnectorNd2_Out))
if Node1 < 0:
Node1 = Node1 + (Generic_Variable * (0 - Node1))
if Node2 < 0:
Node2 = Node2 + (Generic_Variable * (0 - Node2))