当前位置:网站首页>Hands on learning and deep learning -- simple implementation of linear regression
Hands on learning and deep learning -- simple implementation of linear regression
2022-06-12 08:13:00 【Orange acridine 21】
1、 Imported packets
import numpy as np
import torch
import torch.utils.data as Data #PyTorch Provides data Package to read data . because data often ⽤ Make variable name , We will lead ⼊ Of data modular ⽤ Data Instead of .
from torch import nn, optim
from torch.nn import init2、 Generate data set , among features Training data characteristics ,labels Is the label .
num_inputs=2 # The number of entries
num_examples=1000 # Training data set
true_w=[2,-3.4] # Real weight
true_b=4.2 # True deviation
features=torch.tensor(np.random.normal(0,1,(num_examples,num_inputs)),dtype=torch.float)
# The mean for 0, The variance of 1 The random number , Yes num_examples Samples , The number of columns is num_inputs
labels=true_w[0]*features[:,0]+true_w[1]*features[:,1]+true_b
# lables Is equal to w For each column multiplied by features Each column of is then added , Finally, add the deviation true_b;
labels+=torch.tensor(np.random.normal(0,0.01,size=labels.size()),dtype=torch.float)
# Added a noise , The mean for 0, The variance of 0.01, Shape and lables It's the same length 3、 Reading data
PyTorch Provides data Package to read data .
batch_size=10 #10 Of data samples ⼩ Batch
dataset=Data.TensorDataset(features,labels) # Combine the characteristics and labels of training data
data_iter=Data.DataLoader(dataset,batch_size,shuffle=True) # Random read small batch
# Next, read and print the first small batch data sample
for X,y in data_iter:
print(X,y)
break

4、 Defining models , how ⽤ nn.Module Realization ⼀ A linear regression model
class LinearNet(nn.Module): #nn Is the abbreviation of neural network
def __init__(self,n_feature):
super(LinearNet, self).__init__()
self.linear=nn.Linear(n_feature,1)
def forward(self,x): # Return the forward propagation method of the output
y=self.linear(x)
return y
net =LinearNet(num_inputs)
print(net)

In fact, we can ⽤ nn.Sequential Come more ⽅ Convenient construction ⽹ Collateral , Sequential yes ⼀ An orderly container ,⽹ Collateral The layer will be transmitted in accordance with ⼊ Sequential The order of is added to the calculation diagram in turn .
# How to write it ⼀
net = nn.Sequential(
nn.Linear(num_inputs, 1)
# Here you can also pass ⼊ Other layers
)
# How to write it ⼆
net = nn.Sequential()
net.add_module('linear', nn.Linear(num_inputs, 1))
# net.add_module ......
# Write three
from collections import OrderedDict
net = nn.Sequential(OrderedDict([
('linear', nn.Linear(num_inputs, 1))
# ......
]))
print(net)
print(net[0])

Can pass net.parameters() To see all the learnable parameters of the model , This function will return ⼀ individual ⽣ Make it .
for param in net.parameters():
print(param)
5、 Initialize model
#init yes initializer( Initialization settings ) Abbreviated form
init.normal_(net[0].weight,mean=0,std=0.01) # adopt init.normal_ Initialize each element of the weight parameter to random sample at a mean value of 0、 The standard deviation is 0.01 Is a normal distribution
init.constant_(net[0].bias,val=0) # The deviation is initialized to zero 6、 Define the loss function
loss=nn.MSELoss()# The mean square error is calculated using MSELoss class , Also called square L2 norm 7、 Define optimization algorithms
#torch.optim The module provides many common functions ⽤ Optimization algorithm ⽐ Such as SGD、Adam and RMSProp etc.
optimizer=optim.SGD(net.parameters(),lr=0.03) # Take out all the parameters w and b, The learning rate is 0.03
print(optimizer)
About learning rate
# If the learning rate is not specified for a certain parameter , Just make ⽤ Default learning rate of the outermost layer
# Adjust the learning rate
for param_group in optimizer.param_groups:
param_group['lr'] *= 0.1 # The learning rate is the same as before 0.1 times
print(optimizer)

8、 Training models Through the tune ⽤ optim Example of step Function to iterate over model parameters .
num_epochs = 3 # Do three cycles
for epoch in range(1, num_epochs + 1): # Circular function
for X, y in data_iter: # Take out small batches one at a time
output = net(X) # Output to net Inside
l = loss(output, y.view(-1, 1))
optimizer.zero_grad() # Gradient clear , Equivalent to net.zero_grad()
l.backward() # Calculate the gradient
optimizer.step() # call step Function to update the model
print('epoch %d, loss: %f' % (epoch, l.item()))
⽐ Compare the learned model parameters with the real model parameters . We from net Get the required layer , And access to it ᯿( weight ) And deviation ( bias )
dense = net[0]
print(true_w, dense.weight)
print(true_b, dense.bias) 
边栏推荐
- Asp Net project add log function
- Leetcode notes: Weekly contest 280
- Upgrade eigen to version 3.3.5 under Ubuntu 16.04
- MYSQL中的锁的机制
- Model Trick | CVPR 2022 Oral - Stochastic Backpropagation A Memory Efficient Strategy
- (p40-p41) transfer and forward perfect forwarding of move resources
- Mathematical Essays: Notes on the angle between vectors in high dimensional space
- CMAKE 里PRIVATE、PUBLIC、INTERFACE属性示例详解
- DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ... Reading notes
- Leetcode notes: biweekly contest 71
猜你喜欢

计组第一章

Asp Net project add log function

Prediction of COVID-19 by RNN network

(P21-P24)统一的数据初始化方式:列表初始化、使用初始化列表初始化非聚合类型的对象、initializer_lisy模板类的使用

Convolutional neural network CNN based cat dog battle picture classification (tf2.1 py3.6)

(P36-P39)右值和右值引用、右值引用的作用以及使用、未定引用类型的推导、右值引用的传递

MATLAB image processing - Otsu threshold segmentation (with code)

Derivation of Poisson distribution

后MES系统的时代,已逐渐到来

Vscode的Katex问题:ParseError: KaTeX Parse Error: Can‘t Use Function ‘$‘ In Math Mode At Position ...
随机推荐
ctfshow web3
TMUX common commands
"Three.js" auxiliary coordinate axis
. net mysql Too many connections
(P17-P18)通过using定义基础类型和函数指针别名,使用using和typedef给模板定义别名
建立MES系统,需要注意什么?能给企业带来什么好处?
FPGA to flip video up and down (SRAM is61wv102416bll)
数据库基础——规范化、关系模式
工厂的生产效益,MES系统如何提供?
只把MES当做工具?看来你错过了最重要的东西
You get download the installation and use of artifact
(P19-P20)委托构造函数(代理构造函数)和继承构造函数(使用using)
Special notes on using NAT mode in VM virtual machine
对企业来讲,MES设备管理究竟有何妙处?
Beidou satellite navigation system foundation part 1
System service configuration service - detailed version
Servlet
MES系统是什么?MES系统的操作流程是怎样?
qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
如何理解APS系统的生产排程?