• 【ML-SVM案例学习】002梯度下降之求解最优解



    前言

    ML-SVM案例】会有十种SVM案例,供大家用来学习。本文只是实现梯度下降,求解最优解。后面一章将会实现003梯度下降:拉格乘子法


    提示:以下是本篇文章正文内容,下面案例可供参考

    一、代码程序

    1.引入库

    代码如下(示例):

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import math
    from mpl_toolkits.mplot3d import Axes3D
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.一维原始图像与导函数

    代码如下(示例):

    # 解决中文显示问题
    mpl.rcParams['font.sans-serif'] = [u'SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    
    """一维原始图像"""
    def f1(x):
        return 0.5 * (x - 0.25) ** 2
    # 导函数
    def h1(x):
        return 0.5 * 2 * (x - 0.25)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.使用梯度下降法求解

    GD_X = []
    GD_Y = []
    x = 4
    alpha = 0.5
    f_change = f1(x)
    f_current = f_change
    GD_X.append(x)
    GD_Y.append(f_current)
    iter_num = 0
    while f_change > 1e-10 and iter_num < 100:
        iter_num += 1
        x = x - alpha * h1(x)
        tmp = f1(x)
        f_change = np.abs(f_current - tmp)
        f_current  = tmp
        GD_X.append(x)
        GD_Y.append(f_current)
    print(u"最终结果为:(%.5f, %.5f)" % (x, f_current))
    print(u"迭代过程中X的取值,迭代次数:%d" % iter_num)
    print(GD_X)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4.构建数据与画图

    X = np.arange(-4, 4.5, 0.05)
    Y = np.array(list(map(lambda t: f1(t), X)))
    
    plt.figure(facecolor='w')
    plt.plot(X, Y, 'r-', linewidth=2)
    plt.plot(GD_X, GD_Y, 'ko--', linewidth=2)
    plt.title(u'函数$y=0.5 * (θ - 0.25)^2$; \n学习率:%.3f; 最终解:(%.3f, %.3f);迭代次数:%d' % (alpha, x, f_current, iter_num))
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5.二维原始图像与导函数

    """二维原始图像"""
    def f2(x, y):
        return 0.6 * (x + y) ** 2 - x * y
    # 导函数
    def hx2(x, y):
        return 0.6 * 2 * (x + y) - y
    def hy2(x, y):
        return 0.6 * 2 * (x + y) - x
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    6. 使用梯度下降法求解

    GD_X1 = []
    GD_X2 = []
    GD_Y = []
    x1 = 4
    x2 = 4
    alpha = 0.5
    f_change = f2(x1, x2)
    f_current = f_change
    GD_X1.append(x1)
    GD_X2.append(x2)
    GD_Y.append(f_current)
    iter_num = 0
    while f_change > 1e-10 and iter_num < 100:
        iter_num += 1
        prex1 = x1
        prex2 = x2
        x1 = x1 - alpha * hx2(prex1, prex2)
        x2 = x2 - alpha * hy2(prex1, prex2)
        
        tmp = f2(x1, x2)
        f_change = np.abs(f_current - tmp)
        f_current  = tmp
        GD_X1.append(x1)
        GD_X2.append(x2)
        GD_Y.append(f_current)
    print(u"最终结果为:(%.5f, %.5f, %.5f)" % (x1, x2, f_current))
    print(u"迭代过程中X的取值,迭代次数:%d" % iter_num)
    print(GD_X1)
    
    • 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

    7.构建数据与绘图

    # 构建数据
    X1 = np.arange(-4, 4.5, 0.2)
    X2 = np.arange(-4, 4.5, 0.2)
    X1, X2 = np.meshgrid(X1, X2)
    Y = np.array(list(map(lambda t: f2(t[0], t[1]), zip(X1.flatten(), X2.flatten()))))
    Y.shape = X1.shape
    
    
    # 画图
    fig = plt.figure(facecolor='w')
    ax = Axes3D(fig)
    ax.plot_surface(X1, X2, Y, rstride=1, cstride=1, cmap=plt.cm.jet)
    ax.plot(GD_X1, GD_X2, GD_Y, 'ko--')
    
    ax.set_title(u'函数$y=0.6 * (θ1 + θ2)^2 - θ1 * θ2$;\n学习率:%.3f; 最终解:(%.3f, %.3f, %.3f);迭代次数:%d' % (alpha, x1, x2, f_current, iter_num))
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二、完整代码

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import math
    from mpl_toolkits.mplot3d import Axes3D
    
    # 解决中文显示问题
    mpl.rcParams['font.sans-serif'] = [u'SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    
    """一维原始图像"""
    def f1(x):
        return 0.5 * (x - 0.25) ** 2
    # 导函数
    def h1(x):
        return 0.5 * 2 * (x - 0.25)
    
    # 使用梯度下降法求解
    GD_X = []
    GD_Y = []
    x = 4
    alpha = 0.5
    f_change = f1(x)
    f_current = f_change
    GD_X.append(x)
    GD_Y.append(f_current)
    iter_num = 0
    while f_change > 1e-10 and iter_num < 100:
        iter_num += 1
        x = x - alpha * h1(x)
        tmp = f1(x)
        f_change = np.abs(f_current - tmp)
        f_current  = tmp
        GD_X.append(x)
        GD_Y.append(f_current)
    print(u"最终结果为:(%.5f, %.5f)" % (x, f_current))
    print(u"迭代过程中X的取值,迭代次数:%d" % iter_num)
    print(GD_X)
    
    
    # 构建数据
    X = np.arange(-4, 4.5, 0.05)
    Y = np.array(list(map(lambda t: f1(t), X)))
    
    # 画图
    plt.figure(facecolor='w')
    plt.plot(X, Y, 'r-', linewidth=2)
    plt.plot(GD_X, GD_Y, 'ko--', linewidth=2)
    plt.title(u'函数$y=0.5 * (θ - 0.25)^2$; \n学习率:%.3f; 最终解:(%.3f, %.3f);迭代次数:%d' % (alpha, x, f_current, iter_num))
    plt.show()
    
    
    """二维原始图像"""
    def f2(x, y):
        return 0.6 * (x + y) ** 2 - x * y
    # 导函数
    def hx2(x, y):
        return 0.6 * 2 * (x + y) - y
    def hy2(x, y):
        return 0.6 * 2 * (x + y) - x
    
    # 使用梯度下降法求解
    GD_X1 = []
    GD_X2 = []
    GD_Y = []
    x1 = 4
    x2 = 4
    alpha = 0.5
    f_change = f2(x1, x2)
    f_current = f_change
    GD_X1.append(x1)
    GD_X2.append(x2)
    GD_Y.append(f_current)
    iter_num = 0
    while f_change > 1e-10 and iter_num < 100:
        iter_num += 1
        prex1 = x1
        prex2 = x2
        x1 = x1 - alpha * hx2(prex1, prex2)
        x2 = x2 - alpha * hy2(prex1, prex2)
        
        tmp = f2(x1, x2)
        f_change = np.abs(f_current - tmp)
        f_current  = tmp
        GD_X1.append(x1)
        GD_X2.append(x2)
        GD_Y.append(f_current)
    print(u"最终结果为:(%.5f, %.5f, %.5f)" % (x1, x2, f_current))
    print(u"迭代过程中X的取值,迭代次数:%d" % iter_num)
    print(GD_X1)
    
    
    # 构建数据
    X1 = np.arange(-4, 4.5, 0.2)
    X2 = np.arange(-4, 4.5, 0.2)
    X1, X2 = np.meshgrid(X1, X2)
    Y = np.array(list(map(lambda t: f2(t[0], t[1]), zip(X1.flatten(), X2.flatten()))))
    Y.shape = X1.shape
    
    
    # 画图
    fig = plt.figure(facecolor='w')
    ax = Axes3D(fig)
    ax.plot_surface(X1, X2, Y, rstride=1, cstride=1, cmap=plt.cm.jet)
    ax.plot(GD_X1, GD_X2, GD_Y, 'ko--')
    
    ax.set_title(u'函数$y=0.6 * (θ1 + θ2)^2 - θ1 * θ2$;\n学习率:%.3f; 最终解:(%.3f, %.3f, %.3f);迭代次数:%d' % (alpha, x1, x2, f_current, iter_num))
    plt.show()
    
    
    """二维原始图像"""
    def f2(x, y):
        return 0.15 * (x + 0.5) ** 2 + 0.25 * (y  - 0.25) ** 2 + 0.35 * (1.5 * x - 0.2 * y + 0.35 ) ** 2  
    ## 偏函数
    def hx2(x, y):
        return 0.15 * 2 * (x + 0.5) + 0.25 * 2 * (1.5 * x - 0.2 * y + 0.35 ) * 1.5
    def hy2(x, y):
        return 0.25 * 2 * (y  - 0.25) - 0.25 * 2 * (1.5 * x - 0.2 * y + 0.35 ) * 0.2
    
    # 使用梯度下降法求解
    GD_X1 = []
    GD_X2 = []
    GD_Y = []
    x1 = 4
    x2 = 4
    alpha = 0.5
    f_change = f2(x1, x2)
    f_current = f_change
    GD_X1.append(x1)
    GD_X2.append(x2)
    GD_Y.append(f_current)
    iter_num = 0
    while f_change > 1e-10 and iter_num < 100:
        iter_num += 1
        prex1 = x1
        prex2 = x2
        x1 = x1 - alpha * hx2(prex1, prex2)
        x2 = x2 - alpha * hy2(prex1, prex2)
        
        tmp = f2(x1, x2)
        f_change = np.abs(f_current - tmp)
        f_current  = tmp
        GD_X1.append(x1)
        GD_X2.append(x2)
        GD_Y.append(f_current)
    print(u"最终结果为:(%.5f, %.5f, %.5f)" % (x1, x2, f_current))
    print(u"迭代过程中X的取值,迭代次数:%d" % iter_num)
    print(GD_X1)
    
    
    # 构建数据
    X1 = np.arange(-4, 4.5, 0.2)
    X2 = np.arange(-4, 4.5, 0.2)
    X1, X2 = np.meshgrid(X1, X2)
    Y = np.array(list(map(lambda t: f2(t[0], t[1]), zip(X1.flatten(), X2.flatten()))))
    Y.shape = X1.shape
    
    
    # 画图
    fig = plt.figure(facecolor='w')
    ax = Axes3D(fig)
    ax.plot_surface(X1, X2, Y, rstride=1, cstride=1, cmap=plt.cm.jet)
    ax.plot(GD_X1, GD_X2, GD_Y, 'ko--')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    
    ax.set_title(u'函数;\n学习率:%.3f; 最终解:(%.3f, %.3f, %.3f);迭代次数:%d' % (alpha, x1, x2, f_current, iter_num))
    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

    总结

    001梯度下降:一维和二维图像
    003梯度下降:拉格乘子法

  • 相关阅读:
    2024届通信工程保研经验分享(预推免入营即offer)
    pytorch深度学习实战lesson16
    贴片电阻的读数方法
    CesiumJS 2022^ 原理[3] 渲染原理之从 Entity 看 DataSource 架构 - 生成 Primitive 的过程
    无线互动会议室解决方案-总控室部署
    docker 操作redis
    【问题记录与解决】jupyter notebook 无法重命名,无法运行测试代码 || jupyter notebook 中常用的两个快捷键。
    使用jenkins+gitee创建docker镜像并运行
    Vue:生命周期(从出生到销毁)
    前端_Vue_1.初识Vue
  • 原文地址:https://blog.csdn.net/DeepLearning_/article/details/127913350