梯度下降作业

1.手工推导

1.1单元函数

在这里插入图片描述

1.2多元函数

在这里插入图片描述

1.3线性回归

在这里插入图片描述

2.代码

2.1单元函数

import numpy as np
import matplotlib.pyplot as plt


def f(x):
    return 1 / 2 * x * x - 2 * x + 3


def f_(x):
    return x - 2


def tdxj():
    x = 8
    time = 500
    alpha = 0.05
    x_fw = np.linspace(-5,10)
    fig = plt.figure(1, figsize=(5, 5))
    ax = fig.add_subplot(1, 1, 1)
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.plot(x_fw, f(x_fw))
    for i in range(time):
        x1 = x
        y1 = f(x)
        print('第%d次迭代得到:x=%f,y=%f' % (i + 1, x, y1))
        print('第%d次迭代得到:x=%f,y=%f' % (i + 1, x, y1))
        x = x - alpha * f_(x)
        y = f(x)
        ax.plot([x1, x], [y1, y], 'ko', lw=1, ls='-', color='black')
        if(f_(x)<1e-5):
            break
    plt.show()


if __name__ == "__main__":
    tdxj()

在这里插入图片描述
在这里插入图片描述

2.2多元函数

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


def f(x, y):
    return (x - 10)**2+ (y - 10)**2


def f_x(x):
    return 2 * x - 20


def f_y(y):
    return 2 * y - 20


def tdxj():
    x = 20
    y = 20
    alpha = 0.2
    time = 100
    fig = Axes3D(plt.figure())
    axis_x = np.linspace(0, 20, 100)
    axis_y = np.linspace(0, 20, 100)
    axis_x, axis_y = np.meshgrid(axis_x, axis_y)
    z = f(axis_x, axis_y)
    fig.set_xlabel('X', fontsize=14)
    fig.set_ylabel('Y', fontsize=14)
    fig.set_zlabel('Z', fontsize=14)
    fig.view_init(elev=60, azim=300)
    fig.plot_surface(axis_x, axis_y, z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))

    for i in range(time):
        x1 = x
        y1 = y
        f1 = f(x,y)
        print("第%d次迭代的结果是:x=%f,y=%f,f1=%f" % (i + 1, x, y, f1))
        x = x - alpha * 2 * f_x(x)
        y = y - alpha * 2 * f_y(y)
        f1 = f(x, y)
        fig.plot([x1, x], [y1, y], [f1, f], 'ko', lw=2, ls='-')  # 绘制点
    plt.show()


if __name__ == "__main__":
    tdxj()

在这里插入图片描述
在这里插入图片描述

3.参考资料

link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值