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 ...
随机推荐
- 設定 gpio 為 讀取用途,需注意的參數
Schematic 解說 上面的 線路圖, R1 R2 只能有一個被接上, R3 R4 只能有一個被接上, 是使用 gpio 讀取 電壓 判斷為0 或是 1 這時的 gpio 設定,其中一個參數需設為 ...
- AMD嵌入式G系列SoC协助优化Gizmo 2开发板
http://www.gizmosphere.org/ AMD嵌入式G系列SoC协助优化Gizmo 2开发板 http://news.zol.com.cn/491/4910444.html
- memcached和redis区别
Memcached:是高性能分布式内存缓存服务器,本质是一个内存 key-value 数据库,但不支持数据持久化,服务器关闭后,数据全丢失.只支持 key-value 结构. Redis:将大部分数据 ...
- Python数据处理实战
一.运行环境 1.python版本 2.7.13 博客代码均是这个版本2.系统环境:win7 64位系统 二.需求 对杂乱文本数据进行处理 部分数据截图如下,第一个字段是原字段,后面3个是清洗出的字段 ...
- Xcode7 iOS9.0 的真机调试
Xcode7的真机调试: 1.Xcode偏好 -> 账号 -> 增加 Apple ID ->显示 free 2.Target 运行 iOS 版本号 3.修正 Team 项 选择 ...
- AspxGridView在cell内显示颜色
protected void master_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if ...
- JDBC数据源连接池(1)---DBCP
何为数据源呢?也就是数据的来源.我在前面的一篇文章<JDBC原生数据库连接>中,采用了mysql数据库,数据来源于mysql,那么mysql就是一种数据源.在实际工作中,除了mysql,往 ...
- Leetcode 之Longest Valid Parentheses(39)
有一定的难度.用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置. int longestValidParentheses(const string& s) { stack<int& ...
- 【python】pymongo中正则查询时的转义问题
在查询mongo时用到了正则查询 设字符串为 str = '/ab/cd.ef?g=' 直接用正则查询没有匹配. collection.find({"re":{'$regex' ...
- opencv c++基本操作
常用操作 imread imread (char *filename, int flag=1); 第二个参数,int类型的flags,为载入标识,它指定一个加载图像的颜色类型.可以看到它自带缺省值1. ...