博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pytorch框架是如何autograd简易实现反向传播算法的?
阅读量:3929 次
发布时间:2019-05-23

本文共 2238 字,大约阅读时间需要 7 分钟。

import torchx = torch.randn(3,4,requires_grad=True) #对指定的X进行求导b = torch.randn(3,4,requires_grad=True)t = x+by=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 torchx = torch.rand(1)b = torch.rand(1, requires_grad=True)w = torch.rand(1, requires_grad=True)y = w * xz = y + bprint(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 torchimport torch.nn as nnimport numpy as np#大多时候对图像等进行数据读入都为np.array 需要转换#先构建x、yx_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+1input_dim = 1output_dim = 1model = LinearRegressionModel(input_dim, output_dim)print(model)#指定好参数和损失函数进行训练epochs = 1000learning_rate = 0.01optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)#优化器SGDcriterion = 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'))

 

 

 

 

转载地址:http://vxtgn.baihongyu.com/

你可能感兴趣的文章
SemEval2019Task3_ERC | (3) Using Deep Sentiment Analysis Models and Transfer Learning for ERC
查看>>
SemEval2019Task3_ERC | (4) Emotion detection in conversations through Tweets,CNN and LSTM DNN
查看>>
Python杂谈 | (15) 使用Pycharm执行带命令行参数的脚本
查看>>
从源码分析:分析Java中的StringBuilder
查看>>
Linux(Ubuntu18)中启动ssh时的报错
查看>>
Java中的左移时的负数问题
查看>>
从数组形式创建一棵树(用于leetcode测试)
查看>>
线程进阶:多任务处理(17)——Java中的锁(Unsafe基础)
查看>>
Spring/Boot/Cloud系列知识(1)——开篇
查看>>
线程基础:多任务处理(15)——Fork/Join框架(要点2)
查看>>
线程基础:多任务处理(16)——Fork/Join框架(排序算法性能补充)
查看>>
线程基础:多任务处理(14)——Fork/Join框架(要点1)
查看>>
架构设计:系统存储(13)——MySQL横向拆分与业务透明化(1)
查看>>
架构设计:系统存储(14)——MySQL横向拆分与业务透明化(2)
查看>>
架构设计:系统存储(5)——MySQL数据库性能优化(1)
查看>>
架构设计:系统存储(2)——块存储方案(2)
查看>>
架构设计:系统间通信(45)——阶段性问题记录
查看>>
架构设计:系统间通信(44)——自己动手设计ESB(5)
查看>>
架构设计:系统存储(1)——块存储方案(1)
查看>>
架构设计:系统间通信(42)——自己动手设计ESB(3)
查看>>