NameError: 名称 ‘x’ 未定义,梯度下降函数已定义。self.function() 也无法工作

我已经定义了一个梯度下降函数,运行得很好,所有参数也都包含在内。以下是代码。

def gradient_descent(init_m,init_c,x,t,learning_rate,iterations,error_threshold):    m=init_m    c=init_c    error_values=list()    mc_values=list()    for i in range(iterations):            e=error(m,x,c,t)            if e<error_threshold:                    print('Error less than the threshold. Stopping gradient descent')                    break        error_values.append(e)        m,c=update(m,x,c,t,learning_rate)        mc_values.append((m,c))    return m,c,error_values,mc_values

但是当我移动到下一个单元格并尝试运行这个函数时,它会生成一个回溯。我甚至尝试使用 self.gradient_descent,但它也不起作用。以下是代码。

init_m=0.9init_c=0learning_rate=0.001iterations=250error_threshold=0.001m,c,error_values,mc_values= gradient_descent(init_m,init_c,x,t,learning_rate,iterations,error_threshold)

如果有人能提供一些建议,将会非常有帮助。我得到了这个输出。

NameError                                 Traceback (most recent call last)<ipython-input-18-ea467e4f9ae1> in <module>()  4 iterations=250  5 error_threshold=0.001 ----> 6 m,c,error_values,mc_values=   gradient_descent(init_m,init_c,x,t,learning_rate,iterations,error_threshold)   NameError: name 'x' is not defined

回答:

当我们在后续单元格中重新定义函数时,这会起作用。这些变量之前已经被定义了。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注