|
17、如何调试梯度下降的梯度?
这是一种比较笨但是通用的方法来求损失函数在某个点的梯度
跟具体的损失函数形式无关
可以作为一种补充验证
看我们通过数学公式推导出来的梯度是否正确?
不过这种方法的时间复杂度就非常高了 适合于部分验证
- def dJ_debug(theta, X_b, y, epsilon=0.01):
- result = np.empty(shape=len(theta))
- for i in range(len(theta)):
- theta_add = theta.copy()
- theta_minus = theta.copy()
- theta_add[i] = theta_add[i] + epsilon
- theta_minus[i] = theta_minus[i] - epsilon
- result[i] = (J(theta_add, X_b, y) - J(theta_minus, X_b, y)) / (2 * epsilon)
- return result
复制代码
ipynb文件在附件,可提供下载!
视频教程请参考:http://www.ai111.vip/thread-349-1-1.html
东方老师微信:dfy_88888
|
|