#花树学习#算法复现#梯度优化#范数矩阵

@花树-第4章梯度优化P62页

待优化公式

在这里插入图片描述

首先计算该梯度:

在这里插入图片描述

然后得到梯度优化的算法流程:

在这里插入图片描述
我这里设置矩阵A为(2X2)大小,X为(2X1),b为(2X1),这样是因为可以画出图片因此观察较明显。然后就是简单的复现:

附上代码:

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 13 20:20:46 2022

@author: dell
"""

import numpy as np
import matplotlib.pyplot as plt


plt.rcParams['font.sans-serif']=['SimHei'] # 显示中文
plt.rcParams['axes.unicode_minus']=False #显示正负号


A = np.random.rand(2,2)
#X = np.random.randint(0,4,(3,1))
X = np.random.rand(2,1)
b = np.random.randint(0,2,(2,1))

df = np.dot( np.dot(A.T , A) , X) - np.dot(A.T , b)

error = 0.003
a = 0.005
error_1=np.linalg.norm(x=df,ord=2)
y = []
bb = []

while error_1 > error:
    df = np.dot( np.dot(A.T , A) , X) - np.dot(A.T , b)
    error_1=np.linalg.norm(x=df,ord=2)
    X = X- a * df
    y.append(error_1)
    bb.append(X)
    print(error_1,'\n',X)

x_1 = [float(i[0]) for i in bb]
x_2 = [float(i[1]) for i in bb]


plt.figure()


ax = plt.axes(projection='3d')
ax.plot3D(x_1, x_2, y, 'gray')
plt.show()




结果图

在这里插入图片描述
注意每次的结果图是不一样的,因为我对矩阵A,和向量b的取值是随机的。程序后有输出结果,分别是当前误差的输出和当前X的取值(x1,x2)
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack ShuAi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值