# Lab 1实验报告
# 一、问题概述
![](https://www.writebug.com/myres/static/uploads/2022/6/13/d729f9a557fe3bb429e5217a98781d24.writebug)
# 二、数据生成
![](https://www.writebug.com/myres/static/uploads/2022/6/13/4ec461ba453ae3eaa05f357c66c0b04f.writebug)
一个规模为20的训练集如图所示:
![](https://www.writebug.com/myres/static/uploads/2022/6/13/075db4522e2f643ffda3ebbd5becf65c.writebug)
此外,为了保证结果可重复,以下未特殊说明的部分,均取测试集大小为20,随机数种子为0。
# 三、问题求解
## 3.1 无正则项解析解
![](https://www.writebug.com/myres/static/uploads/2022/6/13/be43d7746f11812204dc2b773c62d5e9.writebug)
取训练集规模为20,依次取多项式的阶数为0到8,结果如下图所示:
![](https://www.writebug.com/myres/static/uploads/2022/6/13/909c3a1223b00e054edfdca0bb4683d5.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/b20cd7d2f410ba53c02d2daebd8270b1.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/00d8e00b94f21677a20052b06888a4ea.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/9da4993bddeec091a31724f893099669.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/af6698ed3d5faa831e041044bf7042bf.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/9b7fc147c5d19eee8c0ea35f32b20e3a.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/b2db53e1f7a0f3f143c452cb530fd393.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/4c8aab929241539bfbc36dd237347231.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/21f971ceb67f7c8ba9cabf6b98b63592.writebug)
可以看到在阶数为5之前,拟合的效果随着阶数的增大而逐渐改善,但之后拟合的效果逐渐变差,拟合曲线和正弦曲线开始发生偏离,表明发生了过拟合的现象。
取多项式的阶数为8,依次取训练集的规模为20、60、100,结果如下图所示:
![](https://www.writebug.com/myres/static/uploads/2022/6/13/0e52ba92030ea2a68de23633eec51446.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/75033e223a601b5685a9f3970ce1d92c.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/f2110319a40466511cfbfb1e5d004a46.writebug)
可以看到随着训练集规模的增大,过拟合的现象有所改善。
## 3.2 有正则项解析解
![](https://www.writebug.com/myres/static/uploads/2022/6/13/8b5314e38cb42cdc7164ca40b6e5284b.writebug)
取训练集规模为20,多项式阶数为10,比较引入正则项前后的拟合效果如图所示:
![](https://www.writebug.com/myres/static/uploads/2022/6/13/6d080ef2817cf474a2a1ecfd7f1c0529.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/2ba512ae7b9255369b074281422f57c5.writebug)
可以看到,拟合效果有一定改善。
## 3.3 梯度下降法
(代码见process. gradient_descent)
在上述解析解直接求解的过程中,由于需要进行矩阵求逆的运算,所以在训练集或多项式阶数较大时,可能会有运算效率低下的问题。
我们知道,函数上任意一点的梯度指向函数下降最快的方向,而我们原本的目标,就是求损失函数的最小值。
因此我们可以先随机生成一个![](https://www.writebug.com/myres/static/uploads/2022/6/13/844bee84984443e184bdbed836ba5589.writebug)作为起点,每次沿着梯度的方向前进一定的距离,并更新![](https://www.writebug.com/myres/static/uploads/2022/6/13/25082c2c9d5ec226ec0347c3eb08bbbd.writebug),如此反复,直到函数的梯度小于一定的阈值,即已经位于最小值附近。
我们每次前进的距离为![](https://www.writebug.com/myres/static/uploads/2022/6/13/f083143900a58d4365b57952e392f4f7.writebug),![](https://www.writebug.com/myres/static/uploads/2022/6/13/8e52425b81a0c1decdc2e22c9d176e34.writebug)称为学习率。
取训练集规模为20,依次取多项式的阶数为0到5,学习率为0.0001,梯度阈值为0.0001,结果如下图所示:
![](https://www.writebug.com/myres/static/uploads/2022/6/13/3b24f5e9e884a731c51dbacff23b9392.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/6de50294923b4b3b7ae480ad95c0e774.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/eda9eabacf82a9b4dda4bb6d53323324.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/e79bc3bb39ddcd695bb613322ee41e1b.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/72d3552f23496d1368099bc765ba369f.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/a72c1b686d879266dd8a77d85daff2f1.writebug)
可以发现,梯度下降得到的结果和通过解析式直接求解得到的结果基本是一致的。
但是由于该方法每一步都是沿着这一点的梯度走,实际上是贪心的思想,所以会有陷入局部最优解的风险。
此外,由于迭代的次数并没有保证,所以在阶数较大时,很有可能出现计算效率低下的问题。
各阶数对应迭代次数如下表所示:
| 阶数 |0 |1 |2 |3 |4 |5 |
|----|----|----|----|----|----|----|
| 迭代次数 |31 |345 |7265 |81760 |115462 |178654 |
## 3.4 共轭梯度法
(代码见process. conjugate_gradient)
由于在梯度下降法中,为了达到目标精度,可能需要大量的迭代次数,所以在效率上依然存在较大的问题。
而共轭梯度法每次沿着全局下降最快的一个方向前进,所以理论上只需要迭代![](https://www.writebug.com/myres/static/uploads/2022/6/13/256a69f6e4b8d843a70cba60f2cc06fd.writebug)步就能走到最优解,其中![](https://www.writebug.com/myres/static/uploads/2022/6/13/582c60f3c19601608c391e8c72d44aa3.writebug)为多项式的阶数。
取训练集规模为20,依次取多项式的阶数为0到5,结果如下图所示:
![](https://www.writebug.com/myres/static/uploads/2022/6/13/c02583be022cb8d0bf1048f404aabffb.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/41894ca6063e7b225bf688ab46e9f070.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/8a7a6cf90d1ef459421e46a54a690ceb.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/b270aa513a14415493ea27151549ff3c.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/5d6acd2bf163040443a097238798cbd9.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/13/fcd79d05fd45b94bb9bdffc7bd9f4577.writebug)
可以看到结果与前述方法基本一致,且计算速度极快,是一种特别优秀的方法。
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源包含文件:设计报告word+设计要求+源码 掌握最小二乘法求解(无惩罚项的损失函数)、掌握加惩罚项(2范数)的损失函数优化、梯度下降法、共轭梯度法、理解过拟合、克服过拟合的方法(如加惩罚项、增加样本) 要求: 1. 生成数据,加入噪声; 2. 用高阶多项式函数拟合曲线; 3. 用解析解求解两种loss的最优解(无正则项和有正则项) 4. 优化方法求解最优解(梯度下降,共轭梯度); 5. 用你得到的实验数据,解释过拟合。 6. 用不同数据量,不同超参数,不同的多项式阶数,比较实验效果。 7. 语言不限,可以用matlab,python。求解解析解时可以利用现成的矩阵求逆。梯度下降,共轭梯度要求自己求梯度,迭代优化自己写。不许用现成的平台,例如pytorch,tensorflow的自动微分工具。 详细介绍参考:https://blog.csdn.net/sheziqiong/article/details/125276021
资源推荐
资源详情
资源评论
收起资源包目录
基于Python实现的多项式拟合曲线实验.zip (8个子文件)
README.md 7KB
LICENSE 1KB
设计要求.docx 12KB
src
config.py 1KB
process.py 3KB
main.py 1KB
data.py 2KB
设计报告.docx 766KB
共 8 条
- 1
资源评论
- m0_736963712022-11-17资源内容详尽,对我有使用价值,谢谢资源主的分享。
shejizuopin
- 粉丝: 9509
- 资源: 1288
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功