简体   繁体   English

如何修复返回 NAN 的函数?

[英]How do I fix a function that returns NAN?

I wanted to try to implement gradient descent by myself and I wrote this:我想尝试自己实现梯度下降,我写了这个:

# Creating random sample dataset
import random as rnd 
dataset = []
for i in range(0, 500):
    d_dataset = [i, rnd.randint((i-4), (i+4))]
    dataset.append(d_dataset)

def gradient_descent(t0, t1, lrate, ds):
    length = len(ds)
    c0, c1 = 0, 0
    for element in ds:
        elx = element[0]
        ely = element[1]
        c0 += ((t0 + (t1*elx) - ely)) 
        c1 += ((t0 + (t1*elx) - ely)*elx) 
    t0 -= (lrate * c0 / length)
    t1 -= (lrate * c1 / length)
    return t0, t1

def train(t0, t1, lrate, trainlimit, trainingset):
    k = 0
    while k < trainlimit:
        new_t0, new_t1 = gradient_descent(t0, t1, lrate, trainingset)
        t0, t1 = new_t0, new_t1
        k += 1
    return t0, t1

print(gradient_descent(20, 1, 1, dataset))
print(train(0, 0, 1, 10000, dataset))

Whenever I run this, I get a somewhat normal output from the gradient_descent() but I get (nan, nan) from the train() function.每当我运行它时,我都会从gradient_descent()得到一个有点正常的输出,但我从train()函数得到(nan, nan) I tried running train with the input (0, 0, 1, 10, dataset) and I get this value (-4.705770241957691e+46, -1.5670167612541557e+49) , which seems very wrong.我尝试使用输入(0, 0, 1, 10, dataset)运行train并得到这个值(-4.705770241957691e+46, -1.5670167612541557e+49) ,这似乎非常错误。

Please tell me what I'm doing wrong and how to fix this error.请告诉我我做错了什么以及如何解决这个错误。 Sorry if this has been asked before but I couldn't find any answers on how to fix nan error.抱歉,如果之前有人问过这个问题,但我找不到关于如何修复 nan 错误的任何答案。

When calling print(train(0, 0, 1, 10000, dataset)) , the values returned by gradient_descent(t0, t1, lrate, trainingset) are increasing in every iteration of the while -loop.当调用print(train(0, 0, 1, 10000, dataset)) ,由gradient_descent(t0, t1, lrate, trainingset)返回的值在while循环的每次迭代中都在增加。 When they become larger than the maximum value allowed for float , they will automatically be converted to float('inf') , a float representing infinity.当它们变得大于float允许的最大值时,它们将自动转换为float('inf') ,一个表示无穷大的float Check this maximum value on your system with sys.float_info.max :使用sys.float_info.max检查系统上的sys.float_info.max

import sys
print(sys.float_info.max)

However, your function gradient_descent() can't handle infinite values, which you can verify with the following call to your function:但是,您的函数gradient_descent()无法处理无限值,您可以通过以下对您的函数的调用来验证:

gradient_descent(float('inf'), float('inf'), 1, dataset)

The problem here are the following two lines in gradient_descent() , which are not well defined for t0 and t1 being infinite:这里的问题是gradient_descent()中的以下两行,它们没有很好地定义t0t1是无限的:

c0 += ((t0 + (t1*elx) - ely)) 
c1 += ((t0 + (t1*elx) - ely)*elx) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM