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

边栏推荐
- Promotion hung up! The leader said it wasn't my poor skills
- Algorithm -- climbing stairs (kotlin)
- 05. Security of blog project
- PDK process library installation -csmc
- 02. Develop data storage of blog project
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
- SQLite queries the maximum value and returns the whole row of data
- 指針經典筆試題
- nacos-高可用seata之TC搭建(02)
- Unity Vector3. Use and calculation principle of reflect
猜你喜欢

Promise summary

SQLite add index

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

移植InfoNES到STM32

01. 开发博客项目之项目介绍

巨杉数据库再次亮相金交会,共建数字经济新时代

Pointer classic written test questions

Vulhub vulnerability recurrence 73_ Webmin

04. 项目博客之日志
随机推荐
59. Spiral matrix
Talking about the type and function of lens filter
05. 博客项目之安全
应用安全系列之三十七:日志注入
04. Project blog log
Excel转换为Lua的配置文件
jdbc使用call调用存储过程报错
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
Text classification still stays at Bert? The dual contrast learning framework is too strong
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
【torch】|torch.nn.utils.clip_grad_norm_
Redis消息队列
UCF(暑期团队赛二)
[leetcode daily question] number of enclaves
【云原生】3.1 Kubernetes平台安装KubeSpher
Huawei od computer test question 2
【华为机试真题详解】统计射击比赛成绩
Sliding window problem review
Installation de la Bibliothèque de processus PDK - csmc
02. Develop data storage of blog project