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 ...
随机推荐
- Python学习笔记 - day13 - 进程与线程
概述 我们都知道windows是支持多任务的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行多个任务.打个比方,你一边在用浏览器上网,一边在听MP3,一边在用Word赶作业,这就是多 ...
- java===字符串常用API介绍(转)
本文转自:http://blog.csdn.net/crazy_kid_hnf/article/details/55102861 字符串基本操作 1.substring(from,end)(含头不含尾 ...
- python实战===百度文字识别sdk
http://ai.baidu.com/docs#/OCR-Python-SDK/top
- sublime Text3 === 无法输入input的问题解决办法
sublimetext无法对input或者raw_input执行.因此搜了很多方法后,解决了这个问题: 1.先下载插件sublimerepl ,如果无法下载,请点击https://github.com ...
- 【快速玩转Source Filmmaker】用黑科技做出自己的OC和想要的模型
[快速玩转Source Filmmaker]用黑科技做出自己的OC和想要的模型https://tieba.baidu.com/p/4154097168
- JavaScript中对象的属性类型
JavaScript中,对象的属性有两种:数据属性和访问器属性. 数据属性 特性: 数据属性包括一个数据值的位置.在这个位置可以读取和写入值.数据属性有4个特性. [[configurable]]:可 ...
- django “如何”系列2:如何编写django-admin 命令
应用可以使用manage.py注册自己的动作,例如,你可能想要为你即将发布的应用添加一个manage.py 操作.这节我们将为polls应用添加一个closepoll的命令 添加一个managemen ...
- hdu 1026(优先队列+路径输出)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 从徐飞的文章《Web应用的组件化开发(一)中窥视web应用开发的历史
Web应用的组件化开发(一) 原文来自 徐飞 基本思路 1. 为什么要做组件化? 无论前端也好,后端也好,都是整个软件体系的一部分.软件产品也是产品,它的研发过程也必然是有其目的.绝大多数软件产品是追 ...
- 【hdoj_1085】Holding Bin-Laden Captive![母函数]
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1085 可以这样理解题意:给出1元,2元和5元的三种硬币若干,每种硬币数量给出,现在可以从所有的硬币中,选出 ...