• 技术文章 >Python技术 >Python基础教程

    Python中如何用numpy解决梯度下降最小值

    2021-03-31 16:16:02原创3549

    numpy解决梯度下降.png

    有的小伙伴不会解决numpy梯度下降最小值的问题,今天小编就来带大家一起看看吧。

    问题描述:求解y1 = xx -2 x +3 + 0.01*(-1到1的随机值) 与 y2 = 0 的最小距离点(x,y)

    给定x范围(0,3)

    不使用学习框架,手动编写梯度下降公式求解,提示:x = x - alp*(y1-y2)导数(alp为学习率)


    函数图像为:



    代码内容:


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">import numpy as np

    import matplotlib.pyplot as plt

     

     

    def get_loss(x):

        c,r = x.shape

        loss = (x**2 - 2*x + 3) + (0.01*(2*np.random.rand(c,r)-1))

        return(loss)

     

    x = np.arange(0,3,0.01).reshape(-1,1)

     

     

    """plt.title("loss")

    plt.plot(get_loss(np.array(x)))

    plt.show()"""

     

     

    def get_grad(x):

        grad = 2 * x -2

        return(grad)

     

    np.random.seed(31415)

    x_ = np.random.rand(1)*3

    x_s = []

    alp = 0.001

    print("X0",x_)

    for e in range(2000):

     

        x_ = x_ - alp*(get_grad(x_))

        x_s.append(x_)

        if(e%100 == 0):

            print(e,"steps,x_ = ",x_)

     

    plt.title("loss")

    plt.plot(get_loss(np.array(x_s)))

    plt.show()<br></span></p>


    运行结果:


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">X0 [1.93745582]

    0 steps,x_ =  [1.93558091]

    100 steps,x_ =  [1.76583547]

    200 steps,x_ =  [1.6268875]

    300 steps,x_ =  [1.51314929]

    400 steps,x_ =  [1.42004698]

    500 steps,x_ =  [1.34383651]

    600 steps,x_ =  [1.28145316]

    700 steps,x_ =  [1.23038821]

    800 steps,x_ =  [1.18858814]

    900 steps,x_ =  [1.15437199]

    1000 steps,x_ =  [1.12636379]

    1100 steps,x_ =  [1.1034372]

    1200 steps,x_ =  [1.08467026]

    1300 steps,x_ =  [1.06930826]

    1400 steps,x_ =  [1.05673344]

    1500 steps,x_ =  [1.04644011]

    1600 steps,x_ =  [1.03801434]

    1700 steps,x_ =  [1.03111727]

    1800 steps,x_ =  [1.02547157]

    1900 steps,x_ =  [1.02085018]<br></span></p>

    图片


    今天的numpy解决梯度下降最小值的讲解到这里就结束了。更多Python学习推荐:Python学习网教学中心(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:numpy解决梯度下降;python
    上一篇:Python基础:numpy中vstack和hstack函数 下一篇:Python中numpy求函数的导数实现方法

    相关文章推荐

    • 详解Python中numpy.loadtxt()读取txt文件• Python中numpy如何生成mask图像• Python中如何使用numpy.getmask()函数• Python中numpy数组如何添加和删除元素• Python中numpy怎样按行或列提取矩阵• Python基础:numpy中空值怎样设置• Python实用之numpy中空数组的创建• Python之numpy中mask选取子集• Python基础:numpy中vstack和hstack函数

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网