当前位置:网站首页>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()
边栏推荐
- [JVM] [Chapter 17] [garbage collector]
- 26file filter anonymous inner class and lambda optimization
- Check the useful photo lossless magnification software on Apple computer
- Mongodb basic knowledge summary
- Detailed summary of SQL injection
- 28io stream, byte output stream writes multiple bytes
- Selective parameters in MATLAB functions
- Vulhub vulnerability recurrence 71_ Unomi
- 巨杉数据库再次亮相金交会,共建数字经济新时代
- 备忘一下jvxetable的各种数据集获取方法
猜你喜欢
05. Security of blog project
[effective Objective-C] - memory management
How to use PHP string query function
Vulhub vulnerability recurrence 73_ Webmin
用StopWatch 统计代码耗时
JS array list actual use summary
Codeforces Round #804 (Div. 2) Editorial(A-B)
02. Develop data storage of blog project
nacos-高可用seata之TC搭建(02)
Check the useful photo lossless magnification software on Apple computer
随机推荐
Jvxetable implant j-popup with slot
Promotion hung up! The leader said it wasn't my poor skills
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
Sliding window problem review
改善Jpopup以实现动态控制disable
剑指 Offer II 039. 直方图最大矩形面积
Vulhub vulnerability recurrence 68_ ThinkPHP
HAC cluster modifying administrator user password
Vulhub vulnerability recurrence 71_ Unomi
[imgui] unity MenuItem shortcut key
[Tang Laoshi] C -- encapsulation: classes and objects
In 2022, we must enter the big factory as soon as possible
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
Yyds dry inventory SSH Remote Connection introduction
(column 22) typical column questions of C language: delete the specified letters in the string.
28io stream, byte output stream writes multiple bytes
Fluent implements a loadingbutton with loading animation
LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
Closure, decorator