当前位置:网站首页>三、梯度下降求解最小θ
三、梯度下降求解最小θ
2022-04-23 14:37:00 【beyond谚语】
一、得到目标函数J(θ),求解使得J(θ)最小时的θ值
通过最小二乘法求目标函数最小值



令偏导为0即可求解出最小的θ值,即
二、判定为凸函数
凸函数有需要判断方法,比如:定义、一阶条件、二阶条件等。利用正定性判定使用的是二阶条件。
半正定一定是凸函数,开口朝上,半正定一定有极小值
在用二阶条件进行判定的时候,需要得到Hessian矩阵,根据Hessian的正定性判定函数的凹凸性。比如Hessian矩阵半正定,函数为凸函数;Hessian矩阵正定,函数为严格凸函数
Hessian矩阵:黑塞矩阵(Hessian Matrix),又称为海森矩阵、海瑟矩阵、海塞矩阵等,是一个多元函数的二阶偏导数构成的方阵,描述了函数的局部曲率。
三、Hessian矩阵
黑塞矩阵是由目标函数在点x处的二阶偏导数组成的对称矩阵
正定:对A的特征值全为正数,那么A一定是正定的
不正当:非正定或半正定
若A的特征值≥0,则半正定,否则,A为非正定。
对J(θ)损失函数求二阶导,之后得到的一定是半正定的,因为自己和自己做点乘。
四、解析解

数值解是在一定条件下通过某种近似计算得出来的一个数值,能在给定的精度条件下满足方程,解析解为方程的解析式(比如求根公式之类的),是方程的精确解,能在任意精度下满足方程。
五、梯度下降法
这个课程跟其他课程讲的差不多,这里我就不再赘述了。梯度下降法
梯度下降法:是一种以最快的速度找到最优解的方法。
流程:
1,初始化θ,这里的θ是一组参数,初始化也就是random一下即可
2,求解梯度gradient
3,θ(t+1) = θ(t) - grand*learning_rate
这里的learning_rate常用α表示学习率,是个超参数,太大的话,步子太大容易来回震荡;太小的话,迭代次数很多,耗时。
4,grad < threshold时,迭代停止,收敛,其中threshold也是个超参数
超参数:需要用户传入的参数,若不传使用默认的参数。
六、代码实现
导包
import numpy as np
import matplotlib.pyplot as plt
初始化样本数据
# 这里相当于是随机X维度X1,rand是随机均匀分布
X = 2 * np.random.rand(100, 1)
# 人为的设置真实的Y一列,np.random.randn(100, 1)是设置error,randn是标准正太分布
y = 4 + 3 * X + np.random.randn(100, 1)
# 整合X0和X1
X_b = np.c_[np.ones((100, 1)), X]
print(X_b)
""" [[1. 1.01134124] [1. 0.98400529] [1. 1.69201204] [1. 0.70020158] [1. 0.1160646 ] [1. 0.42502983] [1. 1.90699898] [1. 0.54715372] [1. 0.73002827] [1. 1.29651341] [1. 1.62559406] [1. 1.61745598] [1. 1.86701453] [1. 1.20449051] [1. 1.97722538] [1. 0.5063885 ] [1. 1.61769812] [1. 0.63034575] [1. 1.98271789] [1. 1.17275471] [1. 0.14718811] [1. 0.94934555] [1. 0.69871645] [1. 1.22897542] [1. 0.59516153] [1. 1.19071408] [1. 1.18316576] [1. 0.03684612] [1. 0.3147711 ] [1. 1.07570897] [1. 1.27796797] [1. 1.43159157] [1. 0.71388871] [1. 0.81642577] [1. 1.68275133] [1. 0.53735427] [1. 1.44912342] [1. 0.10624546] [1. 1.14697422] [1. 1.35930391] [1. 0.73655224] [1. 1.08512154] [1. 0.91499434] [1. 0.62176609] [1. 1.60077283] [1. 0.25995875] [1. 0.3119241 ] [1. 0.25099575] [1. 0.93227026] [1. 0.85510054] [1. 1.5681651 ] [1. 0.49828274] [1. 0.14520117] [1. 1.61801978] [1. 1.08275593] [1. 0.53545855] [1. 1.48276384] [1. 1.19092276] [1. 0.19209144] [1. 1.91535667] [1. 1.94012402] [1. 1.27952383] [1. 1.23557691] [1. 0.9941706 ] [1. 1.04642378] [1. 1.02114013] [1. 1.13222297] [1. 0.5126448 ] [1. 1.22900735] [1. 1.49631537] [1. 0.82234995] [1. 1.24810189] [1. 0.67549922] [1. 1.72536141] [1. 0.15290908] [1. 0.17069838] [1. 0.27173192] [1. 0.09084242] [1. 0.13085313] [1. 1.72356775] [1. 1.65718819] [1. 1.7877667 ] [1. 1.70736708] [1. 0.8037657 ] [1. 0.5386607 ] [1. 0.59842584] [1. 0.4433115 ] [1. 0.11305317] [1. 0.15295053] [1. 1.81369029] [1. 1.72434082] [1. 1.08908323] [1. 1.65763828] [1. 0.75378952] [1. 1.61262625] [1. 0.37017158] [1. 1.12323188] [1. 0.22165802] [1. 1.69647343] [1. 1.66041812]] """

