- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在 python 中实现向量化梯度下降。但是,我仍然收到溢出错误。不过,我的数据集中的数字并不是很大。我正在使用这个公式:
我选择此实现是为了避免使用衍生工具。有人对如何解决这个问题有任何建议还是我实现错误?预先感谢您!
数据集链接:https://www.kaggle.com/CooperUnion/anime-recommendations-database/data
## Cleaning Data ##
import math
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = pd.read_csv('anime.csv')
# print(data.corr())
# print(data['members'].isnull().values.any()) # Prints False
# print(data['rating'].isnull().values.any()) # Prints True
members = [] # Corresponding fan club size for row
ratings = [] # Corresponding rating for row
for row in data.iterrows():
if not math.isnan(row[1]['rating']): # Checks for Null ratings
members.append(row[1]['members'])
ratings.append(row[1]['rating'])
plt.plot(members, ratings)
plt.savefig('scatterplot.png')
theta0 = 0.3 # Random guess
theta1 = 0.3 # Random guess
error = 0
def hypothesis(x, theta0, theta1):
return theta0 + theta1 * x
def costFunction(x, y, theta0, theta1, m):
loss = 0
for i in range(m): # Represents summation
loss += (hypothesis(x[i], theta0, theta1) - y[i])**2
loss *= 1 / (2 * m) # Represents 1/2m
return loss
def 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 is:", error)
return theta0, theta1
print(gradientDescent(members, ratings, theta0, theta1, 0.01, len(ratings)))
经过几次迭代后,在gradientDescent函数中调用我的costFunction给出了一个OverflowError:(34,“结果太大”)。但是,我希望我的代码能够不断打印出不断减小的错误值。
Error is: 1.7515692852199285e+23
Error is: 2.012089675182454e+38
Error is: 2.3113586742689143e+53
Error is: 2.6551395730578252e+68
Error is: 3.05005286756189e+83
Error is: 3.503703756035943e+98
Error is: 4.024828599077087e+113
Error is: 4.623463163528686e+128
Error is: 5.311135890211131e+143
Error is: 6.101089907410428e+158
Error is: 7.008538065634975e+173
Error is: 8.050955905074458e+188
Error is: 9.248418197694096e+203
Error is: 1.0623985545062037e+219
Error is: 1.220414847696018e+234
Error is: 1.4019337603196565e+249
Error is: 1.6104509643047377e+264
Error is: 1.8499820618048921e+279
Error is: 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, 'Result too large')
最佳答案
您的数据值确实非常大,这使得您的损失函数非常陡峭。结果是您需要一个微小 alpha,除非您将数据标准化为较小的值。如果 alpha 值太大,你的梯度下降会到处跳跃并且实际上发散,这就是你的错误率上升而不是下降的原因。
根据您当前的数据,alpha 值为 0.0000000001 将使误差收敛。经过 30 次迭代后,我的损失从:
错误是:66634985.91339202
至
错误是:16.90452378179708
关于python - 在 Python 中实现梯度下降并收到溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49865952/
我想循环遍历 gpx 文件并计算总上升和下降。我有一个函数可以计算两组经纬度点之间的高程差异,我已经设置了 simplexml 来读取和循环遍历 gpx 文件 trkseg 点。 问题是,这不准确(实
我有两个在不同时间段拍摄的数组。如何通过将新玩家标记为上升来检查哪些玩家在列表中上升/下降? 附言- 数组已经根据分数排序。 pastData:[ { playerName:'Jo
我想捕获 ctrl/alt/etc 键的起伏,无论表单上的哪个控件获取 keyup 或 keydown 事件。由于我的表单上有大约 100 个控件,如果我要为每个单独的控件添加代码,那将非常难看。我怎
vector1 = c(2, 2, 2, 2, 2, 2) vector2 = c(2, 2, 3, 3, 3, 3) vector3 = c(2, 2, 1, 2, 2, 2) 我想知道向量中的数字
我不知道如何遵循编译器的建议:consider using a let binding to create a longer lived value。 Playground #![allow(unus
我希望有人能帮助我理解 AngularJS 中的 $scope 遇到的一个恼人的问题。请参阅下面我的代码中的注释: app.controller('MyController', function ($
我有一个 flex 搜索集群,其中有2个节点在2核CPU 8GB ram实例上运行。每个节点都传入了参数“ES_JAVA_OPTS = -Xms3g -Xmx3g”。我有4个索引,每个索引有2个分片和
我正在学习 R(及其通过 quantmod lib 在交易任务中的应用)并定期浏览社区以从这里获得许多新知识和技巧。我对 R 的总体印象和特别是 quantmod lib 的印象 - 它很棒。 在这一
当我们点击屏幕时,我正在绘制纹理正方形。我正在使用相同的纹理。在新 ios 设备中点击几次后,FPS 从 120 下降到 4 左右。每次手指点击时,我都会将点击的点以及纹理和纹理的大小传递给着色器。
只有当对象被点击并且需要从列表中移除时它才会掉落。这是代码: if(event.type == TouchEvent.TOUCH_DOWN){ for(Bottle bottl
我有一个基于SpriteKit的小游戏。 在这个游戏中,我使用了很多带有字母(或字母组合)的节点,用户可以四处移动来构建单词。 这些节点基本上是带有 SKLabelNode 的 SKSpriteNod
我有一个简单的CSS布局 wrapper header left-sidebar / main-content / right-sidebar footer 但我的主要内容似乎下降了(float dr
在标题中,我给出了四个不同的部分,并使用 float 属性使所有内容都显示在一条水平线上。 当我调整浏览器窗口大小时,最后一个 div 位于黑色边框线下方。 如何解决。 http://jsfiddle
CSS: .desc{ text-align: center; color:#60A8D5; padding-top: 17px;
这是一段简单的代码,但我为这个问题尝试过的解决方案都没有奏效。 #ONE { float: left; border: 1
我有一个 SceneKit 设置,其中有一个 Sphere 设置为 Dynamic body。 我能够运行该应用程序并看到球体落在静态 body 地板上。 我想做的是设置场景,这样 sfere 最初就
首先,我的类(class): export class FooBar { ... isFavorite: boolean = false; constructor() { this.isF
我正在尝试删除所有端口上的所有传出 RST 和传入 RST。我正在使用 Debian Linux。我尝试了互联网上列出的所有可能的命令组合,但似乎没有任何效果。 例如,我试过: iptables -A
我正在做这样的事情: fn main() { //[1, 0, 0, 0, 99]; // return [2, 0, 0, 0, 99] //[2, 3, 0, 3, 99]; //
我正在使用 Rusqlite,它可以让你做这样的查询: statement.query_row(params!([1, 2, 3]), ...); params!()定义如下: macro_rules
我是一名优秀的程序员,十分优秀!