python脚本问题-语法无效

时间:2019-03-20 14:39:20

标签: python

为了更好地了解机器学习和神经网络的工作原理,我从here提取的以下代码不起作用。它在第31行不断产生“无效语法”错误:

self.weights1 += d_weights1

以下是失败的功能:

    def backprop(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

1 个答案:

答案 0 :(得分:1)

您忘记在第4行的末尾加上括号了

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))

这是更正:

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)))