运行机器学习代码时出现错误。
我刚刚开始探索神经网络和机器学习,但我不知道为什么会发生这种情况或其含义。
for iteration in range(20000):
input_layer = training_inputs
outputs = sigmoid(np.dot(input_layer, synaptic_weights))
error = training_inputs - outputs
adjustment = error * sigmoid_derivative(outputs)
synaptic_weights += np.dot(input_layer.T, adjustment)#error occurs here
*编辑: 这是完整的代码
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
training_inputs = np.array([[0,0,1],
[1,1,1],
[1,0,1],
[0,1,1]])
def sigmoid_derivative(x):
return x * (1-x)
training_outputs = np.array([[0,1,1,0]]).T
np.random.seed(1)
synaptic_weights = 2 * np.random.random((3, 1)) - 1
print ('random starting syanptic weights: ')
print (synaptic_weights)
for iteration in range(1):
input_layer = training_inputs
outputs = sigmoid(np.dot(input_layer, synaptic_weights))
error = training_inputs - outputs
adjustment = error * sigmoid_derivative(outputs)
synaptic_weights += np.dot(input_layer.T, adjustment)
print(' synaptic weights after training: ')
print (synaptic_weights)
print ('outputs after training: ')
print (outputs)
答案 0 :(得分:0)
替换
synaptic_weights += np.dot(input_layer.T, adjustment)
使用
synaptic_weights = synaptic_weights + np.dot(input_layer.T, adjustment)