Pytorch之Variable求导机制
自动求导机制是pytorch中非常重要的性质,免去了手动计算导数,为构建模型节省了时间。下面介绍自动求导机制的基本用法。
#自动求导机制
import torch
from torch.autograd import Variable# 1、简单的求导(求导对象是标量)
x = Variable(torch.Tensor([2]),requires_grad=True)
y = (x + 2) ** 2 + 3
print(y)
y.backward()
print(x.grad)#对矩阵求导
x1 = Variable(torch.randn(10,20),requires_grad=True)
y1 = Variable(torch.randn(10,1),requires_grad=True)
W = Variable(torch.randn(20,1),requires_grad=True)J = torch.mean(y1 - torch.matmul(x1,W)) #matmul表示做矩阵乘法
J.backward()
print(x1.grad)
print(y1.grad)
print(W.grad)
tensor([19.], grad_fn=<AddBackward0>)
tensor([8.])
tensor([[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454],
[-0.1636, 0.0904, 0.0446, -0.1052, -0.2323, 0.0129, -0.1532, 0.0544,
0.0231, -0.0993, -0.0387, -0.1762, 0.0477, 0.1552, 0.0493, 0.0144,
-0.1581, 0.1986, -0.0226, -0.0454]])
tensor([[0.1000],
[0.1000],
[0.1000],
[0.1000],
[0.1000],
[0.1000],
[0.1000],
[0.1000],
[0.1000],
[0.1000]])
tensor([[ 0.0224],
[ 0.0187],
[-0.2078],
[ 0.5092],
[ 0.0677],
[ 0.3497],
[-0.4575],
[-0.5480],
[ 0.4228],
[-0.0869],
[ 0.2876],
[-0.1714],
[ 0.0985],
[-0.1364],
[-0.1502],
[-0.1372],
[-0.0999],
[-0.0006],
[-0.0544],
[-0.0678]])
#复杂情况的自动求导 多维数组自动求导机制
import torch
from torch.autograd import Variablex = Variable(torch.FloatTensor([3]),requires_grad=True)
y = x ** 2 + x * 2 + 3
y.backward(retain_graph=True) #保留计算图
print(x.grad)
y.backward()#不保留计算图
print(x.grad) #得到的是第一次求导的值加上第二次求导的值 8 + 8
tensor([8.])
tensor([16.])
#小练习,向量对向量求导
import torch
from torch.autograd import Variablex = Variable(torch.Tensor([2,3]),requires_grad = True)
k = Variable(torch.zeros_like(x))k[0] = x[0]**2 + 3 * x[1]
k[1] = 2*x[0] + x[1] ** 2print(k)
j = torch.zeros(2,2)
k.backward(torch.FloatTensor([1,0]),retain_graph = True)
j[0] = x.grad.datax.grad.zero_()
k.backward(torch.FloatTensor([0,1]),retain_graph = True)
j[1] = x.grad.data
print(j)
tensor([13., 13.], grad_fn=<CopySlices>)
tensor([[4., 3.],
[2., 6.]])
Pytorch之Variable求导机制的更多相关文章
- Pytorch Autograd (自动求导机制)
Pytorch Autograd (自动求导机制) Introduce Pytorch Autograd库 (自动求导机制) 是训练神经网络时,反向误差传播(BP)算法的核心. 本文通过logisti ...
- pytorch的自动求导机制 - 计算图的建立
一.计算图简介 在pytorch的官网上,可以看到一个简单的计算图示意图, 如下. import torchfrom torch.autograd import Variable x = Variab ...
- PyTorch官方中文文档:自动求导机制
自动求导机制 本说明将概述Autograd如何工作并记录操作.了解这些并不是绝对必要的,但我们建议您熟悉它,因为它将帮助您编写更高效,更简洁的程序,并可帮助您进行调试. 从后向中排除子图 每个变量都有 ...
- Pytorch学习(一)—— 自动求导机制
现在对 CNN 有了一定的了解,同时在 GitHub 上找了几个 examples 来学习,对网络的搭建有了笼统地认识,但是发现有好多基础 pytorch 的知识需要补习,所以慢慢从官网 API进行学 ...
- Pytorch中的自动求梯度机制和Variable类
自动求导机制是每一个深度学习框架中重要的性质,免去了手动计算导数,下面用代码介绍并举例说明Pytorch的自动求导机制. 首先介绍Variable,Variable是对Tensor的一个封装,操作和T ...
- 『PyTorch x TensorFlow』第六弹_从最小二乘法看自动求导
TensoFlow自动求导机制 『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归 下面做了三个简单尝试, 利用包含gradients.assign等tf函数直接构建图进行自动 ...
- Pytorch Tensor, Variable, 自动求导
2018.4.25,Facebook 推出了 PyTorch 0.4.0 版本,在该版本及之后的版本中,torch.autograd.Variable 和 torch.Tensor 同属一类.更确切地 ...
- 【小白学PyTorch】20 TF2的eager模式与求导
[新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx64501661 ...
- 什么是pytorch(2Autograd:自动求导)(翻译)
Autograd: 自动求导 pyTorch里神经网络能够训练就是靠autograd包.我们来看下这个包,然后我们使用它来训练我们的第一个神经网络. autograd 包提供了对张量的所有运算自动求导 ...
随机推荐
- SQL常用
--1.创建schema create schema exp --2.把dbo下面的对象e_A,移到exp下面 alter schema exp transfer dbo.e_A --3分组字 ...
- July 28th 2017 Week 30th Friday
If equal affection cannot be, let the more loving be me. 如果没有相等的爱,那就让我爱多一点吧. There is seldom equal a ...
- [EffectiveC++]item02:尽量以const,enum,inline代替#define
- 使用Visual Studio Code编写和激活ABAP代码 (上)
猪年春节后的第一篇,Jerry祝各位猪年大吉! 2019年的六分之一马上就快过完了,不知道大家在新的一年是否给自己定了新的小目标呢?这里Jerry先预祝大家到2019年年底的时候,在年初制定的小目标都 ...
- AngularJs 与服务器通信 $http, $q, $resource
$http服务是AngularJS系统自带的,可以用来进行网络通信.获取远程服务器的数据.要记住的是,$http是对浏览器XMLHttpRequest的封装,也就是说,它其实是Ajax. $http( ...
- Django template for 循环用法
当列表为空或者非空时执行不同操作: {% for item in list %} ... {% empty %} ... {% endfor %} 使用forloop.counter访问循环的次数,下 ...
- stylus的用法
参考链接:预处器的对比——Sass.LESS和Stylus http://www.w3cplus.com/css/sass-vs-less-vs-stylus-a-preprocessor-sho ...
- ThinkPHP5入门(一)----框架篇
一.命名规范: 下划线法: 函数的命名 配置参数 常量(大写) 数据表和字段 驼峰法: 属性的命名 方法的命名 帕斯卡法: 类名 类文件名 类的命名
- 谷歌浏览器linux,windows下载
https://www.chromedownloads.net/ 提取码自己行提取rpm安装包
- HDU 1599 find the mincost route(floyd求最小环 无向图)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...