Python 函数不断返回 nan?

Python function keeps returning nan?

我目前正在通过重写我用其他语言编写的一些旧程序来学习 python。但出于某种原因,我一直 运行 陷入函数调用不断返回 nan 的问题。下面是一个代码片段。

如果我在梯度下降函数之外调用函数 theta0PartialDerivative returns 一个数字,否则 returns 一个 nan。我不确定是什么问题?

def theta0PartialDerivative():
    multBy=40.0
    total=temp=0

    for i in range(40):
        temp+=theta0
        temp+=theta1*sepalWidth[i]
        temp+=theta2*petalLength[i]
        temp+=theta3*petalWidth[i]
        temp-=sepalLength[i]
        total=total+temp
        temp=0
    return (multBy*total)

def gradientDescent():
    alpha=0.5
    global theta0,theta1,theta2,theta3
    theta0After=theta1After=theta2After=theta3After=1
    while(theta0After!=theta0 and theta1After!=theta1 and 
          theta2After!=theta2 and theta3After!=theta3):
        theta0=theta0After
        theta1=theta1After
        theta2=theta2After
        theta3=theta3After

        theta0After=theta0 - (alpha * theta0PartialDerivative())
        theta1After=theta1 - (alpha * theta1PartialDerivative())
        theta2After=theta2 - (alpha * theta2PartialDerivative())
        theta3After=theta3 - (alpha * theta3PartialDerivative())

theta0=theta1=theta2=theta3=accuracy=0
gradientDescent()

完整文件在这里:https://github.com/MohamedMoustafaNUIG/Linear-Regression-Gradient-Descent-First-Project/blob/master/Linear-Regression-I.py

编辑:真的吗?没人知道是什么问题吗?

找到问题了。步长变量 Alpha 太大(对于我正在处理的数据集)并导致偏导数发散而不是收敛。我将 alpha 从 0.5 更改为 0.13 并且有效