我已经编写了一个用于创建神经网络的python脚本(和库),并且我试图实现单层感知器。我用感知器测试了我的代码,解决了以下问题:
input out (expected)
1.0 1.0 1 (threshold : 0,2; learning rate : 1)
1.0 0.0 -1
0.0 1.0 -1
0.0 0.0 -1
它有效(我得到了预期的输出)。但现在,我想使用具有两个输出单元的感知器来解决同样的问题:
1.0 1.0 1 0
1.0 0.0 0 1
0.0 1.0 0 1
0.0 0.0 0 1
使用与第一次相同的阈值/学习率,我只得到“1”;所以我试着弄乱那些,把它们设置为1 / 0.01,这次我得到了(在50个时代之后,而第一个感知器完成了9个):
1 1
0 1
0 1
0 1
这几乎是正确的,但现在我不知道该怎么办;我的脚本似乎正在工作,但我不知道如何解决它。
这是我的代码: (功能训练感知器)
def trainPerceptron(self, input_data, learning_rate):
# made for a regular perceptron, not a multilayer one
del input_data[0]
epoch = 0
while epoch < 50:
# main training loop.
hasChanged = False # witness if that epoch didn't changed anything
for index_values in range(0, len(input_data)):
# each line in the file (even if in this case, it's values inside an array)
current_data = input_data[index_values]
current_expected_out = input_data[index_values][-len(self.capas[len(self.capas) - 1].cells):]
#current_expected_out.reverse()
for index_cell in range(0, len(self.capas[0].cells) - 1):
# for each cell in the input layer (except bias ones)
# we set the activation
if "bias" not in self.capas[0].cells[index_cell].name:
self.capas[0].cells[index_cell].state = float(current_data[index_cell])
for cell in self.capas[1].cells:
# for each cell in the output layer
y_in = 0
f_y = 0
for connexion in cell.connexionsIn:
# we calculate the sum of the values in
y_in += connexion.origin.state * connexion.weight
if y_in > cell.threshold:
f_y = 1
elif cell.threshold >= y_in >= cell.threshold * -1:
f_y = 0
elif y_in <= cell.threshold * -1:
f_y = -1
cell.state = f_y # we finally set the state of each cell out
if float(cell.state) != float(current_expected_out[int(cell.name[-1])]):
# if the result isn't the good one, we update the weight of the cell's connexions (including bias)
hasChanged = True
for connexionToCorrect in cell.connexionsIn:
if "bias" not in connexionToCorrect.origin.name:
connexionToCorrect.weight += learning_rate * float(current_expected_out[int(connexionToCorrect.destination.name[-1])]) * connexionToCorrect.origin.state
else:
connexionToCorrect.weight += learning_rate * float(current_expected_out[int(connexionToCorrect.destination.name[-1])])
print("Epoch : " + str(epoch))
epoch += 1
if hasChanged is False:
print("Solution found.")
break
(使用感知器来解决问题的功能)
def exploitPerceptron(self, input_data):
final_data = [] # this will be the data outputted by the perceptron
del input_data[0] # comment this line as needed. it's needed because our data file have a useless first line
for index_values in range(0, len(input_data)):
# each line in the file (even if in this case, it's values inside an array)
current_data = input_data[index_values]
current_expected_out = input_data[index_values][-len(self.capas[len(self.capas) - 1].cells):]
current_expected_out.reverse()
for index_cell in range(0, len(self.capas[0].cells) - 1):
# for each cell in the input layer (except bias ones)
# we set the activation
if "bias" not in self.capas[0].cells[index_cell].name:
self.capas[0].cells[index_cell].state = float(current_data[index_cell])
for cell in self.capas[1].cells:
# for each cell in the output layer
y_in = 0
f_y = 0
for connexion in cell.connexionsIn:
# we calculate the sum of the values in
y_in += connexion.origin.state * connexion.weight
if y_in > cell.threshold:
f_y = 1
elif cell.threshold >= y_in >= cell.threshold * -1:
f_y = 0
elif y_in <= cell.threshold * -1:
f_y = -1
cell.state = f_y # we finally set the state of each cell out
result = ""
for cell in self.capas[len(self.capas) - 1].cells:
result += str(cell.state) + " "
print(result)
最后,设置网络的代码:
retu = Red("perceptron")
retu.initCapas([2, 2])
retu.connectOneToAll(0)
retu.addCell(0, "bias0", 1)
retu.addCell(0, "bias1", 1)
retu.createConnexion([0, 2], [1, 0], 0)
retu.createConnexion([0, 3], [1, 1], 0)
# we have 2 neurons in + bias, and two neurons out
retu.setAllThresholdTo(1)
retu.trainPerceptron(retu.processText("and.txt"), 0.01)
retu.exploitPerceptron(retu.processText("and.txt"))