当前位置:网站首页>Station B Liu Erden linear regression pytoch
Station B Liu Erden linear regression pytoch
2022-07-06 05:41:00 【Ning Ranye】
Series articles :
List of articles
Linear regression Pytorch
import matplotlib.pyplot as plt
import torch
x_data = torch.Tensor([[1.0, 2.0, 3.0]])
y_data = torch.Tensor([[2.0, 4.0, 6.0]])
class LinearModel(torch.nn.Module):
# Member method __init__ And forward Must be realized
def __init__(self):
super(LinearModel, self).__init__()
# 1,1, For weight weight And offset b Of size
self.linear = torch.nn.Linear(3, 1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
# Loss function calculation one batch All in loss The average of
criterion = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.0005)
loss_list = []
epoch_list = []
for epoch in range(200):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
loss_list.append(loss.item())
epoch_list.append(epoch)
# The gradient will be backward Hourly accumulation , So in backward Must be reset before
optimizer.zero_grad()
loss.backward()
# update
optimizer.step()
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()
边栏推荐
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- UCF(暑期团队赛二)
- 改善Jpopup以实现动态控制disable
- How can large websites choose better virtual machine service providers?
- JS array list actual use summary
- 04. Project blog log
- 初识数据库
- 实践分享:如何安全快速地从 Centos迁移到openEuler
- 指针经典笔试题
- Jvxetable implant j-popup with slot
猜你喜欢
Figure database ongdb release v-1.0.3
进程和线程
【经验】win11上安装visio
Deep learning -yolov5 introduction to actual combat click data set training
How can large websites choose better virtual machine service providers?
嵌入式面试题(四、常见算法)
毕业设计游戏商城
05. Security of blog project
[experience] install Visio on win11
What preparations should be made for website server migration?
随机推荐
UCF (2022 summer team competition I)
Detailed summary of SQL injection
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Unity gets the width and height of Sprite
SequoiaDB湖仓一体分布式数据库2022.6月刊
[SQL Server Express Way] - authentification et création et gestion de comptes utilisateurs
Codeforces Round #804 (Div. 2) Editorial(A-B)
59. Spiral matrix
Redis message queue
C Advanced - data storage (Part 1)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
03. 开发博客项目之登录
ArcGIS应用基础4 专题图的制作
YYGH-11-定时统计
【torch】|torch. nn. utils. clip_ grad_ norm_
Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
[cloud native] 3.1 kubernetes platform installation kubespher
Qt TCP 分包粘包的解决方法
Codeforces Round #804 (Div. 2) Editorial(A-B)
JS array list actual use summary