文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
注:本文为李沐大神的《动手学深度学习》的课程笔记!
参考资料
ndarray文档
https://mxnet.incubator.apache.org/api/python/ndarray.htmlyield用法
https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/matplotlib用法
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.plot.html
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html指数平滑法
http://blog.csdn.net/tz_zs/article/details/78341306
http://blog.csdn.net/cl1143015961/article/details/41081183
1 | # 导入mxnet的ndarray, autograd |
创建数据集
1 | # 训练数据的维度 |
数据可视化
1 | print(X[0], y[0]) |
(
[ 1.16307867 0.48380461]
<NDArray 2 @cpu(0)>,
[ 4.87962484]
<NDArray 1 @cpu(0)>)
1 | %matplotlib inline |
数据读取
1 | import random |
1 | # 查看data_iter是否是generator函数 |
True
True
(
[[ 1.18770552 -0.46362698]
[-3.15577412 2.19352984]
[-0.45067298 -0.96665388]
[ 0.05416773 -1.21203637]
[-1.49418294 -1.61555624]
[-0.93778831 -1.69338322]
[ 0.91439158 1.31797135]
[ 0.82403505 0.33020774]
[-0.19660901 1.13431609]
[ 0.15364595 1.01133049]]
<NDArray 10x2 @cpu(0)>,
[ 8.17057896 -9.57918072 6.58949089 8.41831684 6.69815683 8.08473206
1.54548573 4.73358202 -0.0632825 1.06603777]
<NDArray 10 @cpu(0)>)
初始化模型参数
1 | # 随机初始化权重w |
定义模型
1 | # 定义运算y = w * x + b |
损失函数
1 | # 定义平方损失 |
优化
1 | # 定义随机梯度下降法 |
数据可视化
1 | # 模型函数 |
训练
1 | # 定义训练的迭代周期 |
Epoch 0, batch 99. Moving average of loss: 0.378590331091. Average loss: 0.625015
Epoch 1, batch 199. Moving average of loss: 0.10108379838. Average loss: 0.000099
Epoch 2, batch 299. Moving average of loss: 0.033726038259. Average loss: 0.000099
Epoch 3, batch 399. Moving average of loss: 0.0120152144263. Average loss: 0.000099
Epoch 4, batch 499. Moving average of loss: 0.00441111205064. Average loss: 0.000101
1 | print w |
[[ 1.99982905]
[-3.40232825]]
<NDArray 2x1 @cpu(0)>
[2, -3.4]
[ 4.20024347]
<NDArray 1 @cpu(0)>
4.2
其他学习率
1 | learning_rate = 0.001 |
1 | learning_rate = 0.1 |
1 | learning_rate = 1 |