在Python中实现梯度下降并遇到溢出错误

梯度下降与溢出错误

我目前正在Python中实现向量化的梯度下降。然而,我不断收到溢出错误。尽管我的数据集中的数字并不特别大。我使用的是这个公式:

向量化梯度下降公式 我选择这种实现方式是为了避免使用导数。是否有人有任何建议来解决这个问题,或者我是否实现错了?提前感谢!

数据集链接: https://www.kaggle.com/CooperUnion/anime-recommendations-database/data

## 清理数据 ##import mathimport matplotlib.pyplot as pltimport numpy as npimport pandas as pddata = pd.read_csv('anime.csv')# print(data.corr())# print(data['members'].isnull().values.any()) # 打印 False# print(data['rating'].isnull().values.any()) # 打印 Truemembers = [] # 对应行中的粉丝俱乐部大小 ratings = [] # 对应行中的评分for row in data.iterrows():    if not math.isnan(row[1]['rating']): # 检查是否为Null评分        members.append(row[1]['members'])        ratings.append(row[1]['rating'])plt.plot(members, ratings)plt.savefig('scatterplot.png')theta0 = 0.3 # 随机猜测theta1 = 0.3 # 随机猜测error = 0

公式

def hypothesis(x, theta0, theta1):    return  theta0 + theta1 * xdef costFunction(x, y, theta0, theta1, m):    loss = 0     for i in range(m): # 表示求和        loss += (hypothesis(x[i], theta0, theta1) - y[i])**2    loss *= 1 / (2 * m) # 表示1/2m    return lossdef gradientDescent(x, y, theta0, theta1, alpha, m, iterations=1500):    for i in range(iterations):        gradient0 = 0        gradient1 = 0        for j in range(m):            gradient0 += hypothesis(x[j], theta0, theta1) - y[j]            gradient1 += (hypothesis(x[j], theta0, theta1) - y[j]) * x[j]        gradient0 *= 1/m        gradient1 *= 1/m        temp0 = theta0 - alpha * gradient0        temp1 = theta1 - alpha * gradient1        theta0 = temp0        theta1 = temp1        error = costFunction(x, y, theta0, theta1, len(y))        print("错误是:", error)    return theta0, theta1print(gradientDescent(members, ratings, theta0, theta1, 0.01, len(ratings)))

错误

在几次迭代后,我的gradientDescent函数中调用的costFunction给我带来了一个溢出错误:(34, ‘结果过大’)。然而,我期望我的代码能够持续打印出不断减少的错误值。

    错误是: 1.7515692852199285e+23    错误是: 2.012089675182454e+38    错误是: 2.3113586742689143e+53    错误是: 2.6551395730578252e+68    错误是: 3.05005286756189e+83    错误是: 3.503703756035943e+98    错误是: 4.024828599077087e+113    错误是: 4.623463163528686e+128    错误是: 5.311135890211131e+143    错误是: 6.101089907410428e+158    错误是: 7.008538065634975e+173    错误是: 8.050955905074458e+188    错误是: 9.248418197694096e+203    错误是: 1.0623985545062037e+219    错误是: 1.220414847696018e+234    错误是: 1.4019337603196565e+249    错误是: 1.6104509643047377e+264    错误是: 1.8499820618048921e+279    错误是: 2.1251399172389593e+294    Traceback (most recent call last):      File "tyreeGradientDescent.py", line 54, in <module>        print(gradientDescent(members, ratings, theta0, theta1, 0.01, len(ratings)))      File "tyreeGradientDescent.py", line 50, in gradientDescent        error = costFunction(x, y, theta0, theta1, len(y))      File "tyreeGradientDescent.py", line 33, in costFunction        loss += (hypothesis(x[i], theta0, theta1) - y[i])**2    OverflowError: (34, '结果过大')

回答:

你的数据值非常大,这使得你的损失函数非常陡峭。结果是,除非你将数据标准化到较小的值,否则你需要一个非常小的alpha。如果alpha值过大,你的梯度下降会在各处跳跃,实际上会发散,这就是为什么你的错误率在上升而不是下降。

使用你当前的数据,alpha值为0.0000000001将使错误收敛。经过30次迭代,我的损失从:

错误是: 66634985.91339202

变为

错误是: 16.90452378179708

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中创建了一个多类分类项目。该项目可以对…

发表回复

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