defbackprop(self): # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1 d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output))) d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))
# update the weights with the derivative (slope) of the loss function self.weights1 += d_weights1 self.weights2 += d_weights2
x = np.array([[0,0,1], [0,1,1], [1,0,1], [1,1,1]]) # data y = np.array([[0],[1],[1],[0]]) # label nn = NeuralNetwork(x,y) for i inrange(1000): # 迭代1000次 nn.feedforward() nn.backprop() print(nn.output)