当前位置:网站首页>pytorch_ 01 automatic derivation mechanism
pytorch_ 01 automatic derivation mechanism
2022-07-07 05:42:00 【Magnetoelectricity】
One of the most powerful things framework does is : Manually define forward propagation that requires derivation , Calculate all the backward propagation
import torch
# Method 1
x = torch.randn(3,4,requires_grad=True)# structure 3 That's ok 4 Columns of the matrix requires_grad=True Indicates that the current X To find the derivative , The default is false
x
# Method 2
x = torch.randn(3,4)#
x.requires_grad=True
x
b = torch.randn(3,4,requires_grad=True)
t = x + b
y = t.sum()
y#y As a loss function , Back propagation is to derive layer by layer from the loss function
y.backward()
b.grad
out:tensor(2.1753, grad_fn=)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
Although not specified t Of requires_grad But you need it , It will also default
x.requires_grad, b.requires_grad, t.requires_grad
out (True, True, True)
# Calculation process
# Yes x w b Initialization of random values
x = torch.rand(1)
b = torch.rand(1, requires_grad = True)
w = torch.rand(1, requires_grad = True)
y = w * x
z = y + b
# Backward propagation calculation
z.backward(retain_graph=True)# stay pytorch In the frame , If you don't empty the gradient, it will add up
Do a linear regression and try water
Construct a set of input data X And its corresponding label y
import numpy as np
x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype=np.float32)#x Now it is ndarry The format of cannot be input into pytorch Training in To put ndarry Turn into tensor Format
x_train = x_train.reshape(-1, 1)# In order to prevent subsequent errors, it is converted into matrix format
x_train.shape
y_values = [2*i + 1 for i in x_values]
y_train = np.array(y_values, dtype=np.float32)
y_train = y_train.reshape(-1, 1)
y_train.shape
import torch
import torch.nn as nn
Linear regression model is actually a full connection layer without activation function
class LinearRegressionModel(nn.Module):# No matter how complex the model is built First define the model class Inherit existing nn.Module modular
def __init__(self, input_dim, output_dim):# Write those layers in the constructor
super(LinearRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim) # call nn The full connection layer of , Dimension of incoming input and output layer
def forward(self, x):# Specify the layer to use in forward propagation
out = self.linear(x)
return out
input_dim = 1
output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
Specify parameters and loss function for training
epochs = 1000# cycles
learning_rate = 0.01# Learning rate
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)# Define optimizer -SGD ( Parameters to be optimized , Learning rate )
criterion = nn.MSELoss()# Appoint MSE Loss function
Training models
for epoch in range(epochs):
epoch += 1
# Pay attention to turning into tensor
inputs = torch.from_numpy(x_train)
labels = torch.from_numpy(y_train)
# The gradient should be cleared every iteration
optimizer.zero_grad()
# Forward propagation results
outputs = model(inputs)
# Calculate the loss
loss = criterion(outputs, labels)
# Backward propagation
loss.backward()
# Update weight parameters
optimizer.step()
if epoch % 50 == 0:
print('epoch {}, loss {}'.format(epoch, loss.item()))
Test model prediction results
predicted = model(torch.from_numpy(x_train).requires_grad_()).data.numpy()# Make a forward propagation to predict , Turn the result into ndarry Format , Convenient for drawing and pandas You need to use ndarry Format
predicted
Save and read the model
torch.save(model.state_dict(), 'model.pkl')# Save in dictionary format Save the weight parameters and offsets
model.load_state_dict(torch.load('model.pkl'))
Use GPU Training
Just pass the data and model into cuda Just inside
import torch
import torch.nn as nn
import numpy as np
class LinearRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LinearRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
def forward(self, x):
out = self.linear(x)
return out
input_dim = 1
output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")# If GPU Configured for use GPU
model.to(device)# Transfer the model to cuda in
criterion = nn.MSELoss()
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
epochs = 1000
for epoch in range(epochs):
epoch += 1
inputs = torch.from_numpy(x_train).to(device)# Transfer training data to cuda in
labels = torch.from_numpy(y_train).to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if epoch % 50 == 0:
print('epoch {}, loss {}'.format(epoch, loss.item()))
边栏推荐
- Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
- 5. Data access - entityframework integration
- R语言【逻辑控制】【数学运算】
- Initial experience of annotation
- “多模态”概念
- Getting started with DES encryption
- In memory, I moved from CSDN to blog park!
- Mybaits之多表查询(联合查询、嵌套查询)
- Jhok-zbl1 leakage relay
- 集群、分布式、微服務的區別和介紹
猜你喜欢
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
Initial experience of annotation
三级菜单数据实现,实现嵌套三级菜单数据
Leakage relay llj-100fs
Digital innovation driven guide
论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
Leetcode: maximum number of "balloons"
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
Differences and introduction of cluster, distributed and microservice
随机推荐
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
纪念下,我从CSDN搬家到博客园啦!
5阶多项式轨迹
三级菜单数据实现,实现嵌套三级菜单数据
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
How digitalization affects workflow automation
Unity让摄像机一直跟随在玩家后上方
Message queuing: how to ensure that messages are not lost
Mybaits multi table query (joint query, nested query)
4. Object mapping Mapster
张平安:加快云上数字创新,共建产业智慧生态
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
LabVIEW is opening a new reference, indicating that the memory is full
Writing process of the first paper
说一说MVCC多版本并发控制器?
Summary of the mean value theorem of higher numbers
Preliminary practice of niuke.com (9)
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
Cve-2021-3156 vulnerability recurrence notes