当前位置:网站首页>Pytorch implementation of regression model
Pytorch implementation of regression model
2022-06-12 06:06:00 【Singing under the hedge】
List of articles
pytorch Implement the regression model
One 、 Code
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
# Build datasets
x = torch.unsqueeze(torch.linspace(-1,1,100),dim=1)# x data(tensor),shape(100,1)
y = x.pow(2) + 0.2*torch.rand(x.size())# noisy y data(tensor),shape(100,1)
# Building neural networks
# Method 1 :
# class Net(torch.nn.Module):
# def __init__(self,n_feature,n_hidden,n_output):
# super(Net,self).__init__()# Inherit __init__ function
# # Define the form of each layer
# self.hidden = torch.nn.Linear(n_feature,n_hidden)# Hidden layer linear output
# self.output = torch.nn.Linear(n_hidden,n_output)# Output layer linear output
#
# def forward(self,x):
# x = F.relu(self.hidden(x))# Activation function
# x = self.output(x)# Output value
# return x
# net = Net(n_feature=1,n_hidden=10,n_output=1)
# Method 2 :
net = torch.nn.Sequential(
torch.nn.Linear(1,10),
torch.nn.ReLU(),
torch.nn.Linear(10,1)
)
# visualization
plt.ion()
plt.show()
# Training network
optimizer = torch.optim.SGD(net.parameters(),lr=0.2)# Stochastic gradient descent , Pass in net All parameters of , Learning rate
loss_func = torch.nn.MSELoss()# Loss function ( Mean square error )
for t in range(100):
pre_y = net(x)# to net Training data , Output predicted value
loss = loss_func(pre_y,y)# Calculate the loss function
optimizer.zero_grad()# Clear the residual update parameter value of the previous step
loss.backward()# Error back propagation
optimizer.step()# Add new parameter update values to net Of parameters On
# mapping
if t%5 == 0 :
plt.cla()
plt.scatter(x.data.numpy(),y.data.numpy())
plt.plot(x.data.numpy(),pre_y.data.numpy(),'r_',lw=5)
plt.text(0.5,0,'Loss=%.4f'%loss.data.numpy(),fontdict={
'size':20,'color':'red'})
plt.pause(0.1)
Two 、 Realization effect

边栏推荐
- [untitled]
- Findasync and include LINQ statements - findasync and include LINQ statements
- Database Experiment 3: data query
- The unity3d script searches for colliders with overlaps within the specified radius
- Review notes of naturallanguageprocessing based on deep learning
- Jpg format and XML format files are separated into different folders
- IO system - code example
- Un mois de DDD hépatique.
- Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email
- 数据库实验一:数据定义实验指导
猜你喜欢

sqlite交叉編譯動態庫

Understand Houdini's (heightfield) remap operation

Makefile文件编写快速掌握

cv2.fillPoly coco annotator segment坐标转化为mask图像

Annotation configuration of filter

Idea common configuration

Why don't databases use hash tables?

Leetcode-1552. Magnetic force between two balls

Project technical structure

SQLite cross compile dynamic library
随机推荐
R language homework (IV): GDP value analysis of Shanghai and Tokyo from 1997 to 2018
zip 和.items()区别
Research Report on water sports shoes industry - market status analysis and development prospect forecast
. Net core - pass Net core will Net to cross platform
nRF52832自定義服務與特性
English语法_副词_有无ly,意义不同
The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY
Leetcode-1706. Where does the club fall
Leetcode-646. Longest number pair chain
Makefile文件编写快速掌握
Un mois de DDD hépatique.
Leetcode-2048. Next larger numerical balance
Brief introduction to project development process
sqlite交叉编译动态库
Leetcode-1260. 2D mesh migration
nus_ data_ Handler source code interprets data types such as structure
nRF52832自定义服务与特性
哈工大信息内容安全实验
Idea common configuration
Directx11 advanced tutorial tiled based deffered shading