# 常规等式求解theta
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
print(theta_best)
""" [[3.9942692 ] [3.01839793]] """
# 创建测试集里面的X1
X_new = np.array([[0], [2]])
X_new_b = np.c_[(np.ones((2, 1))), X_new]
print(X_new_b)
y_predict = X_new_b.dot(theta_best)
print(y_predict)
""" [[1. 0.] [1. 2.]] [[ 3.9942692 ] [10.03106506]] """
绘图
plt.plot(X_new, y_predict, 'r-')
plt.plot(X, y, 'b.')
plt.axis([0, 2, 0, 15])
plt.show()

七、完整代码
import numpy as np
import matplotlib.pyplot as plt
# 这里相当于是随机X维度X1,rand是随机均匀分布
X = 2 * np.random.rand(100, 1)
# 人为的设置真实的Y一列,np.random.randn(100, 1)是设置error,randn是标准正太分布
y = 4 + 3 * X + np.random.randn(100, 1)
# 整合X0和X1
X_b = np.c_[np.ones((100, 1)), X]
print(X_b)
# 常规等式求解theta
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
print(theta_best)
# 创建测试集里面的X1
X_new = np.array([[0], [2]])
X_new_b = np.c_[(np.ones((2, 1))), X_new]
print(X_new_b)
y_predict = X_new_b.dot(theta_best)
print(y_predict)
#绘图
plt.plot(X_new, y_predict, 'r-')
plt.plot(X, y, 'b.')
plt.axis([0, 2, 0, 15])
plt.show()
版权声明
本文为[beyond谚语]所创,转载请带上原文链接,感谢
https://beyondyanyu.blog.csdn.net/article/details/124360870
边栏推荐
- GIS数据处理-cesium中模型位置设置
- 一篇博客让你学会在vscode上编写markdown
- ASEMI整流模块MDQ100-16在智能开关电源中的作用
- C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】
- 顺序表的操作,你真的学会了吗?
- C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
- Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- 8.4 循环神经网络从零实现
- L'externalisation a duré quatre ans.
- [detailed explanation of factory mode] factory method mode
猜你喜欢

51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)

Qt实战:云曦聊天室篇

ArrayList collection basic usage

OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示

ASEMI整流模块MDQ100-16在智能开关电源中的作用

Detailed explanation of SAR command

DVWA之暴力破解(Brute Force)Low-->high

1分钟看懂执行流程,永久掌握for循环(附for循环案例)

C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】

Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
随机推荐
LLVM - 生成加法
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
顺序栈的基本操作
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
全连接层的作用是什么?
压缩映射定理
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
A blog allows you to learn how to write markdown on vscode
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
C语言p2选择分支语句详解
LLVM - 生成for循环
一个月把字节,腾讯,阿里都面了,写点面经总结……
Qt界面优化:Qt去边框与窗体圆角化
Usage of BC
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
循环队列的基本操作(实验)
source insight via samba
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了