当前位置:网站首页>Univariate linear regression model
Univariate linear regression model
2022-06-12 06:06:00 【Singing under the hedge】
One variable linear regression model
List of articles
One 、 Solving steps
1. Determine the model
2. Choose the loss function MES
3. Solve gradient and update w,b
w = w - LR * w.grade
b = b - LR * b.grade
Two 、 Code implementation
# One variable linear regression model
import torch
import matplotlib.pyplot as plt
torch.manual_seed(10)
lr=0.05 # Learning rate
# Create training data
x = torch.rand(20,1)*10
y = 2*x+(5+torch.randn(20,1))# Plus random noise
# Construct linear regression parameters
w = torch.randn((1),requires_grad=True)
b = torch.zeros((1),requires_grad=True)
for iteration in range(1000):
# Forward propagation
wx = torch.mul(w,x)
y_pred = torch.add(wx,b)
# Calculation MSE loss
loss=(0.5*(y-y_pred)**2).mean()
# Back propagation
loss.backward()
# Update parameters
b.data.sub_(lr*b.grad)
w.data.sub_(lr * w.grad)
# The gradient of the clearing tensor
w.grad.zero_()
b.grad.zero_()
# mapping
if iteration %20 ==0 :
plt.scatter(x.data.numpy(),y.data.numpy())
plt.plot(x.data.numpy(),y_pred.data.numpy(),'r-',lw=5)
plt.text(2,20,'Loss=%.4f' % loss.data.numpy(),fontdict={
'size':20,'color':'red'})
plt.xlim(1.5,10)
plt.ylim(8,28)
plt.title("Iteration:{}\nw:{} b:{}".format(iteration,w.data.numpy(),b.data.numpy()))
plt.pause(0.5)
if loss.data.numpy() <1:
break
3、 ... and 、 Realization effect

边栏推荐
- Research Report on water sports shoes industry - market status analysis and development prospect forecast
- Script for unity3d to recursively search for a node with a specific name from all child nodes of a node
- 单通道图片的读入
- Leetcode-553. Optimal division
- Storing texture2d to hard disk JPG file with script under unity3d
- [Yu Yue education] basic reference materials of accounting of Nanjing Normal University
- 哈工大信息内容安全实验
- Why doesn't the database use binary tree, red black tree, B tree and hash table? Instead, a b+ tree is used
- How do I get the date and time from the Internet- How to get DateTime from the internet?
- Why do I object so [1.01 to the power of 365 and 0.99 to the power of 365]
猜你喜欢

BlockingQueue interface introduction

Performance optimization metrics and tools

Directx11 advanced tutorial PBR (1) summary of physical phenomena of light

Houdini & UE4 programmed generation of mountains and multi vegetation scattering points

从传统网络IO 到 IO多路复用

Leetcode-1043. Separate arrays for maximum sum

Project technical structure

Makefile文件编写快速掌握

The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY

Houdini script vex learning
随机推荐
姿态估计之2D人体姿态估计 - PifPaf:Composite Fields for Human Pose Estimation
Leetcode 第 80 场双周赛题解
Leetcode-2048. Next larger numerical balance
项目开发流程简单介绍
2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation
Redis队列
Brief introduction to project development process
json-c常用API
Findasync and include LINQ statements - findasync and include LINQ statements
cv2.fillPoly coco annotator segment坐标转化为mask图像
Brief summary of software project architecture
肝了一個月的 DDD,一文帶你掌握
Why do I object so [1.01 to the power of 365 and 0.99 to the power of 365]
数据库实验一:数据定义实验指导
Leetcode-1604. Warning people who use the same employee card more than or equal to three times within one hour
nRF52832自定義服務與特性
Leetcode-1512. Number of good pairs
Unity C script implements AES encryption and decryption
交叉编译libev
IDEA常用配置