我正在尝试用python实现梯度下降方法。我希望在abs(J-J_new)
达到某个公差水平(即收敛)时停止计算,其中J是成本函数。经过一定次数的迭代后,计算也将停止。我尝试过几种不同的实现,在我所有的尝试中,成本函数实际上是不同的(即|J-J_new| -> inf
)。这对我来说没有什么意义,我也无法确定为什么它会从我的代码中这样做。我正在用4个无关紧要的数据点测试实现。我现在已经把它注释掉了,但是x
和y
最终将从包含400多个数据点的文本文件中读取。下面是我能想到的最简单的实现:
# import necessary packages
import numpy as np
import matplotlib.pyplot as plt
'''
For right now, I will hard code all parameters. After all code is written and I know that I implemented the
algorithm correctly, I will consense the code into a single function.
'''
# Trivial data set to test
x = np.array([1, 3, 6, 8])
y = np.array([3, 5, 6, 5])
# Define parameter values
alpha = 0.1
tol = 1e-06
m = y.size
imax = 100000
# Define initial values
theta_0 = np.array([0.0]) # theta_0 guess
theta_1 = np.array([0.0]) # theta_1 guess
J = sum([(theta_0 - theta_1 * x[i] - y[i])**2 for i in range(m)])
# Begin gradient descent algorithm
converged = False
inum = 0
while not converged:
grad_0 = (1/m) * sum([(theta_0 + theta_1 * x[i] - y[i]) for i in range(m)])
grad_1 = (1/m) * sum([(theta_0 + theta_1 * x[i] - y[i]) * x[i] for i in range(m)])
temp_0 = theta_0 - alpha * grad_0
temp_1 = theta_1 - alpha * grad_1
theta_0 = temp_0
theta_1 = temp_1
J_new = sum([(theta_0 + theta_1 * x[i] - y[i])**2 for i in range(m)])
if abs(J - J_new) <= tol:
print('Converged at iteration', inum)
converged = True
J = J_new
inum = inum + 1
if inum == imax:
print('Maximum number of iterations reached!')
converged = True
我用它做了更多的实验。之所以出现这种分歧,是因为学习率太高。当我改变检查收敛性的方式时,这也很有帮助。我没有使用
abs(J - J_new)
来检查收敛性,而是使用abs(theta0_new - theta_0)
和abs(theta1_new - theta_1)
。如果这两者都在某个公差范围内,则它已收敛。我还重新缩放(标准化)了数据,这似乎也有帮助。代码如下:我只检查了文本文件中的数据集
相关问题 更多 >
编程相关推荐