python实现简单的梯度下降法
代码如下:
# 梯度下降法模拟
import numpy as np
import matplotlib.pyplot as plt
plot_x = np.linspace(-1,6,141) # 计算损失函数对应的导数,即对y=(x-2.5)**2-1求导
def dJ(theda):
return 2*(theda-2.5)
# 计算theda对应的损失函数值
def J(theda):
try:
return (theda-2.5)**2-1
except:
return float('inf') # 梯度下降法开始
theda_history = [] # 用来记录梯度下降的过程
theda = 0.0 # 以0作为开始点
eta = 0.1 # 设置学习率
# epsilon = 1e-8 由于导数可能达不到0,
# 所以设置epsilon,表示损失函数值每次减小不足1e-8就认为已经达到最小值了 # n_itera 用来限制迭代的次数,默认为10000次
# 梯度下降函数
def gradient_descent(initial_theda,eta,n_itera=1e4,epsilon=1e-8):
theda = initial_theda
theda_history.append(initial_theda)
i_itera = 0
while i_itera<n_itera:
gradient = dJ(theda)
last_theda = theda
theda = theda - eta * gradient
theda_history.append(theda)
if(abs(J(theda)-J(last_theda))<epsilon):
break
i_itera += 1
def plot_theda_history():
plt.plot(plot_x,J(plot_x))
plt.plot(np.array(theda_history),J(np.array(theda_history)),color='r',marker='+')
plt.show() gradient_descent(theda,eta)
plot_theda_history()
效果图:

python实现简单的梯度下降法的更多相关文章
- Python实现——一元线性回归(梯度下降法)
2019/3/25 一元线性回归--梯度下降/最小二乘法_又名:一两位小数点的悲剧_ 感觉这个才是真正的重头戏,毕竟前两者都是更倾向于直接使用公式,而不是让计算机一步步去接近真相,而这个梯度下降就不一 ...
- 梯度下降法及一元线性回归的python实现
梯度下降法及一元线性回归的python实现 一.梯度下降法形象解释 设想我们处在一座山的半山腰的位置,现在我们需要找到一条最快的下山路径,请问应该怎么走?根据生活经验,我们会用一种十分贪心的策略,即在 ...
- 梯度下降法实现最简单线性回归问题python实现
梯度下降法是非常常见的优化方法,在神经网络的深度学习中更是必会方法,但是直接从深度学习去实现,会比较复杂.本文试图使用梯度下降来优化最简单的LSR线性回归问题,作为进一步学习的基础. import n ...
- 简单线性回归(梯度下降法) python实现
grad_desc .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bord ...
- 梯度下降法原理与python实现
梯度下降法(Gradient descent)是一个一阶最优化算法,通常也称为最速下降法. 要使用梯度下降法找到一个函数的局部极小值,必须向函数上当前点对应梯度(或者是近似梯度)的反方向的规定步长距离 ...
- 固定学习率梯度下降法的Python实现方案
应用场景 优化算法经常被使用在各种组合优化问题中.我们可以假定待优化的函数对象\(f(x)\)是一个黑盒,我们可以给这个黑盒输入一些参数\(x_0, x_1, ...\),然后这个黑盒会给我们返回其计 ...
- 梯度下降法实现(Python语言描述)
原文地址:传送门 import numpy as np import matplotlib.pyplot as plt %matplotlib inline plt.style.use(['ggplo ...
- 梯度下降法VS随机梯度下降法 (Python的实现)
# -*- coding: cp936 -*- import numpy as np from scipy import stats import matplotlib.pyplot as plt # ...
- 梯度下降法实现-python[转载]
转自:https://www.jianshu.com/p/c7e642877b0e 梯度下降法,思想及代码解读. import numpy as np # Size of the points dat ...
随机推荐
- "Flags mismatch irq" register interrupt handler error
Question : When you see the log "Flags mismatch irq ............", maybe you use the same ...
- ktime使用例子【原创】
#include <linux/kernel.h>#include <linux/init.h>#include <linux/module.h>#include ...
- 安装openssl-0.9.8报错out range of signed 32bit displacement .
安装openssl-0.9.8报错out range of signed 32bit displacement http://blog.csdn.net/wangtingyao1990/article ...
- JDBC数据源连接池(3)---Tomcat集成DBCP
此文续<JDBC数据源连接池(2)---C3P0>. Apache Tomcat作为一款JavaWeb服务器,内置了DBCP数据源连接池.在使用中,只要进行相应配置即可. 首先,确保Web ...
- 解决Ubuntu的错误提示
如果你是一个Ubuntu用户,也许偶尔甚至经常,遇到这样一个错误提示“System Program problem detected”. Ubuntu有一个内建的实用程序叫做Apport, 当一个程序 ...
- 循环select查询结果集
--标记id --每次查询特定列比标记id大的第一条数据, --同时更新标记id,直到查询结果为空 ) set @id='' begin @id=id from T_SGZ where id>@ ...
- 多路复用I/O模型select() 模型 代码实现
多路复用I/O: socket编程之select(),poll(),epoll() 代码: client.c #include <stdio.h> #include <sys/ty ...
- ZOJ-3314
CAPTCHA Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class ...
- 微信小程序radio组件 - 如何改变默认样式大小?
今天在写小程序的时候用到radio组件,但是很懊恼并未提供修改radio组件大小属性,第一感觉准备用css width , height 改变radio的大小,但是怎么搞也无法改变. 但是又不愿意搞个 ...
- lr中exit(-1)和return 0的区别
LR脚本实践:关于lr中exit(-1)和return 0的区别 exit(-1):从当前action里面exit(-1)所在行,当前迭代里面直接退出来,终止运行: return 0:忽略当前acti ...