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 = 1

model = 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
    # 注意转行成tensor
    inputs = 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'))



 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值