本文主要是介绍pytorch框架是如何autograd简易实现反向传播算法的?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import torch
x = torch.randn(3,4,requires_grad=True) #对指定的X进行求导
b = torch.randn(3,4,requires_grad=True)
t = x+b
y=t.sum()
print(y)
y.backward() #执行反向传播
print(b.grad)print(x.requires_grad, b.requires_grad, t.requires_grad) #自动计算t 自动指定为true
for example:
z=b+y
y=x*w
(求偏导逐层来做 链式求导)
import torch
x = torch.rand(1)
b = torch.rand(1, requires_grad=True)
w = torch.rand(1, requires_grad=True)
y = w * x
z = y + b
print(x.requires_grad, b.requires_grad, w.requires_grad, y.requires_grad)
# dw/dx 要先通过对y求导 故y也需要
print(x.is_leaf, w.is_leaf, b.is_leaf, y.is_leaf, z.is_leaf)
#检测是否为leaf节点 True True True False False#反向传播的计算
z.backward(retain_graph=True) #如果对梯度不清零 会累加 实际训练模型时一般不需要累加
print(w.grad)
print(b.grad)
反向传播操作全部已经封装在函数内。
now,构建一个线性回归demo,have a try
import torch
import torch.nn as nn
import numpy as np
#大多时候对图像等进行数据读入都为np.array 需要转换
#先构建x、y
x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype=np.float32)
x_train = x_train.reshape(-1, 1) #转为矩阵
print(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)
print(y_train.shape)#其实线性回归就是一个不加激活函数的全连接层
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#y=2x+1
input_dim = 1
output_dim = 1model = LinearRegressionModel(input_dim, output_dim)
print(model)#指定好参数和损失函数进行训练
epochs = 1000
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
#优化器SGD
criterion = nn.MSELoss()
#定义损失函数 回归函数->MSE#训练模型
for epoch in range(epochs):epoch += 1# 注意转行成tensorinputs = torch.from_numpy(x_train)labels = torch.from_numpy(y_train)# 梯度要清零每一次迭代optimizer.zero_grad()# 前向传播outputs = model(inputs)# 计算损失loss = criterion(outputs, labels)# 返向传播loss.backward()# 更新权重参数optimizer.step()if epoch % 5 == 0:print('epoch {}, loss {}'.format(epoch, loss.item()))#测试模型预测结果
predicted = model(torch.from_numpy(x_train).requires_grad_()).data.numpy()
print(predicted)#模型的保存与读取
torch.save(model.state_dict(), 'model.pkl')
model.load_state_dict(torch.load('model.pkl'))
这篇关于pytorch框架是如何autograd简易实现反向传播算法的?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!