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

边栏推荐
猜你喜欢
随机推荐
指針經典筆試題
Quantitative description of ANC noise reduction
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. Login of development blog project
【华为机试真题详解】统计射击比赛成绩
改善Jpopup以实现动态控制disable
Closure, decorator
Pointer classic written test questions
P2802 回家
[imgui] unity MenuItem shortcut key
大型网站如何选择比较好的云主机服务商?
05. 博客项目之安全
Qt TCP 分包粘包的解决方法
Vulhub vulnerability recurrence 68_ ThinkPHP
Promotion hung up! The leader said it wasn't my poor skills
(column 22) typical column questions of C language: delete the specified letters in the string.
初识数据库
Web Security (VI) the use of session and the difference between session and cookie
嵌入式面试题(一:进程与线程)
移植InfoNES到STM32



![[cloud native] 3.1 kubernetes platform installation kubespher](/img/86/137a65a5b58bc32e596d2a330ca9fc.png)




