问题描述
我有一个具有 3 个特征和 1 个目标变量的数据。 我正在尝试使用梯度下降,然后最小化 RMSE
在尝试运行代码时,我将 nan 作为成本/错误项 试了很多方法都搞不明白。
谁能告诉我我的计算哪里出错了。
这是代码:
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 值,它也返回 nan
theta,J_history = GD(theta0)
我的变量的形状
解决方法
我们提出的唯一合理的解决方案是因为成本太高......不可能将这种方法用于该解决方案。我们尝试使用不同的方法,例如简单的线性回归,并且奏效了。