• 作业11:优化算法比较


    优化算法

    1. 编程实现图6-1,并观察特征

    import numpy as np
    from matplotlib import pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
     
     
    # https://blog.csdn.net/weixin_39228381/article/details/108511882
     
    def func(x, y):
        return x * x / 20 + y * y
     
     
    def paint_loss_func():
        x = np.linspace(-50, 50, 100)  # x的绘制范围是-50到50,从改区间均匀取100个数
        y = np.linspace(-50, 50, 100)  # y的绘制范围是-50到50,从改区间均匀取100个数
     
        X, Y = np.meshgrid(x, y)
        Z = func(X, Y)
     
        fig = plt.figure()  # figsize=(10, 10))
        ax = Axes3D(fig)
        plt.xlabel('x')
        plt.ylabel('y')
     
        ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
        plt.show()
     
     
    paint_loss_func()
    
    • 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

    运行结果:
    运行结果
    特征:有全局最小值、是一个向x轴方向延伸的“碗”状函数

    2. 观察梯度方向

    梯度方向
    特征:Y轴方向梯度大,X轴方向梯度小;很多位置的梯度并没有指向最小位置(0,0)

    3. 编写代码实现算法,并可视化轨迹

    # coding: utf-8
    import numpy as np
    import matplotlib.pyplot as plt
    from collections import OrderedDict
     
     
    class SGD:
        """随机梯度下降法(Stochastic Gradient Descent)"""
     
        def __init__(self, lr=0.01):
            self.lr = lr
     
        def update(self, params, grads):
            for key in params.keys():
                params[key] -= self.lr * grads[key]
     
     
    class Momentum:
        """Momentum SGD"""
     
        def __init__(self, lr=0.01, momentum=0.9):
            self.lr = lr
            self.momentum = momentum
            self.v = None
     
        def update(self, params, grads):
            if self.v is None:
                self.v = {}
                for key, val in params.items():
                    self.v[key] = np.zeros_like(val)
     
            for key in params.keys():
                self.v[key] = self.momentum * self.v[key] - self.lr * grads[key]
                params[key] += self.v[key]
     
     
    class Nesterov:
        """Nesterov's Accelerated Gradient (http://arxiv.org/abs/1212.0901)"""
     
        def __init__(self, lr=0.01, momentum=0.9):
            self.lr = lr
            self.momentum = momentum
            self.v = None
     
        def update(self, params, grads):
            if self.v is None:
                self.v = {}
                for key, val in params.items():
                    self.v[key] = np.zeros_like(val)
     
            for key in params.keys():
                self.v[key] *= self.momentum
                self.v[key] -= self.lr * grads[key]
                params[key] += self.momentum * self.momentum * self.v[key]
                params[key] -= (1 + self.momentum) * self.lr * grads[key]
     
     
    class AdaGrad:
        """AdaGrad"""
     
        def __init__(self, lr=0.01):
            self.lr = lr
            self.h = None
     
        def update(self, params, grads):
            if self.h is None:
                self.h = {}
                for key, val in params.items():
                    self.h[key] = np.zeros_like(val)
     
            for key in params.keys():
                self.h[key] += grads[key] * grads[key]
                params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + 1e-7)
     
     
    class RMSprop:
        """RMSprop"""
     
        def __init__(self, lr=0.01, decay_rate=0.99):
            self.lr = lr
            self.decay_rate = decay_rate
            self.h = None
     
        def update(self, params, grads):
            if self.h is None:
                self.h = {}
                for key, val in params.items():
                    self.h[key] = np.zeros_like(val)
     
            for key in params.keys():
                self.h[key] *= self.decay_rate
                self.h[key] += (1 - self.decay_rate) * grads[key] * grads[key]
                params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + 1e-7)
     
     
    class Adam:
        """Adam (http://arxiv.org/abs/1412.6980v8)"""
     
        def __init__(self, lr=0.001, beta1=0.9, beta2=0.999):
            self.lr = lr
            self.beta1 = beta1
            self.beta2 = beta2
            self.iter = 0
            self.m = None
            self.v = None
     
        def update(self, params, grads):
            if self.m is None:
                self.m, self.v = {}, {}
                for key, val in params.items():
                    self.m[key] = np.zeros_like(val)
                    self.v[key] = np.zeros_like(val)
     
            self.iter += 1
            lr_t = self.lr * np.sqrt(1.0 - self.beta2 ** self.iter) / (1.0 - self.beta1 ** self.iter)
     
            for key in params.keys():
                self.m[key] += (1 - self.beta1) * (grads[key] - self.m[key])
                self.v[key] += (1 - self.beta2) * (grads[key] ** 2 - self.v[key])
     
                params[key] -= lr_t * self.m[key] / (np.sqrt(self.v[key]) + 1e-7)
     
     
    def f(x, y):
        return x ** 2 / 20.0 + y ** 2
     
     
    def df(x, y):
        return x / 10.0, 2.0 * y
     
     
    init_pos = (-7.0, 2.0)
    params = {}
    params['x'], params['y'] = init_pos[0], init_pos[1]
    grads = {}
    grads['x'], grads['y'] = 0, 0
     
    optimizers = OrderedDict()
    optimizers["SGD"] = SGD(lr=0.95)
    optimizers["Momentum"] = Momentum(lr=0.1)
    optimizers["AdaGrad"] = AdaGrad(lr=1.5)
    optimizers["Adam"] = Adam(lr=0.3)
     
    idx = 1
     
    for key in optimizers:
        optimizer = optimizers[key]
        x_history = []
        y_history = []
        params['x'], params['y'] = init_pos[0], init_pos[1]
     
        for i in range(30):
            x_history.append(params['x'])
            y_history.append(params['y'])
     
            grads['x'], grads['y'] = df(params['x'], params['y'])
            optimizer.update(params, grads)
     
        x = np.arange(-10, 10, 0.01)
        y = np.arange(-5, 5, 0.01)
     
        X, Y = np.meshgrid(x, y)
        Z = f(X, Y)
        # for simple contour line
        mask = Z > 7
        Z[mask] = 0
     
        # plot
        plt.subplot(2, 2, idx)
        idx += 1
        plt.plot(x_history, y_history, 'o-', color="red")
        plt.contour(X, Y, Z)  # 绘制等高线
        plt.ylim(-10, 10)
        plt.xlim(-10, 10)
        plt.plot(0, 0, '+')
        plt.title(key)
        plt.xlabel("x")
        plt.ylabel("y")
     
    plt.subplots_adjust(wspace=0, hspace=0)  # 调整子图间距
    plt.show()
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181

    000
    收敛效果排序依次为AdaGrad、Adam、Momentum、SGD。

    4. 分析上图,说明原理(选做)

    1) 为什么SGD会走“之字形”?其它算法为什么会比较平滑?

    是因为图像的变化并不均匀,所以y方向变化很大时,x方向变化很小,只能迂回往复地寻找,效率很低。
    图像

    2)Momentum、AdaGrad对SGD的改进体现在哪里?速度?方向?在图上有哪些体现?

    SGD
    SGD是深度学习中最常见的优化方法之一,虽然是最常使用的优化方法,但是却有不少常见的问题。

    learning rate不易确定,如果选择过小的话,收敛速度会很慢,如果太大,loss function就会在极小值处不停的震荡甚至偏离。每个参数的learning rate都是相同的,如果数据是稀疏的,则希望出现频率低的特征进行大一点的更新。深度神经网络之所以比较难训练,并不是因为容易进入局部最小,而是因为学习过程容易进入马鞍面中,在这种区域中,所有方向的梯度值几乎都是0。

    Momentum(动量)

    Momentum借助了物理中的动量的概念,即前几次的梯度也会参与计算。为了表示动量,引入一个新的变量V,V是之前的梯度的累加,但是在每个回合都会有一定的衰减。它的特点是当前后梯度方向不一致时,能够加速学习,前后梯度方向一致时,能够抑制震荡。
    动量
    Adagrad

    在上述的优化算法中,参数的步长都是相的,那么能否为不同的常数设置不同的步长呢,对于梯度大的参数设置小的步长,对于梯度小的参数,设置大的步长。类比于在缓坡上面,我们可以大步长的前进,在陡坡上面,这需要小步长的前进。adagrad则是参考了这个思路。
    Adagrad

  • 相关阅读:
    PC应用管理工具 连接流程图 支持所有android手机或设备 批量设备批量应用安装卸载等管理 OS升级 push文件夹等
    “ActiveBodyMuscles“ app Tech Support(URL)
    Creo 9.0 基准特征:基准平面
    spring cloud微服务搭建配置中心之携程开源框架Apollo
    Error: Google Play requires that apps target API level 26 or higher. 两种解决办法
    Go函数介绍与一等公民
    前端面试宝典React篇02 为什么 React 要用 JSX?
    二叉树 | 指针pre | 最值、众数、累加树 | leecode刷题笔记
    23、Flink TaskManager 内存调优
    低代码测试自动化
  • 原文地址:https://blog.csdn.net/weixin_51395608/article/details/128157969