Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输出中的梯度下降_Python_Numpy_Gradient Descent - Fatal编程技术网

Python 输出中的梯度下降

Python 输出中的梯度下降,python,numpy,gradient-descent,Python,Numpy,Gradient Descent,我有一个数据,有3个特征和1个目标变量。 我尝试使用梯度下降法,然后最小化RMSE 在尝试运行代码时,我得到了nan作为成本/错误术语 我试过很多方法,但都不明白 谁能告诉我我的计算哪里出错了。 代码如下: m=len(y) 我的变量的形状 我们提出的唯一合理的解决方案是因为成本很高。。无法将此方法用于此解决方案。我们尝试使用一种不同的方法,如简单线性回归,它成功了。这里的m是什么?m=len(y)…我更新了代码,假设Xnorm或ynorm中没有缺失值(nan),您的成本似乎非常高。你有没有检

我有一个数据,有3个特征和1个目标变量。 我尝试使用梯度下降法,然后最小化RMSE

在尝试运行代码时,我得到了nan作为成本/错误术语 我试过很多方法,但都不明白

谁能告诉我我的计算哪里出错了。 代码如下:
m=len(y)

我的变量的形状


我们提出的唯一合理的解决方案是因为成本很高。。无法将此方法用于此解决方案。我们尝试使用一种不同的方法,如简单线性回归,它成功了。

这里的
m
是什么?m=len(y)…我更新了代码,假设
Xnorm
ynorm
中没有缺失值(
nan
),您的成本似乎非常高。你有没有检查你的参数和梯度的大小以确保它们不会爆炸?您的参数值可能会变成np.inf,这可能会导致NAN
# 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,theta_history,J_history = GD(theta0)