python - 在局部最小值处停止梯度下降的条件
问题描述
我有找到最小成本函数的代码,但是当我添加阈值以在局部最小值处停止梯度下降时,我得到了错误,有人可以帮忙。
cannot convert the series to <class 'float'>
Input
Math CS
92 98
56 68
88 81
70 80
代码:-
def gradient_descent(x,y):
iterations=50
n=len(x)
exponent = -9
m_curr=b_curr=0
learning_rate=0.01
for i in range(iterations):
y_pred=m_curr*x+b_curr
cost=(1/n)*sum([val**2 for val in (y-y_pred)])
md=-(2/n)*sum(x*(y-y_pred))
bd=-(2/n)*sum(y-y_pred)
m_curr=m_curr-learning_rate*md
b_curr=b_curr-learning_rate*bd
math.isclose(x,y,rel_tol=math.exp(exponent),abs_tol=0.0)
print("m {},b {},cost {},iteration {},learning_rate {}".format(m_curr,b_curr,cost,i,learning_rate)) x=df['math'] y=df['cs'] gradient_descent(x,y)
解决方案
推荐阅读
- kubernetes - 无法从 Istio Mesh 连接到外部 Kafka 服务
- python - 子集列和重新排序
- r - ggplot2中的次轴刻度
- react-native - React Navigation v5,向我的应用程序标题添加堆栈导航
- html - 移动键盘后表单跳起来
- javascript - html dom准备好后如何在“新创建的链接点击”上调用ajax
- django - 部署到 Heroku 后更改文件路径以访问 views.py 中的静态文件
- php - PHP - 尝试使用 proc_open 执行 git push 后读取流时出现无限循环
- c# - 在 ASP.NET Core MVC 控制器中使用 DeleteConfirmed 类捕获 SqlException 547 时出现问题
- java - 尝试运行 Java 时,Java 运行时环境检测到致命错误