当前位置:网站首页>Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
2022-04-23 05:59:00 【umbrellalalalala】
Reference material :《 Deep learning framework PyTorch: Introduction and practice 》
This paper annotates and interprets the code of linear regression in this book , The formula for solving the gradient in the process of manual back propagation is supplemented .
Catalog
One 、 Generate data set complete code
use “ Fake data ”:
# Set random number seed , Ensure that the following output is consistent when running on different computers
t.manual_seed(1000)
def get_fake_data(batch_size=8):
''' Generate random data :y=x*2+3, Add some noise '''
x = t.rand(batch_size, 1) * 20
y = x * 2 + (1 + t.randn(batch_size, 1) * 3)
return x, y
* Two 、 Linear regression complete code
The code for automatically calculating the gradient is in the comment :
# If you fill in parentheses 1, False report :mat2 must be a matrix
w = t.rand(1, 1)
b = t.zeros(1, 1)
# # If the gradient is calculated automatically
# # Be careful requires_grad The default is False, Not set to True Will be in loss.backward() Report errors
# w = t.rand(1, 1, requires_grad=True)
# b = t.zeros(1, 1, requires_grad=True)
lr = 0.0001
losses = np.zeros(500)
for ii in range(500):
x, y = get_fake_data(batch_size=32)
# Forward propagation , Calculation loss, Using the mean square error
# torch.mul It's multiplication by elements ;torch.mm It's matrix multiplication
y_pred = t.mm(x, w) + b.expand_as(y)
loss = 0.5 * (y_pred - y) ** 2
loss = loss.sum()
losses[ii] = loss.item()
# Back propagation , Manual gradient calculation
dloss = 1
dy_pred = dloss * (y_pred - y)
dw = t.mm(x.t(), dy_pred)
db = dy_pred.sum() # Be careful b It's scalar , When used, it is extended to all elements b Vector
# Update parameters
w.sub_(lr * dw)
b.sub_(lr * db)
# # If the gradient is calculated automatically
# loss.backward()
# w.data.sub_(lr * w.grad.data)
# b.data.sub_(lr * b.grad.data)
# # Note that the gradient is cleared
# w.grad.data.zero_()
# b.grad.data.zero_()
# Every time 1000 Draw a picture for each training
if ii % 50 == 0:
display.clear_output(wait=True)
# predicted
x = t.arange(0, 20).view(-1, 1).float()
y = t.mm(x, w) + b.expand_as(x)
plt.plot(x.numpy(), y.numpy())
# true data
x2, y2 = get_fake_data(batch_size=20)
plt.scatter(x2.numpy(), y2.numpy())
plt.xlim(0, 5)
plt.ylim(0, 13)
plt.show()
plt.pause(0.5)
print(w.item(), b.item())
# print(w.data[0][0], b.data[0][0]) # It's equivalent to the above
Running results :
Observe loss The change of :
plt.plot(losses)
plt.ylim(50, 500)
Output results :
loss Is steadily getting smaller .
3、 ... and 、 Formula for manually calculating gradient
selected from 《 Deep learning 》( Flower Book ):
Remember the above formula G B T GB^T GBT perhaps A T G A^TG ATG that will do , According to this formula , According to this formula ,loss Yes w The gradient of x T d y _ p r e d x^Tdy\_pred xTdy_pred.
Four 、 About output as “nan nan” The situation of
print(w.item(), b.item())
, If the last w and b The value output of is nan
, Then just turn down the learning rate . I set the learning rate as 0.001 It's all going to happen , As the 0.0001 Just fine .
版权声明
本文为[umbrellalalalala]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230543474468.html
边栏推荐
- Graphic numpy array matrix
- 关于二叉树的遍历
- LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
- Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
- 实操—Nacos安装与配置
- container
- JSP syntax and JSTL tag
- 去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
- PyQy5学习(二):QMainWindow+QWidget+QLabel
- Pytorch學習記錄(十三):循環神經網絡((Recurrent Neural Network)
猜你喜欢
Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
自动控制(韩敏版)
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
Development environment EAS login license modification
Manually delete registered services on Eureka
Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
Conda 虚拟环境管理(创建、删除、克隆、重命名、导出和导入)
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
Gaussian processes of sklearn
随机推荐
线性代数第二章-矩阵及其运算
多线程与高并发(2)——synchronized用法详解
在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Dva中在effects中获取state的值
多线程与高并发(3)——synchronized原理
异常的处理:抓抛模型
On traversal of binary tree
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
container
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
Gaussian processes of sklearn
interviewter:介绍一下MySQL日期函数
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
事实最终变量与最终变量
框架解析2.源码-登录认证
图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
Pytoch learning record (x): data preprocessing + batch normalization (BN)