当前位置:网站首页>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()

边栏推荐
- Self built DNS server, the client opens the web page slowly, the solution
- [experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
- 应用安全系列之三十七:日志注入
- (column 22) typical column questions of C language: delete the specified letters in the string.
- UCF (summer team competition II)
- Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
- Go language -- language constants
- Vulhub vulnerability recurrence 67_ Supervisor
- Redis消息队列
- 无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
猜你喜欢

B站刘二大人-Softmx分类器及MNIST实现-Lecture 9

28io stream, byte output stream writes multiple bytes

Pix2pix: image to image conversion using conditional countermeasure networks

指針經典筆試題

Vulhub vulnerability recurrence 67_ Supervisor

Vulhub vulnerability recurrence 71_ Unomi

26file filter anonymous inner class and lambda optimization

Promise summary

指针经典笔试题

大型网站如何选择比较好的云主机服务商?
随机推荐
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
Zoom and pan image in Photoshop 2022
Qt TCP 分包粘包的解决方法
Vulhub vulnerability recurrence 73_ Webmin
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
[force buckle]43 String multiplication
什么是独立IP,独立IP主机怎么样?
【华为机试真题详解】统计射击比赛成绩
The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
js Array 列表 实战使用总结
Figure database ongdb release v-1.0.3
Promotion hung up! The leader said it wasn't my poor skills
[imgui] unity MenuItem shortcut key
Go language -- language constants
[cloud native] 3.1 kubernetes platform installation kubespher
Fluent implements a loadingbutton with loading animation
What is independent IP and how about independent IP host?
HAC集群修改管理员用户密码
Sequoiadb Lake warehouse integrated distributed database, June 2022 issue
Promise summary