python - 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
解决方案
您忘记在第 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)))
推荐阅读
- c++ - 为什么我没有得到这些多边形的任何交集?
- python - 使用 python 和 pandas 将多数组 json 数据转换为扁平数据框
- python - 机器学习 Logistic 回归 L2 正则化
- gnome - Gnome shell 扩展登录回调
- java - 在 C++ 中无法使用 QAndroidJniObject JNI 从 java 上下文中检索 jlong 的值。异常 java.lang.NoSuchMethodError
- php - DOMDocument 移动
外面的标签
- svelte-3 - 在输入时停止页面滚动
- c++ - 我可以让 portaudio 以 root 身份工作(Raspberry pi 零)吗?
- bash - 如何强制为 bash 脚本设置间隔选项
- javascript - 获取反应组件中的最新值