梯度下降在输出中返回 nan
Gradient Descent returning nan in output
我有一个包含 3 个特征和 1 个目标变量的数据。
我正在尝试使用梯度下降,然后最小化 RMSE
在尝试 运行 代码时,我将 nan 作为 cost/error 术语
试了很多方法都搞不定
谁能告诉我我的计算哪里出了问题。
这是代码:
m = len(y)
# calculate gradient
def grad(theta):
dJ = 1/m*np.sum((Xnorm.dot(theta)-ynorm.reshape(len(ynorm),1))*Xnorm,axis=0).reshape(-1,1)
return dJ
def cost(theta):
J = np.sum((Xnorm.dot(theta)-ynorm.reshape(len(ynorm),1))**2,axis=0)
return J
def GD(theta0,learning_rate = 0.0005,epochs=500,TOL=1e-1):
theta_history = [theta0]
J_history = [cost(theta0)]
print(J_history)
thetanew = theta0*10000
# print(f'epoch \t Cost(J) \t')
for epoch in range(epochs):
if epoch%100 == 0:
print('epoch', epoch, 'cost',J_history[-1])
dJ = grad(theta0)
J = cost(theta0)
thetanew = theta0 - learning_rate*dJ
theta_history.append(thetanew)
J_history.append(J)
if np.sum((thetanew - theta0)**2) < TOL:
print('Convergence achieved.')
break
theta0 = thetanew
return thetanew,theta_history,J_history
即使是第一个 theta 值,它 returns nan
theta,theta_history,J_history = GD(theta0)
我的变量的形状
我们提出的唯一合理的解决方案是因为成本高..无法将此方法用于此解决方案。我们尝试使用不同的方法,如简单线性回归,它奏效了。
我有一个包含 3 个特征和 1 个目标变量的数据。 我正在尝试使用梯度下降,然后最小化 RMSE
在尝试 运行 代码时,我将 nan 作为 cost/error 术语 试了很多方法都搞不定
谁能告诉我我的计算哪里出了问题。
这是代码:
m = len(y)
# calculate gradient
def grad(theta):
dJ = 1/m*np.sum((Xnorm.dot(theta)-ynorm.reshape(len(ynorm),1))*Xnorm,axis=0).reshape(-1,1)
return dJ
def cost(theta):
J = np.sum((Xnorm.dot(theta)-ynorm.reshape(len(ynorm),1))**2,axis=0)
return J
def GD(theta0,learning_rate = 0.0005,epochs=500,TOL=1e-1):
theta_history = [theta0]
J_history = [cost(theta0)]
print(J_history)
thetanew = theta0*10000
# print(f'epoch \t Cost(J) \t')
for epoch in range(epochs):
if epoch%100 == 0:
print('epoch', epoch, 'cost',J_history[-1])
dJ = grad(theta0)
J = cost(theta0)
thetanew = theta0 - learning_rate*dJ
theta_history.append(thetanew)
J_history.append(J)
if np.sum((thetanew - theta0)**2) < TOL:
print('Convergence achieved.')
break
theta0 = thetanew
return thetanew,theta_history,J_history
即使是第一个 theta 值,它 returns nan
theta,theta_history,J_history = GD(theta0)
我的变量的形状
我们提出的唯一合理的解决方案是因为成本高..无法将此方法用于此解决方案。我们尝试使用不同的方法,如简单线性回归,它奏效了。