当前位置:网站首页>Pytorch build progression
Pytorch build progression
2022-06-26 08:54:00 【Thick Cub with thorns】
pytorch build Regression
Introduction to neural network construction
import torch
import matplotlib.pyplot as plt
from torch.autograd import Variable
import torch.nn.functional as F
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
y = x.pow(2) + 0.2 * torch.rand(x.size())
X, Y = Variable(x), Variable(y)
plt.scatter(X.data.numpy(), Y.data.numpy())
plt.show()
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-wMxlF2h6-1644318579449)(img/1.png)]
Construction noise scatter
Building neural network
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
# features , Hidden layer , Output layer
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden)
self.predict = torch.nn.Linear(n_hidden, n_output)
def forward(self, x): # The process of forward transmission
x = F.relu(self.hidden(x))
x = self.predict(x) # The value range is not changed during prediction
return x
net = Net(1, 10, 1)
print(net)
out
Net(
(hidden): Linear(in_features=1, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=1, bias=True)
)
The optimizer optimizes
plt.ion() # Real time printing
plt.show()
# Optimizing neural networks
optimizer = torch.optim.SGD(net.parameters(), lr=0.5)
loss_func = torch.nn.MSELoss()
for i in range(100): # Train 100 steps
prediction = net(x) #
loss = loss_func(prediction, y) # error
optimizer.zero_grad() # First the gradient is reduced to 0
loss.backward() # Reverse transfer
optimizer.step() # Optimize the gradient
if i % 5 == 0:
plt.cla()
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
plt.text(0.5, 0, 'LOSS=%.4f' % loss.item(), fontdict={
'size': 20, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
out
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-a9WHfeK6-1644318579451)(img/2.png)]
.pause(0.1)
plt.ioff()
plt.show()
**out**
[ Outside the chain picture transfer in ...(img-a9WHfeK6-1644318579451)]
边栏推荐
- MPC learning notes (I): push MPC formula manually
- Summary of mobile terminal lightweight model data
- [QNX Hypervisor 2.2用户手册]12.2 术语(二)
- Principle of playing card image segmentation
- keras_ Callback function summary
- Summary of common instructions for arm assembly
- Koa_ mySQL_ Integration of TS
- KNN resolution
- Corn image segmentation count_ nanyangjx
- Remote centralized control of distributed sensor signals using wireless technology
猜你喜欢

Yolov5进阶之三训练环境

51 MCU project design: Based on 51 MCU clock perpetual calendar

【Unity Mirror】NetworkTeam的使用

QT_ AI

What are the conditions for Mitsubishi PLC to realize Ethernet wireless communication?

WBC learning notes (II): practical application of WBC control

Digital image processing learning (II): Gaussian low pass filter

多台三菱PLC如何实现无线以太网高速通讯?

XSS cross site scripting attack

leetcode2022年度刷题分类型总结(十二)并查集
随机推荐
Realizing sequence annotation with transformers
鲸会务一站式智能会议系统帮助主办方实现数字化会议管理
Regular Expression 正则表达式
Relationship extraction -- casrel
Clion installation + MinGW configuration + opencv installation
ROS learning notes (6) -- function package encapsulated into Library and called
自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)
Detailed process of generating URDF file from SW model
WBC learning notes (I): manually push WBC formula
Yolov5进阶之三训练环境
STM32 project design: an e-reader making tutorial based on stm32f4
pgsql_ UDF01_ jx
Slider verification - personal test (JD)
Yolov5进阶之一摄像头实时采集识别
[resolved]setonnavigationitemselectedlistener() deprecated
Install Anaconda + NVIDIA graphics card driver + pytorch under win10_ gpu
Software engineering - personal assignment - question review and personal summary
关于极客时间 | MySQL实战45讲的部分总结
Google Chrome collection
鲸会务为活动现场提供数字化升级方案