当前位置:网站首页>B站刘二大人-线性回归 Pytorch
B站刘二大人-线性回归 Pytorch
2022-07-06 05:33:00 【宁然也】
系列文章:
文章目录
线性回归 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):
# 成员方法__init__ 及 forward 必须实现
def __init__(self):
super(LinearModel, self).__init__()
# 1,1,代表权重weight和偏置b的size
self.linear = torch.nn.Linear(3, 1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
# 损失函数计算一个batch中所有loss的均值
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)
# 梯度会在backward时累计,所以在backward之前必须重置
optimizer.zero_grad()
loss.backward()
# update
optimizer.step()
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()

边栏推荐
- Review of double pointer problems
- Force buckle 1189 Maximum number of "balloons"
- PDK process library installation -csmc
- nacos-高可用seata之TC搭建(02)
- First acquaintance with CDN
- Using stopwatch to count code time
- February 12 relativelayout
- [force buckle]43 String multiplication
- Please wait while Jenkins is getting ready to work
- Pix2pix: image to image conversion using conditional countermeasure networks
猜你喜欢

Hyperledger Fabric2. Some basic concepts of X (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

Vulhub vulnerability recurrence 69_ Tiki Wiki

04. Project blog log

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

自建DNS服务器,客户端打开网页慢,解决办法
![[Tang Laoshi] C -- encapsulation: classes and objects](/img/4e/30d2d4652ea2d4cd5fa7cbbb795863.jpg)
[Tang Laoshi] C -- encapsulation: classes and objects

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

27io stream, byte output stream, OutputStream writes data to file
随机推荐
flutter 实现一个有加载动画的按钮(loadingButton)
C# AES对字符串进行加密
Graduation design game mall
[cloud native] 3.1 kubernetes platform installation kubespher
Vulhub vulnerability recurrence 67_ Supervisor
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
Easy to understand IIC protocol explanation
[untitled]
[machine learning notes] univariate linear regression principle, formula and code implementation
Yyds dry inventory SSH Remote Connection introduction
Steady, 35K, byte business data analysis post
03. 开发博客项目之登录
Nacos TC setup of highly available Seata (02)
Imperial cms7.5 imitation "D9 download station" software application download website source code
29io stream, byte output stream continue write line feed
01. Project introduction of blog development project
[imgui] unity MenuItem shortcut key
[effective Objective-C] - memory management
C进阶-数据的存储(上)
pix2pix:使用条件对抗网络的图像到图像转换