代码如下:

# 梯度下降法模拟
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实现简单的梯度下降法的更多相关文章

  1. Python实现——一元线性回归(梯度下降法)

    2019/3/25 一元线性回归--梯度下降/最小二乘法_又名:一两位小数点的悲剧_ 感觉这个才是真正的重头戏,毕竟前两者都是更倾向于直接使用公式,而不是让计算机一步步去接近真相,而这个梯度下降就不一 ...

  2. 梯度下降法及一元线性回归的python实现

    梯度下降法及一元线性回归的python实现 一.梯度下降法形象解释 设想我们处在一座山的半山腰的位置,现在我们需要找到一条最快的下山路径,请问应该怎么走?根据生活经验,我们会用一种十分贪心的策略,即在 ...

  3. 梯度下降法实现最简单线性回归问题python实现

    梯度下降法是非常常见的优化方法,在神经网络的深度学习中更是必会方法,但是直接从深度学习去实现,会比较复杂.本文试图使用梯度下降来优化最简单的LSR线性回归问题,作为进一步学习的基础. import n ...

  4. 简单线性回归(梯度下降法) python实现

    grad_desc .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bord ...

  5. 梯度下降法原理与python实现

    梯度下降法(Gradient descent)是一个一阶最优化算法,通常也称为最速下降法. 要使用梯度下降法找到一个函数的局部极小值,必须向函数上当前点对应梯度(或者是近似梯度)的反方向的规定步长距离 ...

  6. 固定学习率梯度下降法的Python实现方案

    应用场景 优化算法经常被使用在各种组合优化问题中.我们可以假定待优化的函数对象\(f(x)\)是一个黑盒,我们可以给这个黑盒输入一些参数\(x_0, x_1, ...\),然后这个黑盒会给我们返回其计 ...

  7. 梯度下降法实现(Python语言描述)

    原文地址:传送门 import numpy as np import matplotlib.pyplot as plt %matplotlib inline plt.style.use(['ggplo ...

  8. 梯度下降法VS随机梯度下降法 (Python的实现)

    # -*- coding: cp936 -*- import numpy as np from scipy import stats import matplotlib.pyplot as plt # ...

  9. 梯度下降法实现-python[转载]

    转自:https://www.jianshu.com/p/c7e642877b0e 梯度下降法,思想及代码解读. import numpy as np # Size of the points dat ...

随机推荐

  1. Java面试基础知识1

    1.动态绑定是指在执行期间判断所引用对象的实际类型,根据其实际的类型调用其相应的方法. 2.在将超类转换为子类之前,应该使用instanceof进行检查. 3.包含一个或者多个抽象方法的类本身必须被声 ...

  2. WAMP Apache 2.5 配置虚拟主机

    1.在 Apache 的安装目录下 conf/httpd.conf 文件中搜索 hosts,去掉 Include 前面的 “#” 号后,即可启用虚拟主机. # Virtual hosts #Inclu ...

  3. python实战===百度文字识别sdk

    http://ai.baidu.com/docs#/OCR-Python-SDK/top

  4. 2.ubuntu的使用

    1. CTRL+ALT+T 可以将命令模式打开 2. 有可能没办法进行yum ,它会告诉你操作的方法 3. 有些操作需要获得root的权限才可以,我们得进入root状态 --> sudo pas ...

  5. Linux磁盘的性能详细测试办法

    # 测试写入20Gb文件sync && time -p bash -c "(dd if=/dev/zero of=test.dd  bs=1000K count=20000; ...

  6. AngularJS之页面跳转Route

    1.除了引用AngularJs.js外,还要引用路由JS, "~/Scripts/angularjs/angular-route.js" 2.通过$routeProvider定义路 ...

  7. “无法在web服务器上启动调试,不是Debugger User组成员..."

    在使用VS.net2003开发asp.net项目时,有时候在你调试项目时,会提示”无法在web服务器上启动调试,不是Debugger User组成员..."这样一个错误信息.很是让人头疼,一 ...

  8. AC日记——NOI2016区间 bzoj 4653

    4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...

  9. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  10. Laravel5中的Session

    有关Session的配置文件是aonfig/session.PHP文件. 如果不使用基于数据库.cookie或者Redis缓存类的Session的话,不需要改配置文件就可以使用了. 下面一个简单的使用 ...