首页 > 解决方案 > python脚本问题-语法无效

问题描述

为了更好地理解机器学习和神经网络的工作原理,我从这里提取的以下代码不起作用。它在第 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

标签: python

解决方案


您忘记在第 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)))

推荐阅读