深度学习 线性回归 笔记

本文最后更新于:2023年6月17日 下午

一、知识储备

1. 公式

线性回归一般模型:\[\begin{equation} \hat y = w_1x_1+w_2x_2+\cdots+w_nx_n+b\end{equation} \]

线性代数表示:\[\begin{equation}\hat y= w^Tx+b\end{equation}\]

损失函数:\[\begin{equation}\ l^{(i)}(w,b)=\frac{1}{2}(\hat y^{(i)}-y^{(i)}) \end{equation}\]

模型更新过程

\(\begin{split}\begin{aligned} \mathbf{w} &\leftarrow \mathbf{w} - \frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \partial_{\mathbf{w}} l^{(i)}(\mathbf{w}, b) = \mathbf{w} - \frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \mathbf{x}^{(i)} \left(\mathbf{w}^\top \mathbf{x}^{(i)} + b - y^{(i)}\right),\\ b &\leftarrow b - \frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \partial_b l^{(i)}(\mathbf{w}, b) = b - \frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \left(\mathbf{w}^\top \mathbf{x}^{(i)} + b - y^{(i)}\right). \end{aligned}\end{split}\Longrightarrow(\mathbf{w},b) \leftarrow (\mathbf{w},b) - \frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \partial_{(\mathbf{w},b)} l^{(i)}(\mathbf{w},b).\)

2. 函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import  torch

torch.tensor([[2,1,4,3],[1,2,3,4],[4,3,2,1]])
#为张量中每个元素赋值 最外层的列表对应于轴0,内层的列表对应于轴1

X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
torch.cat((X, Y), dim=0), torch.cat((X, Y), dim=1)
#dim== 0 行增加,拼到行后面, dim==1 列增加,拼到列后面

loss.backward()
#自动微分,计算变量的反向传播,意味着跟踪整个计算图,填充关于每个参数的偏导数

#https://www.cnblogs.com/tangjunjun/p/14437631.html
# Eg1:
x = torch.arange(4.0) # X=[0,1,2,3]
x.requires_grad_(True)
y = 2 * torch.dot(x, x) # y = 2 * X^T*X 标量
y.backward() # y标量 y_i' = 4*(Dy/Dx_i) [0,4,8,12]
x.grad
# 输出: tensor([ 0., 4., 8., 12.])

# Eg2:
x.grad.zero_()
y = x.sum() # y = x1 + x2 + x3 + x4 标量
y.backward() # y' = Dy/x_1 Dy/x_2 Dy/x_3 Dy/x_4
x.grad
# 输出: tensor([1., 1., 1., 1.])

# Eg3:
x.grad.zero_()
y = x * x # y 向量
#y.sum().backward() #求和为标量再backward yi' = Dy/Dx_i = 2x_i
#等价于y.backward(torch.ones(len(x))) ==> 赋予每个参数x_i前的权重系数为1 ==> x1+x2+x3+x4 ==>y.sum()
y.backward(torch.tensor([1, 0.1, 0.01,0.001]))
#==>赋予每个参数x_i的权重为对应值 0.1x1+1.0x2+0.01x3+0.0001x4
print(x.grad)
#输出:tensor([0.0000, 0.2000, 0.0400, 0.0060])

二、代码实现

手动实现

简洁实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import torch
from torch.utils import data
from d2l import torch as d2l
from torch import nn

true_w = torch.tensor([-2,3.4])
true_b = 2
# y = -2x1 + 3.4x2 +2

features,labels = d2l.synthetic_data(true_w,true_b,1000)
#synthetic_data(w,b,num_samples)
#生成 1000 x 2 x 的集合 和 对应的 y的结果 作为初始数据
# features 用来存输入的x
# labels 用来存y

def load_array(data_arrays,batch_size,is_train=True):
dataset = data.TensorDataset(*data_arrays)
return data.DataLoader(dataset,batch_size,shuffle=is_train)


batch_size = 10
dataset = load_array((features,labels),batch_size)

# 损失函数
loss = nn.MSELoss()
# 训练网络
net = nn.Sequential(nn.Linear(2,1))
# 优化器
trainer = torch.optim.SGD(net.parameters(),lr=0.03)

epochs = 3

for epoch in range(epochs):
for X,y in dataset:
l = loss(net(X),y)
trainer.zero_grad()
l.backward()
trainer.step()
l = loss(net(X),y)
print(f'epoch{epoch+1},loss{l:f}')

w = net[0].weight.data
print('w的估计误差:', true_w - w.reshape(true_w.shape))
print('W:',w)
b = net[0].bias.data
print('b的估计误差:', true_b - b)
print('b',b)


深度学习 线性回归 笔记
https://anonymouslosty.ink/2023/02/22/深度学习 线性回归 笔记/
作者
Ling yi
发布于
2023年2月22日
更新于
2023年6月17日
许可协议