深度学习笔记(零)

前言

 

学习书籍选择

《动手学深度学习》

请输入图片描述

豆瓣链接:https://book.douban.com/subject/33450010/

在线网站

http://zh.gluon.ai/

本文包含内容

1.算法内容

2.代码分析


正文

算法及代码分析

线性回归模型

\hat{y}=\begin{bmatrix} x_1& x_2 \end{bmatrix}* \begin{bmatrix} \omega_1\\ \omega_2 \end{bmatrix}+ b

所对应代码:

def linreg(x, w, b):
    return nd.dot(x, w) + b

参数优化

\begin{aligned} w_{1} \leftarrow w_{1}-\frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \frac{\partial \ell^{(i)}\left(w_{1}, w_{2}, b\right)}{\partial w_{1}}=w_{1}-\frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} x_{1}^{(i)}\left(x_{1}^{(i)} w_{1}+x_{2}^{(i)} w_{2}+b-y^{(i)}\right) \\ w_{2} \leftarrow w_{2}-\frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \frac{\partial \ell^{(i)}\left(w_{1}, w_{2}, b\right)}{\partial w_{2}}=w_{2}-\frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} x_{2}^{(i)}\left(x_{1}^{(i)} w_{1}+x_{2}^{(i)} w_{2}+b-y^{(i)}\right) \\ b \leftarrow b-\frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \frac{\partial \ell^{(i)}\left(w_{1}, w_{2}, b\right)}{\partial b}=b-\frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}}\left(x_{1}^{(i)} w_{1}+x_{2}^{(i)} w_{2}+b-y^{(i)}\right) \end{aligned}

所对应代码:

def sgd(params, lr, batch_size):
    for param in params:
        param[:] = param - lr * param.grad / batch_size

损失函数 

\ell^{(i)}\left(w_{1}, w_{2}, b\right)=\frac{1}{2}\left(\hat{y}^{(i)}-y^{(i)}\right)^{2}

其中常数1/2是为了在求导后形式上更为简洁。

代码如下:

def squared_loss(y_hat, y):
    return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2

通常我们使用所有样本的平均值作为损失函数值。

也即:

\ell\left(w_{1}, w_{2}, b\right)=\frac{1}{n} \sum_{i=1}^{n} \ell^{(i)}\left(w_{1}, w_{2}, b\right)=\frac{1}{n} \sum_{i=1}^{n} \frac{1}{2}\left(x_{1}^{(i)} w_{1}+x_{2}^{(i)} w_{2}+b-y^{(i)}\right)^{2}

绘制不同学习率的损失函数值下降速率

所用到的库

from matplotlib import pyplot as plt
import numpy

 绘制函数

def plot(x, y, condition):
    plt.plot(range(x), y, label='lr=' + str(condition))  # 加上标签
    plt.legend(loc='upper right')  # 绘制图例:左上方

 获取学习率0.01到0.05步进为0.01的三轮学习后的损失函数值并绘制:

for lr in numpy.arange(0.01, 0.05, 0.01):
    print('when learnrate = %f:' % lr)
    # 重置参数
    w = nd.random.normal(scale=0.01, shape=(num_input, 1))  # 标准差为scale
    b = nd.zeros(shape=(1,))  # 偏移量为零
    w.attach_grad()
    b.attach_grad()

    loss_list = train_wb(w, b)
    plot(num_epochs, loss_list, lr)

plt.show()

绘制结果

 


 疑问

1.attach_grad()是什么作用?

2.backward()是什么作用?


github:https://github.com/TinyMantou/DL_learningnote

参考:https://blog.csdn.net/iwinsx/article/details/94640697

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值